Modify the code of scaling
[vfc/nfvo/lcm.git] / lcm / ns / vnfs / notify_lcm.py
index 504be55..362e347 100644 (file)
@@ -23,6 +23,8 @@ from lcm.pub.exceptions import NSLCMException
 from lcm.pub.database.models import VNFCInstModel, VLInstModel, NfInstModel, PortInstModel, CPInstModel, VmInstModel
 from lcm.pub.msapi.aai import create_network_aai, query_network_aai, delete_network_aai
 from lcm.pub.utils.values import ignore_case_get
+from lcm.pub.msapi.extsys import split_vim_to_owner_region, get_vim_by_id
+from lcm.pub.msapi.aai import create_vserver_aai
 
 
 logger = logging.getLogger(__name__)
@@ -60,11 +62,12 @@ class NotifyLcm(object):
             self.exception('unexpected exception')
 
     def get_vnfinstid(self, mnfinstid, vnfm_inst_id):
+        logger.debug("vnfinstid in vnfm is:%s,vnfmid is:%s", mnfinstid, vnfm_inst_id)
+        logger.debug("mnfinstid=%s, vnfm_inst_id=%s", mnfinstid, vnfm_inst_id)
         nfinst = NfInstModel.objects.filter(mnfinstid=mnfinstid, vnfm_inst_id=vnfm_inst_id).first()
         if nfinst:
             return nfinst.nfinstid
-        else:
-            self.exception('vnfinstid not exist')
+        raise NSLCMException("vnfinstid not exist")
 
     def exception(self, error_msg):
         logger.error('Notify Lcm failed, detail message: %s' % error_msg)
@@ -75,15 +78,17 @@ class NotifyLcm(object):
             vnfcInstanceId = ignore_case_get(vnfc, 'vnfcInstanceId')
             vduId = ignore_case_get(vnfc, 'vduId')
             changeType = ignore_case_get(vnfc, 'changeType')
-            vimId = ignore_case_get(vnfc, 'vimid')
-            vmId = ignore_case_get(vnfc, 'vmid')
-            vmName = ignore_case_get(vnfc, 'vmname')
+            vimId = ignore_case_get(vnfc, 'vimId')
+            vmId = ignore_case_get(vnfc, 'vmId')
+            vmName = ignore_case_get(vnfc, 'vmName')
 
             if changeType == 'added':
                 VNFCInstModel(vnfcinstanceid=vnfcInstanceId, vduid=vduId,
                               nfinstid=self.vnf_instid, vmid=vmId).save()
                 VmInstModel(vmid=vmId, vimid=vimId, resouceid=vmId, insttype=INST_TYPE.VNF,
                             instid=self.vnf_instid, vmname=vmName, hostid='1').save()
+                if REPORT_TO_AAI:
+                    self.create_vserver_in_aai(vimId, vmId, vmName)
             elif changeType == 'removed':
                 VNFCInstModel.objects.filter(vnfcinstanceid=vnfcInstanceId).delete()
             elif changeType == 'modified':
@@ -92,48 +97,50 @@ class NotifyLcm(object):
                                                                                    vmid=vmId)
             else:
                 self.exception('affectedVnfc struct error: changeType not in {added,removed,modified}')
+        logger.debug("Success to create all vserver to aai.")
 
     def update_Vl(self):
         for vl in self.affectedVl:
             vlInstanceId = ignore_case_get(vl, 'vlInstanceId')
-            vldid = ignore_case_get(vl, 'vldid')
+            vldid = ignore_case_get(vl, 'vldId')
             changeType = ignore_case_get(vl, 'changeType')
             networkResource = ignore_case_get(vl, 'networkResource')
             resourceType = ignore_case_get(networkResource, 'resourceType')
             resourceId = ignore_case_get(networkResource, 'resourceId')
+            resourceName = ignore_case_get(networkResource, 'resourceName')
 
             if resourceType != 'network':
                 self.exception('affectedVl struct error: resourceType not euqal network')
 
-            ownerId = self.vnf_instid
-            ownerId = self.get_vnfinstid(self.vnf_instid, self.vnfmid)
+            ownerId = self.get_vnfinstid(self.m_vnfInstanceId, self.vnfmid)
 
             if changeType == 'added':
-                VLInstModel(vlInstanceId=vlInstanceId, vldId=vldid, ownerType=0, ownerId=ownerId,
-                            relatedNetworkId=resourceId, vlType=0).save()
+                VLInstModel(vlinstanceid=vlInstanceId, vldid=vldid, vlinstancename=resourceName, ownertype=0,
+                            ownerid=ownerId, relatednetworkid=resourceId, vltype=0).save()
             elif changeType == 'removed':
