summaryrefslogtreecommitdiff
path: root/python/qpid/management.py
blob: a5ad997a24a30fec1f6d20c4d725c85f031eae46 (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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#
# 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.
#

"""
Management API for Qpid
"""

import qpid
import base64
import socket
from threading    import Thread
from message      import Message
from time         import sleep
from qpid.client  import Client
from qpid.content import Content
from cStringIO    import StringIO
from codec        import Codec, EOF

#===================================================================
# ManagementMetadata
#
#    One instance of this class is created for each ManagedBroker.  It
#    is used to store metadata from the broker which is needed for the
#    proper interpretation of recevied management content.
#
#===================================================================
class ManagementMetadata:

  def parseSchema (self, cls, codec):
    className   = codec.decode_shortstr ()
    configCount = codec.decode_short ()
    instCount   = codec.decode_short ()
    methodCount = codec.decode_short ()
    eventCount  = codec.decode_short ()

    configs = []
    insts   = []
    methods = []
    events  = []

    configs.append (("id", 4, "", "", 1, 1, None, None, None, None, None))
    insts.append   (("id", 4, None, None))

    for idx in range (configCount):
      ft = codec.decode_table ()
      name   = ft["name"]
      type   = ft["type"]
      access = ft["access"]
      index  = ft["index"]
      unit   = None
      min    = None
      max    = None
      maxlen = None
      desc   = None

      for key, value in ft.items ():
        if   key == "unit":
          unit = value
        elif key == "min":
          min = value
        elif key == "max":
          max = value
        elif key == "maxlen":
          maxlen = value
        elif key == "desc":
          desc = value

      config = (name, type, unit, desc, access, index, min, max, maxlen)
      configs.append (config)

    for idx in range (instCount):
      ft = codec.decode_table ()
      name   = ft["name"]
      type   = ft["type"]
      unit   = None
      desc   = None

      for key, value in ft.items ():
        if   key == "unit":
          unit = value
        elif key == "desc":
          desc = value

      inst = (name, type, unit, desc)
      insts.append (inst)

    # TODO: Handle notification of schema change outbound
    self.schema[(className,'C')] = configs
    self.schema[(className,'I')] = insts
    self.schema[(className,'M')] = methods
    self.schema[(className,'E')] = events

  def parseContent (self, cls, codec):
    if cls == 'C' and self.broker.config_cb == None:
      return
    if cls == 'I' and self.broker.inst_cb == None:
      return

    className = codec.decode_shortstr ()

    if (className,cls) not in self.schema:
      return

    row        = []
    timestamps = []

    timestamps.append (codec.decode_longlong ())  # Current Time
    timestamps.append (codec.decode_longlong ())  # Create Time
    timestamps.append (codec.decode_longlong ())  # Delete Time

    for element in self.schema[(className,cls)][:]:
      tc   = element[1]
      name = element[0]
      if   tc == 1: # TODO: Define constants for these
        data = codec.decode_octet ()
      elif tc == 2:
        data = codec.decode_short ()
      elif tc == 3:
        data = codec.decode_long ()
      elif tc == 4:
        data = codec.decode_longlong ()
      elif tc == 5:
        data = codec.decode_octet ()
      elif tc == 6:
        data = codec.decode_shortstr ()
      elif tc == 7:
        data = codec.decode_longstr ()
      else:
        raise ValueError ("Invalid type code: %d" % tc)
      row.append ((name, data))

    if cls == 'C':
      self.broker.config_cb[1] (self.broker.config_cb[0], className, row, timestamps)
    elif cls == 'I':
      self.broker.inst_cb[1]   (self.broker.inst_cb[0], className, row, timestamps)

  def parse (self, codec):
    opcode = chr (codec.decode_octet ())
    cls    = chr (codec.decode_octet ())

    if opcode == 'S':
      self.parseSchema (cls, codec)

    elif opcode == 'C':
      self.parseContent (cls, codec)

    else:
      raise ValueError ("Unknown opcode: %c" % opcode);

  def __init__ (self, broker):
    self.broker = broker
    self.schema = {}


#===================================================================
# ManagedBroker
#
#    An object of this class represents a connection (over AMQP) to a
#    single managed broker.
#
#===================================================================
class ManagedBroker:

  mExchange = "qpid.management"
  dExchange = "amq.direct"

  def checkHeader (self, codec):
    octet = chr (codec.decode_octet ())
    if octet != 'A':
      return 0
    octet = chr (codec.decode_octet ())
    if octet != 'M':
      return 0
    octet = chr (codec.decode_octet ())
    if octet != '0':
      return 0
    octet = chr (codec.decode_octet ())
    if octet != '1':
      return 0
    return 1

  def publish_cb (self, msg):
    codec = Codec (StringIO (msg.content.body), self.spec)

    if self.checkHeader (codec) == 0:
      raise ValueError ("outer header invalid");

    self.metadata.parse (codec)
    msg.complete ()

  def reply_cb (self, msg):
    codec = Codec (StringIO (msg.content.body), self.spec)
    methodId = codec.decode_long ()
    status   = codec.decode_long ()
    sText    = codec.decode_shortstr ()

    args = {}
    if status == 0:
      args["sequence"] = codec.decode_long ()
      args["body"]     = codec.decode_longstr ()

    if self.method_cb != None:
      self.method_cb[1] (self.method_cb[0], methodId, status, sText, args)

    msg.complete ()

  def __init__ (self,
                host     = "localhost",
                port     = 5672,
                username = "guest",
                password = "guest",
                specfile = "../specs/amqp.0-10-preview.xml"):

    self.spec = qpid.spec.load (specfile)
    self.client    = None
    self.channel   = None
    self.queue     = None
    self.rqueue    = None
    self.qname     = None
    self.rqname    = None
    self.metadata  = ManagementMetadata (self)
    self.connected = 0
    self.lastConnectError = None

    #  Initialize the callback records
    self.status_cb = None
    self.schema_cb = None
    self.config_cb = None
    self.inst_cb   = None
    self.method_cb = None

    self.host     = host
    self.port     = port
    self.username = username
    self.password = password

  def statusListener (self, context, callback):
    self.status_cb = (context, callback)

  def schemaListener (self, context, callback):
    self.schema_cb = (context, callback)

  def configListener (self, context, callback):
    self.config_cb = (context, callback)

  def methodListener (self, context, callback):
    self.method_cb = (context, callback)

  def instrumentationListener (self, context, callback):
    self.inst_cb = (context, callback)

  def method (self, methodId, objId, className,
              methodName, args=None, packageName="qpid"):
    codec = Codec (StringIO (), self.spec);
    codec.encode_long     (methodId)
    codec.encode_longlong (objId)
    codec.encode_shortstr (self.rqname)

    # TODO: Encode args according to schema
    if methodName == "echo":
      codec.encode_long (args["sequence"])
      codec.encode_longstr (args["body"])

    msg = Content (codec.stream.getvalue ())
    msg["content_type"] = "application/octet-stream"
    msg["routing_key"]  = "method." + packageName + "." + className + "." + methodName
    msg["reply_to"]     = self.spec.struct ("reply_to")
    self.channel.message_transfer (destination="qpid.management", content=msg)

  def isConnected (self):
    return connected

  def start (self):
    print "Connecting to broker %s:%d" % (self.host, self.port)

    try:
      self.client = Client (self.host, self.port, self.spec)
      self.client.start ({"LOGIN": self.username, "PASSWORD": self.password})
      self.channel = self.client.channel (1)
      response = self.channel.session_open (detached_lifetime=10)
      self.qname  = "mgmt-"  + base64.urlsafe_b64encode (response.session_id)
      self.rqname = "reply-" + base64.urlsafe_b64encode (response.session_id)

      self.channel.queue_declare (queue=self.qname,  exclusive=1, auto_delete=1)
      self.channel.queue_declare (queue=self.rqname, exclusive=1, auto_delete=1)
      
      self.channel.queue_bind (exchange=ManagedBroker.mExchange, queue=self.qname,
                               routing_key="mgmt.#")
      self.channel.queue_bind (exchange=ManagedBroker.dExchange, queue=self.rqname,
                               routing_key=self.rqname)

      self.channel.message_subscribe (queue=self.qname,  destination="mdest")
      self.channel.message_subscribe (queue=self.rqname, destination="rdest")

      self.queue = self.client.queue ("mdest")
      self.queue.listen (self.publish_cb)

      self.channel.message_flow_mode (destination="mdest", mode=1)
      self.channel.message_flow (destination="mdest", unit=0, value=0xFFFFFFFF)
      self.channel.message_flow (destination="mdest", unit=1, value=0xFFFFFFFF)

      self.rqueue = self.client.queue ("rdest")
      self.rqueue.listen (self.reply_cb)

      self.channel.message_flow_mode (destination="rdest", mode=1)
      self.channel.message_flow (destination="rdest", unit=0, value=0xFFFFFFFF)
      self.channel.message_flow (destination="rdest", unit=1, value=0xFFFFFFFF)

      self.connected = 1

    except socket.error, e:
      print "Socket Error Detected:", e[1]
      self.lastConnectError = e
      raise
    except:
      raise

  def stop (self):
    pass