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