Implement api proxy for native charms
[osm/LCM.git] / osm_lcm / ns.py
index e1c01b3..915eece 100644 (file)
@@ -53,7 +53,7 @@ def get_iterable(in_dict, in_key):
 
 def populate_dict(target_dict, key_list, value):
     """
-    Upate target_dict creating nested dictionaries with the key_list. Last key_list item is asigned the value.
+    Update target_dict creating nested dictionaries with the key_list. Last key_list item is asigned the value.
     Example target_dict={K: J}; key_list=[a,b,c];  target_dict will be {K: J, a: {b: {c: value}}}
     :param target_dict: dictionary to be changed
     :param key_list: list of keys to insert at target_dict
@@ -67,6 +67,21 @@ def populate_dict(target_dict, key_list, value):
     target_dict[key_list[-1]] = value
 
 
+def deep_get(target_dict, key_list):
+    """
+    Get a value from target_dict entering in the nested keys. If keys does not exist, it returns None
+    Example target_dict={a: {b: 5}}; key_list=[a,b] returns 5; both key_list=[a,b,c] and key_list=[f,h] return None
+    :param target_dict: dictionary to be read
+    :param key_list: list of keys to read from  target_dict
+    :return: The wanted value if exist, None otherwise
+    """
+    for key in key_list:
+        if not isinstance(target_dict, dict) or key not in target_dict:
+            return None
+        target_dict = target_dict[key]
+    return target_dict
+
+
 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 = 2 * 3600   # global timeout for deployment
@@ -101,6 +116,7 @@ class NsLcm(LcmBase):
             artifacts=None,
             juju_public_key=vca_config.get('pubkey'),
             ca_cert=vca_config.get('cacert'),
+            api_proxy=vca_config.get('apiproxy')
         )
         self.RO = ROclient.ROClient(self.loop, **self.ro_config)
 
@@ -304,36 +320,39 @@ class NsLcm(LcmBase):
             "wim_account": wim_account_2_RO(ns_params.get("wimAccountId")),
             # "scenario": ns_params["nsdId"],
         }
-        if n2vc_key_list:
-            for vnfd_ref, vnfd in vnfd_dict.items():
-                vdu_needed_access = []
-                mgmt_cp = None
-                if vnfd.get("vnf-configuration"):
-                    if vnfd.get("mgmt-interface"):
-                        if vnfd["mgmt-interface"].get("vdu-id"):
-                            vdu_needed_access.append(vnfd["mgmt-interface"]["vdu-id"])
-                        elif vnfd["mgmt-interface"].get("cp"):
-                            mgmt_cp = vnfd["mgmt-interface"]["cp"]
-
-                for vdu in vnfd.get("vdu", ()):
-                    if vdu.get("vdu-configuration"):
+        n2vc_key_list = n2vc_key_list or []
+        for vnfd_ref, vnfd in vnfd_dict.items():
+            vdu_needed_access = []
+            mgmt_cp = None
+            if vnfd.get("vnf-configuration"):
+                ssh_required = deep_get(vnfd, ("vnf-configuration", "config-access", "ssh-access", "required"))
+                if ssh_required and vnfd.get("mgmt-interface"):
+                    if vnfd["mgmt-interface"].get("vdu-id"):
+                        vdu_needed_access.append(vnfd["mgmt-interface"]["vdu-id"])
+                    elif vnfd["mgmt-interface"].get("cp"):
+                        mgmt_cp = vnfd["mgmt-interface"]["cp"]
+
+            for vdu in vnfd.get("vdu", ()):
+                if vdu.get("vdu-configuration"):
+                    ssh_required = deep_get(vdu, ("vdu-configuration", "config-access", "ssh-access", "required"))
+                    if ssh_required:
                         vdu_needed_access.append(vdu["id"])
-                    elif mgmt_cp:
-                        for vdu_interface in vdu.get("interface"):
-                            if vdu_interface.get("external-connection-point-ref") and \
-                                    vdu_interface["external-connection-point-ref"] == mgmt_cp:
-                                vdu_needed_access.append(vdu["id"])
-                                mgmt_cp = None
-                                break
+                elif mgmt_cp:
+                    for vdu_interface in vdu.get("interface"):
+                        if vdu_interface.get("external-connection-point-ref") and \
+                                vdu_interface["external-connection-point-ref"] == mgmt_cp:
+                            vdu_needed_access.append(vdu["id"])
+                            mgmt_cp = None
+                            break
 
-                if vdu_needed_access:
-                    for vnf_member in nsd.get("constituent-vnfd"):
-                        if vnf_member["vnfd-id-ref"] != vnfd_ref:
-                            continue
-                        for vdu in vdu_needed_access:
-                            populate_dict(RO_ns_params,
-                                          ("vnfs", vnf_member["member-vnf-index"], "vdus", vdu, "mgmt_keys"),
-                                          n2vc_key_list)
+            if vdu_needed_access:
+                for vnf_member in nsd.get("constituent-vnfd"):
+                    if vnf_member["vnfd-id-ref"] != vnfd_ref:
+                        continue
+                    for vdu in vdu_needed_access:
+                        populate_dict(RO_ns_params,
+                                      ("vnfs", vnf_member["member-vnf-index"], "vdus", vdu, "mgmt_keys"),
+                                      n2vc_key_list)
 
         if ns_params.get("vduImage"):
             RO_ns_params["vduImage"] = ns_params["vduImage"]
@@ -456,7 +475,7 @@ class NsLcm(LcmBase):
                     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)            
+                    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
@@ -653,6 +672,7 @@ class NsLcm(LcmBase):
         exc = None
         try:
             # wait for any previous tasks in process
+            step = "Waiting for previous tasks"
             await self.lcm_tasks.waitfor_related_HA('ns', 'nslcmops', nslcmop_id)
 
             step = "Getting nslcmop={} from db".format(nslcmop_id)
@@ -697,8 +717,7 @@ class NsLcm(LcmBase):
 
             db_nsr_update["detailed-status"] = "creating"
             db_nsr_update["operational-status"] = "init"
-            if not db_nsr["_admin"].get("deployed") or not db_nsr["_admin"]["deployed"].get("RO") or \
-                    not db_nsr["_admin"]["deployed"]["RO"].get("vnfd"):
+            if not isinstance(deep_get(db_nsr, ("_admin", "deployed", "RO", "vnfd")), list):
                 populate_dict(db_nsr, ("_admin", "deployed", "RO", "vnfd"), [])
                 db_nsr_update["_admin.deployed.RO.vnfd"] = []
 
@@ -721,7 +740,7 @@ class NsLcm(LcmBase):
 
                 machine_spec = {}
                 if native_charm:
-                    machine_spec["username"] = charm_params.get("username"),
+                    machine_spec["username"] = charm_params.get("username")
                     machine_spec["hostname"] = charm_params.get("rw_mgmt_ip")
 
                 # Note: The charm needs to exist on disk at the location
@@ -848,7 +867,6 @@ class NsLcm(LcmBase):
                         if vdu_config["juju"].get("proxy") is False:
                             # native_charm, will be deployed after VM. Skip
                             proxy_charm = None
-
                         if proxy_charm:
                             if not vca_model_name:
                                 step = "creating VCA model name"
@@ -882,7 +900,6 @@ class NsLcm(LcmBase):
                 if ns_config["juju"].get("proxy") is False:
                     # native_charm, will be deployed after VM. Skip
                     proxy_charm = None
-
                 if proxy_charm:
                     step = "deploying proxy charm to configure ns"
                     # TODO is NS magmt IP address needed?
@@ -1107,7 +1124,7 @@ class NsLcm(LcmBase):
 
             # Crate ns at RO
             # if present use it unless in error status
-            RO_nsr_id = db_nsr["_admin"].get("deployed", {}).get("RO", {}).get("nsr_id")
+            RO_nsr_id = deep_get(db_nsr, ("_admin", "deployed", "RO", "nsr_id"))
             if RO_nsr_id:
                 try:
                     step = db_nsr_update["detailed-status"] = "Looking for existing ns at RO"
@@ -1281,8 +1298,8 @@ class NsLcm(LcmBase):
                 vnf_config = vnfd.get("vnf-configuration")
                 if vnf_config and vnf_config.get("juju"):
                     native_charm = vnf_config["juju"].get("proxy") is False
-
-                    if native_charm:
+                    proxy_charm = vnf_config["juju"]["charm"]
+                    if native_charm and proxy_charm:
                         if not vca_model_name:
                             step = "creating VCA model name '{}'".format(nsr_id)
                             self.logger.debug(logging_text + step)
@@ -1291,6 +1308,8 @@ class NsLcm(LcmBase):
                             db_nsr_update["_admin.deployed.VCA-model-name"] = nsr_id
                             self.update_db_2("nsrs", nsr_id, db_nsr_update)
                         step = "deploying native charm for vnf_member_index={}".format(vnf_index)
+                        self.logger.debug(logging_text + step)
+
                         vnfr_params["rw_mgmt_ip"] = db_vnfrs[vnf_index]["ip-address"]
                         charm_params = {
                             "user_values": vnfr_params,
@@ -1323,8 +1342,8 @@ class NsLcm(LcmBase):
 
                     if vdu_config and vdu_config.get("juju"):
                         native_charm = vdu_config["juju"].get("proxy") is False
-
-                        if native_charm:
+                        proxy_charm = vdu_config["juju"]["charm"]
+                        if native_charm and proxy_charm:
                             if not vca_model_name:
                                 step = "creating VCA model name"
                                 await self.n2vc.CreateNetworkService(nsr_id)
@@ -1333,8 +1352,11 @@ class NsLcm(LcmBase):
                                 self.update_db_2("nsrs", nsr_id, db_nsr_update)
                             step = "deploying native charm for vnf_member_index={} vdu_id={}".format(vnf_index,
                                                                                                      vdu["id"])
-                            await self.n2vc.login()
+
+                            self.logger.debug(logging_text + step)
+
                             vdur = db_vnfrs[vnf_index]["vdur"][vdu_index]
+
                             # TODO for the moment only first vdu_id contains a charm deployed
                             if vdur["vdu-id-ref"] != vdu["id"]:
                                 raise LcmException("Mismatch vdur {}, vdu {} at index {} for vnf {}"
@@ -1358,6 +1380,8 @@ class NsLcm(LcmBase):
                                     charm_params["username"] = vdu_config["config-access"]["ssh-access"].get(
                                         "default-user")
 
+                            await self.n2vc.login()
+
                             deploy_charm(vnf_index, vdu["id"], vdur.get("name"), vdur["count-index"],
                                          charm_params, n2vc_info, native_charm)
                             number_to_configure += 1
@@ -1367,8 +1391,8 @@ class NsLcm(LcmBase):
             ns_config = nsd.get("ns-configuration")
             if ns_config and ns_config.get("juju"):
                 native_charm = ns_config["juju"].get("proxy") is False
-
-                if native_charm:
+                proxy_charm = ns_config["juju"]["charm"]
+                if native_charm and proxy_charm:
                     step = "deploying native charm to configure ns"
                     # TODO is NS magmt IP address needed?