Notification stuffs.
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / biz / instantiate_vnf.py
index 161cd7d..f86baab 100644 (file)
@@ -21,13 +21,16 @@ from threading import Thread
 from lcm.pub.database.models import NfInstModel, VmInstModel, NetworkInstModel, \
     SubNetworkInstModel, PortInstModel, StorageInstModel, FlavourInstModel, VNFCInstModel
 from lcm.pub.exceptions import NFLCMException
-from lcm.pub.msapi.gvnfmdriver import notify_lcm_to_nfvo, prepare_notification_data
+from lcm.pub.msapi.gvnfmdriver import prepare_notification_data
+# from lcm.pub.msapi.gvnfmdriver import notify_lcm_to_nfvo
 from lcm.pub.msapi.sdc_run_catalog import query_vnfpackage_by_id
 from lcm.pub.utils.jobutil import JobUtil
 from lcm.pub.utils.timeutil import now_time
+from lcm.pub.utils.notificationsutil import NotificationsUtil
 from lcm.pub.utils.values import ignore_case_get, get_none, get_boolean, get_integer
 from lcm.pub.vimapi import adaptor
 from lcm.nf.biz.grant_vnf import grant_resource
+from lcm.nf.const import GRANT_TYPE
 
 logger = logging.getLogger(__name__)
 
@@ -39,7 +42,7 @@ class InstantiateVnf(Thread):
         self.nf_inst_id = nf_inst_id
         self.job_id = job_id
         self.vim_id = ignore_case_get(ignore_case_get(self.data, "additionalParams"), "vimId")
-        self.grant_type = "Instantiate"
+        self.grant_type = GRANT_TYPE.INSTANTIATE
 
     def run(self):
         try:
@@ -73,8 +76,9 @@ class InstantiateVnf(Thread):
                 inputs = json.loads(inputs)
             for key, val in inputs.items():
                 input_parameters.append({"key": key, "value": val})
-        vnf_package_info = query_vnfpackage_by_id(self.vnfd_id)
-        self.vnfd_info = json.loads(ignore_case_get(ignore_case_get(vnf_package_info, "packageInfo"), "vnfdModel"))
+        vnf_package = query_vnfpackage_by_id(self.vnfd_id)
+        pkg_info = ignore_case_get(vnf_package, "packageInfo")
+        self.vnfd_info = json.loads(ignore_case_get(pkg_info, "vnfdModel"))
 
         self.update_cps()
         metadata = ignore_case_get(self.vnfd_info, "metadata")
@@ -129,8 +133,9 @@ class InstantiateVnf(Thread):
     def lcm_notify(self):
         notification_content = prepare_notification_data(self.nf_inst_id, self.job_id, "ADDED")
         logger.info('Notify request data = %s' % notification_content)
-        resp = notify_lcm_to_nfvo(json.dumps(notification_content))
-        logger.info('Lcm notify end, response %s' % resp)
+        # resp = notify_lcm_to_nfvo(json.dumps(notification_content))
+        # logger.info('Lcm notify end, response %s' % resp)
+        NotificationsUtil().send_notification(notification_content)
 
     def vnf_inst_failed_handle(self, error_msg):
         logger.error('VNF instantiation failed, detail message: %s' % error_msg)
@@ -153,15 +158,38 @@ class InstantiateVnf(Thread):
                         break
 
     def set_location(self, apply_result):
+        vim_connections = ignore_case_get(apply_result, "vimConnections")
+        vnfid = ignore_case_get(apply_result, "vnfInstanceId")
+        directive = ignore_case_get(apply_result, "directive")
+        vim_assets = ignore_case_get(apply_result, "vimAssets")
+        access_info = ignore_case_get(vim_connections[0], "accessInfo")
+        tenant = ignore_case_get(access_info, "tenant")
+        vimid = ignore_case_get(vim_connections[0], "vimId")
+        cloud_owner, cloud_regionid = vimid.split("_")
+        vdu_info = []
+
+        for flavor in ignore_case_get(vim_assets, "vimComputeResourceFlavour"):
+            vdu_info.append({"vduName": flavor["resourceProviderId"],
+                             "flavorName": flavor["vimFlavourId"],
+                             "directive": directive})
+
         for resource_type in ['vdus', 'vls']:
             for resource in ignore_case_get(self.vnfd_info, resource_type):
                 if "location_info" in resource["properties"]:
