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