9a1d3ef6c4315ec5e7145b31b772de8df0b444b1
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / vnfs / tests / test_vnf_create.py
1 # Copyright 2017 ZTE Corporation.
2 #
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
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 import json
16 import uuid
17
18 import mock
19 from django.test import TestCase, Client
20 from rest_framework import status
21
22 from lcm.nf.vnfs.vnf_create.create_vnf_identifier import CreateVnf
23 from lcm.nf.vnfs.vnf_create.inst_vnf import InstVnf
24 from lcm.pub.database.models import NfInstModel, JobStatusModel, NfvoRegInfoModel, VmInstModel, NetworkInstModel, \
25     SubNetworkInstModel, PortInstModel
26 from lcm.pub.utils import restcall
27 from lcm.pub.vimapi import adaptor
28 from lcm.pub.utils.jobutil import JobUtil
29
30 inst_req_data = {
31     "flavourId": "flavour_1",
32     "instantiationLevelId": "instantiationLevel_1",
33     "extVirtualLinks": [
34         {
35             "vlInstanceId": "1",
36             "vim": {
37                 "vimInfoId": "1",
38                 "vimId": "1",
39                 "interfaceInfo": {
40                     "vimType": "vim",
41                     "apiVersion": "v2",
42                     "protocolType": "http"
43                 },
44                 "accessInfo": {
45                     "tenant": "tenant_vCPE",
46                     "username": "vCPE",
47                     "password": "vCPE_321"
48                 },
49                 "interfaceEndpoint": "http://10.43.21.105:80/"
50             },
51             "resourceId": "1246",
52             "extCps": [
53                 {
54                     "cpdId": "11",
55                     "addresses": [
56                         {
57                             "addressType": "MAC",
58                             "l2AddressData": "00:f3:43:20:a2:a3"
59                         },
60                         {
61                             "addressType": "IP",
62                             "l3AddressData": {
63                                 "iPAddressType": "IPv4",
64                                 "iPAddress": "192.168.104.2"
65                             }
66                         }
67                     ],
68                     "numDynamicAddresses": 0
69                 }
70             ]
71         }
72     ],
73     "localizationLanguage": "en_US",
74     "additionalParams": {}
75 }
76
77
78 class TestNsInstantiate(TestCase):
79     def setUp(self):
80         self.client = Client()
81         VmInstModel.objects.create(vmid="1", vimid="1", resouceid="11", insttype=0, instid="1", vmname="test_01",
82                                    operationalstate=1)
83         VmInstModel.objects.create(vmid="2", vimid="2", resouceid="22", insttype=0, instid="2", vmname="test_02",
84                                operationalstate=1)
85         NetworkInstModel.objects.create(networkid='1', vimid='1', resouceid='1', name='pnet_network',
86                                         tenant='admin', insttype=0, instid='1')
87         SubNetworkInstModel.objects.create(subnetworkid='1', vimid='1', resouceid='1', networkid='1',
88                                            name='sub_pnet',tenant='admin', insttype=0, instid='1')
89         PortInstModel.objects.create(portid='1', networkid='1', subnetworkid='1', vimid='1', resouceid='1',
90                                      name='aaa_pnet_cp', tenant='admin', insttype=0, instid='1')
91
92     def tearDown(self):
93         pass
94
95     def assert_job_result(self, job_id, job_progress, job_detail):
96         jobs = JobStatusModel.objects.filter(
97             jobid=job_id,
98             progress=job_progress,
99             descp=job_detail)
100         self.assertEqual(1, len(jobs))
101
102     def test_swagger_ok(self):
103         response = self.client.get("/gvnfmapi/lcm/v1/swagger.json", format='json')
104         self.assertEqual(response.status_code, status.HTTP_200_OK)
105
106     @mock.patch.object(restcall, 'call_req')
107     def test_create_vnf_identifier(self, mock_call_req):
108         r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
109         mock_call_req.side_effect = [r1]
110         data = {
111             "vnfdId": "111",
112             "vnfInstanceName": "vFW_01",
113             "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
114         response = self.client.post("/gvnfmapi/lcm/v1/vnf_instances", data=data, format='json')
115         self.failUnlessEqual(status.HTTP_201_CREATED, response.status_code)
116         context = json.loads(response.content)
117         self.assertTrue(NfInstModel.objects.filter(nfinstid=context['vnfInstanceId']).exists())
118
119     @mock.patch.object(InstVnf, 'run')
120     def test_instantiate_vnf(self, mock_run):
121         response = self.client.post("/gvnfmapi/lcm/v1/vnf_instances/12/instantiate", data={}, format='json')
122         self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
123
124     def test_instantiate_vnf_when_inst_id_not_exist(self):
125         self.nf_inst_id = str(uuid.uuid4())
126         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
127         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
128         data = inst_req_data
129         InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
130         self.assert_job_result(self.job_id, 255, "VNF nf_inst_id is not exist.")
131
132     @mock.patch.object(restcall, 'call_req')
133     def test_instantiate_vnf_when_input_para_not_define_in_vnfd(self, mock_call_req):
134         r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
135         r2 = [0, json.JSONEncoder().encode(''), '200']
136         mock_call_req.side_effect = [r1, r2]
137         create_data = {
138             "vnfdId": "111",
139             "vnfInstanceName": "vFW_01",
140             "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
141         self.nf_inst_id = CreateVnf(create_data).do_biz()
142         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
143         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
144         data = inst_req_data
145         InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
146         self.assert_job_result(self.job_id, 255, "Input parameter is not defined in vnfd_info.")
147
148     @mock.patch.object(restcall, 'call_req')
149     def test_instantiate_vnf_when_get_nfvo_config_failed(self, mock_call_req):
150         r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
151         r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
152         mock_call_req.side_effect = [r1, r2]
153         create_data = {
154             "vnfdId": "111",
155             "vnfInstanceName": "vFW_01",
156             "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
157         self.nf_inst_id = CreateVnf(create_data).do_biz()
158         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
159         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
160         data = inst_req_data
161         InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
162         self.assert_job_result(self.job_id, 255, "Nfvo was not registered")
163
164     @mock.patch.object(restcall, 'call_req')
165     def test_instantiate_vnf_when_applay_grant_failed(self, mock_call_req):
166         NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
167                                         nfvouser='root', nfvopassword='root123')
168         r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
169         r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
170         r3 = [1, json.JSONEncoder().encode(''), '200']
171         mock_call_req.side_effect = [r1, r2, r3]
172         create_data = {
173             "vnfdId": "111",
174             "vnfInstanceName": "vFW_01",
175             "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
176         self.nf_inst_id = CreateVnf(create_data).do_biz()
177         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
178         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
179         data = inst_req_data
180         InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
181         self.assert_job_result(self.job_id, 255, "Nf instancing apply grant exception")
182
183     @mock.patch.object(restcall, 'call_req')
184     # @mock.patch.object(adaptor, 'create_vim_res')
185     def test_instantiate_vnf_when_create_res_failed(self, mock_call_req):
186         NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
187                                         nfvouser='root', nfvopassword='root123')
188         r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
189         r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
190         r3 = [0, json.JSONEncoder().encode('Nf instancing apply grant'), '200']
191         # r4 = [0, json.JSONEncoder().encode('Nf instancing apply resource'), '200']
192         mock_call_req.side_effect = [r1, r2, r3]
193         # mock_create_vim_res.re.return_value = None
194         create_data = {
195             "vnfdId": "111",
196             "vnfInstanceName": "vFW_01",
197             "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
198         self.nf_inst_id = CreateVnf(create_data).do_biz()
199         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
200         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
201         data = inst_req_data
202         InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
203         self.assert_job_result(self.job_id, 255, "Create resource failed")
204
205     # @mock.patch.object(restcall, 'call_req')
206     # # @mock.patch.object(adaptor, 'create_vim_res')
207     # def test_instantiate_vnf_success(self, mock_call_req):
208     #     NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
209     #                                     nfvouser='root', nfvopassword='root123')
210     #     r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
211     #     r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
212     #     r3 = [0, json.JSONEncoder().encode('Nf instancing apply grant'), '200']
213     #     # r4 = [0, json.JSONEncoder().encode('Nf instancing apply resource'), '200']
214     #     mock_call_req.side_effect = [r1, r2, r3]
215     #     # mock_create_vim_res.re.return_value = None
216     #     create_data = {
217     #         "vnfdId": "111",
218     #         "vnfInstanceName": "vFW_01",
219     #         "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
220     #     self.nf_inst_id = CreateVnf(create_data).do_biz()
221     #     self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
222     #     JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
223     #     data = inst_req_data
224     #     InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
225     #     self.assert_job_result(self.job_id, 100, "Instantiate Vnf success.")
226
227
228 vnfd_model_dict = {
229     'local_storages': [],
230     'vdus': [
231         {
232             'volumn_storages': [],
233             'nfv_compute': {
234                 'mem_size': '',
235                 'num_cpus': u'2'},
236             'local_storages': [],
237             'vdu_id': u'vdu_omm.001',
238             'image_file': u'opencos_sss_omm_img_release_20150723-1-disk1',
239             'dependencies': [],
240             'vls': [],
241             'cps': [],
242             'properties': {
243                 'key_vdu': '',
244                 'support_scaling': False,
245                 'vdu_type': '',
246                 'name': '',
247                 'storage_policy': '',
248                 'location_info': {
249                     'vimId': '',
250                     'availability_zone': '',
251                     'region': '',
252                     'dc': '',
253                     'host': '',
254                     'tenant': ''},
255                 'inject_data_list': [],
256                 'watchdog': {
257                     'action': '',
258                     'enabledelay': ''},
259                 'local_affinity_antiaffinity_rule': {},
260                 'template_id': u'omm.001',
261                 'manual_scale_select_vim': False},
262             'description': u'singleommvm'},
263         {
264             'volumn_storages': [],
265             'nfv_compute': {
266                 'mem_size': '',
267                 'num_cpus': u'4'},
268             'local_storages': [],
269             'vdu_id': u'vdu_1',
270             'image_file': u'sss',
271             'dependencies': [],
272             'vls': [],
273             'cps': [],
274             'properties': {
275                 'key_vdu': '',
276                 'support_scaling': False,
277                 'vdu_type': '',
278                 'name': '',
279                 'storage_policy': '',
280                 'location_info': {
281                     'vimId': '',
282                     'availability_zone': '',
283                     'region': '',
284                     'dc': '',
285                     'host': '',
286                     'tenant': ''},
287                 'inject_data_list': [],
288                 'watchdog': {
289                     'action': '',
290                     'enabledelay': ''},
291                 'local_affinity_antiaffinity_rule': {},
292                 'template_id': u'1',
293                 'manual_scale_select_vim': False},
294             'description': u'ompvm'},
295         {
296             'volumn_storages': [],
297             'nfv_compute': {
298                 'mem_size': '',
299                 'num_cpus': u'14'},
300             'local_storages': [],
301             'vdu_id': u'vdu_2',
302             'image_file': u'sss',
303             'dependencies': [],
304             'vls': [],
305             'cps': [],
306             'properties': {
307                 'key_vdu': '',
308                 'support_scaling': False,
309                 'vdu_type': '',
310                 'name': '',
311                 'storage_policy': '',
312                 'location_info': {
313                     'vimId': '',
314                     'availability_zone': '',
315                     'region': '',
316                     'dc': '',
317                     'host': '',
318                     'tenant': ''},
319                 'inject_data_list': [],
320                 'watchdog': {
321                     'action': '',
322                     'enabledelay': ''},
323                 'local_affinity_antiaffinity_rule': {},
324                 'template_id': u'2',
325                 'manual_scale_select_vim': False},
326             'description': u'ompvm'},
327         {
328             'volumn_storages': [],
329             'nfv_compute': {
330                 'mem_size': '',
331                 'num_cpus': u'14'},
332             'local_storages': [],
333             'vdu_id': u'vdu_3',
334             'image_file': u'sss',
335             'dependencies': [],
336             'vls': [],
337             'cps': [],
338             'properties': {
339                 'key_vdu': '',
340                 'support_scaling': False,
341                 'vdu_type': '',
342                 'name': '',
343                 'storage_policy': '',
344                 'location_info': {
345                     'vimId': '',
346                     'availability_zone': '',
347                     'region': '',
348                     'dc': '',
349                     'host': '',
350                     'tenant': ''},
351                 'inject_data_list': [],
352                 'watchdog': {
353                     'action': '',
354                     'enabledelay': ''},
355                 'local_affinity_antiaffinity_rule': {},
356                 'template_id': u'3',
357                 'manual_scale_select_vim': False},
358             'description': u'ompvm'},
359         {
360             'volumn_storages': [],
361             'nfv_compute': {
362                 'mem_size': '',
363                 'num_cpus': u'4'},
364             'local_storages': [],
365             'vdu_id': u'vdu_10',
366             'image_file': u'sss',
367             'dependencies': [],
368             'vls': [],
369             'cps': [],
370             'properties': {
371                 'key_vdu': '',
372                 'support_scaling': False,
373                 'vdu_type': '',
374                 'name': '',
375                 'storage_policy': '',
376                 'location_info': {
377                     'vimId': '',
378                     'availability_zone': '',
379                     'region': '',
380                     'dc': '',
381                     'host': '',
382                     'tenant': ''},
383                 'inject_data_list': [],
384                 'watchdog': {
385                     'action': '',
386                     'enabledelay': ''},
387                 'local_affinity_antiaffinity_rule': {},
388                 'template_id': u'10',
389                 'manual_scale_select_vim': False},
390             'description': u'ppvm'},
391         {
392             'volumn_storages': [],
393             'nfv_compute': {
394                 'mem_size': '',
395                 'num_cpus': u'14'},
396             'local_storages': [],
397             'vdu_id': u'vdu_11',
398             'image_file': u'sss',
399             'dependencies': [],
400             'vls': [],
401             'cps': [],
402             'properties': {
403                 'key_vdu': '',
404                 'support_scaling': False,
405                 'vdu_type': '',
406                 'name': '',
407                 'storage_policy': '',
408                 'location_info': {
409                     'vimId': '',
410                     'availability_zone': '',
411                     'region': '',
412                     'dc': '',
413                     'host': '',
414                     'tenant': ''},
415                 'inject_data_list': [],
416                 'watchdog': {
417                     'action': '',
418                     'enabledelay': ''},
419                 'local_affinity_antiaffinity_rule': {},
420                 'template_id': u'11',
421                 'manual_scale_select_vim': False},
422             'description': u'ppvm'},
423         {
424             'volumn_storages': [],
425             'nfv_compute': {
426                 'mem_size': '',
427                 'num_cpus': u'14'},
428             'local_storages': [],
429             'vdu_id': u'vdu_12',
430             'image_file': u'sss',
431             'dependencies': [],
432             'vls': [],
433             'cps': [],
434             'properties': {
435                 'key_vdu': '',
436                 'support_scaling': False,
437                 'vdu_type': '',
438                 'name': '',
439                 'storage_policy': '',
440                 'location_info': {
441                     'vimId': '',
442                     'availability_zone': '',
443                     'region': '',
444                     'dc': '',
445                     'host': '',
446                     'tenant': ''},
447                 'inject_data_list': [],
448                 'watchdog': {
449                     'action': '',
450                     'enabledelay': ''},
451                 'local_affinity_antiaffinity_rule': {},
452                 'template_id': u'12',
453                 'manual_scale_select_vim': False},
454             'description': u'ppvm'}],
455     'volumn_storages': [],
456     'policies': {
457         'scaling': {
458             'targets': {},
459             'policy_id': u'policy_scale_sss-vnf-template',
460             'properties': {
461                 'policy_file': '*-vnfd.zip/*-vnf-policy.xml'},
462             'description': ''}},
463     'image_files': [
464         {
465             'description': '',
466             'properties': {
467                 'name': u'opencos_sss_omm_img_release_20150723-1-disk1.vmdk',
468                 'checksum': '',
469                 'disk_format': u'VMDK',
470                 'file_url': u'./zte-cn-sss-main-image/OMM/opencos_sss_omm_img_release_20150723-1-disk1.vmdk',
471                 'container_type': 'vm',
472                 'version': '',
473                 'hypervisor_type': 'kvm'},
474             'image_file_id': u'opencos_sss_omm_img_release_20150723-1-disk1'},
475         {
476             'description': '',
477             'properties': {
478                 'name': u'sss.vmdk',
479                 'checksum': '',
480                 'disk_format': u'VMDK',
481                 'file_url': u'./zte-cn-sss-main-image/NE/sss.vmdk',
482                 'container_type': 'vm',
483                 'version': '',
484                 'hypervisor_type': 'kvm'},
485             'image_file_id': u'sss'}],
486     'vls': [],
487     'cps': [],
488     'metadata': {
489         'vendor': u'zte',
490         'is_shared': False,
491         'description': '',
492         'domain_type': u'CN',
493         'version': u'v4.14.10',
494         'vmnumber_overquota_alarm': False,
495         'cross_dc': False,
496         'vnf_type': u'SSS',
497         'vnfd_version': u'V00000001',
498         'id': u'sss-vnf-template',
499         'name': u'sss-vnf-template'},
500     "flavourId": "flavour_1",
501     "instantiationLevelId": "instantiationLevel_1",
502     "extVirtualLinks": [
503         {
504             "vlInstanceId": "1",
505             "vim": {
506                 "vimInfoId": "1",
507                 "vimId": "1",
508                 "interfaceInfo": {
509                     "vimType": "vim",
510                     "apiVersion": "v2",
511                     "protocolType": "http"
512                 },
513                 "accessInfo": {
514                     "tenant": "tenant_vCPE",
515                     "username": "vCPE",
516                     "password": "vCPE_321"
517                 },
518                 "interfaceEndpoint": "http://10.43.21.105:80/"
519             },
520             "resourceId": "1246",
521             "extCps": [
522                 {
523                     "cpdId": "11",
524                     "addresses": [
525                         {
526                             "addressType": "MAC",
527                             "l2AddressData": "00:f3:43:20:a2:a3"
528                         },
529                         {
530                             "addressType": "IP",
531                             "l3AddressData": {
532                                 "iPAddressType": "IPv4",
533                                 "iPAddress": "192.168.104.2"
534                             }
535                         }
536                     ],
537                     "numDynamicAddresses": 0
538                 }
539             ]
540         }
541     ],
542     "localizationLanguage": "en_US",
543     "additionalParams": {}
544 }