293b65367736fa857138ed813a640276d9dec964
[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
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_instid_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
133 vnfd_model_dict = {
134     'local_storages': [],
135     'vdus': [
136         {
137             'volumn_storages': [],
138             'nfv_compute': {
139                 'mem_size': '',
140                 'num_cpus': u'2'},
141             'local_storages': [],
142             'vdu_id': u'vdu_omm.001',
143             'image_file': u'opencos_sss_omm_img_release_20150723-1-disk1',
144             'dependencies': [],
145             'vls': [],
146             'cps': [],
147             'properties': {
148                 'key_vdu': '',
149                 'support_scaling': False,
150                 'vdu_type': '',
151                 'name': '',
152                 'storage_policy': '',
153                 'location_info': {
154                     'vimId': '',
155                     'availability_zone': '',
156                     'region': '',
157                     'dc': '',
158                     'host': '',
159                     'tenant': ''},
160                 'inject_data_list': [],
161                 'watchdog': {
162                     'action': '',
163                     'enabledelay': ''},
164                 'local_affinity_antiaffinity_rule': {},
165                 'template_id': u'omm.001',
166                 'manual_scale_select_vim': False},
167             'description': u'singleommvm'},
168         {
169             'volumn_storages': [],
170             'nfv_compute': {
171                 'mem_size': '',
172                 'num_cpus': u'4'},
173             'local_storages': [],
174             'vdu_id': u'vdu_1',
175             'image_file': u'sss',
176             'dependencies': [],
177             'vls': [],
178             'cps': [],
179             'properties': {
180                 'key_vdu': '',
181                 'support_scaling': False,
182                 'vdu_type': '',
183                 'name': '',
184                 'storage_policy': '',
185                 'location_info': {
186                     'vimId': '',
187                     'availability_zone': '',
188                     'region': '',
189                     'dc': '',
190                     'host': '',
191                     'tenant': ''},
192                 'inject_data_list': [],
193                 'watchdog': {
194                     'action': '',
195                     'enabledelay': ''},
196                 'local_affinity_antiaffinity_rule': {},
197                 'template_id': u'1',
198                 'manual_scale_select_vim': False},
199             'description': u'ompvm'},
200         {
201             'volumn_storages': [],
202             'nfv_compute': {
203                 'mem_size': '',
204                 'num_cpus': u'14'},
205             'local_storages': [],
206             'vdu_id': u'vdu_2',
207             'image_file': u'sss',
208             'dependencies': [],
209             'vls': [],
210             'cps': [],
211             'properties': {
212                 'key_vdu': '',
213                 'support_scaling': False,
214                 'vdu_type': '',
215                 'name': '',
216                 'storage_policy': '',
217                 'location_info': {
218                     'vimId': '',
219                     'availability_zone': '',
220                     'region': '',
221                     'dc': '',
222                     'host': '',
223                     'tenant': ''},
224                 'inject_data_list': [],
225                 'watchdog': {
226                     'action': '',
227                     'enabledelay': ''},
228                 'local_affinity_antiaffinity_rule': {},
229                 'template_id': u'2',
230                 'manual_scale_select_vim': False},
231             'description': u'ompvm'},
232         {
233             'volumn_storages': [],
234             'nfv_compute': {
235                 'mem_size': '',
236                 'num_cpus': u'14'},
237             'local_storages': [],
238             'vdu_id': u'vdu_3',
239             'image_file': u'sss',
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'3',
262                 'manual_scale_select_vim': False},
263             'description': u'ompvm'},
264         {
265             'volumn_storages': [],
266             'nfv_compute': {
267                 'mem_size': '',
268                 'num_cpus': u'4'},
269             'local_storages': [],
270             'vdu_id': u'vdu_10',
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'10',
294                 'manual_scale_select_vim': False},
295             'description': u'ppvm'},
296         {
297             'volumn_storages': [],
298             'nfv_compute': {
299                 'mem_size': '',
300                 'num_cpus': u'14'},
301             'local_storages': [],
302             'vdu_id': u'vdu_11',
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'11',
326                 'manual_scale_select_vim': False},
327             'description': u'ppvm'},
328         {
329             'volumn_storages': [],
330             'nfv_compute': {
331                 'mem_size': '',
332                 'num_cpus': u'14'},
333             'local_storages': [],
334             'vdu_id': u'vdu_12',
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'12',
358                 'manual_scale_select_vim': False},
359             'description': u'ppvm'}],
360     'volumn_storages': [],
361     'policies': {
362         'scaling': {
363             'targets': {},
364             'policy_id': u'policy_scale_sss-vnf-template',
365             'properties': {
366                 'policy_file': '*-vnfd.zip/*-vnf-policy.xml'},
367             'description': ''}},
368     'image_files': [
369         {
370             'description': '',
371             'properties': {
372                 'name': u'opencos_sss_omm_img_release_20150723-1-disk1.vmdk',
373                 'checksum': '',
374                 'disk_format': u'VMDK',
375                 'file_url': u'./zte-cn-sss-main-image/OMM/opencos_sss_omm_img_release_20150723-1-disk1.vmdk',
376                 'container_type': 'vm',
377                 'version': '',
378                 'hypervisor_type': 'kvm'},
379             'image_file_id': u'opencos_sss_omm_img_release_20150723-1-disk1'},
380         {
381             'description': '',
382             'properties': {
383                 'name': u'sss.vmdk',
384                 'checksum': '',
385                 'disk_format': u'VMDK',
386                 'file_url': u'./zte-cn-sss-main-image/NE/sss.vmdk',
387                 'container_type': 'vm',
388                 'version': '',
389                 'hypervisor_type': 'kvm'},
390             'image_file_id': u'sss'}],
391     'vls': [],
392     'cps': [],
393     'metadata': {
394         'vendor': u'zte',
395         'is_shared': False,
396         'description': '',
397         'domain_type': u'CN',
398         'version': u'v4.14.10',
399         'vmnumber_overquota_alarm': False,
400         'cross_dc': False,
401         'vnf_type': u'SSS',
402         'vnfd_version': u'V00000001',
403         'id': u'sss-vnf-template',
404         'name': u'sss-vnf-template'},
405     "flavourId": "flavour_1",
406     "instantiationLevelId": "instantiationLevel_1",
407     "extVirtualLinks": [
408         {
409             "vlInstanceId": "1",
410             "vim": {
411                 "vimInfoId": "1",
412                 "vimId": "1",
413                 "interfaceInfo": {
414                     "vimType": "vim",
415                     "apiVersion": "v2",
416                     "protocolType": "http"
417                 },
418                 "accessInfo": {
419                     "tenant": "tenant_vCPE",
420                     "username": "vCPE",
421                     "password": "vCPE_321"
422                 },
423                 "interfaceEndpoint": "http://10.43.21.105:80/"
424             },
425             "resourceId": "1246",
426             "extCps": [
427                 {
428                     "cpdId": "11",
429                     "addresses": [
430                         {
431                             "addressType": "MAC",
432                             "l2AddressData": "00:f3:43:20:a2:a3"
433                         },
434                         {
435                             "addressType": "IP",
436                             "l3AddressData": {
437                                 "iPAddressType": "IPv4",
438                                 "iPAddress": "192.168.104.2"
439                             }
440                         }
441                     ],
442                     "numDynamicAddresses": 0
443                 }
444             ]
445         }
446     ],
447     "localizationLanguage": "en_US",
448     "additionalParams": {}
449 }