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',
293 'vimConnections': [],
296 'computeReservationId': None,
297 'networkReservationId': None,
298 'storageReservationId': None,
299 'addResources': None,
300 'tempResources': None,
301 'removeResource': None,
302 'updateResource': None,
304 'extVirtualLinks': None,
305 'extManagedVirtualLinks': None,
306 'additionalParams': {},
310 mock_call_req.return_value = [0, json.JSONEncoder().encode(grant), '201']
311 response = self.client.put("/api/gvnfmdriver/v1/resource/grant",
312 data=json.dumps(data), content_type='application/json')
313 self.assertEqual(status.HTTP_201_CREATED, response.status_code)
315 'id': 'Identifier of the garnt',
316 'vnfInstanceId': 'Identifier of the related VNF instance',
317 'vnfLcmOpOccId': 'Identifier of the related VNF LcmOpOcc',
318 'vimConnections': [],
321 'computeReservationId': None,
322 'networkReservationId': None,
323 'storageReservationId': None,
324 'addResources': None,
325 'tempResources': None,
326 'removeResource': None,
327 'updateResource': None,
329 'extVirtualLinks': None,
330 'extManagedVirtualLinks': None,
331 'additionalParams': {},
334 self.assertDictEqual(expect_resp_data, response.data)
336 @mock.patch.object(restcall, 'call_req')
337 def test_grantvnf_failed(self, mock_call_req):
339 "vnfInstanceId": "1",
340 "vnfLcmOpOccId": "2",
343 "operation": "INSTANTIATE",
344 "isAutomaticInvocation": True,
345 "instantiationLevelId": "5",
351 "resourceTemplateId": "3",
352 "resourceTemplate": {
353 "vimConnectionId": "4",
354 "resourceProviderId": "5",
356 "vimLevelResourceType": "7"
360 "placementConstraints": [
362 "affinityOrAntiAffinity": "AFFINITY",
366 "idType": "RES_MGMT",
368 "vimConnectionId": "2",
369 "resourceProviderId": "3"
376 "sameResourceGroup": True,
379 "idType": "RES_MGMT",
381 "vimConnectionId": "2",
382 "resourceProviderId": "3"
387 "additionalParams": {},
397 mock_call_req.return_value = [1, json.JSONEncoder().encode(""), '201']
398 response = self.client.put("/api/gvnfmdriver/v1/resource/grant",
399 data=json.dumps(data), content_type='application/json')
400 self.assertEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code)
402 @mock.patch.object(restcall, 'call_req')
403 def test_notify(self, mock_call_req):
406 "vimInfoId": "111111",
411 "protocolType": "None"
416 "password": "password"
418 "interfaceEndpoint": "http://127.0.0.1/api/v1"
422 "resourceDefinitionId": "xxxxx",
426 "removeResource": "",
428 "computeResourceFlavour": {
439 "additionalParam": ""
441 r2 = [0, json.JSONEncoder().encode(vim_info), "200"]
442 mock_call_req.side_effect = [r2]
444 "vnfmInstId": "876543211",
445 "notificationType": "string",
446 "subscriptionId": "string",
447 "timeStamp": "1234567890",
448 "notificationStatus": "START",
449 "operationState": "STARTING",
450 "vnfInstanceId": "string",
451 "operation": "INSTANTIATE",
452 "isAutomaticInvocation": True,
453 "vnfLcmOpOccId": "string",
457 "changeType": "ADDED",
459 "vimConnectionId": "string",
460 "resourceProviderId": "string",
461 "resourceId": "string",
462 "vimLevelResourceType": "string"
465 "affectedVnfcCpIds": [],
466 "addedStorageResourceIds": [],
467 "removedStorageResourceIds": [],
469 "affectedVirtualLinks": [{
471 "virtualLinkDescId": "string",
472 "changeType": "ADDED",
474 "vimConnectionId": "string",
475 "resourceProviderId": "string",
476 "resourceId": "string",
477 "vimLevelResourceType": "network",
480 "affectedVirtualStorages": [{
482 "virtualStorageDescId": "string",
483 "changeType": "ADDED",
485 "vimConnectionId": "string",
486 "resourceProviderId": "string",
487 "resourceId": "string",
488 "vimLevelResourceType": "network",
493 "vnfInstanceName": "string",
494 "vnfInstanceDescription": "string",
495 "vnfConfigurableProperties": {},
498 "vimConnectionInfo": [{
506 "vnfPkgId": "string",
508 "vnfProvider": "string",
509 "vnfProductName": "string",
510 "vnfSoftwareVersion": "string",
511 "vnfdVersion": "string"
513 "changedExtConnectivity": [{
516 "vimConnectionId": "string",
517 "resourceProviderId": "string",
518 "resourceId": "string",
519 "vimLevelResourceType": "string"
524 "vimConnectionId": "string",
525 "resourceProviderId": "string",
526 "resourceId": "string",
527 "vimLevelResourceType": "string"
529 "cpInstanceId": "string"
533 response = self.client.post("/api/gvnfmdriver/v1/vnfs/lifecyclechangesnotification",
534 data=json.dumps(req_data),
535 content_type='application/json')
536 self.assertEqual(status.HTTP_200_OK, response.status_code)
537 expect_resp_data = None
538 self.assertEqual(expect_resp_data, response.data)
540 @mock.patch.object(restcall, 'call_req')
541 def test_get_vnfpkgs(self, mock_call_req):
548 mock_call_req.return_value = [0, json.JSONEncoder().encode(vnfpkgs_info), '200']
549 resp = self.client.get("/api/gvnfmdriver/v1/vnfpackages")
550 self.assertEqual(status.HTTP_200_OK, resp.status_code)
551 self.assertEqual(1, len(resp.data["csars"]))
552 self.assertEqual("1", resp.data["csars"][0]["csarId"])
553 self.assertEqual("2", resp.data["csars"][0]["vnfdId"])
555 @mock.patch.object(restcall, 'call_req')
556 def test_get_vnfpkgs_failed(self, mock_call_req):
557 mock_call_req.return_value = [1, json.JSONEncoder().encode(""), '200']
558 resp = self.client.get("/api/gvnfmdriver/v1/vnfpackages")
559 self.assertEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, resp.status_code)
561 @mock.patch.object(restcall, 'call_req')
562 def test_get_vnflcmopocc_with_id(self, mock_call_req):
563 vnfLcmOpOccId = "99442b18-a5c7-11e8-998c-bf1755941f16"
565 "vnfmId": "19ecbb3a-3242-4fa3-9926-8dfb7ddc29ee",
567 "type": "gvnfmdriver",
571 "description": "vnfm",
572 "certificateUrl": "",
573 "url": "http://10.74.44.11",
576 "createTime": "2016-07-06 15:33:18"
578 dummy_single_vnf_lcm_op = {
580 "operationState": "STARTING",
581 "stateEnteredTime": "2018-07-09",
582 "startTime": "2018-07-09",
583 "vnfInstanceId": "cd552c9c-ab6f-11e8-b354-236c32aa91a1",
585 "operation": "SCALE",
586 "isAutomaticInvocation": False,
587 "operationParams": {},
588 "isCancelPending": False,
591 "resourceChanges": None,
593 "changedExtConnectivity": None,
598 "vnfInstance": "demo"
601 mock_call_req.return_value = [0, json.JSONEncoder().encode(dummy_single_vnf_lcm_op), status.HTTP_200_OK]
602 resp = self.client.get("/api/gvnfmdriver/v1/%s/vnf_lcm_op_occs/%s" % (vnfm_info['vnfmId'], vnfLcmOpOccId))
603 self.assertEqual(dummy_single_vnf_lcm_op, resp.data)
604 self.assertEqual(status.HTTP_200_OK, resp.status_code)
606 @mock.patch.object(restcall, 'call_req')
607 def test_get_vnflcmopocc_failed(self, mock_call_req):
608 vnfLcmOpOccId = "99442b18-a5c7-11e8-998c-bf1755941f16"
610 "vnfmId": "19ecbb3a-3242-4fa3-9926-8dfb7ddc29ee",
612 "type": "gvnfmdriver",
616 "description": "vnfm",
617 "certificateUrl": "",
618 "url": "http://10.74.44.11",
621 "createTime": "2016-07-06 15:33:18"
623 mock_call_req.return_value = [1, json.JSONEncoder().encode({}), status.HTTP_500_INTERNAL_SERVER_ERROR]
624 resp = self.client.get("/api/gvnfmdriver/v1/%s/vnf_lcm_op_occs/%s" % (vnfm_info['vnfmId'], vnfLcmOpOccId))
625 self.assertEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, resp.status_code)