Fix bug 1442: set SDN target in vim_info for NS VLD
[osm/LCM.git] / osm_lcm / netslice.py
index df32473..c3a48e6 100644 (file)
@@ -1,13 +1,24 @@
 # -*- coding: utf-8 -*-
+##
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+##
 
 import asyncio
 import logging
 import logging.handlers
 import traceback
-import ns
-from ns import populate_dict as populate_dict
-import ROclient
-from lcm_utils import LcmException, LcmBase
+from osm_lcm import ROclient
+from osm_lcm.lcm_utils import LcmException, LcmBase, populate_dict, get_iterable, deep_get
 from osm_common.dbbase import DbException
 from time import time
 from copy import deepcopy
@@ -16,23 +27,11 @@ from copy import deepcopy
 __author__ = "Felipe Vicens, Pol Alemany, Alfonso Tierno"
 
 
-def get_iterable(in_dict, in_key):
-    """
-    Similar to <dict>.get(), but if value is None, False, ..., An empty tuple is returned instead
-    :param in_dict: a dictionary
-    :param in_key: the key to look for at in_dict
-    :return: in_dict[in_var] or () if it is None or not present
-    """
-    if not in_dict.get(in_key):
-        return ()
-    return in_dict[in_key]
-
-
 class NetsliceLcm(LcmBase):
 
-    total_deploy_timeout = 2 * 3600   # global timeout for deployment
+    timeout_nsi_deploy = 2 * 3600  # default global timeout for deployment a nsi
 
-    def __init__(self, db, msg, fs, lcm_tasks, ro_config, vca_config, loop):
+    def __init__(self, msg, lcm_tasks, config, loop, ns):
         """
         Init, Connect to database, filesystem storage, and messaging
         :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
@@ -42,10 +41,11 @@ class NetsliceLcm(LcmBase):
         self.logger = logging.getLogger('lcm.netslice')
         self.loop = loop
         self.lcm_tasks = lcm_tasks
-        self.ns = ns.NsLcm(db, msg, fs, lcm_tasks, ro_config, vca_config, loop)
-        self.ro_config = ro_config
+        self.ns = ns
+        self.ro_config = config["ro_config"]
+        self.timeout = config["timeout"]
 
-        super().__init__(db, msg, fs, self.logger)
+        super().__init__(msg, self.logger)
 
     def nsi_update_nsir(self, nsi_update_nsir, db_nsir, nsir_desc_RO):
         """
@@ -70,6 +70,12 @@ class NetsliceLcm(LcmBase):
                 raise LcmException("ns_update_nsir: Not found vld={} at RO info".format(vld["id"]))
 
     async def instantiate(self, nsir_id, nsilcmop_id):
+
+        # Try to lock HA task here
+        task_is_locked_by_me = self.lcm_tasks.lock_HA('nsi', 'nsilcmops', nsilcmop_id)
+        if not task_is_locked_by_me:
+            return
+
         logging_text = "Task netslice={} instantiate={} ".format(nsir_id, nsilcmop_id)
         self.logger.debug(logging_text + "Enter")
         # get all needed from database
@@ -81,6 +87,7 @@ class NetsliceLcm(LcmBase):
         nsilcmop_operation_state = None
         vim_2_RO = {}
         RO = ROclient.ROClient(self.loop, **self.ro_config)
+        nsi_vld_instantiationi_params = {}
 
         def ip_profile_2_RO(ip_profile):
             RO_ip_profile = deepcopy((ip_profile))
