bug 585 change charm model to nsr_id. Create/Delete model name
[osm/LCM.git] / osm_lcm / ns.py
index 7bfb898..d6318ad 100644 (file)
@@ -29,7 +29,7 @@ from lcm_utils import LcmException, LcmExceptionNoMgmtIP, LcmBase
 
 from osm_common.dbbase import DbException
 from osm_common.fsbase import FsException
-from n2vc.vnf import N2VC, N2VCPrimitiveExecutionFailed
+from n2vc.vnf import N2VC, N2VCPrimitiveExecutionFailed, NetworkServiceDoesNotExist
 
 from copy import copy, deepcopy
 from http import HTTPStatus
@@ -69,9 +69,9 @@ def populate_dict(target_dict, key_list, value):
 
 class NsLcm(LcmBase):
     timeout_vca_on_error = 5 * 60   # Time for charm from first time at blocked,error status to mark as failed
-    total_deploy_timeout = 30 * 60   # global timeout for deployment
-    timeout_charm_delete = 5 * 60
-    timeout_primitive = 5 * 60  # timeout for primitive execution
+    total_deploy_timeout = 2 * 3600   # global timeout for deployment
+    timeout_charm_delete = 10 * 60
+    timeout_primitive = 10 * 60  # timeout for primitive execution
 
     def __init__(self, db, msg, fs, lcm_tasks, ro_config, vca_config, loop):
         """
@@ -99,6 +99,8 @@ class NsLcm(LcmBase):
             # it unset and pass it via DeployCharms
             # artifacts=vca_config[''],
             artifacts=None,
+            juju_public_key=vca_config.get('pubkey'),
+            ca_cert=vca_config.get('cacert'),
         )
 
     def vnfd2RO(self, vnfd, new_id=None, additionalParams=None, nsrId=None):
@@ -197,7 +199,8 @@ class NsLcm(LcmBase):
                 if model_name == vca_deployed["model"] and application_name == vca_deployed["application"]:
                     break
             else:
-                self.logger.error(logging_text + " Not present at nsr._admin.deployed.VCA")
+                self.logger.error(logging_text + " Not present at nsr._admin.deployed.VCA. Received model_name={}".
+                                  format(model_name))
                 return
             if task:
                 if task.cancelled():
@@ -242,6 +245,7 @@ class NsLcm(LcmBase):
         :return: The RO ns descriptor
         """
         vim_2_RO = {}
+        wim_2_RO = {}
         # TODO feature 1417: Check that no instantiation is set over PDU
         # check if PDU forces a concrete vim-network-id and add it
         # check if PDU contains a SDN-assist info (dpid, switch, port) and pass it to RO
@@ -258,6 +262,21 @@ class NsLcm(LcmBase):
             vim_2_RO[vim_account] = RO_vim_id
             return RO_vim_id
 
+        def wim_account_2_RO(wim_account):
+            if isinstance(wim_account, str):
+                if wim_account in wim_2_RO:
+                    return wim_2_RO[wim_account]
+
+                db_wim = self.db.get_one("wim_accounts", {"_id": wim_account})
+                if db_wim["_admin"]["operationalState"] != "ENABLED":
+                    raise LcmException("WIM={} is not available. operationalState={}".format(
+                        wim_account, db_wim["_admin"]["operationalState"]))
+                RO_wim_id = db_wim["_admin"]["deployed"]["RO-account"]
+                wim_2_RO[wim_account] = RO_wim_id
+                return RO_wim_id
+            else:
+                return wim_account
+
         def ip_profile_2_RO(ip_profile):
             RO_ip_profile = deepcopy((ip_profile))
             if "dns-server" in RO_ip_profile:
@@ -281,6 +300,7 @@ class NsLcm(LcmBase):
             # "name": ns_params["nsName"],
             # "description": ns_params.get("nsDescription"),
             "datacenter": vim_account_2_RO(ns_params["vimAccountId"]),
+            "wim_account": wim_account_2_RO(ns_params.get("wimAccountId")),
             # "scenario": ns_params["nsdId"],
         }
         if n2vc_key_list:
