Fix 1025. Do not remove last project reference if it will be deleted
[osm/NBI.git] / osm_nbi / instance_topics.py
index 09a8c1b..776edc1 100644 (file)
@@ -18,7 +18,8 @@ from uuid import uuid4
 from http import HTTPStatus
 from time import time
 from copy import copy, deepcopy
-from osm_nbi.validation import validate_input, ValidationError, ns_instantiate, ns_action, ns_scale, nsi_instantiate
+from osm_nbi.validation import validate_input, ValidationError, ns_instantiate, ns_terminate, ns_action, ns_scale,\
+    nsi_instantiate
 from osm_nbi.base_topic import BaseTopic, EngineException, get_iterable, deep_get
 # from descriptor_topics import DescriptorTopic
 from yaml import safe_dump
@@ -485,7 +486,7 @@ class NsLcmOpTopic(BaseTopic):
         "instantiate": ns_instantiate,
         "action": ns_action,
         "scale": ns_scale,
-        "terminate": None,
+        "terminate": ns_terminate,
     }
 
     def __init__(self, db, fs, msg, auth):
@@ -759,12 +760,13 @@ class NsLcmOpTopic(BaseTopic):
                                 vnfr_update[iface_text + ".{}".format(k)] = v
                                 vnfr_update_rollback[iface_text + ".{}".format(k)] = vdur_interface.get(v)
                         if pdu_interface.get("ip-address"):
-                            if vdur_interface.get("mgmt-interface"):
+                            if vdur_interface.get("mgmt-interface") or vdur_interface.get("mgmt-vnf"):
                                 vnfr_update_rollback[vdu_text + ".ip-address"] = vdur.get("ip-address")
                                 vnfr_update[vdu_text + ".ip-address"] = pdu_interface["ip-address"]
                             if vdur_interface.get("mgmt-vnf"):
                                 vnfr_update_rollback["ip-address"] = vnfr.get("ip-address")
                                 vnfr_update["ip-address"] = pdu_interface["ip-address"]
+                                vnfr_update[vdu_text + ".ip-address"] = pdu_interface["ip-address"]
                         if pdu_interface.get("vim-network-name") or pdu_interface.get("vim-network-id"):
                             ifaces_forcing_vim_network.append({
                                 "name": vdur_interface.get("vnf-vld-id") or vdur_interface.get("ns-vld-id"),
@@ -947,6 +949,19 @@ class NsLcmOpTopic(BaseTopic):
         }
         return nslcmop
 
+    def _get_enabled_vims(self, session):
+        """
+        Retrieve and return VIM accounts that are accessible by current user and has state ENABLE
+        :param session: current session with user information
+        """
+        db_filter = self._get_project_filter(session)
+        db_filter["_admin.operationalState"] = "ENABLED"
+        vims = self.db.get_list("vim_accounts", db_filter)
+        vimAccounts = []
+        for vim in vims:
+            vimAccounts.append(vim['_id'])
+        return vimAccounts
+
     def new(self, rollback, session, indata=None, kwargs=None, headers=None, slice_object=False):
         """
         Performs a new operation over a ns
@@ -970,7 +985,7 @@ class NsLcmOpTopic(BaseTopic):
 
         try:
             # Override descriptor with query string kwargs
-            self._update_input_with_kwargs(indata, kwargs)
+            self._update_input_with_kwargs(indata, kwargs, yaml_format=True)
             operation = indata["lcmOperationType"]
             nsInstanceId = indata["nsInstanceId"]
 
@@ -1002,6 +1017,9 @@ class NsLcmOpTopic(BaseTopic):
             nslcmop_desc = self._create_nslcmop(nsInstanceId, operation, indata)
             _id = nslcmop_desc["_id"]
             self.format_on_new(nslcmop_desc, session["project_id"], make_public=session["public"])
+            if indata.get("placement-engine"):
+                # Save valid vim accounts in lcm operation descriptor
+                nslcmop_desc['operationParams']['validVimAccounts'] = self._get_enabled_vims(session)
             self.db.create("nslcmops", nslcmop_desc)
             rollback.append({"topic": "nslcmops", "_id": _id})
             if not slice_object: