Modify vfc-vnflcm instantiation code
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / vnfs / vnf_create / inst_vnf.py
index d242bd8..a1f6639 100644 (file)
 # 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 json
 import logging
 import traceback
 import uuid
 from threading import Thread
 
-from lcm.nf.vnfs.const import vnfd_model_dict
 from lcm.pub.database.models import NfInstModel, VmInstModel, NetworkInstModel, \
     SubNetworkInstModel, PortInstModel, StorageInstModel, FlavourInstModel, VNFCInstModel, NfvoRegInfoModel
 from lcm.pub.exceptions import NFLCMException
-from lcm.pub.msapi.catalog import query_rawdata_from_catalog
-from lcm.pub.msapi.gvnfmdriver import apply_grant_to_nfvo, notify_lcm_to_nfvo, get_packageinfo_by_vnfdid
-from lcm.pub.utils import toscautil
+from lcm.pub.msapi.gvnfmdriver import apply_grant_to_nfvo, 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.values import ignore_case_get, get_none, get_boolean, get_integer
@@ -43,7 +42,6 @@ class InstVnf(Thread):
         self.nfvo_inst_id = ''
         self.vnfm_inst_id = ''
         self.package_id = ''
-        # self.csar_id = ''
         self.vnfd_info = []
 
     def run(self):
@@ -69,13 +67,7 @@ class InstVnf(Thread):
 
         JobUtil.add_job_status(self.job_id, 5, 'Get packageinfo by vnfd_id')
         self.vnfd_id = vnf_insts[0].vnfdid
-        package_info = get_packageinfo_by_vnfdid(self.vnfd_id)
-        for val in ignore_case_get(package_info, "csars"):
-            if self.vnfd_id == ignore_case_get(val, "vnfdId"):
-                self.package_id = ignore_case_get(val, "csarId")
-                break
-
-        JobUtil.add_job_status(self.job_id, 10, 'Get rawdata from catalog by csar_id')
+        JobUtil.add_job_status(self.job_id, 10, 'Get vnf package info from catalog by csar_id')
         input_parameters = []
         inputs = ignore_case_get(self.data['additionalParams'], "inputs")
         if inputs:
@@ -83,45 +75,59 @@ class InstVnf(Thread):
                 inputs = json.loads(inputs)
             for key, val in inputs.items():
                 input_parameters.append({"key": key, "value": val})
-        raw_data = query_rawdata_from_catalog(self.package_id, input_parameters)
-        self.vnfd_info = toscautil.convert_vnfd_model(raw_data["rawData"])  # convert to inner json
-        self.vnfd_info = json.JSONDecoder().decode(self.vnfd_info)
+        self.vnfd_info = query_vnfpackage_by_id(self.vnfd_id)
+        # self.vnfd_info = vnfd_model_dict  # just for test
 
-        #self.vnfd_info = vnfd_model_dict  # just for test
         self.update_cps()
-
         self.check_parameter_exist()
         metadata = ignore_case_get(self.vnfd_info, "metadata")
-        version = ignore_case_get(metadata, "vnfd_version")
+        version = ignore_case_get(metadata, "vnfdVersion")
         vendor = ignore_case_get(metadata, "vendor")
-        netype = ignore_case_get(metadata, "vnf_type")
+        netype = ignore_case_get(metadata, "type")
         vnfsoftwareversion = ignore_case_get(metadata, "version")
-        vnfd_model = self.vnfd_info
         NfInstModel.objects.filter(nfinstid=self.nf_inst_id).\
-            update(package_id=self.package_id, flavour_id=ignore_case_get(self.data, "flavourId"), version=version,
-                   vendor=vendor, netype=netype, vnfd_model=vnfd_model, status='NOT_INSTANTIATED', vnfdid=self.vnfd_id,
-                   localizationLanguage=ignore_case_get(self.data, 'localizationLanguage'), input_params=self.data,
-                   vnfSoftwareVersion=vnfsoftwareversion, lastuptime=now_time())
+            update(package_id=self.package_id,
+                   flavour_id=ignore_case_get(self.data, "flavourId"),
+                   version=version,
+                   vendor=vendor,
+                   netype=netype,
+                   vnfd_model=self.vnfd_info,
+                   status='NOT_INSTANTIATED',
+                   vnfdid=self.vnfd_id,
+                   localizationLanguage=ignore_case_get(self.data, 'localizationLanguage'),
+                   input_params=self.data,
+                   vnfSoftwareVersion=vnfsoftwareversion,
+                   lastuptime=now_time())
 
         logger.info("self.vim_id = %s" % self.vim_id)
