1 # Copyright 2017 ZTE Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
18 from django.test import TestCase, Client
19 from rest_framework import status
21 from lcm.nf.vnfs.vnf_create.inst_vnf import InstVnf
22 from lcm.pub.database.models import NfInstModel, JobStatusModel, VmInstModel, NetworkInstModel, \
23 SubNetworkInstModel, PortInstModel
24 from lcm.pub.utils import restcall
25 from lcm.pub.utils.jobutil import JobUtil
26 from lcm.pub.utils.timeutil import now_time
29 class TestNFInstantiate(TestCase):
31 self.client = Client()
32 VmInstModel.objects.create(vmid="1", vimid="1", resouceid="11", insttype=0, instid="1", vmname="test_01",
34 VmInstModel.objects.create(vmid="2", vimid="2", resouceid="22", insttype=0, instid="2",
35 vmname="test_02", operationalstate=1)
36 NetworkInstModel.objects.create(networkid='1', vimid='1', resouceid='1', name='pnet_network',
37 tenant='admin', insttype=0, instid='1')
38 SubNetworkInstModel.objects.create(subnetworkid='1', vimid='1', resouceid='1', networkid='1',
39 name='sub_pnet', tenant='admin', insttype=0, instid='1')
40 PortInstModel.objects.create(portid='1', networkid='1', subnetworkid='1', vimid='1', resouceid='1',
41 name='aaa_pnet_cp', tenant='admin', insttype=0, instid='1')
45 VmInstModel.objects.all().delete()
46 NetworkInstModel.objects.all().delete()
47 SubNetworkInstModel.objects.all().delete()
48 PortInstModel.objects.all().delete()
50 def assert_job_result(self, job_id, job_progress, job_detail):
51 jobs = JobStatusModel.objects.filter(
53 progress=job_progress,
55 self.assertEqual(1, len(jobs))
57 def test_swagger_ok(self):
58 response = self.client.get("/openoapi/vnflcm/v1/swagger.json", format='json')
59 self.assertEqual(response.status_code, status.HTTP_200_OK)
61 @mock.patch.object(restcall, 'call_req')
62 def test_create_vnf_identifier(self, mock_call_req):
63 r1 = [0, json.JSONEncoder().encode({'package_id':'222', 'csar_id':'2222'}), '200'] # get csar_id from nslcm by vnfd_id
64 r2 = [0, json.JSONEncoder().encode(vnfd_raw_data), '200'] # get rawdata from catalog by csar_id
65 mock_call_req.side_effect = [r1, r2]
68 "vnfInstanceName": "vFW_01",
69 "vnfInstanceDescription": "vFW in Nanjing TIC Edge"}
70 response = self.client.post("/openoapi/vnflcm/v1/vnf_instances", data=data, format='json')
71 self.failUnlessEqual(status.HTTP_201_CREATED, response.status_code)
72 context = json.loads(response.content)
73 self.assertTrue(NfInstModel.objects.filter(nfinstid=context['vnfInstanceId']).exists())
75 @mock.patch.object(InstVnf, 'run')
76 def test_instantiate_vnf(self, mock_run):
77 mock_run.re.return_value = None
78 response = self.client.post("/openoapi/vnflcm/v1/vnf_instances/12/instantiate", data={}, format='json')
79 self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
81 def test_instantiate_vnf_when_inst_id_not_exist(self):
82 self.nf_inst_id = str(uuid.uuid4())
83 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
84 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
86 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
87 self.assert_job_result(self.job_id, 255, "VNF nf_inst_id is not exist.")
89 @mock.patch.object(restcall, 'call_req')
90 def test_instantiate_vnf_when_get_package_info_by_vnfdid_failed(self, mock_call_req):
91 NfInstModel.objects.create(nfinstid='1111', nf_name='vFW_01', package_id='222',
92 version='', vendor='', netype='', vnfd_model='', status='NOT_INSTANTIATED',
93 nf_desc='vFW in Nanjing TIC Edge', vnfdid='111', create_time=now_time())
94 r1 = [1, json.JSONEncoder().encode({'package_id':'222', 'csar_id':'2222'}), '200'] # get csar_id from nslcm by vnfd_id
95 mock_call_req.side_effect = [r1]
96 self.nf_inst_id = '1111'
97 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
98 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
100 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
101 self.assert_job_result(self.job_id, 255, "Failed to query package_info of vnfdid(111) from nslcm.")
103 @mock.patch.object(restcall, 'call_req')
104 def test_instantiate_vnf_when_get_rawdata_by_csarid_failed(self, mock_call_req):
105 NfInstModel.objects.create(nfinstid='1111', nf_name='vFW_01', package_id='222',
106 version='', vendor='', netype='', vnfd_model='', status='NOT_INSTANTIATED',
107 nf_desc='vFW in Nanjing TIC Edge', vnfdid='111', create_time=now_time())
108 r1 = [0, json.JSONEncoder().encode({'package_id':'222', 'csar_id':'2222'}), '200'] # get csar_id from nslcm by vnfd_id
109 r2 = [1, json.JSONEncoder().encode(vnfd_raw_data), '200'] # get rawdata from catalog by csar_id
110 mock_call_req.side_effect = [r1, r2]
111 self.nf_inst_id = '1111'
112 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
113 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
115 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
116 self.assert_job_result(self.job_id, 255, "Failed to query rawdata of CSAR(2222) from catalog.")
118 @mock.patch.object(restcall, 'call_req')
119 def test_instantiate_vnf_when_applay_grant_failed(self, mock_call_req):
120 NfInstModel.objects.create(nfinstid='1111', nf_name='vFW_01', package_id='222',
121 version='', vendor='', netype='', vnfd_model='', status='NOT_INSTANTIATED',
122 nf_desc='vFW in Nanjing TIC Edge', vnfdid='111', create_time=now_time())
123 r1 = [0, json.JSONEncoder().encode({'package_id': '222', 'csar_id': '2222'}), '200'] # get csar_id from nslcm by vnfd_id
124 r2 = [0, json.JSONEncoder().encode(vnfd_raw_data), '200'] # get rawdata from catalog by csar_id
125 r3 = [1, json.JSONEncoder().encode({"vim":
128 "accessinfo": {"tenant": '2'}
130 }), '200'] # apply_grant_to_nfvo
131 mock_call_req.side_effect = [r1, r2, r3]
132 self.nf_inst_id = '1111'
133 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
134 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
136 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
137 self.assert_job_result(self.job_id, 255, "Nf instancing apply grant exception")
139 # @mock.patch.object(restcall, 'call_req')
140 # def test_instantiate_vnf_when_(self, mock_call_req):
141 # NfInstModel.objects.create(nfinstid='1111', nf_name='vFW_01', package_id='222',
142 # version='', vendor='', netype='', vnfd_model='', status='NOT_INSTANTIATED',
143 # nf_desc='vFW in Nanjing TIC Edge', vnfdid='111', create_time=now_time())
144 # r1 = [0, json.JSONEncoder().encode({'package_id': '222', 'csar_id': '2222'}),
145 # '200'] # get csar_id from nslcm by vnfd_id
146 # r2 = [0, json.JSONEncoder().encode(vnfd_raw_data), '200'] # get rawdata from catalog by csar_id
147 # r3 = [0, json.JSONEncoder().encode({"vim":{"vimid": '1', "accessinfo": {"tenant": '2'}}}), '200'] # apply_grant_to_nfvo
148 # mock_call_req.side_effect = [r1, r2, r3]
149 # self.nf_inst_id = '1111'
150 # self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
151 # JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
152 # data = inst_req_data
153 # InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
154 # self.assert_job_result(self.job_id, 255, "Nf instancing apply grant exception")
158 # @mock.patch.object(restcall, 'call_req')
159 # # @mock.patch.object(adaptor, 'create_vim_res')
160 # def test_instantiate_vnf_when_create_res_failed(self, mock_call_req):
161 # NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
162 # nfvouser='root', nfvopassword='root123')
163 # r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
164 # r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
165 # r3 = [0, json.JSONEncoder().encode('Nf instancing apply grant'), '200']
166 # # r4 = [0, json.JSONEncoder().encode('Nf instancing apply resource'), '200']
167 # mock_call_req.side_effect = [r1, r2, r3]
168 # # mock_create_vim_res.re.return_value = None
171 # "vnfInstanceName": "vFW_01",
172 # "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
173 # self.nf_inst_id = CreateVnf(create_data).do_biz()
174 # self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
175 # JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
176 # data = inst_req_data
177 # InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
178 # self.assert_job_result(self.job_id, 255, "Create resource failed")
180 # @mock.patch.object(restcall, 'call_req')
181 # # @mock.patch.object(adaptor, 'create_vim_res')
182 # def test_instantiate_vnf_success(self, mock_call_req):
183 # NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
184 # nfvouser='root', nfvopassword='root123')
185 # r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
186 # r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
187 # r3 = [0, json.JSONEncoder().encode('Nf instancing apply grant'), '200']
188 # r4 = [0, json.JSONEncoder().encode('None'), '200']
189 # mock_call_req.side_effect = [r1, r2, r3, r4]
190 # # mock_create_vim_res.re.return_value = None
193 # "vnfInstanceName": "vFW_01",
194 # "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
195 # self.nf_inst_id = CreateVnf(create_data).do_biz()
196 # self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
197 # JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
198 # data = inst_req_data
199 # InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
200 # self.assert_job_result(self.job_id, 100, "Instantiate Vnf success.")
203 "flavourId": "flavour_1",
204 "instantiationLevelId": "instantiationLevel_1",
214 "protocolType": "http"
217 "tenant": "tenant_vCPE",
219 "password": "vCPE_321"
221 "interfaceEndpoint": "http://10.43.21.105:80/"
223 "resourceId": "1246",
229 "addressType": "MAC",
230 "l2AddressData": "00:f3:43:20:a2:a3"
235 "iPAddressType": "IPv4",
236 "iPAddress": "192.168.104.2"
240 "numDynamicAddresses": 0
245 "localizationLanguage": "en_US",
246 "additionalParams": {}
250 'local_storages': [],
253 'volumn_storages': [],
257 'local_storages': [],
258 'vdu_id': u'vdu_omm.001',
259 'image_file': u'opencos_sss_omm_img_release_20150723-1-disk1',
265 'support_scaling': False,
268 'storage_policy': '',
271 'availability_zone': '',
276 'inject_data_list': [],
280 'local_affinity_antiaffinity_rule': {},
281 'template_id': u'omm.001',
282 'manual_scale_select_vim': False},
283 'description': u'singleommvm'},
285 'volumn_storages': [],
289 'local_storages': [],
291 'image_file': u'sss',
297 'support_scaling': False,
300 'storage_policy': '',
303 'availability_zone': '',
308 'inject_data_list': [],
312 'local_affinity_antiaffinity_rule': {},
314 'manual_scale_select_vim': False},
315 'description': u'ompvm'},
317 'volumn_storages': [],
321 'local_storages': [],
323 'image_file': u'sss',
329 'support_scaling': False,
332 'storage_policy': '',
335 'availability_zone': '',
340 'inject_data_list': [],
344 'local_affinity_antiaffinity_rule': {},
346 'manual_scale_select_vim': False},
347 'description': u'ompvm'},
349 'volumn_storages': [],
353 'local_storages': [],
355 'image_file': u'sss',
361 'support_scaling': False,
364 'storage_policy': '',
367 'availability_zone': '',
372 'inject_data_list': [],
376 'local_affinity_antiaffinity_rule': {},
378 'manual_scale_select_vim': False},
379 'description': u'ompvm'},
381 'volumn_storages': [],
385 'local_storages': [],
387 'image_file': u'sss',
393 'support_scaling': False,
396 'storage_policy': '',
399 'availability_zone': '',
404 'inject_data_list': [],
408 'local_affinity_antiaffinity_rule': {},
409 'template_id': u'10',
410 'manual_scale_select_vim': False},
411 'description': u'ppvm'},
413 'volumn_storages': [],
417 'local_storages': [],
419 'image_file': u'sss',
425 'support_scaling': False,
428 'storage_policy': '',
431 'availability_zone': '',
436 'inject_data_list': [],
440 'local_affinity_antiaffinity_rule': {},
441 'template_id': u'11',
442 'manual_scale_select_vim': False},
443 'description': u'ppvm'},
445 'volumn_storages': [],
449 'local_storages': [],
451 'image_file': u'sss',
457 'support_scaling': False,
460 'storage_policy': '',
463 'availability_zone': '',
468 'inject_data_list': [],
472 'local_affinity_antiaffinity_rule': {},
473 'template_id': u'12',
474 'manual_scale_select_vim': False},
475 'description': u'ppvm'}],
476 'volumn_storages': [],
480 'policy_id': u'policy_scale_sss-vnf-template',
482 'policy_file': '*-vnfd.zip/*-vnf-policy.xml'},
488 'name': u'opencos_sss_omm_img_release_20150723-1-disk1.vmdk',
490 'disk_format': u'VMDK',
491 'file_url': u'./zte-cn-sss-main-image/OMM/opencos_sss_omm_img_release_20150723-1-disk1.vmdk',
492 'container_type': 'vm',
494 'hypervisor_type': 'kvm'},
495 'image_file_id': u'opencos_sss_omm_img_release_20150723-1-disk1'},
501 'disk_format': u'VMDK',
502 'file_url': u'./zte-cn-sss-main-image/NE/sss.vmdk',
503 'container_type': 'vm',
505 'hypervisor_type': 'kvm'},
506 'image_file_id': u'sss'}],
513 'domain_type': u'CN',
514 'version': u'v4.14.10',
515 'vmnumber_overquota_alarm': False,
518 'vnfd_version': u'V00000001',
519 'id': u'sss-vnf-template',
520 'name': u'sss-vnf-template'},
521 "flavourId": "flavour_1",
522 "instantiationLevelId": "instantiationLevel_1",
532 "protocolType": "http"
535 "tenant": "tenant_vCPE",
537 "password": "vCPE_321"
539 "interfaceEndpoint": "http://10.43.21.105:80/"
541 "resourceId": "1246",
547 "addressType": "MAC",
548 "l2AddressData": "00:f3:43:20:a2:a3"
553 "iPAddressType": "IPv4",
554 "iPAddress": "192.168.104.2"
558 "numDynamicAddresses": 0
563 "localizationLanguage": "en_US",
564 "additionalParams": {}
572 "plugin_info": "vbrasplugin_1.0",
574 "request_reclassification": False,
579 "vnfd_version": "1.0.0",
580 "id": "zte_vbras_1.0",
585 "id": "aaa_dnet_cp_0xu2j5sbigxc8h1ega3if0ld1",
586 "type_name": "tosca.nodes.nfv.ext.zte.CP",
587 "template_name": "aaa_dnet_cp",
590 "type_name": "integer",
594 "type_name": "string",
595 "value": "bidirectional"
598 "type_name": "string",
601 "sfc_encapsulation": {
602 "type_name": "string",
606 "type_name": "integer",
613 "source_requirement_index": 0,
614 "target_node_id": "AAA_image_d8aseebr120nbm7bo1ohkj194",
615 "target_capability_name": "feature"
620 "id": "LB_Image_oj5l2ay8l2g6vcq6fsswzduha",
621 "type_name": "tosca.nodes.nfv.ext.ImageFile",
622 "template_name": "LB_Image",
625 "type_name": "string",
629 "type_name": "string",
630 "value": "/SoftwareImages/image-lb"
633 "type_name": "string",
643 "plugin_info": "vbrasplugin_1.0",
645 "request_reclassification": False,
650 "vnfd_version": "1.0.0",
651 "id": "zte_vbras_1.0",
656 "name": "aaa_dnet_cp",
657 "type_name": "tosca.nodes.nfv.ext.zte.CP",
658 "default_instances": 1,
662 "type_name": "integer",
666 "requirement_templates": [
668 "name": "virtualbinding",
669 "target_node_template_name": "AAA",
670 "target_capability_name": "virtualbinding"