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