-        NfvoRegInfoModel.objects.create(nfvoid=self.nf_inst_id,
-            vnfminstid=ignore_case_get(self.data, "vnfmId"), apiurl=self.vim_id)
+        NfvoRegInfoModel.objects.create(
+            nfvoid=self.nf_inst_id,
+            vnfminstid=ignore_case_get(self.data, "vnfmId"),
+            apiurl=self.vim_id)
         JobUtil.add_job_status(self.job_id, 15, 'Nf instancing pre-check finish')
         logger.info("Nf instancing pre-check finish")
 
     def apply_grant(self):
         logger.info('[NF instantiation] send resource grand request to nfvo start')
-        content_args = {'vnfInstanceId': self.nf_inst_id, 'vnfDescriptorId': '',
-                        'lifecycleOperation': 'Instantiate', 'jobId': self.job_id,
-                        'addResource': [], 'removeResource': [],
-                        'placementConstraint': [], 'additionalParam': {}}
+        content_args = {
+            'vnfInstanceId': self.nf_inst_id,
+            'vnfDescriptorId': '',
+            'lifecycleOperation': 'Instantiate',
+            'jobId': self.job_id,
+            'addResource': [],
+            'removeResource': [],
+            'placementConstraint': [],
+            'additionalParam': {}
+        }
 
         vdus = ignore_case_get(self.vnfd_info, "vdus")
         res_index = 1
         for vdu in vdus:
-            res_def = {'type': 'VDU',
-                       'resDefId': str(res_index),
-                       'resDesId': ignore_case_get(vdu, "vdu_id")}
+            res_def = {
+                'type': 'VDU',
+                'resDefId': str(res_index),
+                'resDesId': ignore_case_get(vdu, "vdu_id")
+            }
             content_args['addResource'].append(res_def)
             res_index += 1
 
@@ -133,8 +139,8 @@ class InstVnf(Thread):
         content_args['additionalParam']['vimid'] = vnfmInfo[0].apiurl
         logger.info('content_args=%s' % content_args)
         apply_result = apply_grant_to_nfvo(json.dumps(content_args))
-        #vim_info = ignore_case_get(apply_result, "vim")
-        #vim_info = ignore_case_get(json.JSONDecoder().decode(apply_result), "vim")
+        # vim_info = ignore_case_get(apply_result, "vim")
+        # vim_info = ignore_case_get(json.JSONDecoder().decode(apply_result), "vim")
 
         for vdu in ignore_case_get(self.vnfd_info, "vdus"):
             if "location_info" in vdu["properties"]:
@@ -201,8 +207,8 @@ class InstVnf(Thread):
                 {'vsInstanceId': port.portid,
                  'cpdid': port.nodeId,
                  'changeType': 'added',
-                 'storageResource': {'vimId': port.vimid, 'resourceId': port.resouceid,
-                                     'resourceName': port.name, 'resourceType': 'port'}})
+                 'portResource': {'vimId': port.vimid, 'resourceId': port.resouceid,
+                                  'resourceName': port.name, 'resourceType': 'port'}})
         affected_vs = []
         vss = StorageInstModel.objects.filter(instid=self.nf_inst_id)
         for vs in vss:
@@ -221,7 +227,8 @@ class InstVnf(Thread):
             'affectedVirtualLink': affected_vl,
             'affectedVirtualStorage': affected_vs,
             'affectedCp': affected_cp
-            }
+        }
+
         vnfmInfo = NfvoRegInfoModel.objects.filter(nfvoid=self.nf_inst_id)
         if len(vnfmInfo) == 0:
             raise NFLCMException('nf_inst_id(%s) does not exist in NfvoRegInfoModel' % self.nf_inst_id)