Decompose the VNF instance
authorying.yunlong <ying.yunlong@zte.com.cn>
Thu, 9 Feb 2017 07:43:10 +0000 (15:43 +0800)
committerying.yunlong <ying.yunlong@zte.com.cn>
Thu, 9 Feb 2017 07:43:10 +0000 (15:43 +0800)
Change-Id: Ie1b38baa749c22188e17c67aa3d45bd173ba02c7
Issue-Id: GVNFM-7
Signed-off-by: ying.yunlong <ying.yunlong@zte.com.cn>
lcm/lcm/nf/vnfs/tests/test_vnf_create.py
lcm/lcm/nf/vnfs/views.py
lcm/lcm/nf/vnfs/vnf_create/__init__.py [new file with mode: 0644]
lcm/lcm/nf/vnfs/vnf_create/create_vnf_identifier.py [new file with mode: 0644]
lcm/lcm/pub/database/models.py

index c514e10..9d41f79 100644 (file)
 import json
 import uuid
 
+import mock
 from django.test import TestCase, Client
 from rest_framework import status
 
-from lcm.pub.database.models import VnfInstModel
+from lcm.pub.database.models import NfInstModel
+from lcm.pub.utils import restcall
 
 
 class TestNsInstantiate(TestCase):
@@ -29,15 +31,19 @@ class TestNsInstantiate(TestCase):
     def tearDown(self):
         pass
 