-                VLInstModel.objects.filter(vlInstanceId=vlInstanceId).delete()
+                VLInstModel.objects.filter(vlinstanceid=vlInstanceId).delete()
             elif changeType == 'modified':
-                VLInstModel.objects.filter(vlInstanceId=vlInstanceId)\
-                    .update(vldId=vldid, ownerType=0, ownerId=ownerId, relatedNetworkId=resourceId, vlType=0)
+                VLInstModel.objects.filter(vlinstanceid=vlInstanceId)\
+                    .update(vldid=vldid, vlinstancename=resourceName, ownertype=0, ownerid=ownerId,
+                            relatednetworkid=resourceId, vltype=0)
             else:
                 self.exception('affectedVl struct error: changeType not in {added,removed,modified}')
 
     def update_Cp(self):
         for cp in self.affectedCp:
             virtualLinkInstanceId = ignore_case_get(cp, 'virtualLinkInstanceId')
-            ownertype = ignore_case_get(cp, 'ownertype')
+            ownertype = ignore_case_get(cp, 'ownerType')
             if not ownertype:
                 ownertype = 0
-            ownerid = self.vnf_instid if str(ownertype) == "0" else ignore_case_get(cp, 'ownerid')
-            cpInstanceId = ignore_case_get(cp, 'cpinstanceid')
-            cpdId = ignore_case_get(cp, 'cpdid')
-            changeType = ignore_case_get(cp, 'changetype')
+            ownerid = self.vnf_instid if str(ownertype) == "0" else ignore_case_get(cp, 'ownerId')
+            cpInstanceId = ignore_case_get(cp, 'cpInstanceId')
+            cpdId = ignore_case_get(cp, 'cpdId')
+            changeType = ignore_case_get(cp, 'changeType')
             relatedportId = ''
             portResource = ignore_case_get(cp, 'portResource')
             if portResource:
-                vimId = ignore_case_get(portResource, 'vimid')
-                resourceId = ignore_case_get(portResource, 'resourceid')
+                vimId = ignore_case_get(portResource, 'vimId')
+                resourceId = ignore_case_get(portResource, 'resourceId')
                 resourceName = ignore_case_get(portResource, 'resourceName')
                 tenant = ignore_case_get(portResource, 'tenant')
                 ipAddress = ignore_case_get(portResource, 'ipAddress')
@@ -181,8 +188,7 @@ class NotifyLcm(object):
                     logger.error('affectedVl struct error: resourceType not euqal network')
                     raise NSLCMException("affectedVl struct error: resourceType not euqal network")
 
-                # ownerId = self.vnf_instid
-                ownerId = self.get_vnfinstid(self.vnf_instid, self.vnfmid)
+                ownerId = self.get_vnfinstid(self.m_vnfInstanceId, self.vnfmid)
 
                 if changeType in ['added', 'modified']:
                     self.create_network_and_subnet_in_aai(vlInstanceId, ownerId)
@@ -241,3 +247,43 @@ class NotifyLcm(object):
             logger.debug("Fail to delete network[%s] to aai, detail message: %s" % (vlInstanceId, e.message))
         except:
             logger.error(traceback.format_exc())
+
+    def create_vserver_in_aai(self, vim_id, vserver_id, vserver_name):
+        logger.debug("NotifyLcm::create_vserver_in_aai::report vserver instance to aai.")
+        try:
+            cloud_owner, cloud_region_id = split_vim_to_owner_region(vim_id)
+
+            # query vim_info from aai
+            vim_info = get_vim_by_id(vim_id)
+            tenant_id = vim_info["tenantId"]
+            data = {
+                "vserver-id": vserver_id,
+                "vserver-name": vserver_name,
+                "prov-status": "ACTIVE",
+                "vserver-selflink": "",
+                "in-maint": True,
+                "is-closed-loop-disabled": False,
+                "relationship-list": {
+                    "relationship": [
+                        {
+                            "related-to": "generic-vnf",
+                            "relationship-data": [
+                                {
+                                    "relationship-key": "generic-vnf.vnf-id",
+                                    "relationship-value": self.vnf_instid
+                                }
+                            ]
+                        }
+                    ]
+                }
+            }
+
+            # create vserver instance in aai
+            resp_data, resp_status = create_vserver_aai(cloud_owner, cloud_region_id, tenant_id, vserver_id, data)
+            logger.debug("Success to create vserver[%s] to aai, vnf instance=[%s], resp_status: [%s]."
+                         % (vserver_id, self.vnf_instid, resp_status))
+        except NSLCMException as e:
+            logger.debug("Fail to create vserver to aai, vnf instance=[%s], detail message: %s"
+                         % (self.vnf_instid, e.message))
+        except:
+            logger.error(traceback.format_exc())