/
opserver_introspect_utils.py
executable file
·363 lines (326 loc) · 11.8 KB
/
opserver_introspect_utils.py
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
import sys
vizdtestdir = sys.path[0]
sys.path.insert(1, vizdtestdir + '/../../')
import urllib2
import xmltodict
import json
import requests
import socket
from lxml import etree
from tcutils.verification_util import *
from opserver_results import *
from opserver_util import OpServerUtils
from tcutils.util import *
class VerificationOpsSrv (VerificationUtilBase):
def __init__(self, ip, port=8081, logger=LOG, inputs=None):
super(VerificationOpsSrv, self).__init__(ip, port, logger=logger, args=inputs,
use_admin_auth=True)
def get_ops_generator(self, generator=None,
moduleid=None, node_type=None,
instanceid='0'):
'''http://nodea29:8081/analytics/uves/generator\
/nodea18:Control:Contrail-Control:0?flat'''
if (generator == None):
generator = socket.gethostname()
if (moduleid == None):
self.logger.info("module id not passed")
return None
if instanceid == None:
instanceid = 0
if node_type == None:
self.logger.info("node type is not passed")
return None
res = None
try:
generator_dict = self.dict_get(
'analytics/uves/generator/' + generator + \
':' + node_type + ':' + moduleid + ':' \
+ instanceid + '?flat')
res = OpGeneratorResult(generator_dict)
except Exception as e:
print e
finally:
return res
def get_ops_vrouter(self, vrouter=None):
if (vrouter == None):
vrouter = socket.gethostname()
res = None
try:
vrouter_dict = self.dict_get(
'analytics/uves/vrouter/' + vrouter + '?flat')
res = OpVRouterResult(vrouter_dict)
except Exception as e:
print e
finally:
return res
def get_ops_bgprouter(self, bgprouter=None):
if (bgprouter == None):
bgprouter = socket.gethostname()
res = None
try:
bgprouter_dict = self.dict_get(
'analytics/uves/control-node/' + bgprouter + '?flat')
res = OpBGPRouterResult(bgprouter_dict)
except Exception as e:
print e
finally:
return res
def get_ops_vn(self, vn_fq_name='default-virtual-network'):
res = None
try:
vn_dict = self.dict_get(
'analytics/uves/virtual-network/' + vn_fq_name + '?flat')
res = OpVNResult(vn_dict)
except Exception as e:
print e
finally:
return res
def get_ops_vm(self, vm='default-virtual-machine'):
res = None
try:
vm_dict = self.dict_get(
'analytics/uves/virtual-machine/' + vm + '?flat')
res = OpVMResult(vm_dict)
except Exception as e:
print e
finally:
return res
def get_ops_svc_instance(self, project='admin',
svc_instance=None):
'''analytics/uves/service-instance/default-domain:\
admin:svc-instance1?flat'''
res = None
try:
si_dict = self.dict_get(
'analytics/uves/service-instance/' + svc_instance + '?flat')
res = OpSIResult(si_dict)
except Exception as e:
print e
finally:
return res
def get_ops_svc_template(self,
left_vn=None,
right_vn=None):
'''analytics/uves/service-chain/\
sc:default-domain:admin:vn1:\
default-domain:admin:fip_vn?flat'''
res = None
try:
st_dict = self.dict_get(
'analytics/uves/service-chain/sc:' + left_vn + \
':' + right_vn + '?flat')
res = OpSTResult(st_dict)
except Exception as e:
print e
finally:
return res
def get_hrefs_to_all_UVEs_of_a_given_UVE_type(self, uveType=None):
'''Get all hrefs for a uve type'''
if uveType:
if uveType == 'tables':
dct = self.dict_get('analytics/' + uveType)
else:
dct = self.dict_get('analytics/uves/' + uveType)
else:
dct = self.dict_get('analytics/uves')
ret_value = []
for elem in dct:
self.ame = OpHrefResult(elem)
ret_value.append(self.ame)
return ret_value
def get_hrefs_to_all_tables(self, uveType=None):
'''Get all hrefs for a uve type'''
if uveType == 'tables':
dct = self.dict_get('analytics/' + uveType)
else:
dct = self.dict_get('analytics/uves/' + uveType)
ret_value = []
for elem in dct:
self.tme = OpHrefTableResult(elem)
ret_value.append(self.tme)
return ret_value
def send_trace_to_database(self, node=None,
module=None, instance_id='0',
trace_buffer_name=None):
'''http://<opserver-ip>:8081/analytics/\
send-tracebuffer/nodeb8/Contrail-Vrouter-Agent/UveTrace'''
res = None
try:
res = self.dict_get('analytics/send-tracebuffer/' + node +
'/' + module + '/' +
instance_id + '/' + trace_buffer_name)
except Exception as e:
print e
finally:
return res
def get_ops_bgp_peer(self, peer_toupe=None):
'''http://nodea18:8081/analytics/uves/bgp-peer/\
default-domain:default-project:ip-fabric:__default\
__:nodea19:default-domain:default-project:ip-fabric:\
__default__:nodea18?flat'''
res = None
try:
bgp_node = peer_toupe[0]
peer = peer_toupe[1]
link = 'analytics/uves/bgp-peer\
/default-domain:default-project:\
ip-fabric:__default__:' +\
bgp_node + ':' + 'default-domain:\
default-project:ip-fabric:__default__:' \
+ peer + '?flat'
dct = self.dict_get("".join(link.split()))
res = OpBGPPeerResult(dct)
except Exception as e:
print e
finally:
return res
def get_ops_bgp_xmpp_peer(self, peer_toupe=None):
'''http://nodea29.englab.juniper.net:8081\
/analytics/uves/xmpp-peer/nodea29:10.204.216.15?flat'''
res = None
try:
bgp_node = peer_toupe[0]
peer = peer_toupe[1]
dct = self.dict_get(
'analytics/uves/xmpp-peer/' + bgp_node + ':' + peer + '?flat')
res = OpBGPXmppPeerResult(dct)
except Exception as e:
print e
finally:
return res
def get_ops_collector(self, collector=None):
'''http://nodea18:8081/analytics/uves/analytics-node/nodea29?flat'''
res = None
try:
c_dict = self.dict_get(
'analytics/uves/analytics-node/' + collector + '?flat')
res = OpCollectorResult(c_dict)
except Exception as e:
print e
finally:
return res
def get_ops_alarms(self):
'''http://nodea18:8081/analytics/alarms'''
res = None
try:
c_dict = self.dict_get(
'analytics/alarms')
res = OpCollectorResult(c_dict)
except Exception as e:
print e
finally:
return res
def get_ops_config(self, config=None):
'''http://nodea18:8081/analytics/uves/config-node/nodea11?flat'''
res = None
try:
c_dict = self.dict_get(
'analytics/uves/config-node/' + config + '?flat')
res = OpConfigResult(c_dict)
except Exception as e:
print e
finally:
return res
def get_ops_db(self, db=None):
'''http://10.204.216.7:8081/analytics/uves/database/nodea11?flat'''
res = None
try:
c_dict = self.dict_get(
'analytics/uves/database-node/' + db + '?flat')
res = OpDbResult(c_dict)
except Exception as e:
print e
finally:
return res
def get_ops_vm_intf(self, intf):
'''http://nodea24:8081/analytics/uves/
virtual-machine-interface/
default-domain:admin:0e207bb1-5811-4595-a8b6-18e890838f60?flat'''
res = None
try:
c_dict = self.dict_get(
'analytics/uves/virtual-machine-interface/' + intf + '?flat')
res = OpVmIntfResult(c_dict)
except Exception as e:
print e
finally:
return res
def get_ops_sc_uve(self):
'''http://nodea18:8081/analytics/uves/service-chain/*'''
res = None
try:
c_dict = self.dict_get(
'analytics/uves/service-chain/*')
res = OpServiceChainResult(c_dict)
except Exception as e:
print e
finally:
return res
# @timeout(600, os.strerror(errno.ETIMEDOUT))
def post_query(self, table, start_time=None, end_time=None,
select_fields=None,
where_clause='',
sort_fields=None, sort=None, limit=None, filter=None, dir=None):
res = None
try:
flows_url = OpServerUtils.opserver_query_url(
self._ip, str(self._port))
print flows_url
query_dict = OpServerUtils.get_query_dict(
table, start_time, end_time,
select_fields,
where_clause,
sort_fields, sort, limit, filter, dir)
print json.dumps(query_dict)
res = []
resp = OpServerUtils.post_url_http(
flows_url, json.dumps(query_dict))
if resp is not None:
resp = json.loads(resp)
qid = resp['href'].rsplit('/', 1)[1]
result = OpServerUtils.get_query_result(
self._ip, str(self._port), qid)
for item in result:
res.append(item)
except Exception as e:
print str(e)
finally:
return res
def post_db_purge(self,purge_input):
res = []
json_body = OpServerUtils.get_json_body(purge_input = purge_input)
print json.dumps(json_body)
try:
purge_url = OpServerUtils.opserver_db_purge_url(
self._ip, str(self._port))
print purge_url
resp = OpServerUtils.post_url_http(
purge_url, json.dumps(json_body))
if resp is not None:
resp = json.loads(resp)
res.append(resp)
except Exception as e:
print str(e)
finally:
return res
class VerificationOpsSrvIntrospect (VerificationUtilBase):
def __init__(self, ip, port, logger=LOG):
super(VerificationOpsSrvIntrospect, self).__init__(ip, port,drv=XmlDrv, logger=logger)
def get_collector_connectivity(self):
connaction_status = dict()
try:
c_dict = self.dict_get(
'Snh_CollectorInfoRequest?')
ip = c_dict.xpath('ip')[0].text
port = c_dict.xpath('port')[0].text
status = c_dict.xpath('status')[0].text
connaction_status['ip']= ip
connaction_status['port']= port
connaction_status['status']= status
except Exception as e:
print e
finally:
return connaction_status
if __name__ == '__main__':
vns = VerificationOpsSrvIntrospect('127.0.0.1',8090)
intr = vns.get_collector_connectivity()