fix bug 923; fix bug 940
[osm/LW-UI.git] / lib / osm / osm_util.py
index f61e201..acc4726 100644 (file)
@@ -22,7 +22,7 @@ class OsmUtil():
                                                                 str(item['member-vnf-index-ref']) != str(
                                                                     args['member-vnf-index']) or str(
                                                                     item['vnfd-id-ref']) != str(args['vnfd-id-ref'])]
-            elif node_type == 'cp':
+            elif node_type == 'ns_cp':
                 for vld in nsd['vld']:
                     if vld['id'] == args['vld_id']:
                         vld['vnfd-connection-point-ref'] = [item for item in vld['vnfd-connection-point-ref'] if
@@ -38,21 +38,26 @@ class OsmUtil():
             if node_type == 'vnf_vl':
                 vnfd['internal-vld'] = [item for item in vnfd['internal-vld'] if item['id'] != element_id]
             if node_type == 'cp':
-                vnfd['connection-point'] = [item for item in vnfd['connection-point'] if item['name'] != element_id]
-            if node_type == 'vdu':
-                # check
-                vnfd['vdu'] = [item for item in vnfd['vdu'] if item['name'] != element_id]
-            if node_type == 'int_cp':
-
+                vnfd['connection-point'] = [item for item in vnfd['connection-point'] if item['name'] != args['name']]
+                if vnfd['mgmt-interface']['cp'] == args['name']:
+                    del vnfd['mgmt-interface']['cp']
                 for vdu in vnfd['vdu']:
                     if 'interface' in vdu:
-                        vdu['interface'] = [item for item in vdu['interface'] if 'internal-connection-point-ref' not in item
-                        or ('internal-connection-point-ref'in item and item['internal-connection-point-ref'] != element_id)]
-                    if 'internal-connection-point' in vdu:
-                        vdu['internal-connection-point'] = [item for item in vdu['internal-connection-point'] if item['id'] != element_id]
-
-
-
+                        vdu['interface'] = [item for item in vdu['interface'] if 'external-connection-point-ref' not in item
+                        or ('external-connection-point-ref'in item and item['external-connection-point-ref'] != args['name'])]
+            if node_type == 'vdu':
+                vdus = []
+                for vdu in vnfd['vdu']:
+                    if vdu['name'] != element_id:
+                        vdus.append(vdu)
+                    else:
+                        for intcp in vdu['internal-connection-point']:
+                            for intVld in vnfd['internal-vld']:
+                                intVld['internal-connection-point'] = [item for item in intVld['internal-connection-point']
+                                                                    if item['id-ref'] != intcp['id']]
+                vnfd['vdu'] = vdus
+            if node_type == 'int_cp':
+                OsmUtil.remove_int_cp(element_id, vnfd)
         return descriptor
 
     @staticmethod
@@ -72,6 +77,29 @@ class OsmUtil():
                     if str(v['member-vnf-index']) == str(old['member-vnf-index']) and str(v['vnfd-id-ref']) == str(
                             old['vnfd-id-ref']):
                         print 'update here'
+        elif descriptor_type == 'vnfd':
+            if 'vnfd-catalog' in descriptor:
+                vnfd = descriptor['vnfd-catalog']['vnfd'][0]
+            elif 'vnfd:vnfd-catalog' in descriptor:
+                vnfd = descriptor['vnfd:vnfd-catalog']['vnfd'][0]
+
+            if node_type == 'vnf_vl':
+                for k, v in enumerate(vnfd['internal-vld']):
+                    if v['id'] == old['id']:
+                        vnfd['internal-vld'][k].update(updated)
+            if node_type == 'cp':
+                for k, v in enumerate(vnfd['connection-point']):
+                    if v['name'] == old['name']:
+                        vnfd['connection-point'][k].update(updated)
+                for vdu in vnfd['vdu']:
+                    if 'interface' in vdu:
+                        for intf in vdu['interface']:
+                            if 'external-connection-point-ref' in intf and intf['external-connection-point-ref'] == old['name']:
+                                intf['external-connection-point-ref'] = updated['name']
+            if node_type == 'vdu':
+                for k, v in enumerate(vnfd['vdu']):
+                    if v['name'] == old['name']:
+                        vnfd['vdu'][k].update(updated)
 
         return descriptor
 
@@ -95,7 +123,7 @@ class OsmUtil():
                 indexes = []
                 for cvnfd in nsd['constituent-vnfd']:
                     indexes.append(int(cvnfd["member-vnf-index"]))
-                memberindex = max(indexes) + 1
+                memberindex = max(indexes) + 1 if len(indexes) > 0 else 1
                 nsd['constituent-vnfd'].append({
                     "member-vnf-index": memberindex,
                     "vnfd-id-ref": element_id
@@ -119,25 +147,31 @@ class OsmUtil():
             elif 'vnfd:vnfd-catalog' in descriptor:
                 vnfd = descriptor['vnfd:vnfd-catalog']['vnfd'][0]
             if node_type == 'vdu':
+                if 'vdu' not in vnfd:
+                    vnfd['vdu'] = []
                 vnfd['vdu'].append({
                     "count": "1",
                     "description": "",
                     "monitoring-param": [],
                     "internal-connection-point": [],
                     "image": "ubuntu",
-                    "cloud-init-file": "",
+                    #"cloud-init-file": "",
                     "vm-flavor": {},
                     "interface": [],
                     "id": element_id,
                     "name": element_id
                 })
             if node_type == 'cp':
+                if 'connection-point' not in vnfd:
+                    vnfd['connection-point'] = []
                 vnfd['connection-point'].append({
                     "type": "VPORT",
                     "name": element_id
                 })
 
             if node_type == 'vnf_vl':
+                if 'internal-vld' not in vnfd:
+                    vnfd['internal-vld'] = []
                 vnfd['internal-vld'].append({
                     "short-name": element_id,
                     "name": element_id,
@@ -153,7 +187,7 @@ class OsmUtil():
                             "virtual-interface": {
                                 "type": "VIRTIO"
                             },
-                            "name": element_id,
+                            "name": args["name"],
                             "mgmt-interface": True,
                             "type": "EXTERNAL",
                             "external-connection-point-ref": args["external-connection-point-ref"]
@@ -196,5 +230,27 @@ class OsmUtil():
             elif 'nsd:nsd-catalog' in descriptor:
                 nsd = descriptor['nsd:nsd-catalog']['nsd'][0]
             nsd.update(updated)
+        elif descriptor_type == 'vnfd':
+            if 'vnfd-catalog' in descriptor:
+                vnfd = descriptor['vnfd-catalog']['vnfd'][0]
+            elif 'vnfd:vnfd-catalog' in descriptor:
+                vnfd = descriptor['vnfd:vnfd-catalog']['vnfd'][0]
+            vnfd.update(updated)
 
         return descriptor
+
+    @staticmethod
+    def remove_int_cp(element_id, vnfd):
+        for vdu in vnfd['vdu']:
+            if 'interface' in vdu:
+                vdu['interface'] = [item for item in vdu['interface'] if 'internal-connection-point-ref' not in item
+                                    or ('internal-connection-point-ref' in item and item[
+                    'internal-connection-point-ref'] != element_id)]
+            if 'internal-connection-point' in vdu:
+                vdu['internal-connection-point'] = [item for item in vdu['internal-connection-point'] if
+                                                    item['id'] != element_id]
+        for intVld in vnfd['internal-vld']:
+            intVld['internal-connection-point'] = [item for item in intVld['internal-connection-point'] if
+                                                   item['id-ref'] != element_id]
+
+        return vnfd