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