summaryrefslogtreecommitdiff
path: root/qpid/cpp/bindings/qmf/tests/python_agent.py
blob: 83531830b1fe6c0504b110dee9413707d4518bcf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/env python
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
# 
#   http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#


import qmf
import sys
import time


class Model:
    # attr_reader :parent_class, :child_class
    def __init__(self):
        self.parent_class = qmf.SchemaObjectClass("org.apache.qpid.qmf", "parent")
        self.parent_class.add_property(qmf.SchemaProperty("name", qmf.TYPE_SSTR, {"index":True}))
        self.parent_class.add_property(qmf.SchemaProperty("state", qmf.TYPE_SSTR))

        self.parent_class.add_property(qmf.SchemaProperty("uint64val", qmf.TYPE_UINT64))
        self.parent_class.add_property(qmf.SchemaProperty("uint32val", qmf.TYPE_UINT32))
        self.parent_class.add_property(qmf.SchemaProperty("uint16val", qmf.TYPE_UINT16))
        self.parent_class.add_property(qmf.SchemaProperty("uint8val", qmf.TYPE_UINT8))

        self.parent_class.add_property(qmf.SchemaProperty("int64val", qmf.TYPE_INT64))
        self.parent_class.add_property(qmf.SchemaProperty("int32val", qmf.TYPE_INT32))
        self.parent_class.add_property(qmf.SchemaProperty("int16val", qmf.TYPE_INT16))
        self.parent_class.add_property(qmf.SchemaProperty("int8val", qmf.TYPE_INT8))

        self.parent_class.add_property(qmf.SchemaProperty("sstrval", qmf.TYPE_SSTR))
        self.parent_class.add_property(qmf.SchemaProperty("lstrval", qmf.TYPE_LSTR))

        self.parent_class.add_property(qmf.SchemaProperty("mapval", qmf.TYPE_MAP))


        self.parent_class.add_statistic(qmf.SchemaStatistic("queryCount", qmf.TYPE_UINT32, {"unit":"query", "desc":"Query count"}))

        _method = qmf.SchemaMethod("echo", {"desc":"Check responsiveness of the agent object"})
        _method.add_argument(qmf.SchemaArgument("sequence", qmf.TYPE_UINT32, {"dir":qmf.DIR_IN_OUT}))
        self.parent_class.add_method(_method)

        _method = qmf.SchemaMethod("set_numerics", {"desc":"Set the numeric values in the object"})
        _method.add_argument(qmf.SchemaArgument("test", qmf.TYPE_SSTR, {"dir":qmf.DIR_IN}))
        self.parent_class.add_method(_method)

        _method = qmf.SchemaMethod("set_map", {"desc":"Set the map value in the object"})
        _method.add_argument(qmf.SchemaArgument("value", qmf.TYPE_MAP, {"dir":qmf.DIR_IN}))
        _method.add_argument(qmf.SchemaArgument("output", qmf.TYPE_MAP, {"dir":qmf.DIR_OUT}))
        self.parent_class.add_method(_method)

        _method = qmf.SchemaMethod("set_short_string", {"desc":"Set the short string value in the object"})
        _method.add_argument(qmf.SchemaArgument("value", qmf.TYPE_SSTR, {"dir":qmf.DIR_IN_OUT}))
        self.parent_class.add_method(_method)

        _method = qmf.SchemaMethod("set_long_string", {"desc":"Set the long string value in the object"})
        _method.add_argument(qmf.SchemaArgument("value", qmf.TYPE_LSTR, {"dir":qmf.DIR_IN_OUT}))
        self.parent_class.add_method(_method)

        _method = qmf.SchemaMethod("create_child", {"desc":"Create a new child object"})
        _method.add_argument(qmf.SchemaArgument("child_name", qmf.TYPE_LSTR, {"dir":qmf.DIR_IN}))
        _method.add_argument(qmf.SchemaArgument("child_ref", qmf.TYPE_REF, {"dir":qmf.DIR_OUT}))
        self.parent_class.add_method(_method)

        _method = qmf.SchemaMethod("probe_userid", {"desc":"Return the user-id for this method call"})
        _method.add_argument(qmf.SchemaArgument("userid", qmf.TYPE_SSTR, {"dir":qmf.DIR_OUT}))
        self.parent_class.add_method(_method)

        self.child_class = qmf.SchemaObjectClass("org.apache.qpid.qmf", "child")
        self.child_class.add_property(qmf.SchemaProperty("name", qmf.TYPE_SSTR, {"index":True}))

        self.event_class = qmf.SchemaEventClass("org.apache.qpid.qmf", "test_event", qmf.SEV_NOTICE)
        self.event_class.add_argument(qmf.SchemaArgument("uint32val", qmf.TYPE_UINT32))
        self.event_class.add_argument(qmf.SchemaArgument("strval", qmf.TYPE_LSTR))


    def register(self, agent):
        agent.register_class(self.parent_class)
        agent.register_class(self.child_class)
        agent.register_class(self.event_class)



