summaryrefslogtreecommitdiff
path: root/extras/qmf/src/py/qmf2-prototype/tests/console_test.py
blob: ac0e064f2018fafa17d9aeeb7d56159210bd9b06 (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
# 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 logging
import time
from threading import Semaphore


from qpid.messaging import *
from qmf2.common import (Notifier, QmfQuery, QmfQueryPredicate, MsgKey,
                       SchemaClassId, SchemaClass, QmfData) 
from qmf2.console import Console


class ExampleNotifier(Notifier):
    def __init__(self):
        self._sema4 = Semaphore(0)   # locked

    def indication(self):
        self._sema4.release()

    def waitForWork(self):
        print("Waiting for event...")
        self._sema4.acquire()
        print("...event present")


logging.getLogger().setLevel(logging.INFO)

print( "Starting Connection" )
_c = Connection("localhost")
_c.connect()

print( "Starting Console" )

_notifier = ExampleNotifier()
_myConsole = Console(notifier=_notifier)
_myConsole.addConnection( _c )

# Allow discovery only for the agent named "qmf.testAgent"
# @todo: replace "manual" query construction with 
# a formal class-based Query API
_query = QmfQuery.create_predicate(QmfQuery.TARGET_AGENT, 
                                   QmfQueryPredicate({QmfQuery.CMP_EQ:
                                                          [QmfQuery.KEY_AGENT_NAME,
                                                           "qmf.testAgent"]}))
_myConsole.enable_agent_discovery(_query)

_done = False
while not _done:
#    try:
    _notifier.waitForWork()

    _wi = _myConsole.get_next_workitem(timeout=0)
    while _wi:
        print("!!! work item received %d:%s" % (_wi.get_type(),
                                                str(_wi.get_params())))


        if _wi.get_type() == _wi.AGENT_ADDED:
            _agent = _wi.get_params().get("agent")
            if not _agent:
                print("!!!! AGENT IN REPLY IS NULL !!! ")

            _query = QmfQuery.create_wildcard(QmfQuery.TARGET_OBJECT_ID)
            oid_list = _myConsole.doQuery(_agent, _query)

            print("!!!************************** REPLY=%s" % oid_list)

            for oid in oid_list:
                _query = QmfQuery.create_id(QmfQuery.TARGET_OBJECT, 
                                            oid)
                obj_list = _myConsole.doQuery(_agent, _query)

                print("!!!************************** REPLY=%s" % obj_list)

                if obj_list is None:
                    obj_list={}

                for obj in obj_list:
                    resp = obj.invoke_method( "set_meth", 
                                              {"arg_int": -11,
                                               "arg_str": "are we not goons?"},
                                              None,
                                              3)
                    if resp is None:
                        print("!!!*** NO RESPONSE FROM METHOD????") 
                    else:
                        print("!!! method succeeded()=%s" % resp.succeeded())
                        print("!!! method exception()=%s" % resp.get_exception())
                        print("!!! method get args() = %s" % resp.get_arguments())

                    if not obj.is_described():
                        resp = obj.invoke_method( "bad method", 
                                                  {"arg_int": -11,
                                                   "arg_str": "are we not goons?"},
                                                  None,
                                                  3)
                        if resp is None:
                            print("!!!*** NO RESPONSE FROM METHOD????") 
                        else:
                            print("!!! method succeeded()=%s" % resp.succeeded())
                            print("!!! method exception()=%s" % resp.get_exception())
                            print("!!! method get args() = %s" % resp.get_arguments())


            #---------------------------------
            #_query = QmfQuery.create_id(QmfQuery.TARGET_OBJECT, "99another name")

            #obj_list = _myConsole.doQuery(_agent, _query)

            #---------------------------------

            # _query = QmfQuery.create_wildcard(QmfQuery.TARGET_PACKAGES)

            # package_list = _myConsole.doQuery(_agent, _query)

            # for pname in package_list:
            #     print("!!! Querying for schema from package: %s" % pname)
            #     _query = QmfQuery.create_predicate(QmfQuery.TARGET_SCHEMA_ID,
            #                                        QmfQueryPredicate(
            #             {QmfQuery.CMP_EQ: [SchemaClassId.KEY_PACKAGE, pname]}))

            #     schema_id_list = _myConsole.doQuery(_agent, _query)
            #     for sid in schema_id_list:
            #         _query = QmfQuery.create_predicate(QmfQuery.TARGET_SCHEMA,
            #                                            QmfQueryPredicate(
            #                 {QmfQuery.CMP_EQ: [SchemaClass.KEY_SCHEMA_ID,
            #                                    sid.map_encode()]}))

            #         schema_list = _myConsole.doQuery(_agent, _query)
            #         for schema in schema_list:
            #             sid = schema.get_class_id()
            #             _query = QmfQuery.create_predicate(
            #                 QmfQuery.TARGET_OBJECT_ID,
            #                 QmfQueryPredicate({QmfQuery.CMP_EQ:
            #                                        [QmfData.KEY_SCHEMA_ID,
            #                                         sid.map_encode()]}))

            #             oid_list = _myConsole.doQuery(_agent, _query)
            #             for oid in oid_list:
            #                 _query = QmfQuery.create_id(
            #                     QmfQuery.TARGET_OBJECT, oid)
            #                 _reply = _myConsole.doQuery(_agent, _query)

            #                 print("!!!************************** REPLY=%s" % _reply)


        _myConsole.release_workitem(_wi)
        _wi = _myConsole.get_next_workitem(timeout=0)
#    except:
#        logging.info( "shutting down..." )
#        _done = True

print( "Removing connection" )
_myConsole.removeConnection( _c, 10 )

print( "Destroying console:" )
_myConsole.destroy( 10 )

print( "******** console test done ********" )