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']
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']
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_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']
125 r3 = [1, json.JSONEncoder().encode(''), '200']
126 mock_call_req.side_effect = [r1, r2, r3]
127 self.nf_inst_id = '1111'
128 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
129 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
131 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
132 self.assert_job_result(self.job_id, 255, "Nf instancing apply grant exception")
136 # @mock.patch.object(restcall, 'call_req')
137 # def test_instantiate_vnf_when_applay_grant_failed(self, mock_call_req):
138 # NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
139 # nfvouser='root', nfvopassword='root123')
140 # r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
141 # r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
142 # r3 = [1, json.JSONEncoder().encode(''), '200']
143 # mock_call_req.side_effect = [r1, r2, r3]
146 # "vnfInstanceName": "vFW_01",
147 # "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
148 # self.nf_inst_id = CreateVnf(create_data).do_biz()
149 # self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
150 # JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
151 # data = inst_req_data
152 # InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
153 # self.assert_job_result(self.job_id, 255, "Nf instancing apply grant exception")
155 # @mock.patch.object(restcall, 'call_req')
156 # # @mock.patch.object(adaptor, 'create_vim_res')
157 # def test_instantiate_vnf_when_create_res_failed(self, mock_call_req):
158 # NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
159 # nfvouser='root', nfvopassword='root123')
160 # r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
161 # r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
162 # r3 = [0, json.JSONEncoder().encode('Nf instancing apply grant'), '200']
163 # # r4 = [0, json.JSONEncoder().encode('Nf instancing apply resource'), '200']
164 # mock_call_req.side_effect = [r1, r2, r3]
165 # # mock_create_vim_res.re.return_value = None
168 # "vnfInstanceName": "vFW_01",
169 # "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
170 # self.nf_inst_id = CreateVnf(create_data).do_biz()
171 # self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
172 # JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
173 # data = inst_req_data
174 # InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
175 # self.assert_job_result(self.job_id, 255, "Create resource failed")
177 # @mock.patch.object(restcall, 'call_req')
178 # # @mock.patch.object(adaptor, 'create_vim_res')
179 # def test_instantiate_vnf_success(self, mock_call_req):
180 # NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
181 # nfvouser='root', nfvopassword='root123')
182 # r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
183 # r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
184 # r3 = [0, json.JSONEncoder().encode('Nf instancing apply grant'), '200']
185 # r4 = [0, json.JSONEncoder().encode('None'), '200']
186 # mock_call_req.side_effect = [r1, r2, r3, r4]
187 # # mock_create_vim_res.re.return_value = None
190 # "vnfInstanceName": "vFW_01",
191 # "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
192 # self.nf_inst_id = CreateVnf(create_data).do_biz()
193 # self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
194 # JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
195 # data = inst_req_data
196 # InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
197 # self.assert_job_result(self.job_id, 100, "Instantiate Vnf success.")
200 "flavourId": "flavour_1",
201 "instantiationLevelId": "instantiationLevel_1",
211 "protocolType": "http"
214 "tenant": "tenant_vCPE",
216 "password": "vCPE_321"
218 "interfaceEndpoint": "http://10.43.21.105:80/"
220 "resourceId": "1246",
226 "addressType": "MAC",
227 "l2AddressData": "00:f3:43:20:a2:a3"
232 "iPAddressType": "IPv4",
233 "iPAddress": "192.168.104.2"
237 "numDynamicAddresses": 0
242 "localizationLanguage": "en_US",
243 "additionalParams": {}
247 'local_storages': [],
250 'volumn_storages': [],
254 'local_storages': [],
255 'vdu_id': u'vdu_omm.001',
256 'image_file': u'opencos_sss_omm_img_release_20150723-1-disk1',
262 'support_scaling': False,
265 'storage_policy': '',
268 'availability_zone': '',
273 'inject_data_list': [],
277 'local_affinity_antiaffinity_rule': {},
278 'template_id': u'omm.001',
279 'manual_scale_select_vim': False},
280 'description': u'singleommvm'},
282 'volumn_storages': [],
286 'local_storages': [],
288 'image_file': u'sss',
294 'support_scaling': False,
297 'storage_policy': '',
300 'availability_zone': '',
305 'inject_data_list': [],
309 'local_affinity_antiaffinity_rule': {},
311 'manual_scale_select_vim': False},
312 'description': u'ompvm'},
314 'volumn_storages': [],
318 'local_storages': [],
320 'image_file': u'sss',
326 'support_scaling': False,
329 'storage_policy': '',
332 'availability_zone': '',
337 'inject_data_list': [],
341 'local_affinity_antiaffinity_rule': {},
343 'manual_scale_select_vim': False},
344 'description': u'ompvm'},
346 'volumn_storages': [],
350 'local_storages': [],
352 'image_file': u'sss',
358 'support_scaling': False,
361 'storage_policy': '',
364 'availability_zone': '',
369 'inject_data_list': [],
373 'local_affinity_antiaffinity_rule': {},
375 'manual_scale_select_vim': False},
376 'description': u'ompvm'},
378 'volumn_storages': [],
382 'local_storages': [],
384 'image_file': u'sss',
390 'support_scaling': False,
393 'storage_policy': '',
396 'availability_zone': '',
401 'inject_data_list': [],
405 'local_affinity_antiaffinity_rule': {},
406 'template_id': u'10',
407 'manual_scale_select_vim': False},
408 'description': u'ppvm'},
410 'volumn_storages': [],
414 'local_storages': [],
416 'image_file': u'sss',
422 'support_scaling': False,
425 'storage_policy': '',
428 'availability_zone': '',
433 'inject_data_list': [],
437 'local_affinity_antiaffinity_rule': {},
438 'template_id': u'11',
439 'manual_scale_select_vim': False},
440 'description': u'ppvm'},
442 'volumn_storages': [],
446 'local_storages': [],
448 'image_file': u'sss',
454 'support_scaling': False,
457 'storage_policy': '',
460 'availability_zone': '',
465 'inject_data_list': [],
469 'local_affinity_antiaffinity_rule': {},
470 'template_id': u'12',
471 'manual_scale_select_vim': False},
472 'description': u'ppvm'}],
473 'volumn_storages': [],
477 'policy_id': u'policy_scale_sss-vnf-template',
479 'policy_file': '*-vnfd.zip/*-vnf-policy.xml'},
485 'name': u'opencos_sss_omm_img_release_20150723-1-disk1.vmdk',
487 'disk_format': u'VMDK',
488 'file_url': u'./zte-cn-sss-main-image/OMM/opencos_sss_omm_img_release_20150723-1-disk1.vmdk',
489 'container_type': 'vm',
491 'hypervisor_type': 'kvm'},
492 'image_file_id': u'opencos_sss_omm_img_release_20150723-1-disk1'},
498 'disk_format': u'VMDK',
499 'file_url': u'./zte-cn-sss-main-image/NE/sss.vmdk',
500 'container_type': 'vm',
502 'hypervisor_type': 'kvm'},
503 'image_file_id': u'sss'}],
510 'domain_type': u'CN',
511 'version': u'v4.14.10',
512 'vmnumber_overquota_alarm': False,
515 'vnfd_version': u'V00000001',
516 'id': u'sss-vnf-template',
517 'name': u'sss-vnf-template'},
518 "flavourId": "flavour_1",
519 "instantiationLevelId": "instantiationLevel_1",
529 "protocolType": "http"
532 "tenant": "tenant_vCPE",
534 "password": "vCPE_321"
536 "interfaceEndpoint": "http://10.43.21.105:80/"
538 "resourceId": "1246",
544 "addressType": "MAC",
545 "l2AddressData": "00:f3:43:20:a2:a3"
550 "iPAddressType": "IPv4",
551 "iPAddress": "192.168.104.2"
555 "numDynamicAddresses": 0
560 "localizationLanguage": "en_US",
561 "additionalParams": {}
569 "plugin_info": "vbrasplugin_1.0",
571 "request_reclassification": False,
576 "vnfd_version": "1.0.0",
577 "id": "zte_vbras_1.0",
582 "id": "aaa_dnet_cp_0xu2j5sbigxc8h1ega3if0ld1",
583 "type_name": "tosca.nodes.nfv.ext.zte.CP",
584 "template_name": "aaa_dnet_cp",
587 "type_name": "integer",
591 "type_name": "string",
592 "value": "bidirectional"
595 "type_name": "string",
598 "sfc_encapsulation": {
599 "type_name": "string",
603 "type_name": "integer",
610 "source_requirement_index": 0,
611 "target_node_id": "AAA_image_d8aseebr120nbm7bo1ohkj194",
612 "target_capability_name": "feature"
617 "id": "LB_Image_oj5l2ay8l2g6vcq6fsswzduha",
618 "type_name": "tosca.nodes.nfv.ext.ImageFile",
619 "template_name": "LB_Image",
622 "type_name": "string",
626 "type_name": "string",
627 "value": "/SoftwareImages/image-lb"
630 "type_name": "string",
640 "plugin_info": "vbrasplugin_1.0",
642 "request_reclassification": False,
647 "vnfd_version": "1.0.0",
648 "id": "zte_vbras_1.0",
653 "name": "aaa_dnet_cp",
654 "type_name": "tosca.nodes.nfv.ext.zte.CP",
655 "default_instances": 1,
659 "type_name": "integer",
663 "requirement_templates": [
665 "name": "virtualbinding",
666 "target_node_template_name": "AAA",
667 "target_capability_name": "virtualbinding"