class App(qmf.AgentHandler):
    '''
    Object that handles events received by the Agent.
    '''
    def get_query(self, context, query, userId):
        '''
        Respond to a Query request from a console.
        '''
        #print "Query: user=%s context=%d class=%s" % (userId, context, query.class_name())
        #if query.object_id():
        #    print query.object_id().object_num_low()
        self._parent.inc_attr("queryCount")
        if query.class_name() == 'parent':
            self._agent.query_response(context, self._parent)
        elif query.object_id() == self._parent_oid:
            self._agent.query_response(context, self._parent)
        self._agent.query_complete(context)
    
    
    def method_call(self, context, name, object_id, args, userId):
        '''
        Invoke a method call requested by the console.
        '''
        #print "Method: name=%s user=%s context=%d object_id=%s args=%s" % (name, userId, context, object_id, args)
        if name == "echo":
            self._agent.method_response(context, 0, "OK", args)

        elif name == "set_map":
            map = args['value']
            map['added'] = "Added Text"
            args['output'] = map
            self._agent.method_response(context, 0, "OK", args)

        elif name == "set_numerics":
            _retCode = 0
            _retText = "OK"

            if args['test'] == "big":
                #
                # note the alternate forms for setting object attributes:
                #
                self._parent.set_attr("uint64val", 0x9494949449494949)
                self._parent.uint32val = 0xa5a55a5a
                self._parent.set_attr("uint16val", 0xb66b)
                self._parent["uint8val"] = 0xc7

                self._parent.int64val = 1000000000000000000
                self._parent.set_attr("int32val", 1000000000)
                self._parent["int16val"] = 10000
                self._parent.set_attr("int8val",  100)

                self._parent.set_attr("mapval", {'u64'  : self._parent['uint64val'],
                                                 'u32'  : self._parent['uint32val'],
                                                 'u16'  : self._parent['uint16val'],
                                                 'u8'   : self._parent['uint8val'],
                                                 'i64'  : self._parent['int64val'],
                                                 'i32'  : self._parent['int32val'],
                                                 'i16'  : self._parent['int16val'],
                                                 'i8'   : self._parent['int8val'],
                                                 'sstr' : "Short String",
                                                 'map'  : {'first' : 'FIRST', 'second' : 'SECOND'}})

                event = qmf.QmfEvent(self._model.event_class)
                event.uint32val = self._parent.get_attr("uint32val")
                event.strval = "Unused"

                self._agent.raise_event(event)

                ## Test the __getattr__ implementation:
                ## @todo: remove once python_client implements this
                ## form of property access
                assert self._parent["uint8val"] == 0xc7
                assert self._parent.uint64val == 0x9494949449494949

            # note the alternative argument access syntax:
            elif args.test == "small":
                self._parent.set_attr("uint64val", 4)
                self._parent.set_attr("uint32val", 5)
                self._parent.set_attr("uint16val", 6)
                self._parent.set_attr("uint8val",  7)

                self._parent.set_attr("int64val", 8)
                self._parent.set_attr("int32val", 9)
                self._parent.set_attr("int16val", 10)
                self._parent.set_attr("int8val",  11)

                event = qmf.QmfEvent(self._model.event_class)
                event.uint32val = self._parent.uint32val
                event.strval = "Unused"
                self._agent.raise_event(event)

            elif args['test'] == "negative":
                self._parent.set_attr("uint64val", 0)
                self._parent.set_attr("uint32val", 0)
                self._parent.set_attr("uint16val", 0)
                self._parent.set_attr("uint8val",  0)

                self._parent.set_attr("int64val", -10000000000)
                self._parent.set_attr("int32val", -100000)
                self._parent.set_attr("int16val", -1000)
                self._parent.set_attr("int8val",  -100)

                event = qmf.QmfEvent(self._model.event_class)
                event.uint32val = self._parent.uint32val
                event.strval = "Unused"
                self._agent.raise_event(event)

            else:
                _retCode = 1
                _retText = "Invalid argument value for test"

            self._agent.method_response(context, _retCode, _retText, args)

        elif name == "set_short_string":
            self._parent.set_attr('sstrval', args['value'])
            event = qmf.QmfEvent(self._model.event_class)
            event.uint32val = 0
            event.strval = self._parent.sstrval
            self._agent.raise_event(event)

            self._agent.method_response(context, 0, "OK", args)

        elif name == "set_long_string":
            self._parent.set_attr('lstrval', args['value'])
            event = qmf.QmfEvent(self._model.event_class)
            event.uint32val = 0
            event.strval = self._parent.lstrval
            self._agent.raise_event(event)

            self._agent.method_response(context, 0, "OK", args)

        elif name == "create_child":
            #
            # Instantiate an object based on the Child Schema Class
            #
            _oid = self._agent.alloc_object_id(2)
            args['child_ref'] = _oid
            self._child = qmf.AgentObject(self._model.child_class)
            self._child.set_attr("name", args["child_name"])
            self._child.set_object_id(_oid)
            self._agent.method_response(context, 0, "OK", args)

        elif name == "probe_userid":
            args['userid'] = userId
            self._agent.method_response(context, 0, "OK", args)

        else:
            self._agent.method_response(context, 1, "Unimplemented Method: %s" % name, args)


    def main(self):
        '''
        Agent application's main processing loop.
        '''
        # Connect to the broker
        self._settings = qmf.ConnectionSettings()
        self._settings.sendUserId = True
        if len(sys.argv) > 1:
            self._settings.host = str(sys.argv[1])
        if len(sys.argv) > 2:
            self._settings.port = int(sys.argv[2])
        self._connection = qmf.Connection(self._settings)

        # Instantiate an Agent to serve me queries and method calls
        self._agent = qmf.Agent(self)

        # Dynamically define the parent and child schemas, then
        # register them with the agent
        self._model = Model()
        self._model.register(self._agent)

        # Tell the agent about our connection to the broker
        self._agent.set_connection(self._connection)

        # Instantiate and populate an instance of the Parent
        # Schema Object
        self._parent = qmf.AgentObject(self._model.parent_class)

        ## @todo how do we force a test failure?
        # verify the properties() and statistics() object methods:
        assert len(self._parent.properties()) == 13
        assert len(self._parent.statistics()) == 1

        self._parent.set_attr("name", "Parent One")
        self._parent.set_attr("state", "OPERATIONAL")

        self._parent.set_attr("uint64val", 0)
        self._parent.set_attr("uint32val", 0)
        self._parent.set_attr("uint16val", 0)
        self._parent.set_attr("uint8val",  0)

        self._parent.set_attr("int64val", 0)
        self._parent.set_attr("int32val", 0)
        self._parent.set_attr("int16val", 0)
        self._parent.set_attr("int8val",  0)

        self._parent_oid = self._agent.alloc_object_id(1)
        self._parent.set_object_id(self._parent_oid)

        # Now wait for events arriving on the connection
        # to the broker...
        while True:
            time.sleep(1000)



app = App()
app.main()