forked from Juniper/contrail-third-party
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathifmap-python-patch1.diff
More file actions
375 lines (340 loc) · 15.6 KB
/
ifmap-python-patch1.diff
File metadata and controls
375 lines (340 loc) · 15.6 KB
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
diff --git a/third_party/ifmap-python-client/__init__.py b/third_party/ifmap-python-client/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/third_party/ifmap-python-client/ifmap/client.py b/third_party/ifmap-python-client/ifmap/client.py
index c0f748c..103caa6 100644
--- a/third_party/ifmap-python-client/ifmap/client.py
+++ b/third_party/ifmap-python-client/ifmap/client.py
@@ -5,7 +5,18 @@
# Open Source, see LICENSE
#
+import ssl
+import gevent
+import geventhttpclient
+from geventhttpclient import HTTPClient
+
import urllib
+
+import base64
+import cStringIO
+import sys
+
+
from logging import getLogger
log = getLogger(__name__) # when imported, the logger will be named "ifmap.client"
@@ -23,23 +34,33 @@ except ImportError:
f = http_client_lib.urlopen(http_client_lib.Request(url, body, headers))
return f.info(), f.read()
+#import urllib2 as http_client_lib
+#class Http(): # wrapper to use when httplib2 not available
+# def request(self, url, method, body, headers):
+# f = http_client_lib.urlopen(http_client_lib.Request(url, body, headers))
+# return f.info(), f.read()
+
namespaces = {
'env' : "http://www.w3.org/2003/05/soap-envelope",
'ifmap' : "http://www.trustedcomputinggroup.org/2010/IFMAP/2",
- 'meta' : "http://www.trustedcomputinggroup.org/2010/IFMAP-METADATA/2",
+ 'meta' : "http://www.trustedcomputinggroup.org/2010/IFMAP-METADATA/2"
}
class client:
"""
IF-MAP client
"""
- http = Http()
+
__url = None
__session_id = None
__publisher_id = None
__last_sent = None
__last_received = None
__namespaces = None
+ __ssl_options = {
+ 'cert_reqs' : ssl.CERT_NONE,
+ 'ssl_version' : ssl.PROTOCOL_TLSv1,
+ }
__envelope ="""<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" %(ns)s>
@@ -49,18 +70,31 @@ class client:
</env:Envelope>
"""
- def __init__(self, url, user=None, password=None, namespaces={}):
+ def __init__(self, url, user=None, password=None, namespaces={}, ssl_opts=None):
if user and password:
- self.__password_mgr=http_client_lib.HTTPPasswordMgrWithDefaultRealm()
- self.__password_mgr.add_password(None, url, user, password)
- handler = http_client_lib.HTTPBasicAuthHandler(self.__password_mgr)
- opener = http_client_lib.build_opener(handler)
- http_client_lib.install_opener(opener)
+# self.__password_mgr=http_client_lib.HTTPPasswordMgrWithDefaultRealm()
+# self.__password_mgr.add_password(None, url, user, password)
+# handler = http_client_lib.HTTPBasicAuthHandler(self.__password_mgr)
+# opener = http_client_lib.build_opener(handler)
+# http_client_lib.install_opener(opener)
- if namespaces:
- self.__namespaces = namespaces
+ #pycurl.global_init(pycurl.GLOBAL_SSL)
+
+ pass
+
+ #if namespaces:
+ self.__namespaces = namespaces
+ if ssl_opts:
+ self.__ssl_options.update(ssl_opts)
self.__url = url
+ self.__username = user
+ self.__password = password
+ self._http = HTTPClient(*self.__url, ssl = True,
+ connection_timeout = None,
+ network_timeout = None,
+ ssl_options = self.__ssl_options)
+
def last_sent(self):
return self.__last_sent
@@ -71,31 +105,77 @@ class client:
def envelope(self, body) :
_ns = ""
for ns_prefix, ns_uri in self.__namespaces.items():
- if ns_prefix == "env": break # don't add the envelope namespace again
+ #if ns_prefix == "env": break # don't add the envelope namespace again
+ if ns_prefix == "env": continue # don't add the envelope namespace again
_ns += "xmlns:"+ns_prefix+'="'+ns_uri+'" '
return self.__envelope % {'body':body, 'ns': _ns}
def call(self, method, body):
xml = self.envelope(body)
+ #headers={
+ # 'Content-type': 'text/xml; charset="UTF-8"',
+ # 'Content-length': str(len(xml)),
+ # "SOAPAction": '"%s"' % (method),
+ #}
+
+ base64string = base64.encodestring('%s:%s' % (self.__username, self.__password)).replace('\n', '')
+ # pycurl
+ #headers=[
+ # 'Content-type: text/xml; charset="UTF-8"',
+ # 'Content-length: %s' %(str(len(xml))),
+ # 'Authorization : Basic %s' %(base64string),
+ # 'SOAPAction: %s' % (method),
+ #]
+
+ # geventhttp
headers={
'Content-type': 'text/xml; charset="UTF-8"',
- 'Content-length': str(len(xml)),
- "SOAPAction": '"%s"' % (method),
+ 'Content-length': '%s' %(str(len(xml))),
+ 'Authorization': 'Basic %s' %(base64string),
+ 'SOAPAction': '%s' % (method),
}
+
try:
log.info("sending IF-MAP message to server")
log.debug("======== sending IF-MAP message ========")
log.debug("\n%s\n", xml)
log.debug("======== /sending IF-MAP message ========")
- response, content = self.http.request(self.__url,"POST", body=xml, headers=headers )
+ #response, content = self.http.request(self.__url,"POST", body=xml, headers=headers )
+
+ #self.http = pycurl.Curl()
+ #self.http.setopt(pycurl.URL, self.__url)
+ #self.http.setopt(pycurl.HTTPHEADER, headers)
+ #self.http.setopt(pycurl.POSTFIELDS, xml)
+ #self.http.setopt(pycurl.VERBOSE, True)
+ #self.http.setopt(pycurl.SSL_VERIFYPEER, 0)
+ #self.http.setopt(pycurl.SSL_VERIFYHOST, 0)
+ #content = cStringIO.StringIO()
+ #self.http.setopt(pycurl.WRITEFUNCTION,
+ # content.write)
+ #self.http.perform()
+
+ #self.http = HTTPClient(*self.__url, ssl = True,
+ # ssl_options = {'cert_reqs': gevent.ssl.CERT_NONE,
+ # 'ssl_version': PROTOCOL_SSLv3})
+ #response = self.http.post('/', body = xml, headers = headers)
+ response = self._http.post('/', body = xml, headers = headers)
+ content = response.read()
+
self.__last_sent = xml
+
+ #self.__last_received = content
+ #pycurl self.__last_received = content.getvalue()
self.__last_received = content
log.debug("======== received IF-MAP response ========")
+ #log.debug("\n%s\n", content)
+ #pycurl log.debug("\n%s\n", content.getvalue())
log.debug("\n%s\n", content)
log.debug("======== /receive IF-MAP response ========")
+ #return content
+ #pycurl return content.getvalue()
return content
except HttpException, e:
diff --git a/third_party/ifmap-python-client/ifmap/id.py b/third_party/ifmap-python-client/ifmap/id.py
index 04178f7..7a71d51 100644
--- a/third_party/ifmap-python-client/ifmap/id.py
+++ b/third_party/ifmap-python-client/ifmap/id.py
@@ -139,7 +139,7 @@ class Identity(ifmapIDFactory):
if self.__type:
self.__XML +=' type="'+self.__type+'"'
if self.__other_type:
- self.__XML +=' other-type="'+self.__other_type+'"'
+ self.__XML +=' other-type-definition="'+self.__other_type+'"'
if self.__administrative_domain:
self.__XML += ' administrative-domain="'+self.__administrative_domain+'"'
self.__XML += " />"
diff --git a/third_party/ifmap-python-client/ifmap/metadata.py b/third_party/ifmap-python-client/ifmap/metadata.py
index 303d22e..17f4515 100644
--- a/third_party/ifmap-python-client/ifmap/metadata.py
+++ b/third_party/ifmap-python-client/ifmap/metadata.py
@@ -33,4 +33,4 @@ class Metadata(MetadataBase):
def __str__(self):
__attr = ' '+ attr(self.__attributes)
return '<metadata><' + self.__name + self.__ns_uri + __attr + '>' + self.__value + self.__elements + '</' + self.__name + '></metadata>'
-
\ No newline at end of file
+
diff --git a/third_party/ifmap-python-client/ifmap/request.py b/third_party/ifmap-python-client/ifmap/request.py
index a9df7a3..47bc1f6 100644
--- a/third_party/ifmap-python-client/ifmap/request.py
+++ b/third_party/ifmap-python-client/ifmap/request.py
@@ -15,6 +15,7 @@ class NewSessionRequest(RequestBase):
self.__max_poll_result = max_poll_result
def __str__(self):
+ #import pdb; pdb.set_trace()
return '<ifmap:newSession %s' % (attr({'max-poll-result-size':self.__max_poll_result})) + '/>';
class RenewSessionRequest(RequestBase):
diff --git a/third_party/ifmap-python-client/ifmap/response.py b/third_party/ifmap-python-client/ifmap/response.py
index f66040e..179fd01 100644
--- a/third_party/ifmap-python-client/ifmap/response.py
+++ b/third_party/ifmap-python-client/ifmap/response.py
@@ -16,10 +16,13 @@ class Response():
Take a result string and process it
"""
if result:
- __env = ElementTree.fromstring(result)
- __body = __env.find('{http://www.w3.org/2003/05/soap-envelope}Body')
- __response = __body.find('{http://www.trustedcomputinggroup.org/2010/IFMAP/2}response')
- self.__xml = __response.find('*')
+ env = ElementTree.fromstring(result)
+ body = env.find('{http://www.w3.org/2003/05/soap-envelope}Body')
+ response = body.find('{http://www.trustedcomputinggroup.org/2010/IFMAP/2}response')
+ # xml.etree.ElementTree find is broken in python 2.6
+ children = response.findall('*')
+ if len(children):
+ self.__xml = children[0]
def element(self):
"""
@@ -38,6 +41,7 @@ class newSessionResult(Response):
newSessionResult
"""
def __init__(self, result):
+ #import pdb; pdb.set_trace()
self.__newSession = Response(result).element()
def get_session_id(self):
@@ -48,4 +52,4 @@ class newSessionResult(Response):
-
\ No newline at end of file
+
diff --git a/third_party/ifmap-python-client/ifmap/util.py b/third_party/ifmap-python-client/ifmap/util.py
index e777f07..e4d06dd 100644
--- a/third_party/ifmap-python-client/ifmap/util.py
+++ b/third_party/ifmap-python-client/ifmap/util.py
@@ -13,7 +13,7 @@ def attr(attributes):
if attributes and (type(attributes) == type({})): # check if it is a dictionary
__xml = ""
for label, value in attributes.items():
- if value:
+ if value != None:
__xml += label + '="' + value + '" '
return __xml
else:
@@ -25,9 +25,10 @@ def link_ids(id1, id2):
Returns XML for id1 or links id1 and id2 together
"""
if id1 and id2: # Both exist, so link them
- return '<link>' + id1 + id2 + '</link>'
+ #return '<link>' + id1 + id2 + '</link>'
+ return id1 + id2
else:
return id1
-
\ No newline at end of file
+
diff --git a/third_party/ifmap-python-client/testmap.py b/third_party/ifmap-python-client/testmap.py
index 8c9142a..17b5456 100644
--- a/third_party/ifmap-python-client/testmap.py
+++ b/third_party/ifmap-python-client/testmap.py
@@ -140,33 +140,71 @@ from ifmap.metadata import Metadata
def client_test():
print 'testing ifmap client (this requires a running server)'
- mapclient = client("https://127.0.0.1:8443", 'test', 'test', namespaces)
+ mapclient = client("https://127.0.0.1:443", 'test', 'test', namespaces)
result = mapclient.call('newSession', NewSessionRequest())
mapclient.set_session_id(newSessionResult(result).get_session_id())
mapclient.set_publisher_id(newSessionResult(result).get_publisher_id())
- meta = str(Metadata('role', 'employee', {'ifmap-cardinality':'multiValue'}))
- pubreq = PublishRequest(mapclient.get_session_id(), str(PublishUpdateOperation(id1=str(IPAddress("10.0.0.1")), metadata=meta, lifetime='forever')))
+ meta = str(Metadata('iperms', 'rwxrwxrwx',
+ {'ifmap-cardinality':'multiValue'}, ns_prefix = "ct",
+ elements = '<ct:mperms>rwxrwxrwx</ct:mperms>'))
+ pubreq = PublishRequest(mapclient.get_session_id(),
+ str(PublishUpdateOperation(
+ id1=str(Identity(name = "ct:cloud", type = "other",
+ other_type = "extended")),
+ metadata=meta,
+ lifetime='forever')))
result = mapclient.call('publish', pubreq)
- searchreq = SearchRequest(mapclient.get_session_id(), str(IPAddress("10.0.0.1")), validation="None")
- result = mapclient.call('search', searchreq)
+ meta = str(Metadata('iperms', 'rwxrwxrwx',
+ {'ifmap-cardinality':'multiValue'}, ns_prefix = "ct",
+ elements = '<ct:mperms>rwxrwxrwx</ct:mperms>'))
+ pubreq = PublishRequest(mapclient.get_session_id(),
+ str(PublishUpdateOperation(
+ id1=str(Identity(name = "ct:tenant:infra", type = "other",
+ other_type = "extended")),
+ metadata=meta,
+ lifetime='forever')))
+ result = mapclient.call('publish', pubreq)
+
+ meta = str(Metadata('belongs-to', '',
+ {'ifmap-cardinality':'singleValue'}, ns_prefix = "ct",
+ elements = '<ct:mperms>rwxrwxrwx</ct:mperms>'))
+ pubreq = PublishRequest(mapclient.get_session_id(),
+ str(PublishUpdateOperation(
+ id1=str(Identity(name = "ct:cloud", type = "other",
+ other_type = "extended")),
+ id2=str(Identity(name = "ct:tenant:infra", type = "other",
+ other_type = "extended")),
+ metadata=meta,
+ lifetime='forever')))
+ result = mapclient.call('publish', pubreq)
+
+ #meta = str(Metadata('role', 'employee', {'ifmap-cardinality':'multiValue'}))
+ #pubreq = PublishRequest(mapclient.get_session_id(), str(PublishUpdateOperation(id1=str(IPAddress("10.0.0.1")), metadata=meta, lifetime='forever')))
+ #result = mapclient.call('publish', pubreq)
+
+ #searchreq = SearchRequest(mapclient.get_session_id(), str(IPAddress("10.0.0.1")), validation="None")
+ #result = mapclient.call('search', searchreq)
- subreq = SubscribeRequest(mapclient.get_session_id(), operations=str(SubscribeUpdateOperation('subscription-1',str(IPAddress("10.0.0.1")))))
- result = mapclient.call('subscribe', subreq)
+ #subreq = SubscribeRequest(mapclient.get_session_id(), operations=str(SubscribeUpdateOperation('subscription-1',str(IPAddress("10.0.0.1")))))
+ #result = mapclient.call('subscribe', subreq)
- pollreq = PollRequest(mapclient.get_session_id())
- result = mapclient.call('poll', pollreq)
+ #pollreq = PollRequest(mapclient.get_session_id())
+ #result = mapclient.call('poll', pollreq)
- subreq = SubscribeRequest(mapclient.get_session_id(), operations=str(SubscribeDeleteOperation('subscription-1')))
- result = mapclient.call('subscribe', subreq)
+ #subreq = SubscribeRequest(mapclient.get_session_id(), operations=str(SubscribeDeleteOperation('subscription-1')))
+ #result = mapclient.call('subscribe', subreq)
- purgereq = PurgeRequest(mapclient.get_session_id(), mapclient.get_publisher_id())
- result = mapclient.call('purge', purgereq)
+ #purgereq = PurgeRequest(mapclient.get_session_id(), mapclient.get_publisher_id())
+ #result = mapclient.call('purge', purgereq)
- endreq = EndSessionRequest(mapclient.get_session_id())
- result = mapclient.call('endSession', endreq)
+ #endreq = EndSessionRequest(mapclient.get_session_id())
+ #result = mapclient.call('endSession', endreq)
+ import time
+ while True:
+ time.sleep(10)