-                    resource["properties"]["location_info"]["vimid"] = ignore_case_get(apply_result, "vimid")
-                    resource["properties"]["location_info"]["tenant"] = ignore_case_get(apply_result, "tenant")
+                    resource["properties"]["location_info"]["vimid"] = vimid
+                    resource["properties"]["location_info"]["tenant"] = tenant
+                    resource["properties"]["location_info"]["vnfId"] = vnfid
+                    resource["properties"]["location_info"]["cloudOwner"] = cloud_owner
+                    resource["properties"]["location_info"]["cloudRegionId"] = cloud_regionid
+                    resource["properties"]["location_info"]["vduInfo"] = vdu_info
                 else:
                     resource["properties"]["location_info"] = {
-                        "vimid": ignore_case_get(apply_result, "vimid"),
-                        "tenant": ignore_case_get(apply_result, "tenant")}
+                        "vimid": vimid,
+                        "tenant": tenant,
+                        "vnfId": vnfid,
+                        "cloudOwner": cloud_owner,
+                        "cloudRegionId": cloud_regionid,
+                        "vduInfo": vdu_info}
 
     '''
     def get_subnet_ids(self, ext_cp):
@@ -180,7 +208,7 @@ def volume_save(job_id, nf_inst_id, ret):
     StorageInstModel.objects.create(
         storageid=str(uuid.uuid4()),
         vimid=ignore_case_get(ret, "vimId"),
-        resouceid=ignore_case_get(ret, "id"),
+        resourceid=ignore_case_get(ret, "id"),
         name=ignore_case_get(ret, "name"),
         tenant=ignore_case_get(ret, "tenantId"),
         create_time=ignore_case_get(ret, "createTime"),
@@ -198,7 +226,7 @@ def network_save(job_id, nf_inst_id, ret):
         networkid=str(uuid.uuid4()),
         name=ignore_case_get(ret, "name"),
         vimid=ignore_case_get(ret, "vimId"),
-        resouceid=ignore_case_get(ret, "id"),
+        resourceid=ignore_case_get(ret, "id"),
         tenant=ignore_case_get(ret, "tenantId"),
         segmentid=str(ignore_case_get(ret, "segmentationId")),
         network_type=ignore_case_get(ret, "networkType"),
@@ -218,7 +246,7 @@ def subnet_save(job_id, nf_inst_id, ret):
         subnetworkid=str(uuid.uuid4()),
         name=ignore_case_get(ret, "name"),
         vimid=ignore_case_get(ret, "vimId"),
-        resouceid=ignore_case_get(ret, "id"),
+        resourceid=ignore_case_get(ret, "id"),
         tenant=ignore_case_get(ret, "tenantId"),
         networkid=ignore_case_get(ret, "networkId"),
         cidr=ignore_case_get(ret, "cidr"),
@@ -241,7 +269,7 @@ def port_save(job_id, nf_inst_id, ret):
         subnetworkid=ignore_case_get(ret, "subnetId"),
         name=ignore_case_get(ret, "name"),
         vimid=ignore_case_get(ret, "vimId"),
-        resouceid=ignore_case_get(ret, "id"),
+        resourceid=ignore_case_get(ret, "id"),
         tenant=ignore_case_get(ret, "tenantId"),
         macaddress=ignore_case_get(ret, "macAddress"),
         ipaddress=ignore_case_get(ret, "ip"),
@@ -259,7 +287,7 @@ def flavor_save(job_id, nf_inst_id, ret):
         flavourid=str(uuid.uuid4()),
         name=ignore_case_get(ret, "name"),
         vimid=ignore_case_get(ret, "vimId"),
-        resouceid=ignore_case_get(ret, "id"),
+        resourceid=ignore_case_get(ret, "id"),
         tenant=ignore_case_get(ret, "tenantId"),
         vcpu=get_integer(ignore_case_get(ret, "vcpu")),
         memory=get_integer(ignore_case_get(ret, "memory")),
@@ -279,7 +307,7 @@ def vm_save(job_id, nf_inst_id, ret):
         vmid=vm_id,
         vmname=ignore_case_get(ret, "name"),
         vimid=ignore_case_get(ret, "vimId"),
-        resouceid=ignore_case_get(ret, "id"),
+        resourceid=ignore_case_get(ret, "id"),
         tenant=ignore_case_get(ret, "tenantId"),
         nic_array=ignore_case_get(ret, "nicArray"),
         metadata=ignore_case_get(ret, "metadata"),