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