@@ -122,6 +129,7 @@ class NetsliceLcm(LcmBase):
             :param vld_id The VLD id inside nsir to be created
             :param nsir_id The nsir id
             """
+            nonlocal nsi_vld_instantiationi_params
             ip_vld = None
             mgmt_network = False
             RO_vld_sites = []
@@ -142,7 +150,7 @@ class NetsliceLcm(LcmBase):
                     break
 
             # Creating netslice-vld at RO
-            RO_nsir = db_nsir["_admin"].get("deployed", {}).get("RO", [])
+            RO_nsir = deep_get(db_nsir, ("_admin", "deployed", "RO"), [])
 
             if vld_id in RO_nsir:
                 db_nsir_update["_admin.deployed.RO"] = RO_nsir
@@ -151,148 +159,161 @@ class NetsliceLcm(LcmBase):
             else:
                 # TODO: Check VDU type in all descriptors finding SRIOV / PT
                 # Updating network names and datacenters from instantiation parameters for each VLD
-                RO_ns_params = {}
-                RO_ns_params["name"] = netslice_vld["name"]
-                RO_ns_params["datacenter"] = vim_account_2_RO(db_nsir["instantiation_parameters"]["vimAccountId"])
                 for instantiation_params_vld in get_iterable(db_nsir["instantiation_parameters"], "netslice-vld"):
                     if instantiation_params_vld.get("name") == netslice_vld["name"]:
                         ip_vld = deepcopy(instantiation_params_vld)
+                        ip_vld.pop("name")
+                        nsi_vld_instantiationi_params[netslice_vld["name"]] = ip_vld
 
-                if netslice_vld.get("mgmt-network"):
-                    mgmt_network = True
-
-                # Creating scenario if vim-network-name / vim-network-id are present as instantiation parameter
-                # Use vim-network-id instantiation parameter
-                vim_network_option = None
-                if ip_vld:
-                    if ip_vld.get("vim-network-id"):
-                        vim_network_option = "vim-network-id"
-                    elif ip_vld.get("vim-network-name"):
-                        vim_network_option = "vim-network-name"
-                    if ip_vld.get("ip-profile"):
-                        populate_dict(RO_ns_params, ("networks", netslice_vld["name"], "ip-profile"),
-                                      ip_profile_2_RO(ip_vld["ip-profile"]))
-
-                if vim_network_option:
-                    if ip_vld.get(vim_network_option):
-                        if isinstance(ip_vld.get(vim_network_option), list):
-                            for vim_net_id in ip_vld.get(vim_network_option):
-                                for vim_account, vim_net in vim_net_id.items():
+                db_nsir_update_RO = {}
+                db_nsir_update_RO["vld_id"] = netslice_vld["name"]
+                if self.ro_config["ng"]:
+                    db_nsir_update_RO["netslice_scenario_id"] = vld_shared.get("instance_scenario_id") if vld_shared \
+                        else "nsir:{}:vld.{}".format(nsir_id, netslice_vld["name"])
+                else:  # if not self.ro_config["ng"]:
+                    if netslice_vld.get("mgmt-network"):
+                        mgmt_network = True
+                    RO_ns_params = {}
+                    RO_ns_params["name"] = netslice_vld["name"]
+                    RO_ns_params["datacenter"] = vim_account_2_RO(db_nsir["instantiation_parameters"]["vimAccountId"])
+
+                    # Creating scenario if vim-network-name / vim-network-id are present as instantiation parameter
+                    # Use vim-network-id instantiation parameter
+                    vim_network_option = None
+                    if ip_vld:
+                        if ip_vld.get("vim-network-id"):
+                            vim_network_option = "vim-network-id"
+                        elif ip_vld.get("vim-network-name"):
+                            vim_network_option = "vim-network-name"
+                        if ip_vld.get("ip-profile"):
+                            populate_dict(RO_ns_params, ("networks", netslice_vld["name"], "ip-profile"),
+                                          ip_profile_2_RO(ip_vld["ip-profile"]))
+
+                    if vim_network_option:
+                        if ip_vld.get(vim_network_option):
+                            if isinstance(ip_vld.get(vim_network_option), list):
+                                for vim_net_id in ip_vld.get(vim_network_option):
+                                    for vim_account, vim_net in vim_net_id.items():
+                                        RO_vld_sites.append({
+                                            "netmap-use": vim_net,
+                                            "datacenter": vim_account_2_RO(vim_account)
+                                        })
+                            elif isinstance(ip_vld.get(vim_network_option), dict):
+                                for vim_account, vim_net in ip_vld.get(vim_network_option).items():
                                     RO_vld_sites.append({
                                         "netmap-use": vim_net,
                                         "datacenter": vim_account_2_RO(vim_account)
                                     })
-                        elif isinstance(ip_vld.get(vim_network_option), dict):
-                            for vim_account, vim_net in ip_vld.get(vim_network_option).items():
+                            else:
                                 RO_vld_sites.append({
-                                    "netmap-use": vim_net,
-                                    "datacenter": vim_account_2_RO(vim_account)
-                                })
-                        else:
-                            RO_vld_sites.append({
-                                "netmap-use": ip_vld[vim_network_option],
-                                "datacenter": vim_account_2_RO(netslice_vld["vimAccountId"])})
-                        
-                # Use default netslice vim-network-name from template
-                else:
-                    for nss_conn_point_ref in get_iterable(netslice_vld, "nss-connection-point-ref"):
-                        if nss_conn_point_ref.get("vimAccountId"):
-                            if nss_conn_point_ref["vimAccountId"] != netslice_vld["vimAccountId"]:
-                                RO_vld_sites.append({
-                                    "netmap-create": None,
-                                    "datacenter": vim_account_2_RO(nss_conn_point_ref["vimAccountId"])})
+                                    "netmap-use": ip_vld[vim_network_option],
+                                    "datacenter": vim_account_2_RO(netslice_vld["vimAccountId"])})
+
+                    # Use default netslice vim-network-name from template
+                    else:
+                        for nss_conn_point_ref in get_iterable(netslice_vld, "nss-connection-point-ref"):
+                            if nss_conn_point_ref.get("vimAccountId"):
+                                if nss_conn_point_ref["vimAccountId"] != netslice_vld["vimAccountId"]:
+                                    RO_vld_sites.append({
+                                        "netmap-create": None,
+                                        "datacenter": vim_account_2_RO(nss_conn_point_ref["vimAccountId"])})
 
-                if vld_shared:
-                    populate_dict(RO_ns_params, ("networks", netslice_vld["name"], "use-network"), vld_shared)
+                    if vld_shared:
+                        populate_dict(RO_ns_params, ("networks", netslice_vld["name"], "use-network"), vld_shared)
 
-                if RO_vld_sites:
-                    populate_dict(RO_ns_params, ("networks", netslice_vld["name"], "sites"), RO_vld_sites)
+                    if RO_vld_sites:
+                        populate_dict(RO_ns_params, ("networks", netslice_vld["name"], "sites"), RO_vld_sites)
 
-                RO_ns_params["scenario"] = {"nets": [{"name": netslice_vld["name"],
-                                            "external": mgmt_network, "type": "bridge"}]}
+                    RO_ns_params["scenario"] = {"nets": [{"name": netslice_vld["name"],
+                                                "external": mgmt_network, "type": "bridge"}]}
 
-                # self.logger.debug(logging_text + step)
-                db_nsir_update["detailed-status"] = "Creating netslice-vld at RO"
-                desc = await RO.create("ns", descriptor=RO_ns_params)
-                db_nsir_update_RO = {}
-                db_nsir_update_RO["netslice_scenario_id"] = desc["uuid"]
-                db_nsir_update_RO["vld_id"] = RO_ns_params["name"]
+                    # self.logger.debug(logging_text + step)
+                    desc = await RO.create("ns", descriptor=RO_ns_params)
+                    db_nsir_update_RO["netslice_scenario_id"] = desc["uuid"]
                 db_nsir_update["_admin.deployed.RO"].append(db_nsir_update_RO)
-        
+
         def overwrite_nsd_params(self, db_nsir, nslcmop):
-            RO_list = []
+            nonlocal nsi_vld_instantiationi_params
+            nonlocal db_nsir_update
             vld_op_list = []
             vld = None
             nsr_id = nslcmop.get("nsInstanceId")
             # Overwrite instantiation parameters in netslice runtime
-            if db_nsir.get("_admin"):
-                if db_nsir["_admin"].get("deployed"):
-                    db_admin_deployed_nsir = db_nsir["_admin"].get("deployed")
-                    if db_admin_deployed_nsir.get("RO"):
-                        RO_list = db_admin_deployed_nsir["RO"]
-
-            for RO_item in RO_list:
-                for netslice_vld in get_iterable(db_nsir["_admin"], "netslice-vld"):
-                    # if is equal vld of _admin with vld of netslice-vld then go for the CPs
-                    if RO_item.get("vld_id") == netslice_vld.get("id"):
-                        # Search the cp of netslice-vld that match with nst:netslice-subnet
-                        for nss_cp_item in get_iterable(netslice_vld, "nss-connection-point-ref"):
-                            # Search the netslice-subnet of nst that match
-                            for nss in get_iterable(db_nsir["_admin"], "netslice-subnet"):
-                                # Compare nss-ref equal nss from nst
-                                if nss_cp_item["nss-ref"] == nss["nss-id"]:
-                                    db_nsds = self.db.get_one("nsds", {"_id": nss["nsdId"]})
-                                    # Go for nsd, and search the CP that match with nst:CP to get vld-id-ref
-                                    for cp_nsd in db_nsds["connection-point"]:
-                                        if cp_nsd["name"] == nss_cp_item["nsd-connection-point-ref"]:
-                                            if nslcmop.get("operationParams"):
-                                                if nslcmop["operationParams"].get("nsName") == nss["nsName"]:
-                                                    vld_id = RO_item["vld_id"]
-                                                    netslice_scenario_id = RO_item["netslice_scenario_id"]
-                                                    nslcmop_vld = {}
-                                                    nslcmop_vld["ns-net"] = {vld_id: netslice_scenario_id}
-                                                    nslcmop_vld["name"] = cp_nsd["vld-id-ref"]
-                                                    for vld in get_iterable(nslcmop["operationParams"], "vld"):
-                                                        if vld["name"] == cp_nsd["vld-id-ref"]:
-                                                            nslcmop_vld.update(vld)
-                                                    vld_op_list.append(nslcmop_vld)
+            RO_list = db_nsir_admin["deployed"]["RO"]
+
+            for ro_item_index, RO_item in enumerate(RO_list):
+                netslice_vld = next((n for n in get_iterable(db_nsir["_admin"], "netslice-vld")
+                                     if RO_item.get("vld_id") == n.get("id")), None)
+                if not netslice_vld:
+                    continue
+                # if is equal vld of _admin with vld of netslice-vld then go for the CPs
+                # Search the cp of netslice-vld that match with nst:netslice-subnet
+                for nss_cp_item in get_iterable(netslice_vld, "nss-connection-point-ref"):
+                    # Search the netslice-subnet of nst that match
+                    nss = next((nss for nss in get_iterable(db_nsir["_admin"], "netslice-subnet")
+                                if nss_cp_item["nss-ref"] == nss["nss-id"]), None)
+                    # Compare nss-ref equal nss from nst
+                    if not nss:
+                        continue
+                    db_nsds = self.db.get_one("nsds", {"_id": nss["nsdId"]})
+                    # Go for nsd, and search the CP that match with nst:CP to get vld-id-ref
+                    for cp_nsd in db_nsds.get("sapd", ()):
+                        if cp_nsd["id"] == nss_cp_item["nsd-connection-point-ref"]:
+                            if nslcmop.get("operationParams"):
+                                if nslcmop["operationParams"].get("nsName") == nss["nsName"]:
+                                    vld_id = RO_item["vld_id"]
+                                    netslice_scenario_id = RO_item["netslice_scenario_id"]
+                                    nslcmop_vld = {}
+                                    nslcmop_vld["name"] = cp_nsd["virtual-link-desc"]
+                                    for vld in get_iterable(nslcmop["operationParams"], "vld"):
+                                        if vld["name"] == cp_nsd["virtual-link-desc"]:
+                                            nslcmop_vld.update(vld)
+                                    if self.ro_config["ng"]:
+                                        nslcmop_vld["common_id"] = netslice_scenario_id
+                                        nslcmop_vld.update(nsi_vld_instantiationi_params.get(RO_item["vld_id"], {}))
+                                    else:
+                                        nslcmop_vld["ns-net"] = {vld_id: netslice_scenario_id}
+                                    vld_op_list.append(nslcmop_vld)
             nslcmop["operationParams"]["vld"] = vld_op_list
-            self.update_db_2("nslcmops", nslcmop["_id"], nslcmop)
+            self.update_db_2("nslcmops", nslcmop["_id"], {"operationParams.vld": vld_op_list})
             return nsr_id, nslcmop
 
         try:
+            # wait for any previous tasks in process
+            await self.lcm_tasks.waitfor_related_HA('nsi', 'nsilcmops', nsilcmop_id)
+
             step = "Getting nsir={} from db".format(nsir_id)
             db_nsir = self.db.get_one("nsis", {"_id": nsir_id})
             step = "Getting nsilcmop={} from db".format(nsilcmop_id)
             db_nsilcmop = self.db.get_one("nsilcmops", {"_id": nsilcmop_id})
 
-            # look if previous tasks is in process
-            task_name, task_dependency = self.lcm_tasks.lookfor_related("nsi", nsir_id, nsilcmop_id)
-            if task_dependency:
-                step = db_nsilcmop_update["detailed-status"] = \
-                    "Waiting for related tasks to be completed: {}".format(task_name)
-                self.logger.debug(logging_text + step)
-                self.update_db_2("nsilcmops", nsilcmop_id, db_nsilcmop_update)
-                _, pending = await asyncio.wait(task_dependency, timeout=3600)
-                if pending:
-                    raise LcmException("Timeout waiting related tasks to be completed")
+            start_deploy = time()
+            nsi_params = db_nsilcmop.get("operationParams")
+            if nsi_params and nsi_params.get("timeout_nsi_deploy"):
+                timeout_nsi_deploy = nsi_params["timeout_nsi_deploy"]
+            else:
+                timeout_nsi_deploy = self.timeout.get("nsi_deploy", self.timeout_nsi_deploy)
 
             # Empty list to keep track of network service records status in the netslice
             nsir_admin = db_nsir_admin = db_nsir.get("_admin")
 
+            step = "Creating slice operational-status init"
             # Slice status Creating
             db_nsir_update["detailed-status"] = "creating"
             db_nsir_update["operational-status"] = "init"
-            self.update_db_2("nsis", nsir_id, db_nsir_update)     
-            
+            db_nsir_update["_admin.nsiState"] = "INSTANTIATED"
+
+            step = "Instantiating netslice VLDs before NS instantiation"
             # Creating netslice VLDs networking before NS instantiation
+            db_nsir_update["detailed-status"] = step
+            self.update_db_2("nsis", nsir_id, db_nsir_update)
             db_nsir_update["_admin.deployed.RO"] = db_nsir_admin["deployed"]["RO"]
             for vld_item in get_iterable(nsir_admin, "netslice-vld"):
                 await netslice_scenario_create(self, vld_item, nsir_id, db_nsir, db_nsir_admin, db_nsir_update)
+
+            step = "Instantiating netslice subnets"
+            db_nsir_update["detailed-status"] = step
             self.update_db_2("nsis", nsir_id, db_nsir_update)
-            
-            db_nsir_update["detailed-status"] = "Creating netslice subnets at RO"
-            self.update_db_2("nsis", nsir_id, db_nsir_update)  
 
             db_nsir = self.db.get_one("nsis", {"_id": nsir_id})
 
@@ -300,16 +321,14 @@ class NetsliceLcm(LcmBase):
             # netslice_scenarios = db_nsir["_admin"]["deployed"]["RO"]
             # db_nsir_update_RO = deepcopy(netslice_scenarios)
             # for netslice_scenario in netslice_scenarios:
-            #    await netslice_scenario_check(self, netslice_scenario["netslice_scenario_id"], 
+            #    await netslice_scenario_check(self, netslice_scenario["netslice_scenario_id"],
             #                                  nsir_id, db_nsir_update_RO)
-            
+
             # db_nsir_update["_admin.deployed.RO"] = db_nsir_update_RO
             # self.update_db_2("nsis", nsir_id, db_nsir_update)
 
             # Iterate over the network services operation ids to instantiate NSs
-            # TODO: (future improvement) look another way check the tasks instead of keep asking
-            # -> https://docs.python.org/3/library/asyncio-task.html#waiting-primitives
-            # steps: declare ns_tasks, add task when terminate is called, await asyncio.wait(vca_task_list, timeout=300)
+            step = "Instantiating Netslice Subnets"
             db_nsir = self.db.get_one("nsis", {"_id": nsir_id})
             nslcmop_ids = db_nsilcmop["operationParams"].get("nslcmops_ids")
             for nslcmop_id in nslcmop_ids:
@@ -319,19 +338,17 @@ class NetsliceLcm(LcmBase):
                 step = "Launching ns={} instantiate={} task".format(nsr_id, nslcmop_id)
                 task = asyncio.ensure_future(self.ns.instantiate(nsr_id, nslcmop_id))
                 self.lcm_tasks.register("ns", nsr_id, nslcmop_id, "ns_instantiate", task)
-            
+
             # Wait until Network Slice is ready
-            step = nsir_status_detailed = " Waiting nsi ready. nsi_id={}".format(nsir_id)
+            step = " Waiting nsi ready."
             nsrs_detailed_list_old = None
             self.logger.debug(logging_text + step)
-            
-            # TODO: substitute while for await (all task to be done or not)
-            deployment_timeout = 2 * 3600   # Two hours
-            while deployment_timeout > 0:
+
+            # For HA, it is checked from database, as the ns operation may be managed by other LCM worker
+            while time() <= start_deploy + timeout_nsi_deploy:
                 # Check ns instantiation status
                 nsi_ready = True
                 nsir = self.db.get_one("nsis", {"_id": nsir_id})
-                nsir_admin = nsir["_admin"]
                 nsrs_detailed_list = nsir["_admin"]["nsrs-detailed-list"]
                 nsrs_detailed_list_new = []
                 for nslcmop_item in nslcmop_ids:
@@ -341,32 +358,33 @@ class NetsliceLcm(LcmBase):
                     for nss in nsrs_detailed_list:
                         if nss["nsrId"] == nslcmop["nsInstanceId"]:
                             nss.update({"nsrId": nslcmop["nsInstanceId"], "status": nslcmop["operationState"],
-                                        "detailed-status":
-                                        nsir_status_detailed + "; {}".format(nslcmop.get("detailed-status")),
+                                        "detailed-status": nslcmop.get("detailed-status"),
                                         "instantiated": True})
                             nsrs_detailed_list_new.append(nss)
-                    if status not in ["COMPLETED", "PARTIALLY_COMPLETED", "FAILED", "FAILED_TEMP"]:  
+                    if status not in ["COMPLETED", "PARTIALLY_COMPLETED", "FAILED", "FAILED_TEMP"]:
                         nsi_ready = False
 
                 if nsrs_detailed_list_new != nsrs_detailed_list_old:
-                    nsir_admin["nsrs-detailed-list"] = nsrs_detailed_list_new
                     nsrs_detailed_list_old = nsrs_detailed_list_new
-                    db_nsir_update["_admin"] = nsir_admin
-                    self.update_db_2("nsis", nsir_id, db_nsir_update)
+                    self.update_db_2("nsis", nsir_id, {"_admin.nsrs-detailed-list": nsrs_detailed_list_new})
 
                 if nsi_ready:
-                    step = "Network Slice Instance is ready. nsi_id={}".format(nsir_id)
-                    for items in nsrs_detailed_list:
-                        if "FAILED" in items.values():
-                            raise LcmException("Error deploying NSI: {}".format(nsir_id))
+                    error_list = []
+                    step = "Network Slice Instance instantiated"
+                    for nss in nsrs_detailed_list:
+                        if nss["status"] in ("FAILED", "FAILED_TEMP"):
+                            error_list.append("NS {} {}: {}".format(nss["nsrId"], nss["status"],
+                                                                    nss["detailed-status"]))
+                    if error_list:
+                        step = "instantiating"
+                        raise LcmException("; ".join(error_list))
                     break
+
                 # TODO: future improvement due to synchronism -> await asyncio.wait(vca_task_list, timeout=300)
                 await asyncio.sleep(5, loop=self.loop)
-                deployment_timeout -= 5
-            
-            if deployment_timeout <= 0:
-                raise LcmException("Timeout waiting nsi to be ready. nsi_id={}".format(nsir_id))
+
+            else:   # timeout_nsi_deploy reached:
+                raise LcmException("Timeout waiting nsi to be ready.")
 
             db_nsir_update["operational-status"] = "running"
             db_nsir_update["detailed-status"] = "done"
@@ -391,13 +409,13 @@ class NetsliceLcm(LcmBase):
                 if db_nsir:
                     db_nsir_update["detailed-status"] = "ERROR {}: {}".format(step, exc)
                     db_nsir_update["operational-status"] = "failed"
+                    db_nsir_update["config-status"] = "configured"
                 if db_nsilcmop:
                     db_nsilcmop_update["detailed-status"] = "FAILED {}: {}".format(step, exc)
                     db_nsilcmop_update["operationState"] = nsilcmop_operation_state = "FAILED"
                     db_nsilcmop_update["statusEnteredTime"] = time()
             try:
                 if db_nsir:
-                    db_nsir_update["_admin.nsiState"] = "INSTANTIATED"
                     db_nsir_update["_admin.nsilcmop"] = None
                     self.update_db_2("nsis", nsir_id, db_nsir_update)
                 if db_nsilcmop:
@@ -414,9 +432,15 @@ class NetsliceLcm(LcmBase):
             self.lcm_tasks.remove("nsi", nsir_id, nsilcmop_id, "nsi_instantiate")
 
     async def terminate(self, nsir_id, nsilcmop_id):
+
+        # Try to lock HA task here
+        task_is_locked_by_me = self.lcm_tasks.lock_HA('nsi', 'nsilcmops', nsilcmop_id)
+        if not task_is_locked_by_me:
+            return
+
         logging_text = "Task nsi={} terminate={} ".format(nsir_id, nsilcmop_id)
         self.logger.debug(logging_text + "Enter")
-        exc = None 
+        exc = None
         db_nsir = None
         db_nsilcmop = None
         db_nsir_update = {"_admin.nsilcmop": nsilcmop_id}
@@ -427,12 +451,15 @@ class NetsliceLcm(LcmBase):
         nsilcmop_operation_state = None
         autoremove = False  # autoremove after terminated
         try:
+            # wait for any previous tasks in process
+            await self.lcm_tasks.waitfor_related_HA('nsi', 'nsilcmops', nsilcmop_id)
+
             step = "Getting nsir={} from db".format(nsir_id)
             db_nsir = self.db.get_one("nsis", {"_id": nsir_id})
             nsir_deployed = deepcopy(db_nsir["_admin"].get("deployed"))
             step = "Getting nsilcmop={} from db".format(nsilcmop_id)
             db_nsilcmop = self.db.get_one("nsilcmops", {"_id": nsilcmop_id})
-            
+
             # TODO: Check if makes sense check the nsiState=NOT_INSTANTIATED when terminate
             # CASE: Instance was terminated but there is a second request to terminate the instance
             if db_nsir["_admin"]["nsiState"] == "NOT_INSTANTIATED":
@@ -444,25 +471,14 @@ class NetsliceLcm(LcmBase):
             db_nsir_update["detailed-status"] = "Terminating Netslice subnets"
             self.update_db_2("nsis", nsir_id, db_nsir_update)
 
-            # look if previous tasks is in process
-            task_name, task_dependency = self.lcm_tasks.lookfor_related("nsi", nsir_id, nsilcmop_id)
-            if task_dependency:
-                step = db_nsilcmop_update["detailed-status"] = \
-                    "Waiting for related tasks to be completed: {}".format(task_name)
-                self.logger.debug(logging_text + step)
-                self.update_db_2("nsilcmops", nsilcmop_id, db_nsilcmop_update)
-                _, pending = await asyncio.wait(task_dependency, timeout=3600)
-                if pending:
-                    raise LcmException("Timeout waiting related tasks to be completed")
-                       
             # Gets the list to keep track of network service records status in the netslice
-            nsir_admin = db_nsir["_admin"]
-            nsrs_detailed_list = []       
+            nsrs_detailed_list = []
 
             # Iterate over the network services operation ids to terminate NSs
-            # TODO: (future improvement) look another way check the tasks instead of keep asking  
+            # TODO: (future improvement) look another way check the tasks instead of keep asking
             # -> https://docs.python.org/3/library/asyncio-task.html#waiting-primitives
             # steps: declare ns_tasks, add task when terminate is called, await asyncio.wait(vca_task_list, timeout=300)
+            step = "Terminating Netslice Subnets"
             nslcmop_ids = db_nsilcmop["operationParams"].get("nslcmops_ids")
             nslcmop_new = []
             for nslcmop_id in nslcmop_ids:
@@ -489,13 +505,12 @@ class NetsliceLcm(LcmBase):
             step = nsir_status_detailed = " Waiting nsi terminated. nsi_id={}".format(nsir_id)
             nsrs_detailed_list_old = None
             self.logger.debug(logging_text + step)
-            
+
             termination_timeout = 2 * 3600   # Two hours
             while termination_timeout > 0:
                 # Check ns termination status
                 nsi_ready = True
                 db_nsir = self.db.get_one("nsis", {"_id": nsir_id})
-                nsir_admin = db_nsir["_admin"]
                 nsrs_detailed_list = db_nsir["_admin"].get("nsrs-detailed-list")
                 nsrs_detailed_list_new = []
                 for nslcmop_item in nslcmop_ids:
@@ -512,15 +527,12 @@ class NetsliceLcm(LcmBase):
                         nsi_ready = False
 
                 if nsrs_detailed_list_new != nsrs_detailed_list_old:
-                    nsir_admin["nsrs-detailed-list"] = nsrs_detailed_list_new
                     nsrs_detailed_list_old = nsrs_detailed_list_new
-                    db_nsir_update["_admin"] = nsir_admin
-                    self.update_db_2("nsis", nsir_id, db_nsir_update)
-                    
+                    self.update_db_2("nsis", nsir_id, {"_admin.nsrs-detailed-list": nsrs_detailed_list_new})
+
                 if nsi_ready:
                     # Check if it is the last used nss and mark isinstantiate: False
                     db_nsir = self.db.get_one("nsis", {"_id": nsir_id})
-                    nsir_admin = db_nsir["_admin"]
                     nsrs_detailed_list = db_nsir["_admin"].get("nsrs-detailed-list")
                     for nss in nsrs_detailed_list:
                         _filter = {"_admin.nsrs-detailed-list.ANYINDEX.nsrId": nss["nsrId"],
@@ -529,8 +541,6 @@ class NetsliceLcm(LcmBase):
                         nsis_list = self.db.get_one("nsis", _filter, fail_on_empty=False, fail_on_more=False)
                         if not nsis_list:
                             nss.update({"instantiated": False})
-                    db_nsir_update["_admin"] = nsir_admin
-                    self.update_db_2("nsis", nsir_id, db_nsir_update)
 
                     step = "Network Slice Instance is terminated. nsi_id={}".format(nsir_id)
                     for items in nsrs_detailed_list:
@@ -549,19 +559,20 @@ class NetsliceLcm(LcmBase):
             for nsir_deployed_RO in get_iterable(nsir_deployed, "RO"):
                 RO_nsir_id = nsir_deployed_RO.get("netslice_scenario_id")
                 try:
-                    step = db_nsir_update["detailed-status"] = "Deleting netslice-vld at RO"
-                    db_nsilcmop_update["detailed-status"] = "Deleting netslice-vld at RO"
-                    self.logger.debug(logging_text + step)
-                    desc = await RO.delete("ns", RO_nsir_id)
-                    RO_delete_action = desc["action_id"]
-                    nsir_deployed_RO["vld_delete_action_id"] = RO_delete_action
-                    nsir_deployed_RO["vld_status"] = "DELETING"
-                    db_nsir_update["_admin.deployed"] = nsir_deployed
-                    self.update_db_2("nsis", nsir_id, db_nsir_update)
-                    if RO_delete_action:
-                        # wait until NS is deleted from VIM
-                        step = "Waiting ns deleted from VIM. RO_id={}".format(RO_nsir_id)
+                    if not self.ro_config["ng"]:
+                        step = db_nsir_update["detailed-status"] = "Deleting netslice-vld at RO"
+                        db_nsilcmop_update["detailed-status"] = "Deleting netslice-vld at RO"
                         self.logger.debug(logging_text + step)
+                        desc = await RO.delete("ns", RO_nsir_id)
+                        RO_delete_action = desc["action_id"]
+                        nsir_deployed_RO["vld_delete_action_id"] = RO_delete_action
+                        nsir_deployed_RO["vld_status"] = "DELETING"
+                        db_nsir_update["_admin.deployed"] = nsir_deployed
+                        self.update_db_2("nsis", nsir_id, db_nsir_update)
+                        if RO_delete_action:
+                            # wait until NS is deleted from VIM
+                            step = "Waiting ns deleted from VIM. RO_id={}".format(RO_nsir_id)
+                            self.logger.debug(logging_text + step)
                 except ROclient.ROClientException as e:
                     if e.http_code == 404:  # not found
                         nsir_deployed_RO["vld_id"] = None