SOL003 API Align
[vfc/nfvo/lcm.git] / lcm / ns_vnfs / tests / tests.py
1 # Copyright 2016-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.pub.database.models import NfInstModel, JobModel, NSInstModel, VmInstModel
22 from lcm.pub.exceptions import NSLCMException
23 from lcm.pub.utils import restcall
24 from lcm.pub.utils.jobutil import JOB_MODEL_STATUS
25 from lcm.pub.utils.jobutil import JobUtil, JOB_TYPE
26 from lcm.pub.utils.timeutil import now_time
27 from lcm.pub.utils.values import ignore_case_get
28 from lcm.ns_vnfs.biz.create_vnfs import CreateVnfs
29 from lcm.ns_vnfs.biz.heal_vnfs import NFHealService
30 from lcm.ns_vnfs.biz.scale_vnfs import NFManualScaleService
31 from lcm.ns_vnfs.biz.terminate_nfs import TerminateVnfs
32 from lcm.ns_vnfs.const import VNF_STATUS, INST_TYPE
33 from lcm.ns_vnfs.biz import create_vnfs
34
35
36 class TestGetVnfViews(TestCase):
37     def setUp(self):
38         self.client = Client()
39         self.nf_inst_id = str(uuid.uuid4())
40         NfInstModel(nfinstid=self.nf_inst_id, nf_name='vnf1', vnfm_inst_id='1', vnf_id='vnf_id1',
41                     status=VNF_STATUS.ACTIVE, create_time=now_time(), lastuptime=now_time()).save()
42
43     def tearDown(self):
44         NfInstModel.objects.all().delete()
45
46     def test_get_vnf(self):
47         response = self.client.get("/api/nslcm/v1/ns/vnfs/%s" % self.nf_inst_id)
48         self.failUnlessEqual(status.HTTP_200_OK, response.status_code)
49         context = json.loads(response.content)
50         self.failUnlessEqual(self.nf_inst_id, context['vnfInstId'])
51
52
53 class TestCreateVnfViews(TestCase):
54     def setUp(self):
55         self.ns_inst_id = str(uuid.uuid4())
56         self.job_id = str(uuid.uuid4())
57         self.data = {
58             "nsInstanceId": self.ns_inst_id,
59             "additionalParamForNs": {
60                 "inputs": json.dumps({
61
62                 })
63             },
64             "additionalParamForVnf": [
65                 {
66                     "vnfprofileid": "VBras",
67                     "additionalparam": {
68                         "inputs": json.dumps({
69                             "vnf_param1": "11",
70                             "vnf_param2": "22"
71                         }),
72                         "vnfminstanceid": "1",
73                         "vimId": "zte_test"
74                     }
75                 }
76             ],
77             "vnfIndex": "1"
78         }
79         self.client = Client()
80         NSInstModel(id=self.ns_inst_id, name='ns', nspackage_id='1', nsd_id='nsd_id', description='description',
81                     status='instantiating', nsd_model=json.dumps(nsd_model_dict), create_time=now_time(),
82                     lastuptime=now_time()).save()
83
84     def tearDown(self):
85         NfInstModel.objects.all().delete()
86         JobModel.objects.all().delete()
87
88     @mock.patch.object(CreateVnfs, 'run')
89     def test_create_vnf(self, mock_run):
90         response = self.client.post("/api/nslcm/v1/ns/vnfs", data=self.data)
91         self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
92         context = json.loads(response.content)
93         self.assertTrue(NfInstModel.objects.filter(nfinstid=context['vnfInstId']).exists())
94
95     @mock.patch.object(restcall, 'call_req')
96     def test_create_vnf_thread(self, mock_call_req):
97         nf_inst_id, job_id = create_vnfs.prepare_create_params()
98         mock_vals = {
99             "/api/ztevnfmdriver/v1/1/vnfs":
100                 [0, json.JSONEncoder().encode({"jobId": self.job_id, "vnfInstanceId": 3}), '200'],
101             "/api/catalog/v1/vnfpackages/zte_vbras":
102                 [0, json.JSONEncoder().encode(nf_package_info), '200'],
103             "/external-system/esr-vnfm-list/esr-vnfm/1?depth=all":
104                 [0, json.JSONEncoder().encode(vnfm_info), '200'],
105             "/api/resmgr/v1/vnf":
106                 [0, json.JSONEncoder().encode({}), '200'],
107             "/api/resmgr/v1/vnfinfo":
108                 [0, json.JSONEncoder().encode({}), '200'],
109             "/network/generic-vnfs/generic-vnf/%s" % nf_inst_id:
110                 [0, json.JSONEncoder().encode({}), '201'],
111             "/cloud-infrastructure/cloud-regions/cloud-region/zte/test?depth=all":
112                 [0, json.JSONEncoder().encode(vim_info), '201'],
113             "/cloud-infrastructure/cloud-regions/cloud-region/zte/test/tenants/tenant/admin/vservers/vserver/1":
114                 [0, json.JSONEncoder().encode({}), '201'],
115             "/api/ztevnfmdriver/v1/1/jobs/" + self.job_id + "?responseId=0":
116                 [0, json.JSONEncoder().encode({"jobid": self.job_id,
117                                                "responsedescriptor": {"progress": "100",
118                                                                       "status": JOB_MODEL_STATUS.FINISHED,
119                                                                       "responseid": "3",
120                                                                       "statusdescription": "creating",
121                                                                       "errorcode": "0",
122                                                                       "responsehistorylist": [
123                                                                           {"progress": "0",
124                                                                            "status": JOB_MODEL_STATUS.PROCESSING,
125                                                                            "responseid": "2",
126                                                                            "statusdescription": "creating",
127                                                                            "errorcode": "0"}]}}), '200']}
128
129         def side_effect(*args):
130             return mock_vals[args[4]]
131         mock_call_req.side_effect = side_effect
132         data = {
133             'ns_instance_id': ignore_case_get(self.data, 'nsInstanceId'),
134             'additional_param_for_ns': ignore_case_get(self.data, 'additionalParamForNs'),
135             'additional_param_for_vnf': ignore_case_get(self.data, 'additionalParamForVnf'),
136             'vnf_index': ignore_case_get(self.data, 'vnfIndex')
137         }
138         CreateVnfs(data, nf_inst_id, job_id).run()
139         self.assertTrue(NfInstModel.objects.get(nfinstid=nf_inst_id).status, VNF_STATUS.ACTIVE)
140
141
142 class TestTerminateVnfViews(TestCase):
143     def setUp(self):
144         self.client = Client()
145         self.ns_inst_id = str(uuid.uuid4())
146         self.nf_inst_id = '1'
147         self.vnffg_id = str(uuid.uuid4())
148         self.vim_id = str(uuid.uuid4())
149         self.job_id = str(uuid.uuid4())
150         self.nf_uuid = '111'
151         self.tenant = "tenantname"
152         self.vnfd_model = {
153             "metadata": {
154                 "vnfdId": "1",
155                 "vnfdName": "PGW001",
156                 "vnfProvider": "zte",
157                 "vnfdVersion": "V00001",
158                 "vnfVersion": "V5.10.20",
159                 "productType": "CN",
160                 "vnfType": "PGW",
161                 "description": "PGW VNFD description",
162                 "isShared": True,
163                 "vnfExtendType": "driver"
164             }
165         }
166         NSInstModel.objects.all().delete()
167         NfInstModel.objects.all().delete()
168         VmInstModel.objects.all().delete()
169         NSInstModel(id=self.ns_inst_id, name="ns_name").save()
170         NfInstModel.objects.create(nfinstid=self.nf_inst_id,
171                                    nf_name='name_1',
172                                    vnf_id='1',
173                                    vnfm_inst_id='1',
174                                    ns_inst_id='111,2-2-2',
175                                    max_cpu='14',
176                                    max_ram='12296',
177                                    max_hd='101',
178                                    max_shd="20",
179                                    max_net=10,
180                                    status='active',
181                                    mnfinstid=self.nf_uuid,
182                                    package_id='pkg1',
183                                    vnfd_model=self.vnfd_model)
184         VmInstModel.objects.create(vmid="1",
185                                    vimid="zte_test",
186                                    resouceid="1",
187                                    insttype=INST_TYPE.VNF,
188                                    instid=self.nf_inst_id,
189                                    vmname="test",
190                                    hostid='1')
191
192     def tearDown(self):
193         NSInstModel.objects.all().delete()
194         NfInstModel.objects.all().delete()
195
196     @mock.patch.object(TerminateVnfs, 'run')
197     def test_terminate_vnf_url(self, mock_run):
198         req_data = {
199             "terminationType": "forceful",
200             "gracefulTerminationTimeout": "600"}
201
202         response = self.client.post("/api/nslcm/v1/ns/vnfs/%s" % self.nf_inst_id, data=req_data)
203         self.failUnlessEqual(status.HTTP_201_CREATED, response.status_code)
204
205     @mock.patch.object(restcall, 'call_req')
206     def test_terminate_vnf(self, mock_call_req):
207         job_id = JobUtil.create_job("VNF", JOB_TYPE.TERMINATE_VNF, self.nf_inst_id)
208
209         nfinst = NfInstModel.objects.filter(nfinstid=self.nf_inst_id)
210         if nfinst:
211             self.failUnlessEqual(1, 1)
212         else:
213             self.failUnlessEqual(1, 0)
214
215         vnf_info = {
216             "vnf-id": "vnf-id-test111",
217             "vnf-name": "vnf-name-test111",
218             "vnf-type": "vnf-type-test111",
219             "in-maint": True,
220             "is-closed-loop-disabled": False,
221             "resource-version": "1505465356262"
222         }
223         job_info = {
224             "jobId": job_id,
225             "responsedescriptor": {
226                 "progress": "100",
227                 "status": JOB_MODEL_STATUS.FINISHED,
228                 "responseid": "3",
229                 "statusdescription": "creating",
230                 "errorcode": "0",
231                 "responsehistorylist": [
232                     {
233                         "progress": "0",
234                         "status": JOB_MODEL_STATUS.PROCESSING,
235                         "responseid": "2",
236                         "statusdescription": "creating",
237                         "errorcode": "0"
238                     }
239                 ]
240             }
241         }
242
243         mock_vals = {
244             "/external-system/esr-vnfm-list/esr-vnfm/1?depth=all":
245                 [0, json.JSONEncoder().encode(vnfm_info), '200'],
246             "/api/ztevnfmdriver/v1/1/vnfs/111/terminate":
247                 [0, json.JSONEncoder().encode({"jobId": job_id}), '200'],
248             "/api/resmgr/v1/vnf/1":
249                 [0, json.JSONEncoder().encode({"jobId": job_id}), '200'],
250             "/cloud-infrastructure/cloud-regions/cloud-region/zte/test?depth=all":
251                 [0, json.JSONEncoder().encode(vim_info), '201'],
252             "/cloud-infrastructure/cloud-regions/cloud-region/zte/test/tenants/tenant/admin/vservers/vserver/1?depth=all":
253                 [0, json.JSONEncoder().encode(vserver_info), '201'],
254             "/cloud-infrastructure/cloud-regions/cloud-region/zte/test/tenants/tenant/admin/vservers/vserver/1?resource-version=1505465356263":
255                 [0, json.JSONEncoder().encode({}), '200'],
256             "/api/ztevnfmdriver/v1/1/jobs/" + job_id + "?responseId=0":
257                 [0, json.JSONEncoder().encode(job_info), '200'],
258             "/network/generic-vnfs/generic-vnf/1?depth=all":
259             [0, json.JSONEncoder().encode(vnf_info), '200'],
260             "/network/generic-vnfs/generic-vnf/1?resource-version=1505465356262":
261             [0, json.JSONEncoder().encode({}), '200']
262         }
263
264         def side_effect(*args):
265             return mock_vals[args[4]]
266         mock_call_req.side_effect = side_effect
267
268         req_data = {
269             "terminationType": "forceful",
270             "gracefulTerminationTimeout": "600"
271         }
272
273         TerminateVnfs(req_data, self.nf_inst_id, job_id).run()
274         nfinst = NfInstModel.objects.filter(nfinstid=self.nf_inst_id)
275         if nfinst:
276             self.failUnlessEqual(1, 0)
277         else:
278             self.failUnlessEqual(1, 1)
279
280
281 class TestScaleVnfViews(TestCase):
282     def setUp(self):
283         self.client = Client()
284         self.ns_inst_id = str(uuid.uuid4())
285         self.nf_inst_id = str(uuid.uuid4())
286         self.vnffg_id = str(uuid.uuid4())
287         self.vim_id = str(uuid.uuid4())
288         self.job_id = str(uuid.uuid4())
289         self.nf_uuid = '111'
290         self.tenant = "tenantname"
291         NSInstModel(id=self.ns_inst_id, name="ns_name").save()
292         NfInstModel.objects.create(nfinstid=self.nf_inst_id, nf_name='name_1', vnf_id='1',
293                                    vnfm_inst_id='1', ns_inst_id='111,2-2-2',
294                                    max_cpu='14', max_ram='12296', max_hd='101', max_shd="20", max_net=10,
295                                    status='active', mnfinstid=self.nf_uuid, package_id='pkg1',
296                                    vnfd_model='{"metadata": {"vnfdId": "1","vnfdName": "PGW001",'
297                                               '"vnfProvider": "zte","vnfdVersion": "V00001","vnfVersion": "V5.10.20",'
298                                               '"productType": "CN","vnfType": "PGW",'
299                                               '"description": "PGW VNFD description",'
300                                               '"isShared":true,"vnfExtendType":"driver"}}')
301
302     def tearDown(self):
303         NSInstModel.objects.all().delete()
304         NfInstModel.objects.all().delete()
305
306     def test_scale_vnf(self):
307         vnfd_info = {
308             "vnf_flavours": [{
309                 "flavour_id": "flavour1",
310                 "description": "",
311                 "vdu_profiles": [
312                     {
313                         "vdu_id": "vdu1Id",
314                         "instances_minimum_number": 1,
315                         "instances_maximum_number": 4,
316                         "local_affinity_antiaffinity_rule": [
317                             {
318                                 "affinity_antiaffinity": "affinity",
319                                 "scope": "node",
320                             }
321                         ]
322                     }
323                 ],
324                 "scaling_aspects": [
325                     {
326                         "id": "demo_aspect",
327                         "name": "demo_aspect",
328                         "description": "demo_aspect",
329                         "associated_group": "elementGroup1",
330                         "max_scale_level": 5
331                     }
332                 ]
333             }],
334             "element_groups": [{
335                 "group_id": "elementGroup1",
336                 "description": "",
337                 "properties": {
338                     "name": "elementGroup1",
339                 },
340                 "members": ["gsu_vm", "pfu_vm"]
341             }]
342         }
343
344         req_data = {
345             "scaleVnfData": [
346                 {
347                     "type": "SCALE_OUT",
348                     "aspectId": "demo_aspect1",
349                     "numberOfSteps": 1,
350                     "additionalParam": vnfd_info
351                 },
352                 {
353                     "type": "SCALE_OUT",
354                     "aspectId": "demo_aspect2",
355                     "numberOfSteps": 1,
356                     "additionalParam": vnfd_info
357                 }
358             ]
359         }
360
361         NFManualScaleService(self.nf_inst_id, req_data).run()
362         nsIns = NfInstModel.objects.filter(nfinstid=self.nf_inst_id)
363         self.assertIsNotNone(nsIns)
364
365     @mock.patch.object(NFManualScaleService, "send_nf_scaling_request")
366     def test_scale_vnf_success(self, mock_send_nf_scaling_request):
367         vnfd_info = {
368             "vnf_flavours": [{
369                 "flavour_id": "flavour1",
370                 "description": "",
371                 "vdu_profiles": [
372                     {
373                         "vdu_id": "vdu1Id",
374                         "instances_minimum_number": 1,
375                         "instances_maximum_number": 4,
376                         "local_affinity_antiaffinity_rule": [
377                             {
378                                 "affinity_antiaffinity": "affinity",
379                                 "scope": "node",
380                             }
381                         ]
382                     }
383                 ],
384                 "scaling_aspects": [
385                     {
386                         "id": "demo_aspect",
387                         "name": "demo_aspect",
388                         "description": "demo_aspect",
389                         "associated_group": "elementGroup1",
390                         "max_scale_level": 5
391                     }
392                 ]
393             }],
394             "element_groups": [{
395                 "group_id": "elementGroup1",
396                 "description": "",
397                 "properties": {
398                     "name": "elementGroup1",
399                 },
400                 "members": ["gsu_vm", "pfu_vm"]
401             }]
402         }
403
404         req_data = {
405             "scaleVnfData": [
406                 {
407                     "type": "SCALE_OUT",
408                     "aspectId": "demo_aspect1",
409                     "numberOfSteps": 1,
410                     "additionalParam": vnfd_info
411                 },
412                 {
413                     "type": "SCALE_OUT",
414                     "aspectId": "demo_aspect2",
415                     "numberOfSteps": 1,
416                     "additionalParam": vnfd_info
417                 }
418             ]
419         }
420         scale_service = NFManualScaleService(self.nf_inst_id, req_data)
421         scale_service.run()
422         nsIns = NfInstModel.objects.filter(nfinstid=self.nf_inst_id)
423         self.assertIsNotNone(nsIns)
424
425         jobs = JobModel.objects.filter(jobid=scale_service.job_id)
426         self.assertIsNotNone(100, jobs[0].progress)
427
428
429 class TestHealVnfViews(TestCase):
430     def setUp(self):
431         self.client = Client()
432         self.ns_inst_id = str(uuid.uuid4())
433         self.nf_inst_id = str(uuid.uuid4())
434         self.nf_uuid = '111'
435
436         self.job_id = JobUtil.create_job("VNF", JOB_TYPE.HEAL_VNF, self.nf_inst_id)
437
438         NSInstModel(id=self.ns_inst_id, name="ns_name").save()
439         NfInstModel.objects.create(nfinstid=self.nf_inst_id,
440                                    nf_name='name_1',
441                                    vnf_id='1',
442                                    vnfm_inst_id='1',
443                                    ns_inst_id='111,2-2-2',
444                                    max_cpu='14',
445                                    max_ram='12296',
446                                    max_hd='101',
447                                    max_shd="20",
448                                    max_net=10,
449                                    status='active',
450                                    mnfinstid=self.nf_uuid,
451                                    package_id='pkg1',
452                                    vnfd_model=json.dumps({
453                                        "metadata": {
454                                            "vnfdId": "1",
455                                            "vnfdName": "PGW001",
456                                            "vnfProvider": "zte",
457                                            "vnfdVersion": "V00001",
458                                            "vnfVersion": "V5.10.20",
459                                            "productType": "CN",
460                                            "vnfType": "PGW",
461                                            "description": "PGW VNFD description",
462                                            "isShared": True,
463                                            "vnfExtendType": "driver"
464                                        }
465                                    }))
466
467     def tearDown(self):
468         NSInstModel.objects.all().delete()
469         NfInstModel.objects.all().delete()
470
471     @mock.patch.object(restcall, "call_req")
472     def test_heal_vnf(self, mock_call_req):
473
474         mock_vals = {
475             "/api/ztevnfmdriver/v1/1/vnfs/111/heal":
476                 [0, json.JSONEncoder().encode({"jobId": self.job_id}), '200'],
477             "/external-system/esr-vnfm-list/esr-vnfm/1":
478                 [0, json.JSONEncoder().encode(vnfm_info), '200'],
479             "/api/resmgr/v1/vnf/1":
480                 [0, json.JSONEncoder().encode({"jobId": self.job_id}), '200'],
481             "/api/ztevnfmdriver/v1/1/jobs/" + self.job_id + "?responseId=0":
482                 [0, json.JSONEncoder().encode({
483                     "jobId": self.job_id,
484                     "responsedescriptor": {
485                         "progress": "100",
486                         "status": JOB_MODEL_STATUS.FINISHED,
487                         "responseid": "3",
488                         "statusdescription": "creating",
489                         "errorcode": "0",
490                         "responsehistorylist": [{
491                             "progress": "0",
492                             "status": JOB_MODEL_STATUS.PROCESSING,
493                             "responseid": "2",
494                             "statusdescription": "creating",
495                             "errorcode": "0"
496                         }]
497                     }
498                 }), '200']}
499
500         def side_effect(*args):
501             return mock_vals[args[4]]
502
503         mock_call_req.side_effect = side_effect
504
505         req_data = {
506             "action": "vmReset",
507             "affectedvm": {
508                 "vmid": "1",
509                 "vduid": "1",
510                 "vmname": "name",
511             }
512         }
513
514         NFHealService(self.nf_inst_id, req_data).run()
515
516         self.assertEqual(NfInstModel.objects.get(nfinstid=self.nf_inst_id).status, VNF_STATUS.ACTIVE)
517
518     @mock.patch.object(NFHealService, "run")
519     def test_heal_vnf_non_existing_vnf(self, mock_biz):
520         mock_biz.side_effect = NSLCMException("VNF Not Found")
521
522         nf_inst_id = "1"
523
524         req_data = {
525             "action": "vmReset",
526             "affectedvm": {
527                 "vmid": "1",
528                 "vduid": "1",
529                 "vmname": "name",
530             }
531         }
532
533         self.assertRaises(NSLCMException, NFHealService(nf_inst_id, req_data).run)
534         self.assertEqual(len(NfInstModel.objects.filter(nfinstid=nf_inst_id)), 0)
535
536
537 class TestGetVnfmInfoViews(TestCase):
538     def setUp(self):
539         self.client = Client()
540         self.vnfm_id = str(uuid.uuid4())
541
542     def tearDown(self):
543         pass
544
545     @mock.patch.object(restcall, "call_req")
546     def test_get_vnfm_info(self, mock_call_req):
547         vnfm_info_aai = {
548             "vnfm-id": "example-vnfm-id-val-62576",
549             "vim-id": "example-vim-id-val-35114",
550             "certificate-url": "example-certificate-url-val-90242",
551             "esr-system-info-list": {
552                 "esr-system-info": [
553                     {
554                         "esr-system-info-id": "example-esr-system-info-id-val-78484",
555                         "system-name": "example-system-name-val-23790",
556                         "type": "example-type-val-52596",
557                         "vendor": "example-vendor-val-47399",
558                         "version": "example-version-val-42051",
559                         "service-url": "example-service-url-val-10731",
560                         "user-name": "example-user-name-val-65946",
561                         "password": "example-password-val-22505",
562                         "system-type": "example-system-type-val-27221",
563                         "protocal": "example-protocal-val-54632",
564                         "ssl-cacert": "example-ssl-cacert-val-45965",
565                         "ssl-insecure": True,
566                         "ip-address": "example-ip-address-val-19212",
567                         "port": "example-port-val-57641",
568                         "cloud-domain": "example-cloud-domain-val-26296",
569                         "default-tenant": "example-default-tenant-val-87724"
570                     }
571                 ]
572             }
573         }
574         r1 = [0, json.JSONEncoder().encode(vnfm_info_aai), '200']
575         mock_call_req.side_effect = [r1]
576         esr_system_info = ignore_case_get(ignore_case_get(vnfm_info_aai, "esr-system-info-list"), "esr-system-info")
577         expect_data = {
578             "vnfmId": vnfm_info_aai["vnfm-id"],
579             "name": vnfm_info_aai["vnfm-id"],
580             "type": ignore_case_get(esr_system_info[0], "type"),
581             "vimId": vnfm_info_aai["vim-id"],
582             "vendor": ignore_case_get(esr_system_info[0], "vendor"),
583             "version": ignore_case_get(esr_system_info[0], "version"),
584             "description": "vnfm",
585             "certificateUrl": vnfm_info_aai["certificate-url"],
586             "url": ignore_case_get(esr_system_info[0], "service-url"),
587             "userName": ignore_case_get(esr_system_info[0], "user-name"),
588             "password": ignore_case_get(esr_system_info[0], "password"),
589             "createTime": ""
590         }
591
592         response = self.client.get("/api/nslcm/v1/vnfms/%s" % self.vnfm_id)
593         self.failUnlessEqual(status.HTTP_200_OK, response.status_code, response.content)
594         context = json.loads(response.content)
595         self.assertEqual(expect_data, context)
596
597
598 class TestGetVimInfoViews(TestCase):
599     def setUp(self):
600         self.client = Client()
601         self.vim_id = "zte_test"
602
603     def tearDown(self):
604         pass
605
606     @mock.patch.object(restcall, "call_req")
607     def test_get_vim_info(self, mock_call_req):
608         r1 = [0, json.JSONEncoder().encode(vim_info), '200']
609         mock_call_req.side_effect = [r1]
610         esr_system_info = ignore_case_get(ignore_case_get(vim_info, "esr-system-info-list"), "esr-system-info")
611         expect_data = {
612             "vimId": self.vim_id,
613             "name": self.vim_id,
614             "url": ignore_case_get(esr_system_info[0], "service-url"),
615             "userName": ignore_case_get(esr_system_info[0], "user-name"),
616             "password": ignore_case_get(esr_system_info[0], "password"),
617             # "tenant": ignore_case_get(tenants[0], "tenant-id"),
618             "tenant": ignore_case_get(esr_system_info[0], "default-tenant"),
619             "vendor": ignore_case_get(esr_system_info[0], "vendor"),
620             "version": ignore_case_get(esr_system_info[0], "version"),
621             "description": "vim",
622             "domain": "",
623             "type": ignore_case_get(esr_system_info[0], "type"),
624             "createTime": ""
625         }
626
627         response = self.client.get("/api/nslcm/v1/vims/%s" % self.vim_id)
628         self.failUnlessEqual(status.HTTP_200_OK, response.status_code)
629         context = json.loads(response.content)
630         self.assertEqual(expect_data["url"], context["url"])
631
632
633 vnfd_model_dict = {
634     'local_storages': [],
635     'vdus': [
636         {
637             'volumn_storages': [
638
639             ],
640             'nfv_compute': {
641                 'mem_size': '',
642                 'num_cpus': u'2'
643             },
644             'local_storages': [
645
646             ],
647             'vdu_id': u'vdu_omm.001',
648             'image_file': u'opencos_sss_omm_img_release_20150723-1-disk1',
649             'dependencies': [
650
651             ],
652             'vls': [
653
654             ],
655             'cps': [
656
657             ],
658             'properties': {
659                 'key_vdu': '',
660                 'support_scaling': False,
661                 'vdu_type': '',
662                 'name': '',
663                 'storage_policy': '',
664                 'location_info': {
665                     'vimId': '',
666                     'availability_zone': '',
667                     'region': '',
668                     'dc': '',
669                     'host': '',
670                     'tenant': ''
671                 },
672                 'inject_data_list': [
673
674                 ],
675                 'watchdog': {
676                     'action': '',
677                     'enabledelay': ''
678                 },
679                 'local_affinity_antiaffinity_rule': {
680
681                 },
682                 'template_id': u'omm.001',
683                 'manual_scale_select_vim': False
684             },
685             'description': u'singleommvm'
686         },
687         {
688             'volumn_storages': [
689
690             ],
691             'nfv_compute': {
692                 'mem_size': '',
693                 'num_cpus': u'4'
694             },
695             'local_storages': [
696
697             ],
698             'vdu_id': u'vdu_1',
699             'image_file': u'sss',
700             'dependencies': [
701
702             ],
703             'vls': [
704
705             ],
706             'cps': [
707
708             ],
709             'properties': {
710                 'key_vdu': '',
711                 'support_scaling': False,
712                 'vdu_type': '',
713                 'name': '',
714                 'storage_policy': '',
715                 'location_info': {
716                     'vimId': '',
717                     'availability_zone': '',
718                     'region': '',
719                     'dc': '',
720                     'host': '',
721                     'tenant': ''
722                 },
723                 'inject_data_list': [
724
725                 ],
726                 'watchdog': {
727                     'action': '',
728                     'enabledelay': ''
729                 },
730                 'local_affinity_antiaffinity_rule': {
731
732                 },
733                 'template_id': u'1',
734                 'manual_scale_select_vim': False
735             },
736             'description': u'ompvm'
737         },
738         {
739             'volumn_storages': [
740
741             ],
742             'nfv_compute': {
743                 'mem_size': '',
744                 'num_cpus': u'14'
745             },
746             'local_storages': [
747
748             ],
749             'vdu_id': u'vdu_2',
750             'image_file': u'sss',
751             'dependencies': [
752
753             ],
754             'vls': [
755
756             ],
757             'cps': [
758
759             ],
760             'properties': {
761                 'key_vdu': '',
762                 'support_scaling': False,
763                 'vdu_type': '',
764                 'name': '',
765                 'storage_policy': '',
766                 'location_info': {
767                     'vimId': '',
768                     'availability_zone': '',
769                     'region': '',
770                     'dc': '',
771                     'host': '',
772                     'tenant': ''
773                 },
774                 'inject_data_list': [
775
776                 ],
777                 'watchdog': {
778                     'action': '',
779                     'enabledelay': ''
780                 },
781                 'local_affinity_antiaffinity_rule': {
782
783                 },
784                 'template_id': u'2',
785                 'manual_scale_select_vim': False
786             },
787             'description': u'ompvm'
788         },
789         {
790             'volumn_storages': [
791
792             ],
793             'nfv_compute': {
794                 'mem_size': '',
795                 'num_cpus': u'14'
796             },
797             'local_storages': [
798
799             ],
800             'vdu_id': u'vdu_3',
801             'image_file': u'sss',
802             'dependencies': [
803
804             ],
805             'vls': [
806
807             ],
808             'cps': [
809
810             ],
811             'properties': {
812                 'key_vdu': '',
813                 'support_scaling': False,
814                 'vdu_type': '',
815                 'name': '',
816                 'storage_policy': '',
817                 'location_info': {
818                     'vimId': '',
819                     'availability_zone': '',
820                     'region': '',
821                     'dc': '',
822                     'host': '',
823                     'tenant': ''
824                 },
825                 'inject_data_list': [
826
827                 ],
828                 'watchdog': {
829                     'action': '',
830                     'enabledelay': ''
831                 },
832                 'local_affinity_antiaffinity_rule': {
833
834                 },
835                 'template_id': u'3',
836                 'manual_scale_select_vim': False
837             },
838             'description': u'ompvm'
839         },
840         {
841             'volumn_storages': [
842
843             ],
844             'nfv_compute': {
845                 'mem_size': '',
846                 'num_cpus': u'4'
847             },
848             'local_storages': [
849
850             ],
851             'vdu_id': u'vdu_10',
852             'image_file': u'sss',
853             'dependencies': [
854
855             ],
856             'vls': [
857
858             ],
859             'cps': [
860
861             ],
862             'properties': {
863                 'key_vdu': '',
864                 'support_scaling': False,
865                 'vdu_type': '',
866                 'name': '',
867                 'storage_policy': '',
868                 'location_info': {
869                     'vimId': '',
870                     'availability_zone': '',
871                     'region': '',
872                     'dc': '',
873                     'host': '',
874                     'tenant': ''
875                 },
876                 'inject_data_list': [
877
878                 ],
879                 'watchdog': {
880                     'action': '',
881                     'enabledelay': ''
882                 },
883                 'local_affinity_antiaffinity_rule': {
884
885                 },
886                 'template_id': u'10',
887                 'manual_scale_select_vim': False
888             },
889             'description': u'ppvm'
890         },
891         {
892             'volumn_storages': [
893
894             ],
895             'nfv_compute': {
896                 'mem_size': '',
897                 'num_cpus': u'14'
898             },
899             'local_storages': [
900
901             ],
902             'vdu_id': u'vdu_11',
903             'image_file': u'sss',
904             'dependencies': [
905
906             ],
907             'vls': [
908
909             ],
910             'cps': [
911
912             ],
913             'properties': {
914                 'key_vdu': '',
915                 'support_scaling': False,
916                 'vdu_type': '',
917                 'name': '',
918                 'storage_policy': '',
919                 'location_info': {
920                     'vimId': '',
921                     'availability_zone': '',
922                     'region': '',
923                     'dc': '',
924                     'host': '',
925                     'tenant': ''
926                 },
927                 'inject_data_list': [
928
929                 ],
930                 'watchdog': {
931                     'action': '',
932                     'enabledelay': ''
933                 },
934                 'local_affinity_antiaffinity_rule': {
935
936                 },
937                 'template_id': u'11',
938                 'manual_scale_select_vim': False
939             },
940             'description': u'ppvm'
941         },
942         {
943             'volumn_storages': [
944
945             ],
946             'nfv_compute': {
947                 'mem_size': '',
948                 'num_cpus': u'14'
949             },
950             'local_storages': [
951
952             ],
953             'vdu_id': u'vdu_12',
954             'image_file': u'sss',
955             'dependencies': [
956
957             ],
958             'vls': [
959
960             ],
961             'cps': [
962
963             ],
964             'properties': {
965                 'key_vdu': '',
966                 'support_scaling': False,
967                 'vdu_type': '',
968                 'name': '',
969                 'storage_policy': '',
970                 'location_info': {
971                     'vimId': '',
972                     'availability_zone': '',
973                     'region': '',
974                     'dc': '',
975                     'host': '',
976                     'tenant': ''
977                 },
978                 'inject_data_list': [
979
980                 ],
981                 'watchdog': {
982                     'action': '',
983                     'enabledelay': ''
984                 },
985                 'local_affinity_antiaffinity_rule': {
986
987                 },
988                 'template_id': u'12',
989                 'manual_scale_select_vim': False
990             },
991             'description': u'ppvm'
992         }
993     ],
994     'volumn_storages': [
995
996     ],
997     'policies': {
998         'scaling': {
999             'targets': {
1000
1001             },
1002             'policy_id': u'policy_scale_sss-vnf-template',
1003             'properties': {
1004                 'policy_file': '*-vnfd.zip/*-vnf-policy.xml'
1005             },
1006             'description': ''
1007         }
1008     },
1009     'image_files': [
1010         {
1011             'description': '',
1012             'properties': {
1013                 'name': u'opencos_sss_omm_img_release_20150723-1-disk1.vmdk',
1014                 'checksum': '',
1015                 'disk_format': u'VMDK',
1016                 'file_url': u'./zte-cn-sss-main-image/OMM/opencos_sss_omm_img_release_20150723-1-disk1.vmdk',
1017                 'container_type': 'vm',
1018                 'version': '',
1019                 'hypervisor_type': 'kvm'
1020             },
1021             'image_file_id': u'opencos_sss_omm_img_release_20150723-1-disk1'
1022         },
1023         {
1024             'description': '',
1025             'properties': {
1026                 'name': u'sss.vmdk',
1027                 'checksum': '',
1028                 'disk_format': u'VMDK',
1029                 'file_url': u'./zte-cn-sss-main-image/NE/sss.vmdk',
1030                 'container_type': 'vm',
1031                 'version': '',
1032                 'hypervisor_type': 'kvm'
1033             },
1034             'image_file_id': u'sss'
1035         }
1036     ],
1037     'vls': [
1038
1039     ],
1040     'cps': [
1041
1042     ],
1043     'metadata': {
1044         'vendor': u'zte',
1045         'is_shared': False,
1046         'description': '',
1047         'domain_type': u'CN',
1048         'version': u'v4.14.10',
1049         'vmnumber_overquota_alarm': False,
1050         'cross_dc': False,
1051         'vnf_type': u'SSS',
1052         'vnfd_version': u'V00000001',
1053         'id': u'sss-vnf-template',
1054         'name': u'sss-vnf-template'
1055     }
1056 }
1057
1058 nsd_model_dict = {
1059     "vnffgs": [
1060
1061     ],
1062     "inputs": {
1063         "externalDataNetworkName": {
1064             "default": "",
1065             "type": "string",
1066             "description": ""
1067         }
1068     },
1069     "pnfs": [
1070
1071     ],
1072     "fps": [
1073
1074     ],
1075     "server_groups": [
1076
1077     ],
1078     "ns_flavours": [
1079
1080     ],
1081     "vnfs": [
1082         {
1083             "dependency": [
1084
1085             ],
1086             "properties": {
1087                 "plugin_info": "vbrasplugin_1.0",
1088                 "vendor": "zte",
1089                 "is_shared": "False",
1090                 "request_reclassification": "False",
1091                 "vnfd_version": "1.0.0",
1092                 "version": "1.0",
1093                 "nsh_aware": "True",
1094                 "cross_dc": "False",
1095                 "externalDataNetworkName": {
1096                     "get_input": "externalDataNetworkName"
1097                 },
1098                 "id": "zte_vbras",
1099                 "name": "vbras"
1100             },
1101             "vnf_id": "VBras",
1102             "networks": [
1103
1104             ],
1105             "description": ""
1106         }
1107     ],
1108     "ns_exposed": {
1109         "external_cps": [
1110
1111         ],
1112         "forward_cps": [
1113
1114         ]
1115     },
1116     "vls": [
1117         {
1118             "vl_id": "ext_mnet_network",
1119             "description": "",
1120             "properties": {
1121                 "network_type": "vlan",
1122                 "name": "externalMNetworkName",
1123                 "dhcp_enabled": False,
1124                 "location_info": {
1125                     "host": True,
1126                     "vimid": 2,
1127                     "region": True,
1128                     "tenant": "admin",
1129                     "dc": ""
1130                 },
1131                 "end_ip": "190.168.100.100",
1132                 "gateway_ip": "190.168.100.1",
1133                 "start_ip": "190.168.100.2",
1134                 "cidr": "190.168.100.0/24",
1135                 "mtu": 1500,
1136                 "network_name": "sub_mnet",
1137                 "ip_version": 4
1138             }
1139         }
1140     ],
1141     "cps": [
1142
1143     ],
1144     "policies": [
1145
1146     ],
1147     "metadata": {
1148         "invariant_id": "vbras_ns",
1149         "description": "vbras_ns",
1150         "version": 1,
1151         "vendor": "zte",
1152         "id": "vbras_ns",
1153         "name": "vbras_ns"
1154     }
1155 }
1156
1157 vserver_info = {
1158     "vserver-id": "example-vserver-id-val-70924",
1159     "vserver-name": "example-vserver-name-val-61674",
1160     "vserver-name2": "example-vserver-name2-val-19234",
1161     "prov-status": "example-prov-status-val-94916",
1162     "vserver-selflink": "example-vserver-selflink-val-26562",
1163     "in-maint": True,
1164     "is-closed-loop-disabled": True,
1165     "resource-version": "1505465356263",
1166     "volumes": {
1167         "volume": [
1168             {
1169                 "volume-id": "example-volume-id-val-71854",
1170                 "volume-selflink": "example-volume-selflink-val-22433"
1171             }
1172         ]
1173     },
1174     "l-interfaces": {
1175         "l-interface": [
1176             {
1177                 "interface-name": "example-interface-name-val-24351",
1178                 "interface-role": "example-interface-role-val-43242",
1179                 "v6-wan-link-ip": "example-v6-wan-link-ip-val-4196",
1180                 "selflink": "example-selflink-val-61295",
1181                 "interface-id": "example-interface-id-val-95879",
1182                 "macaddr": "example-macaddr-val-37302",
1183                 "network-name": "example-network-name-val-44254",
1184                 "management-option": "example-management-option-val-49009",
1185                 "interface-description": "example-interface-description-val-99923",
1186                 "is-port-mirrored": True,
1187                 "in-maint": True,
1188                 "prov-status": "example-prov-status-val-4698",
1189                 "is-ip-unnumbered": True,
1190                 "allowed-address-pairs": "example-allowed-address-pairs-val-5762",
1191                 "vlans": {
1192                     "vlan": [
1193                         {
1194                             "vlan-interface": "example-vlan-interface-val-58193",
1195                             "vlan-id-inner": 54452151,
1196                             "vlan-id-outer": 70239293,
1197                             "speed-value": "example-speed-value-val-18677",
1198                             "speed-units": "example-speed-units-val-46185",
1199                             "vlan-description": "example-vlan-description-val-81675",
1200                             "backdoor-connection": "example-backdoor-connection-val-44608",
1201                             "vpn-key": "example-vpn-key-val-7946",
1202                             "orchestration-status": "example-orchestration-status-val-33611",
1203                             "in-maint": True,
1204                             "prov-status": "example-prov-status-val-8288",
1205                             "is-ip-unnumbered": True,
1206                             "l3-interface-ipv4-address-list": [
1207                                 {
1208                                     "l3-interface-ipv4-address": "example-l3-interface-ipv4-address-val-25520",
1209                                     "l3-interface-ipv4-prefix-length": 69931928,
1210                                     "vlan-id-inner": 86628520,
1211                                     "vlan-id-outer": 62729236,
1212                                     "is-floating": True,
1213                                     "neutron-network-id": "example-neutron-network-id-val-64021",
1214                                     "neutron-subnet-id": "example-neutron-subnet-id-val-95049"
1215                                 }
1216                             ],
1217                             "l3-interface-ipv6-address-list": [
1218                                 {
1219                                     "l3-interface-ipv6-address": "example-l3-interface-ipv6-address-val-64310",
1220                                     "l3-interface-ipv6-prefix-length": 57919834,
1221                                     "vlan-id-inner": 79150122,
1222                                     "vlan-id-outer": 59789973,
1223                                     "is-floating": True,
1224                                     "neutron-network-id": "example-neutron-network-id-val-31713",
1225                                     "neutron-subnet-id": "example-neutron-subnet-id-val-89568"
1226                                 }
1227                             ]
1228                         }
1229                     ]
1230                 },
1231                 "sriov-vfs": {
1232                     "sriov-vf": [
1233                         {
1234                             "pci-id": "example-pci-id-val-16747",
1235                             "vf-vlan-filter": "example-vf-vlan-filter-val-4613",
1236                             "vf-mac-filter": "example-vf-mac-filter-val-68168",
1237                             "vf-vlan-strip": True,
1238                             "vf-vlan-anti-spoof-check": True,
1239                             "vf-mac-anti-spoof-check": True,
1240                             "vf-mirrors": "example-vf-mirrors-val-6270",
1241                             "vf-broadcast-allow": True,
1242                             "vf-unknown-multicast-allow": True,
1243                             "vf-unknown-unicast-allow": True,
1244                             "vf-insert-stag": True,
1245                             "vf-link-status": "example-vf-link-status-val-49266",
1246                             "neutron-network-id": "example-neutron-network-id-val-29493"
1247                         }
1248                     ]
1249                 },
1250                 "l-interfaces": {
1251                     "l-interface": [
1252                         {
1253                             "interface-name": "example-interface-name-val-98222",
1254                             "interface-role": "example-interface-role-val-78360",
1255                             "v6-wan-link-ip": "example-v6-wan-link-ip-val-76921",
1256                             "selflink": "example-selflink-val-27117",
1257                             "interface-id": "example-interface-id-val-11260",
1258                             "macaddr": "example-macaddr-val-60378",
1259                             "network-name": "example-network-name-val-16258",
1260                             "management-option": "example-management-option-val-35097",
1261                             "interface-description": "example-interface-description-val-10475",
1262                             "is-port-mirrored": True,
1263                             "in-maint": True,
1264                             "prov-status": "example-prov-status-val-65203",
1265                             "is-ip-unnumbered": True,
1266                             "allowed-address-pairs": "example-allowed-address-pairs-val-65028"
1267                         }
1268                     ]
1269                 },
1270                 "l3-interface-ipv4-address-list": [
1271                     {
1272                         "l3-interface-ipv4-address": "example-l3-interface-ipv4-address-val-72779",
1273                         "l3-interface-ipv4-prefix-length": 55956636,
1274                         "vlan-id-inner": 98174431,
1275                         "vlan-id-outer": 20372128,
1276                         "is-floating": True,
1277                         "neutron-network-id": "example-neutron-network-id-val-39596",
1278                         "neutron-subnet-id": "example-neutron-subnet-id-val-51109"
1279                     }
1280                 ],
1281                 "l3-interface-ipv6-address-list": [
1282                     {
1283                         "l3-interface-ipv6-address": "example-l3-interface-ipv6-address-val-95203",
1284                         "l3-interface-ipv6-prefix-length": 57454747,
1285                         "vlan-id-inner": 53421060,
1286                         "vlan-id-outer": 16006050,
1287                         "is-floating": True,
1288                         "neutron-network-id": "example-neutron-network-id-val-54216",
1289                         "neutron-subnet-id": "example-neutron-subnet-id-val-1841"
1290                     }
1291                 ]
1292             }
1293         ]
1294     }
1295 }
1296
1297
1298 vnfm_info = {
1299     "vnfm-id": "example-vnfm-id-val-97336",
1300     "vim-id": "zte_test",
1301     "certificate-url": "example-certificate-url-val-18046",
1302     "resource-version": "example-resource-version-val-42094",
1303     "esr-system-info-list": {
1304         "esr-system-info": [
1305             {
1306                 "esr-system-info-id": "example-esr-system-info-id-val-7713",
1307                 "system-name": "example-system-name-val-19801",
1308                 "type": "ztevnfmdriver",
1309                 "vendor": "example-vendor-val-50079",
1310                 "version": "example-version-val-93146",
1311                 "service-url": "example-service-url-val-68090",
1312                 "user-name": "example-user-name-val-14470",
1313                 "password": "example-password-val-84190",
1314                 "system-type": "example-system-type-val-42773",
1315                 "protocal": "example-protocal-val-85736",
1316                 "ssl-cacert": "example-ssl-cacert-val-33989",
1317                 "ssl-insecure": True,
1318                 "ip-address": "example-ip-address-val-99038",
1319                 "port": "example-port-val-27323",
1320                 "cloud-domain": "example-cloud-domain-val-55163",
1321                 "default-tenant": "example-default-tenant-val-99383",
1322                 "resource-version": "example-resource-version-val-15424"
1323             }
1324         ]
1325     }
1326 }
1327
1328 vim_info = {
1329     "cloud-owner": "example-cloud-owner-val-97336",
1330     "cloud-region-id": "example-cloud-region-id-val-35532",
1331     "cloud-type": "example-cloud-type-val-18046",
1332     "owner-defined-type": "example-owner-defined-type-val-9413",
1333     "cloud-region-version": "example-cloud-region-version-val-85706",
1334     "identity-url": "example-identity-url-val-71252",
1335     "cloud-zone": "example-cloud-zone-val-27112",
1336     "complex-name": "example-complex-name-val-85283",
1337     "sriov-automation": True,
1338     "cloud-extra-info": "example-cloud-extra-info-val-90854",
1339     "cloud-epa-caps": "example-cloud-epa-caps-val-2409",
1340     "resource-version": "example-resource-version-val-42094",
1341     "esr-system-info-list": {
1342         "esr-system-info": [
1343             {
1344                 "esr-system-info-id": "example-esr-system-info-id-val-7713",
1345                 "system-name": "example-system-name-val-19801",
1346                 "type": "example-type-val-24477",
1347                 "vendor": "example-vendor-val-50079",
1348                 "version": "example-version-val-93146",
1349                 "service-url": "example-service-url-val-68090",
1350                 "user-name": "example-user-name-val-14470",
1351                 "password": "example-password-val-84190",
1352                 "system-type": "example-system-type-val-42773",
1353                 "protocal": "example-protocal-val-85736",
1354                 "ssl-cacert": "example-ssl-cacert-val-33989",
1355                 "ssl-insecure": True,
1356                 "ip-address": "example-ip-address-val-99038",
1357                 "port": "example-port-val-27323",
1358                 "cloud-domain": "example-cloud-domain-val-55163",
1359                 "default-tenant": "admin",
1360                 "resource-version": "example-resource-version-val-15424"
1361             }
1362         ]
1363     }
1364 }
1365
1366 nf_package_info = {
1367     "csarId": "zte_vbras",
1368     "packageInfo": {
1369         "vnfdId": "1",
1370         "vnfPackageId": "zte_vbras",
1371         "vnfdProvider": "1",
1372         "vnfdVersion": "1",
1373         "vnfVersion": "1",
1374         "csarName": "1",
1375         "vnfdModel": vnfd_model_dict,
1376         "downloadUrl": "1"
1377     },
1378     "imageInfo": []
1379 }