@@ -402,6 +422,10 @@ class NsLcm(LcmBase):
             if "ip-profile" in vld_params:
                 populate_dict(RO_ns_params, ("networks", vld_params["name"], "ip-profile"),
                               ip_profile_2_RO(vld_params["ip-profile"]))
+
+            if "wimAccountId" in vld_params and vld_params["wimAccountId"] is not None:
+                populate_dict(RO_ns_params, ("networks", vld_params["name"], "wim_account"),
+                              wim_account_2_RO(vld_params["wimAccountId"])),
             if vld_params.get("vim-network-name"):
                 RO_vld_sites = []
                 if isinstance(vld_params["vim-network-name"], dict):
@@ -426,6 +450,12 @@ class NsLcm(LcmBase):
                     RO_vld_sites.append({"netmap-use": vld_params["vim-network-id"]})
                 if RO_vld_sites:
                     populate_dict(RO_ns_params, ("networks", vld_params["name"], "sites"), RO_vld_sites)
+            if vld_params.get("ns-net"):
+                if isinstance(vld_params["ns-net"], dict):
+                    for vld_id, instance_scenario_id in vld_params["ns-net"].items():
+                        RO_vld_ns_net = {"instance_scenario_id": instance_scenario_id, "osm_id": vld_id}
+                if RO_vld_ns_net:
+                    populate_dict(RO_ns_params, ("networks", vld_params["name"], "use-network"), RO_vld_ns_net)            
             if "vnfd-connection-point-ref" in vld_params:
                 for cp_params in vld_params["vnfd-connection-point-ref"]:
                     # look for interface
@@ -539,7 +569,7 @@ class NsLcm(LcmBase):
                     continue
                 vnfr_update = {}
                 if vnf_RO.get("ip_address"):
-                    db_vnfr["ip-address"] = vnfr_update["ip-address"] = vnf_RO["ip_address"]
+                    db_vnfr["ip-address"] = vnfr_update["ip-address"] = vnf_RO["ip_address"].split(";")[0]
                 elif not db_vnfr.get("ip-address"):
                     raise LcmExceptionNoMgmtIP("ns member_vnf_index '{}' has no IP address".format(vnf_index))
 
@@ -554,7 +584,10 @@ class NsLcm(LcmBase):
                             vdur_RO_count_index += 1
                             continue
                         vdur["vim-id"] = vdur_RO.get("vim_vm_id")
-                        vdur["ip-address"] = vdur_RO.get("ip_address")
+                        if vdur_RO.get("ip_address"):
+                            vdur["ip-address"] = vdur_RO["ip_address"].split(";")[0]
+                        else:
+                            vdur["ip-address"] = None
                         vdur["vdu-id-ref"] = vdur_RO.get("vdu_osm_id")
                         vdur["name"] = vdur_RO.get("vim_name")
                         vdur["status"] = vdur_RO.get("status")
@@ -645,8 +678,10 @@ class NsLcm(LcmBase):
 
             # Get or generates the _admin.deployed,VCA list
             vca_deployed_list = None
+            vca_model_name = None
             if db_nsr["_admin"].get("deployed"):
                 vca_deployed_list = db_nsr["_admin"]["deployed"].get("VCA")
+                vca_model_name = db_nsr["_admin"]["deployed"].get("VCA-model-name")
             if vca_deployed_list is None:
                 vca_deployed_list = []
                 db_nsr_update["_admin.deployed.VCA"] = vca_deployed_list
@@ -801,7 +836,7 @@ class NsLcm(LcmBase):
             while time() <= start_deploy + self.total_deploy_timeout:
                 desc = await RO.show("ns", RO_nsr_id)
                 ns_status, ns_status_info = RO.check_ns_status(desc)
-                db_nsr_update["admin.deployed.RO.nsr_status"] = ns_status
+                db_nsr_update["_admin.deployed.RO.nsr_status"] = ns_status
                 if ns_status == "ERROR":
                     raise ROclient.ROClientException(ns_status_info)
                 elif ns_status == "BUILD":
