fix composer bug
[osm/LW-UI.git] / lib / osm / osm_rdcl_parser.py
1 #
2 # Copyright 2018 EveryUP Srl
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16
17
18 import logging
19 # from lib.rdcl_graph import RdclGraph
20 import copy
21
22 logging.basicConfig(level=logging.DEBUG)
23 log = logging.getLogger('OsmParser')
24
25
26 class RdclGraph(object):
27 """ Operates on the graph representation used for the GUI graph views """
28 node_ids = []
29 node_t3d_base = {
30 'info': {
31 'property': {
32 'custom_label': '',
33 },
34 'type': '',
35 'group': []
36 }
37 }
38
39 def __init__(self):
40 pass
41
42 def add_link(self, source, target, view, group, graph_object, optional={}):
43 if (source is None or source not in self.node_ids) or (target is None or target not in self.node_ids):
44 return
45 edge_obj = {
46 'source': source,
47 'target': target,
48 'view': view,
49 'group': [group],
50 }
51
52 edge_obj.update(optional)
53 #if edge_obj not in graph_object['edges']:
54 # graph_object['edges'].append(edge_obj)
55 graph_object['edges'].append(edge_obj)
56
57 def add_node(self, id, type, group, positions, graph_object, optional={}):
58 if id is None:
59 return
60 node = next((x for x in graph_object['vertices'] if x['id'] == id), None)
61 if node is not None:
62 node['info']['group'].append(group)
63 else:
64 node = copy.deepcopy(self.node_t3d_base)
65 node['id'] = id
66 node['info']['type'] = type
67 if group is not None:
68 node['info']['group'].append(group)
69 if positions and id in positions['vertices'] and 'x' in positions['vertices'][id] and 'y' in \
70 positions['vertices'][id]:
71 node['fx'] = positions['vertices'][id]['x']
72 node['fy'] = positions['vertices'][id]['y']
73 node['info'].update(optional)
74 graph_object['vertices'].append(node)
75 self.node_ids.append(id)
76
77 def is_directed_edge(self, source_type=None, target_type=None, layer=None, model={}):
78 if source_type is None or target_type is None or layer is None:
79 return None
80 if layer in model['layer'] and 'allowed_edges' in model['layer'][layer]:
81 if source_type in model['layer'][layer]['allowed_edges'] and target_type in \
82 model['layer'][layer]['allowed_edges'][source_type]['destination']:
83 edge_pro = model['layer'][layer]['allowed_edges'][source_type]['destination'][target_type]
84 return edge_pro['direct_edge'] if 'direct_edge' in edge_pro else False
85
86 return None
87
88
89 class OsmParser(RdclGraph):
90 """ Operates on the graph representation used for the GUI graph views """
91
92 def nsr_to_graph(self, nsr_full):
93
94 graph = {'vertices': [], 'edges': [], 'model': {
95 "layer": {
96
97 "nsr": {
98 "nodes": {
99 "vnfr": {
100 "addable": {},
101 "removable": {},
102 "expands": "vnfr"
103 },
104 "ns_vl": {
105 "addable": {},
106 "removable": {}
107 },
108 "ns_cp": {
109 "addable": {},
110 "removable": {}
111 },
112
113 },
114 "allowed_edges": {
115 "ns_vl": {
116 "destination": {
117 "vnfr": {
118 "callback": "addLink",
119 "direct_edge": False,
120 "removable": {}
121 }
122 }
123 },
124 "vnfr": {
125 "destination": {
126 "ns_vl": {
127 "callback": "addLink",
128 "direct_edge": False,
129 "removable": {}
130 },
131
132 }
133 }
134
135 }
136 },
137
138 "vnfr": {
139 "nodes": {
140 "vdur": {},
141 "cp": {},
142 "int_cp": {},
143 "vnf_vl": {}
144
145 },
146 "allowed_edges": {
147 "vdur": {
148 "destination": {
149 "cp": {
150 "direct_edge": False,
151 },
152 "int_cp": {
153 "direct_edge": False,
154 },
155 "vnf_vl": {
156 "direct_edge": False,
157 }
158 }
159 },
160 "cp": {
161 "destination": {
162 "vdur": {
163 "direct_edge": False,
164 }
165 }
166 },
167 "int_cp": {
168 "destination": {
169 "vdur": {
170 "direct_edge": False,
171 },
172 "vnf_vl": {
173 "direct_edge": False,
174 }
175 }
176 },
177 "vnf_vl": {
178 "destination": {
179 "vdur": {
180 "direct_edge": False
181 }
182 }
183 }
184 }
185 },
186 "name": "OSM",
187 "version": 1,
188 "description": "osm"
189 }
190 }, 'graph_parameters': {'view': {'nsr': {}, 'vnfr': {}}}}
191
192 nsr = nsr_full['nsr']
193
194 graph['graph_parameters']['view']['nsr'] = {}
195 nsr_graph_param = graph['graph_parameters']['view']['nsr']
196 nsr_graph_param['id'] = nsr['_id'] if '_id' in nsr else None
197 nsr_graph_param['nsdId'] = nsr['nsdId'] if 'nsdId' in nsr else None
198 nsr_graph_param['name-ref'] = nsr['name-ref'] if 'name-ref' in nsr else None
199 nsr_graph_param['operational-status'] = nsr['operational-status'] if 'operational-status' in nsr else None
200 nsr_graph_param['config-status'] = nsr['config-status'] if 'config-status' in nsr else None
201 nsr_graph_param['detailed-status'] = nsr['detailed-status'] if 'detailed-status' in nsr else None
202 nsr_graph_param['create-time'] = nsr['create-time'] if 'create-time' in nsr else None
203 nsr_graph_param['instantiate_params'] = nsr['instantiate_params'] if 'instantiate_params' in nsr else None
204
205 map_vnf_index_to_id = {}
206 for vnfr_id in nsr['constituent-vnfr-ref']:
207 current_vnfr = nsr_full['vnfr'][vnfr_id]
208
209 graph['graph_parameters']['view']['vnfr'][vnfr_id] = {}
210 vnfr_graph_param = graph['graph_parameters']['view']['vnfr'][vnfr_id]
211 vnfr_graph_param['id'] = vnfr_id
212 vnfr_graph_param['vnfd-id'] = current_vnfr['vnfd-id']
213 vnfr_graph_param['vnfd-ref'] = current_vnfr['vnfd-ref']
214 vnfr_graph_param['member-vnf-index-ref'] = current_vnfr['member-vnf-index-ref']
215 vnfr_graph_param['vim-account-id'] = current_vnfr['vim-account-id']
216 vnfr_graph_param['created-time'] = current_vnfr['created-time']
217
218 vnfr_label = current_vnfr['vnfd-ref'] + ':' + current_vnfr['member-vnf-index-ref']
219 map_vnf_index_to_id[current_vnfr['member-vnf-index-ref']] = vnfr_id
220 self.add_node(vnfr_id, 'vnfr', None, None, graph,
221 {'property': {'custom_label': vnfr_label}, 'osm': current_vnfr})
222
223 for cp in current_vnfr['connection-point']:
224 if cp['id']:
225 cp_id = vnfr_label + ':' + cp['id']
226 self.add_node(cp_id, 'cp', vnfr_id, None, graph, {'osm': cp})
227
228 for vdur in current_vnfr['vdur']:
229 vdur_id = vnfr_label + ':' + vdur['vdu-id-ref']
230 self.add_node(vdur_id, 'vdur', vnfr_id, None, graph, {'osm': vdur})
231 if current_vnfr['vnfd-id'] in nsr_full['vnfd']:
232 for vdu in nsr_full['vnfd'][current_vnfr['vnfd-id']]['vdu']:
233 if vdu['id'] == vdur['vdu-id-ref']:
234 if 'internal-connection-point' in vdu:
235 for int_cp in vdu['internal-connection-point']:
236 cp_id = vnfr_label + ':' + int_cp['id']
237 self.add_node(cp_id, 'int_cp', vnfr_id, None, graph, {'osm': int_cp})
238 for interface in vdu['interface']:
239 if interface['type'] == "EXTERNAL":
240 cp_id = vnfr_label + ':' + interface['external-connection-point-ref']
241 self.add_link(cp_id, vdur_id, 'vnfr', vnfr_id, graph)
242 elif interface['type'] == "INTERNAL":
243 cp_id = vnfr_label + ':' + interface['internal-connection-point-ref']
244 self.add_link(cp_id, vdur_id, 'vnfr', vnfr_id, graph)
245
246 if current_vnfr['vnfd-id'] in nsr_full['vnfd'] and 'internal-vld' in nsr_full['vnfd'][
247 current_vnfr['vnfd-id']]:
248 for vnfd_vld in nsr_full['vnfd'][current_vnfr['vnfd-id']]['internal-vld']:
249 vld_id = vnfr_label + ':' + vnfd_vld['id']
250 self.add_node(vld_id, 'vnf_vl', vnfr_id, None, graph, {'osm': vnfd_vld})
251 if vnfd_vld['internal-connection-point']:
252 for int_cp in vnfd_vld['internal-connection-point']:
253 int_cp_id = vnfr_label + ':' + int_cp['id-ref']
254 self.add_link(vld_id, int_cp_id, 'vnfr', vnfr_id, graph)
255
256 for ns_vld in nsr['nsd']['vld']:
257 self.add_node(ns_vld['id'], 'ns_vl', None, None, graph,
258 {'property': {'custom_label': ns_vld['name']}, 'osm': ns_vld})
259 for cp_ref in ns_vld['vnfd-connection-point-ref']:
260 ns_vld['id']+ ':' + str(cp_ref['member-vnf-index-ref']) + ':' + cp_ref['vnfd-connection-point-ref']
261 cp_id = ns_vld['id']+ ':' + str(cp_ref['member-vnf-index-ref']) + ':' + cp_ref['vnfd-connection-point-ref']
262 cp_label = str(cp_ref['vnfd-connection-point-ref'])
263 self.add_node(cp_id,'ns_cp','nsd',None,graph, {'property': {'custom_label': cp_label}, 'osm': cp_ref})
264 self.add_link(cp_id, ns_vld['id'], 'nsr', None, graph)
265 self.add_link(cp_id, map_vnf_index_to_id[str(cp_ref['member-vnf-index-ref'])], 'nsr', None, graph)
266
267 return graph
268
269 def vnfd_to_graph(self, vnfd_catalog):
270 graph = {'vertices': [], 'edges': [], 'model': {
271 "layer": {
272 "vnfd": {
273 "nodes": {
274 "vdu": {
275 "addable": {
276 "callback": "addNode"
277 },
278 "removable": {
279 "callback": "removeNode"
280 }
281 },
282 "cp": {
283 "addable": {
284 "callback": "addNode"
285 },
286 "removable": {
287 "callback": "removeNode"
288 }
289 },
290 "int_cp": {
291 "removable": {
292 "callback": "removeNode"
293 }
294 },
295 "vnf_vl": {
296 "addable": {
297 "callback": "addNode"
298 },
299 "removable": {
300 "callback": "removeNode"
301 }
302 }
303 },
304 "allowed_edges": {
305 "vdu": {
306 "destination": {
307 "cp": {
308 "callback": "addLink",
309 "direct_edge": False,
310 "removable": {}
311 },
312 "vnf_vl": {
313 "callback": "addLink",
314 "direct_edge": False,
315 "removable": {}
316 }
317 }
318 },
319 "cp": {
320 "destination": {
321 "vdu": {
322 "callback": "addLink",
323 "direct_edge": False,
324 "removable": {}
325 }
326 }
327 },
328 # "int_cp": {
329 # "destination": {
330 # "vdu": {
331 # "direct_edge": False,
332 # },
333 # "vnf_vl": {
334 # "direct_edge": False,
335 # }
336 # }
337 # },
338 "vnf_vl": {
339 "destination": {
340 "int_cp": {
341 "direct_edge": False
342 },
343 "vdu": {
344 "callback": "addLink",
345 "direct_edge": False,
346 "removable": {}
347 }
348 }
349 }
350 }
351 },
352 "name": "OSM",
353 "version": 1,
354 "description": "osm"
355 }, "callback": {"addNode": {"class": "OSMController"}, "removeNode": {"class": "OSMController"},
356 "removeLink": {"class": "OSMController"}, "addLink": {"class": "OSMController"}}
357 }, 'graph_parameters': {'view': {'vnfd': {}}}}
358 if 'vnfd-catalog' in vnfd_catalog:
359 vnfd = vnfd_catalog['vnfd-catalog']['vnfd'][0]
360 elif 'vnfd:vnfd-catalog' in vnfd_catalog:
361 vnfd = vnfd_catalog['vnfd:vnfd-catalog']['vnfd'][0]
362 else:
363 return graph
364 vnfd_graph_param = graph['graph_parameters']['view']['vnfd']
365 vnfd_graph_param['id'] = vnfd['id'] if 'id' in vnfd else None
366 vnfd_graph_param['name'] = vnfd['name'] if 'name' in vnfd else None
367 vnfd_graph_param['short-name'] = vnfd['short-name'] if 'short-name' in vnfd else None
368 vnfd_graph_param['description'] = vnfd['description'] if 'description' in vnfd else None
369 vnfd_graph_param['version'] = vnfd['version'] if 'version' in vnfd else None
370 vnfd_graph_param['vendor'] = vnfd['vendor'] if 'vendor' in vnfd else None
371 if 'connection-point' in vnfd:
372 for extCp in vnfd['connection-point']:
373 self.add_node(extCp['name'], 'cp', vnfd['id'], None, graph,
374 {'property': {'custom_label': extCp['name']}, 'osm': extCp})
375 if 'vdu' in vnfd:
376 for vdu in vnfd['vdu']:
377 self.add_node(vdu['id'], 'vdu', vnfd['id'], None, graph,
378 {'property': {'custom_label': vdu['id']}, 'osm': vdu})
379 if 'internal-connection-point' in vdu:
380 for intCp in vdu['internal-connection-point']:
381 self.add_node(intCp['id'], 'int_cp', vnfd['id'], None, graph,
382 {'property': {'custom_label': intCp['id']}, 'osm': intCp})
383 if 'interface' in vdu:
384 for interface in vdu['interface']:
385 if interface['type'] == "EXTERNAL":
386 self.add_link(vdu['id'], interface['external-connection-point-ref'], 'vnfd', vnfd['id'], graph)
387 elif interface['type'] == "INTERNAL":
388 self.add_link(vdu['id'], interface['internal-connection-point-ref'], 'vnfd', vnfd['id'], graph, {'short': True})
389 if 'internal-vld' in vnfd:
390 for intVl in vnfd['internal-vld']:
391 self.add_node(intVl['id'], 'vnf_vl', intVl['id'], None, graph,
392 {'property': {'custom_label': intVl['id']}, 'osm': intVl})
393 for intCp in intVl['internal-connection-point']:
394 self.add_link(intVl['id'], intCp['id-ref'], 'vnfd', vnfd['id'], graph)
395
396 return graph
397
398 def nsd_to_graph(self, nsd_catalog):
399 graph = {'vertices': [], 'edges': [], 'model': {
400 "layer": {
401 "nsd": {
402 "nodes": {
403 "vnf": {
404 "addable": {"callback": "addNode"},
405 "removable": {"callback": "removeNode"}
406 },
407 "ns_cp": {
408 "removable": {"callback": "removeNode"}
409 },
410 "ns_vl": {
411 "addable": {
412 "callback": "addNode"
413 },
414 "removable": {
415 "callback": "removeNode"
416 }
417 }
418 },
419 "allowed_edges": {
420 "vnf":{
421 "destination": {
422 "ns_cp": {
423 "direct_edge": False,
424 "removable" : {
425 "callback": "removeLink",
426 }
427 },
428 "ns_vl": {
429 "direct_edge": False,
430 "callback": "addLink",
431 }
432 }
433 },
434 "ns_vl": {
435 "destination": {
436 "ns_cp": {
437 "direct_edge": False,
438 "removable": {
439 "callback": "removeLink",
440 }
441 },
442 "vnf": {
443 "direct_edge": False,
444 "callback": "addLink",
445 }
446 }
447 },
448 "ns_cp": {
449 "destination": {
450 "ns_vl": {
451 "direct_edge": False,
452 "removable": {
453 "callback": "removeLink",
454 }
455 },
456 "vnf": {
457 "direct_edge": False,
458 "removable": {
459 "callback": "removeLink",
460 }
461 }
462 }
463 }
464 }
465 },
466 "vnfd": {
467 "nodes": {
468 "vdu": {},
469 "cp": {},
470 "int_cp": {},
471 "vnf_vl": {}
472 },
473 "allowed_edges": {
474 "vdu": {
475 "destination": {
476 "cp": {
477 "direct_edge": False,
478 },
479 "int_cp": {
480 "direct_edge": False,
481 },
482 "vnf_vl": {
483 "direct_edge": False,
484 }
485 }
486 },
487 "cp": {
488 "destination": {
489 "vdu": {
490 "direct_edge": False,
491 }
492 }
493 },
494 "int_cp": {
495 "destination": {
496 "vdu": {
497 "direct_edge": False,
498 },
499 "vnf_vl": {
500 "direct_edge": False,
501 }
502 }
503 },
504 "vnf_vl": {
505 "destination": {
506 "vdu": {
507 "direct_edge": False
508 }
509 }
510 }
511 }
512 },
513 "name": "OSM",
514 "version": 1,
515 "description": "osm"
516 }, "callback": {"addNode": {"class": "OSMController"}, "removeNode": {"class": "OSMController"},
517 "removeLink": {"class": "OSMController"}, "addLink": {"class": "OSMController"}}
518 }, 'graph_parameters': {'view': {'nsd': {}}}}
519 if 'nsd-catalog' in nsd_catalog:
520 nsd = nsd_catalog['nsd-catalog']['nsd'][0]
521 elif 'nsd:nsd-catalog' in nsd_catalog:
522 nsd = nsd_catalog['nsd:nsd-catalog']['nsd'][0]
523 else:
524 return graph
525
526 nsd_graph_param = graph['graph_parameters']['view']['nsd']
527 nsd_graph_param['id'] = nsd['id'] if 'id' in nsd else None
528 nsd_graph_param['name'] = nsd['name'] if 'name' in nsd else None
529 nsd_graph_param['short-name'] = nsd['short-name'] if 'short-name' in nsd else None
530 nsd_graph_param['description'] = nsd['description'] if 'description' in nsd else None
531 nsd_graph_param['version'] = nsd['version'] if 'version' in nsd else None
532 nsd_graph_param['vendor'] = nsd['vendor'] if 'vendor' in nsd else None
533
534 if 'constituent-vnfd' in nsd:
535 for vnfd in nsd['constituent-vnfd']:
536 costinuent_id = vnfd['vnfd-id-ref']+":"+str(vnfd['member-vnf-index'])
537 self.add_node(costinuent_id, 'vnf', None, None, graph,
538 {'property': {'custom_label': costinuent_id}, 'osm': vnfd})
539
540 if 'vld' in nsd:
541 for vld in nsd['vld']:
542 self.add_node(vld['id'], 'ns_vl', None, None, graph,
543 {'property': {'custom_label': vld['id']}, 'osm': vld})
544 if 'vnfd-connection-point-ref' in vld:
545 for cp_ref in vld['vnfd-connection-point-ref']:
546 vnfd_id = cp_ref['vnfd-id-ref'] + ':' + str(cp_ref['member-vnf-index-ref'])
547 cp_id = vld['id']+ ':' + str(cp_ref['member-vnf-index-ref']) + ':' + cp_ref['vnfd-connection-point-ref']
548 cp_label = vld['id']+ ':' + cp_ref['vnfd-connection-point-ref']
549 node_payload = {'vld_id': vld['id']}
550 node_payload.update(cp_ref)
551 self.add_node(cp_id,'ns_cp',None,None,graph,
552 {'property': {'custom_label': cp_label}, 'osm': node_payload})
553
554 self.add_link(cp_id, vld['id'], 'nsd', None, graph)
555 self.add_link(cp_id, vnfd_id, 'nsd', None, graph)
556 return graph
557
558
559 if __name__ == '__main__':
560 parser = OsmParser()
561 print parser.nsr_to_graph({})