1 # Copyright 2017 ZTE Corporation.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 from django.test import Client
18 from django.test import TestCase
19 from rest_framework import status
20 from driver.pub.utils import restcall
23 class InterfacesTest(TestCase):
25 self.client = Client()
30 @mock.patch.object(restcall, 'call_req')
31 def test_instantiate_vnf(self, mock_call_req):
35 'name': 'ZTE_VNFM_237_62',
36 'vimId': '516cee95-e8ca-4d26-9268-38e343c2e31e',
37 'url': 'http: //192.168.237.165: 2324',
40 'vnfmId': 'b0797c9b-3da9-459c-b25c-3813e9d8fd70',
42 'type': 'gvnfmdriver',
43 'createTime': '2016-10-3111: 08: 39',
48 # "jobId": "NF-CREATE-8-b384535c-9f45-11e6-8749-fa163e91c2f9"
51 "jobId": "NF-INST-8-6ffa8083-6705-49b3-ae54-cbd6265fbe7a"
53 r1 = [0, json.JSONEncoder().encode(vnfm_info), "200"]
54 ret = [0, json.JSONEncoder().encode(create_vnf_resp), '200']
55 ret2 = [0, json.JSONEncoder().encode(job_info), '200']
56 mock_call_req.side_effect = [r1, ret, r1, ret2]
58 'vnfInstanceName': 'VFW_f88c0cb7-512a-44c4-bd09-891663f19367',
59 'vnfPackageId': 'd852e1be-0aac-48f1-b1a4-cd825f6cdf9a',
60 'vnfDescriptorId': 'vcpe_vfw_zte_1_0',
62 'sdncontroller': 'e4d637f1-a4ec-4c59-8b20-4e8ab34daba9',
63 'NatIpRange': '192.167.0.10-192.168.0.20',
64 'm6000_mng_ip': '192.168.11.11',
65 'externalPluginManageNetworkName': 'plugin_net_2014',
66 'location': '516cee95-e8ca-4d26-9268-38e343c2e31e',
67 'externalManageNetworkName': 'mng_net_2017',
68 'sfc_data_network': 'sfc_data_net_2016',
69 'externalDataNetworkName': 'Flow_out_net',
73 response = self.client.post("/api/gvnfmdriver/v1/1/vnfs",
74 data=json.dumps(req_data), content_type="application/json")
75 self.assertEqual(status.HTTP_201_CREATED, response.status_code)
78 "jobId": "NF-INST-8-6ffa8083-6705-49b3-ae54-cbd6265fbe7a"
80 self.assertEqual(expect_data, response.data)
82 @mock.patch.object(restcall, 'call_req')
83 def test_terminate_vnf(self, mock_call_req):
85 "vnfmId": "19ecbb3a-3242-4fa3-9926-8dfb7ddc29ee",
87 "type": "gvnfmdriver",
91 "description": "vnfm",
93 "url": "http://10.74.44.11",
96 "createTime": "2016-07-06 15:33:18"
98 job_info = {"vnfInstanceId": "1", "vnfLcOpId": "1"}
101 "responseDescriptor": {
104 "statusDescription": "",
107 "responseHistoryList": [
111 "statusDescription": "",
118 r1 = [0, json.JSONEncoder().encode(vnfm_info), "200"]
119 r2 = [0, json.JSONEncoder().encode(job_info), "200"]
120 job_ret = [0, json.JSONEncoder().encode(job_status_info), "200"]
121 r3 = [0, json.JSONEncoder().encode(None), "200"]
122 mock_call_req.side_effect = [r1, r2, r1, job_ret, r1, r3]
123 response = self.client.post("/api/gvnfmdriver/v1/ztevnfmid/vnfs/2/terminate")
124 self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code)
125 self.assertEqual(None, response.data)
127 @mock.patch.object(restcall, 'call_req')
128 def test_query_vnf(self, mock_call_req):
130 "vnfmId": "19ecbb3a-3242-4fa3-9926-8dfb7ddc29ee",
132 "type": "gvnfmdriver",
136 "description": "vnfm",
137 "certificateUrl": "",
138 "url": "http://10.74.44.11",
141 "createTime": "2016-07-06 15:33:18"
146 "instantiationState": "INSTANTIATED",
147 "vnfSoftwareVersion": "v1.2.3"
150 r1 = [0, json.JSONEncoder().encode(vnfm_info), "200"]
151 r2 = [0, json.JSONEncoder().encode(job_info), "200"]
152 mock_call_req.side_effect = [r1, r2]
153 response = self.client.get("/api/gvnfmdriver/v1/19ecbb3a-3242-4fa3-9926-8dfb7ddc29ee/vnfs/88")
154 self.assertEqual(status.HTTP_200_OK, response.status_code)
157 "vnfInstanceId": "88",
158 "vnfStatus": "ACTIVE",
162 self.assertEqual(expect_resp_data, response.data)
164 @mock.patch.object(restcall, 'call_req')
165 def test_operation_status(self, mock_call_req):
169 'name': 'ZTE_VNFM_237_62',
170 'vimId': '516cee95-e8ca-4d26-9268-38e343c2e31e',
171 'url': 'http: //192.168.237.165: 2324',
172 'certificateUrl': '',
174 'vnfmId': 'b0797c9b-3da9-459c-b25c-3813e9d8fd70',
176 'type': 'gvnfmdriver',
177 'createTime': '2016-10-3111: 08: 39',
181 "jobId": "NF-CREATE-11-ec6c2f2a-9f48-11e6-9405-fa163e91c2f9",
182 "responseDescriptor": {
185 "status": "PROCESSING",
186 "statusDescription": "OMC VMs are decommissioned in VIM",
188 "responseHistoryList": [
194 "statusdescription": "'JsonParser' object has no attribute 'parser_info'"
201 "vnfLcOpId": "NF-CREATE-11-ec6c2f2a-9f48-11e6-9405-fa163e91c2f9",
202 "responseDescriptor": {
205 "lcmOperationStatus": "PROCESSING",
206 "statusDescription": "OMC VMs are decommissioned in VIM",
208 "responseHistoryList": [
214 "statusdescription": "'JsonParser' object has no attribute 'parser_info'"
220 r1 = [0, json.JSONEncoder().encode(vnfm_info), '200']
221 r2 = [0, json.JSONEncoder().encode(resp_body), '200']
222 mock_call_req.side_effect = [r1, r2]
223 response = self.client.get("/api/gvnfmdriver/v1/%s/jobs/%s?responseId=0"
224 % (vnfm_info["vnfmId"], expected_body["jobId"]))
225 self.assertEqual(status.HTTP_200_OK, response.status_code)
226 self.assertDictEqual(expected_body, response.data)
228 @mock.patch.object(restcall, 'call_req')
229 def test_grantvnf(self, mock_call_req):
231 "vnfInstanceId": "1",
232 "vnfLcmOpOccId": "2",
235 "operation": "INSTANTIATE",
236 "isAutomaticInvocation": True,
237 "instantiationLevelId": "5",
243 "resourceTemplateId": "3",
244 "resourceTemplate": {
245 "vimConnectionId": "4",
246 "resourceProviderId": "5",
248 "vimLevelResourceType": "7"
252 "placementConstraints": [
254 "affinityOrAntiAffinity": "AFFINITY",
258 "idType": "RES_MGMT",
260 "vimConnectionId": "2",
261 "resourceProviderId": "3"
268 "sameResourceGroup": True,
271 "idType": "RES_MGMT",
273 "vimConnectionId": "2",
274 "resourceProviderId": "3"
279 "additionalParams": {},
290 'id': 'Identifier of the garnt',
291 'vnfInstanceId': 'Identifier of the related VNF instance',
292 'vnfLcmOpOccId': 'Identifier of the related VNF LcmOpOcc',
294 # 'vimConnections': [],
297 # 'computeReservationId': None,
298 # 'networkReservationId': None,
299 # 'storageReservationId': None,
300 # 'addResources': None,
301 # 'tempResources': None,
302 # 'removeResource': None,
303 # 'updateResource': None,
305 # 'extVirtualLinks': None,
306 # 'extManagedVirtualLinks': None,
307 # 'additionalParams': None,
309 'self': {'href': 'URI of this resource'},
310 'vnfLcmOpOcc': {'href': 'Related VNF lifecycle management operation occurrence'},
311 'vnfInstance': {'href': 'Related VNF instance'}
315 mock_call_req.return_value = [0, json.JSONEncoder().encode(grant), '201']
316 response = self.client.put("/api/gvnfmdriver/v1/resource/grant",
317 data=json.dumps(data), content_type='application/json')
318 self.assertEqual(status.HTTP_201_CREATED, response.status_code)
320 'id': 'Identifier of the garnt',
321 'vnfInstanceId': 'Identifier of the related VNF instance',
322 'vnfLcmOpOccId': 'Identifier of the related VNF LcmOpOcc',
324 # 'vimConnections': [],
327 # 'computeReservationId': None,
328 # 'networkReservationId': None,
329 # 'storageReservationId': None,
330 # 'addResources': None,
331 # 'tempResources': None,
332 # 'removeResource': None,
333 # 'updateResource': None,
335 # 'extVirtualLinks': None,
336 # 'extManagedVirtualLinks': None,
337 # 'additionalParams': None,
339 'self': {'href': 'URI of this resource'},
340 'vnfLcmOpOcc': {'href': 'Related VNF lifecycle management operation occurrence'},
341 'vnfInstance': {'href': 'Related VNF instance'}
344 self.assertDictEqual(expect_resp_data, response.data)
346 @mock.patch.object(restcall, 'call_req')
347 def test_grantvnf_failed(self, mock_call_req):
349 "vnfInstanceId": "1",
350 "vnfLcmOpOccId": "2",
353 "operation": "INSTANTIATE",
354 "isAutomaticInvocation": True,
355 "instantiationLevelId": "5",
361 "resourceTemplateId": "3",
362 "resourceTemplate": {
363 "vimConnectionId": "4",
364 "resourceProviderId": "5",
366 "vimLevelResourceType": "7"
370 "placementConstraints": [
372 "affinityOrAntiAffinity": "AFFINITY",
376 "idType": "RES_MGMT",
378 "vimConnectionId": "2",
379 "resourceProviderId": "3"
386 "sameResourceGroup": True,
389 "idType": "RES_MGMT",
391 "vimConnectionId": "2",
392 "resourceProviderId": "3"
397 "additionalParams": {},
407 mock_call_req.return_value = [1, json.JSONEncoder().encode(""), '201']
408 response = self.client.put("/api/gvnfmdriver/v1/resource/grant",
409 data=json.dumps(data), content_type='application/json')
410 self.assertEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code)
412 @mock.patch.object(restcall, 'call_req')
413 def test_notify(self, mock_call_req):
416 "vimInfoId": "111111",
421 "protocolType": "None"
426 "password": "password"
428 "interfaceEndpoint": "http://127.0.0.1/api/v1"
432 "resourceDefinitionId": "xxxxx",
436 "removeResource": "",
438 "computeResourceFlavour": {
449 "additionalParam": ""
451 r2 = [0, json.JSONEncoder().encode(vim_info), "200"]
452 mock_call_req.side_effect = [r2]
454 "vnfmInstId": "876543211",
455 "notificationType": "string",
456 "subscriptionId": "string",
457 "timeStamp": "1234567890",
458 "notificationStatus": "START",
459 "operationState": "STARTING",
460 "vnfInstanceId": "string",
461 "operation": "INSTANTIATE",
462 "isAutomaticInvocation": True,
463 "vnfLcmOpOccId": "string",
467 "changeType": "ADDED",
469 "vimConnectionId": "string",
470 "resourceProviderId": "string",
471 "resourceId": "string",
472 "vimLevelResourceType": "string"
475 "affectedVnfcCpIds": [],
476 "addedStorageResourceIds": [],
477 "removedStorageResourceIds": [],
479 "affectedVirtualLinks": [{
481 "virtualLinkDescId": "string",
482 "changeType": "ADDED",
484 "vimConnectionId": "string",
485 "resourceProviderId": "string",
486 "resourceId": "string",
487 "vimLevelResourceType": "network",
490 "affectedVirtualStorages": [{
492 "virtualStorageDescId": "string",
493 "changeType": "ADDED",
495 "vimConnectionId": "string",
496 "resourceProviderId": "string",
497 "resourceId": "string",
498 "vimLevelResourceType": "network",
503 "vnfInstanceName": "string",
504 "vnfInstanceDescription": "string",
505 "vnfConfigurableProperties": {},
508 "vimConnectionInfo": [{
516 "vnfPkgId": "string",
518 "vnfProvider": "string",
519 "vnfProductName": "string",
520 "vnfSoftwareVersion": "string",
521 "vnfdVersion": "string"
523 "changedExtConnectivity": [{
526 "vimConnectionId": "string",
527 "resourceProviderId": "string",
528 "resourceId": "string",
529 "vimLevelResourceType": "string"
534 "vimConnectionId": "string",
535 "resourceProviderId": "string",
536 "resourceId": "string",
537 "vimLevelResourceType": "string"
539 "cpInstanceId": "string"
543 response = self.client.post("/api/gvnfmdriver/v1/vnfs/lifecyclechangesnotification",
544 data=json.dumps(req_data),
545 content_type='application/json')
546 self.assertEqual(status.HTTP_200_OK, response.status_code)
547 expect_resp_data = None
548 self.assertEqual(expect_resp_data, response.data)
550 @mock.patch.object(restcall, 'call_req')
551 def test_get_vnfpkgs(self, mock_call_req):
558 mock_call_req.return_value = [0, json.JSONEncoder().encode(vnfpkgs_info), '200']
559 resp = self.client.get("/api/gvnfmdriver/v1/vnfpackages")
560 self.assertEqual(status.HTTP_200_OK, resp.status_code)
561 self.assertEqual(1, len(resp.data["csars"]))
562 self.assertEqual("1", resp.data["csars"][0]["csarId"])
563 self.assertEqual("2", resp.data["csars"][0]["vnfdId"])
565 @mock.patch.object(restcall, 'call_req')
566 def test_get_vnfpkgs_failed(self, mock_call_req):
567 mock_call_req.return_value = [1, json.JSONEncoder().encode(""), '200']
568 resp = self.client.get("/api/gvnfmdriver/v1/vnfpackages")
569 self.assertEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, resp.status_code)
571 @mock.patch.object(restcall, 'call_req')
572 def test_get_vnflcmopocc_with_id(self, mock_call_req):
573 vnfLcmOpOccId = "99442b18-a5c7-11e8-998c-bf1755941f16"
575 "vnfmId": "19ecbb3a-3242-4fa3-9926-8dfb7ddc29ee",
577 "type": "gvnfmdriver",
581 "description": "vnfm",
582 "certificateUrl": "",
583 "url": "http://10.74.44.11",
586 "createTime": "2016-07-06 15:33:18"
588 dummy_single_vnf_lcm_op = {
590 "operationState": "STARTING",
591 "stateEnteredTime": "2018-07-09",
592 "startTime": "2018-07-09",
593 "vnfInstanceId": "cd552c9c-ab6f-11e8-b354-236c32aa91a1",
595 "operation": "SCALE",
596 "isAutomaticInvocation": False,
597 "operationParams": {},
598 "isCancelPending": False,
601 "resourceChanges": None,
603 "changedExtConnectivity": None,
608 "vnfInstance": "demo"
611 mock_call_req.return_value = [0, json.JSONEncoder().encode(dummy_single_vnf_lcm_op), status.HTTP_200_OK]
612 resp = self.client.get("/api/gvnfmdriver/v1/%s/vnf_lcm_op_occs/%s" % (vnfm_info['vnfmId'], vnfLcmOpOccId))
613 self.assertEqual(dummy_single_vnf_lcm_op, resp.data)
614 self.assertEqual(status.HTTP_200_OK, resp.status_code)
616 @mock.patch.object(restcall, 'call_req')
617 def test_get_vnflcmopocc_failed(self, mock_call_req):
618 vnfLcmOpOccId = "99442b18-a5c7-11e8-998c-bf1755941f16"
620 "vnfmId": "19ecbb3a-3242-4fa3-9926-8dfb7ddc29ee",
622 "type": "gvnfmdriver",
626 "description": "vnfm",
627 "certificateUrl": "",
628 "url": "http://10.74.44.11",
631 "createTime": "2016-07-06 15:33:18"
633 mock_call_req.return_value = [1, json.JSONEncoder().encode({}), status.HTTP_500_INTERNAL_SERVER_ERROR]
634 resp = self.client.get("/api/gvnfmdriver/v1/%s/vnf_lcm_op_occs/%s" % (vnfm_info['vnfmId'], vnfLcmOpOccId))
635 self.assertEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, resp.status_code)