@@ -859,7 +894,7 @@ class NsLcm(LcmBase):
 
                 # ns_name will be ignored in the current version of N2VC
                 # but will be implemented for the next point release.
-                model_name = "default"    # TODO bug 585  nsr_id
+                model_name = nsr_id
                 if vdu_id:
                     vdu_id_text = vdu_id + "-"
                 else:
@@ -934,6 +969,13 @@ class NsLcm(LcmBase):
                     proxy_charm = vnf_config["juju"]["charm"]
 
                     if proxy_charm:
+                        if not vca_model_name:
+                            step = "creating VCA model name '{}'".format(nsr_id)
+                            self.logger.debug(logging_text + step)
+                            await self.n2vc.CreateNetworkService(nsr_id)
+                            vca_model_name = nsr_id
+                            db_nsr_update["_admin.deployed.VCA-model-name"] = nsr_id
+                            self.update_db_2("nsrs", nsr_id, db_nsr_update)
                         step = "connecting to N2VC to configure vnf {}".format(vnf_index)
                         vnfr_params["rw_mgmt_ip"] = db_vnfrs[vnf_index]["ip-address"]
                         charm_params = {
@@ -945,6 +987,7 @@ class NsLcm(LcmBase):
                         # Login to the VCA. If there are multiple calls to login(),
                         # subsequent calls will be a nop and return immediately.
                         await self.n2vc.login()
+
                         deploy_charm(vnf_index, None, None, None, charm_params, n2vc_info)
                         number_to_configure += 1
 
@@ -957,6 +1000,12 @@ class NsLcm(LcmBase):
                         proxy_charm = vdu_config["juju"]["charm"]
 
                         if proxy_charm:
+                            if not vca_model_name:
+                                step = "creating VCA model name"
+                                await self.n2vc.CreateNetworkService(nsr_id)
+                                vca_model_name = nsr_id
+                                db_nsr_update["_admin.deployed.VCA-model-name"] = nsr_id
+                                self.update_db_2("nsrs", nsr_id, db_nsr_update)
                             step = "connecting to N2VC to configure vdu {} from vnf {}".format(vdu["id"], vnf_index)
                             await self.n2vc.login()
                             vdur = db_vnfrs[vnf_index]["vdur"][vdu_index]
@@ -1149,7 +1198,21 @@ class NsLcm(LcmBase):
             db_nsr_update["operational-status"] = "terminating"
             db_nsr_update["config-status"] = "terminating"
 
-            if nsr_deployed and nsr_deployed.get("VCA"):
+            if nsr_deployed and nsr_deployed.get("VCA-model-name"):
+                vca_model_name = nsr_deployed["VCA-model-name"]
+                step = "deleting VCA model name '{}' and all charms".format(vca_model_name)
+                self.logger.debug(logging_text + step)
+                try:
+                    await self.n2vc.DestroyNetworkService(vca_model_name)
+                except NetworkServiceDoesNotExist:
+                    pass
+                db_nsr_update["_admin.deployed.VCA-model-name"] = None
+                if nsr_deployed.get("VCA"):
+                    for vca_index in range(0, len(nsr_deployed["VCA"])):
+                        db_nsr_update["_admin.deployed.VCA.{}".format(vca_index)] = None
+                self.update_db_2("nsrs", nsr_id, db_nsr_update)
+            # for backward compatibility if charm have been created with "default"  model name delete one by one
+            elif nsr_deployed and nsr_deployed.get("VCA"):
                 try:
                     step = "Scheduling configuration charms removing"
                     db_nsr_update["detailed-status"] = "Deleting charms"
@@ -1425,7 +1488,7 @@ class NsLcm(LcmBase):
             )
             while time() - start_primitive_time < self.timeout_primitive:
                 primitive_result_ = await self.n2vc.GetPrimitiveStatus(model_name, primitive_id)
-                if primitive_result_ == "running":
+                if primitive_result_ in ("running", "pending"):
                     pass
                 elif primitive_result_ in ("completed", "failed"):
                     primitive_result = "COMPLETED" if primitive_result_ == "completed" else "FAILED"