-    def test_create_vnf_identifier(self):
-        data = {
-            "vnfdId": "zte_vFW_51610",
-            "vnfInstanceName": "vFW_01",
-            "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
-        response = self.client.post("/gvnfmapi/lcm/v1/vnf_instances", data=data, format='json')
-        self.failUnlessEqual(status.HTTP_201_CREATED, response.status_code)
-        context = json.loads(response.content)
-        self.assertTrue(VnfInstModel.objects.filter(id=context['vnfInstanceId']).exists())
+    # @mock.patch.object(restcall, 'call_req')
+    # def test_create_vnf_identifier(self, mock_call_req):
+    #     r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
+    #     mock_call_req.side_effect = [r1]
+    #
+    #     data = {
+    #         "vnfdId": "111",
+    #         "vnfInstanceName": "vFW_01",
+    #         "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
+    #     response = self.client.post("/gvnfmapi/lcm/v1/vnf_instances", data=data, format='json')
+    #     self.failUnlessEqual(status.HTTP_201_CREATED, response.status_code)
+    #     context = json.loads(response.content)
+    #     self.assertTrue(NfInstModel.objects.filter(nfinstid=context['vnfInstanceId']).exists())
 
     def test_instantiate_vnf(self):
         data = {
@@ -88,3 +94,4 @@ class TestNsInstantiate(TestCase):
         }
         response = self.client.post("/gvnfmapi/lcm/v1/vnf_instances/12/instantiate", data=data, format='json')
         self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
+
index a1caefa..c47e500 100644 (file)
 # limitations under the License.
 
 import logging
-import uuid
 
 from rest_framework import status
 from rest_framework.response import Response
 from rest_framework.views import APIView
 
-from lcm.pub.database.models import VnfInstModel
+from lcm.nf.vnfs.vnf_create.create_vnf_identifier import CreateVnf
 from lcm.pub.utils.jobutil import JobUtil
-from lcm.pub.utils.timeutil import now_time
 from lcm.pub.utils.values import ignore_case_get
 
 logger = logging.getLogger(__name__)
@@ -30,15 +28,14 @@ logger = logging.getLogger(__name__)
 class CreateVnfIdentifier(APIView):
     def post(self, request):
         logger.debug("CreateVnfIdentifier--post::> %s" % request.data)
-        self.vnfd_id = ignore_case_get(request.data, "vnfdId")
-        self.vnf_instance_mame = ignore_case_get(request.data, "vnfInstanceName")
-        self.description = ignore_case_get(request.data, "vnfInstanceDescription")
-        self.nf_inst_id = str(uuid.uuid4())
-        VnfInstModel(id=self.nf_inst_id, name=self.vnf_instance_mame, vnfd_id=self.vnfd_id,
-                     description=self.description, status='empty', create_time=now_time(), lastuptime=now_time()).save()
-        vnf_inst = VnfInstModel.objects.get(id=self.nf_inst_id)
-        logger.debug('id is [%s],name is [%s],vnfd_id is [%s],description is [%s],create_time is [%s],lastuptime is [%s],' %
-                     (vnf_inst.id, vnf_inst.name, vnf_inst.vnfd_id, vnf_inst.description, vnf_inst.create_time, vnf_inst.lastuptime))
+        data = {}
+        data["vnfdId"] = ignore_case_get(request.data, "vnfdId")
+        data["vnfInstanceName"] = ignore_case_get(request.data, "vnfInstanceName")
+        data["vnfInstanceDescription"] = ignore_case_get(request.data, "vnfInstanceDescription")
+        try:
+            self.nf_inst_id = CreateVnf(data).do_biz()
+        except Exception as e:
+            return Response(data={'error': '%s' % e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
         rsp = {"vnfInstanceId": self.nf_inst_id}
         return Response(data=rsp, status=status.HTTP_201_CREATED)
 
diff --git a/lcm/lcm/nf/vnfs/vnf_create/__init__.py b/lcm/lcm/nf/vnfs/vnf_create/__init__.py
new file mode 100644 (file)
index 0000000..c7b6818
--- /dev/null
@@ -0,0 +1,13 @@
+# Copyright 2017 ZTE Corporation.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# 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.
diff --git a/lcm/lcm/nf/vnfs/vnf_create/create_vnf_identifier.py b/lcm/lcm/nf/vnfs/vnf_create/create_vnf_identifier.py
new file mode 100644 (file)
index 0000000..0eb4474
--- /dev/null
@@ -0,0 +1,65 @@
+# Copyright 2017 ZTE Corporation.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# 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 uuid
+
+from lcm.pub.database.models import NfInstModel
+from lcm.pub.exceptions import NFLCMException
+from lcm.pub.utils.restcall import req_by_msb
+from lcm.pub.utils.timeutil import now_time
+from lcm.pub.utils.values import ignore_case_get
+
+logger = logging.getLogger(__name__)
+# Query vnfd_rawdata by vnfdid
+def vnfd_rawdata_get(vnfdid):
+    ret = req_by_msb("openoapi/nslcm/v1/vnfpackage/%s" % vnfdid, "GET")
+    return ret
+
+class CreateVnf:
+    def __init__(self, data):
+        self.data = data
+
+    def do_biz(self):
+        logger.debug("CreateVnfIdentifier--CreateVnf::> %s" % self.data)
+        self.vnfd_id = ignore_case_get(self.data, "vnfdId")
+        self.vnf_instance_mame = ignore_case_get(self.data, "vnfInstanceName")
+        self.description = ignore_case_get(self.data, "vnfInstanceDescription")
+        is_exist = NfInstModel.objects.filter(nf_name=self.vnf_instance_mame).exists()
+        logger.debug("check_ns_inst_name_exist::is_exist=%s" % is_exist)
+        if is_exist:
+            raise NFLCMException('VNF is already exist.')
+
+        #get rawdata by vnfd_id
+        ret = vnfd_rawdata_get(self.vnfd_id)
+        if ret[0] != 0:
+            raise NFLCMException('Get vnfd_raw_data failed.')
+        dst_plan = json.JSONDecoder().decode(ret[1])
+        self.vnfd_version = dst_plan['metadata']['vnfd_version']
+        self.vendor = dst_plan['metadata']['vendor']
+        self.producttype = dst_plan['metadata']['domain_type']
+        self.netype = dst_plan['metadata']['vnf_type']
+        self.vnfd_model = dst_plan
+        self.vnfSoftwareVersion = dst_plan['metadata']['version']
+
+        self.nf_inst_id = str(uuid.uuid4())
+        NfInstModel.objects.create(nfinstid=self.nf_inst_id, mnfinstid=self.nf_inst_id, nf_name=self.vnf_instance_mame,
+                                   package_id='todo', vnfm_inst_id='todo', version=self.vnfd_version, vendor=self.vendor,
+                                   producttype=self.producttype,netype=self.netype, vnfd_model=self.vnfd_model,
+                                   instantiationState='NOT_INSTANTIATED', nf_desc=self.description, vnfdid=self.vnfd_id,
+                                   vnfSoftwareVersion=self.vnfSoftwareVersion, vnfConfigurableProperties='todo',
+                                   localizationLanguage='EN_US',create_time=now_time())
+        is_exist = NfInstModel.objects.filter(nf_name=self.vnf_instance_mame).exists()
+        logger.debug("check_ns_inst_name_exist::is_exist=%s" % is_exist)
+        return self.nf_inst_id
\ No newline at end of file
index 898bc53..8f7d76a 100644 (file)
 from django.db import models
 
 
-class VnfInstModel(models.Model):
+# class VnfInstModel(models.Model):
+#     class Meta:
+#         db_table = 'GVNFM_VNFINST'
+#
+#     id = models.CharField(db_column='ID', primary_key=True, max_length=200)
+#     name = models.CharField(db_column='NAME', max_length=200)
+#     vnfd_id = models.CharField(db_column='VNFDID', max_length=200)
+#     description = models.CharField(db_column='DESCRIPTION', max_length=255, null=True, blank=True)
+#     status = models.CharField(db_column='STATUS', max_length=200, null=True, blank=True)
+#     create_time = models.CharField(db_column='CREATETIME', max_length=200, null=True, blank=True)
+#     lastuptime = models.CharField(db_column='LASTUPTIME', max_length=200, null=True, blank=True)
+
+class NfInstModel(models.Model):
     class Meta:
-        db_table = 'GVNFM_VNFINST'
+        db_table = 'NFINST'
 
-    id = models.CharField(db_column='ID', primary_key=True, max_length=200)
-    name = models.CharField(db_column='NAME', max_length=200)
-    vnfd_id = models.CharField(db_column='VNFDID', max_length=200)
-    description = models.CharField(db_column='DESCRIPTION', max_length=255, null=True, blank=True)
-    status = models.CharField(db_column='STATUS', max_length=200, null=True, blank=True)
+    nfinstid = models.CharField(db_column='NFINSTID', max_length=200, primary_key=True)
+    mnfinstid = models.CharField(db_column='M_NFINSTID', max_length=200, blank=True, null=True)
+    nf_name = models.CharField(db_column='NFNAME', max_length=100, blank=True, null=True) #CreateVnfRequest.vnfInstanceName
+    template_id = models.CharField(db_column='TEMPLATEID', max_length=200, blank=True, null=True)
+    vnf_id = models.CharField(db_column='VNFID', max_length=200, blank=True, null=True)
+    package_id = models.CharField(db_column='PACKAGEID', max_length=200, blank=True, null=True)
+    vnfm_inst_id = models.CharField(db_column='VNFMINSTID', max_length=200, blank=True, null=True)
+    multivim = models.IntegerField(db_column='MULTIVIM', default=0)
+    ns_inst_id = models.CharField(db_column='NSINSTID', max_length=200, blank=True, null=True)
+    status = models.CharField(db_column='STATUS', max_length=20, blank=True, null=True)
+    flavour_id = models.CharField(db_column='FLAVOURID', max_length=200, blank=True, null=True) #InstantiateVnfRequest.flavourId
+    vnf_level = models.CharField(db_column='VNFLEVEL', max_length=200, blank=True, null=True) #InstantiateVnfRequest.instantiationLevelId
+    location = models.CharField(db_column='LOCATION', max_length=200, blank=True, null=True)
+    deploy_environment = models.CharField(db_column='DEPLOYENVIRONMENT', max_length=100, blank=True, null=True)
+    max_vm = models.IntegerField(db_column='MAXVM', blank=True, null=True)
+    max_cpu = models.IntegerField(db_column='MAXCPU', blank=True, null=True)
+    max_ram = models.IntegerField(db_column='MAXRAM', blank=True, null=True)
+    max_hd = models.IntegerField(db_column='MAXHD', blank=True, null=True)
+    max_shd = models.IntegerField(db_column='MAXSHD', blank=True, null=True)
+    max_net = models.IntegerField(db_column='MAXNET', blank=True, null=True)
+    version = models.CharField(db_column='VERSION', max_length=255, null=True)
+    vendor = models.CharField(db_column='VENDOR', max_length=255, null=True, blank=True)
+    producttype = models.CharField(db_column='PRODUCTTYPE', max_length=255, null=True)
+    netype = models.CharField(db_column='NETYPE', max_length=255, null=True)
+    vnfd_model = models.TextField(db_column='VNFDMODEL', max_length=20000, blank=True, null=True)
+    input_params = models.TextField(db_column='INPUTPARAMS', max_length=2000, blank=True, null=True)  #InstantiateVnfRequest.additionalParams
+    scale_params = models.TextField(db_column='SCALEPARAMS', max_length=2000, null=True, blank=True)
     create_time = models.CharField(db_column='CREATETIME', max_length=200, null=True, blank=True)
-    lastuptime = models.CharField(db_column='LASTUPTIME', max_length=200, null=True, blank=True)
+    lastuptime = models.CharField(db_column='LASTUPTIME', max_length=200, blank=True, null=True)
+    extension = models.TextField(db_column='EXTENSION', max_length=65535, blank=True, null=True)
+    initallocatedata = models.TextField(db_column='INITALLOCATEDATA', max_length=20000, blank=True, null=True)
+    predefinedvm = models.TextField(db_column='PREDEFINEDVM', max_length=65535, blank=True, null=True)
+    vnfextendtype = models.CharField(db_column='VNFEXTENDTYPE', max_length=255, null=True)
+
+    instantiationState = models.CharField(db_column='INSTANTIATIONSTATE', max_length=200, blank=True, null=True)
+    nf_desc = models.CharField(db_column='VNFINSTANCEDESC', max_length=200, blank=True, null=True)
+    vnfdid = models.CharField(db_column='VNFDID', max_length=200, blank=True, null=True)
+    vnfSoftwareVersion = models.CharField(db_column='VNFSOFTWAREVER', max_length=200, blank=True, null=True)
+    vnfConfigurableProperties = models.TextField(db_column='VNFCONFIGURABLEPROPERTIES', max_length=20000, blank=True, null=True)
+    localizationLanguage = models.CharField(db_column='LOCALIZATIONLANGUAGE', max_length=255, null=True)
 
 class JobModel(models.Model):
     class Meta: