1 # Copyright 2019 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 mock import MagicMock
18 from django.test import TestCase
19 from rest_framework import status
20 from rest_framework.test import APIClient
21 from lcm.ns.biz.ns_instant import BuildInWorkflowThread
22 from lcm.ns.biz.ns_instant import InstantNSService
23 from lcm.pub.database.models import NSInstModel
24 from lcm.pub.utils import restcall
25 from lcm.pub.config import config
27 nsd_model = json.dumps({
58 "descriptor_id": "zte_ran_du_0001",
59 "descriptor_invariant_id": "1111",
62 "function_description": "RAN DU Function",
89 "basepath": "c:\\users\\10030173\\appdata\\local\\temp\\tmpvg5vto",
94 "key_name": "ran_ext_net",
98 "key_name": "ran_flat_net",
99 "vl_id": "vl_flat_net"
104 "key_name": "ran_ext_net",
105 "vl_id": "vl_ext_net"
108 "key_name": "ran_flat_net",
109 "vl_id": "vl_flat_net"
115 "descriptor_id": "zte_ran_cucp_0001",
116 "flavour_description": "default",
117 "software_version": "1.0.1",
119 "descriptor_version": "1.0",
121 "id": "zte_ran_cucp_0001",
131 "key_name": "ran_ext_net",
132 "vl_id": "vl_ext_net"
135 "key_name": "ran_flat_net",
136 "vl_id": "vl_flat_net"
141 "key_name": "ran_ext_net",
142 "vl_id": "vl_ext_net"
145 "key_name": "ran_flat_net",
146 "vl_id": "vl_flat_net"
152 "descriptor_id": "zte_ran_cuup_0001",
153 "flavour_description": "default",
154 "software_version": "1.0.1",
156 "descriptor_version": "1.0",
158 "id": "zte_ran_cuup_0001",
170 "vl_id": "vl_ext_net",
173 "connectivity_type": {
174 "layer_protocol": "ipv4"
177 "cidr": "10.0.0.0/24",
178 "max_bit_rate_requirements": {
182 "networkName": "ran_ext_net",
183 "min_bit_rate_requirements": {
193 "vl_id": "vl_flat_net",
196 "connectivity_type": {
197 "layer_protocol": "ipv4"
200 "cidr": "10.1.0.0/24",
201 "max_bit_rate_requirements": {
205 "networkName": "ran_flat_net",
206 "min_bit_rate_requirements": {
219 "template_name": "RAN-NS",
220 "template_version": "1.0",
221 "template_author": "ZTE"
224 vnfminfo = {"vnfmId": "1"}
227 class TestInstantiateNs(TestCase):
230 self.client = APIClient()
231 NSInstModel.objects.filter().delete()
232 self.url = "/api/nslcm/v1/ns_instances/%s/instantiate"
234 "additionalParamForNs": {
237 "nsFlavourId": 'default',
238 "localizationLanguage": [{
239 "vnfProfileId": "vnfd1",
240 "locationConstraints": {
241 "countryCode": "countryCode",
243 "civicAddressElement": [
251 self.nsd_model = nsd_model
252 self.updated_nsd_model = {
272 self.vnfms = json.dumps({
277 self.vnfm = json.dumps({
281 "certificate-url": "http://127.0.0.0/ztevnfm/v1/auth",
282 "esr-system-info-list": {
283 "esr-system-info": [{
287 "service-url": "http://127.0.0.0/ztevnfm/v1",
288 "user-name": "admin",
289 "password": "admin123"
293 NSInstModel(id="2", nspackage_id="7", nsd_id="2", status="active").save()
298 @mock.patch.object(restcall, 'call_req')
299 @mock.patch('lcm.pub.msapi.sdc_run_catalog.parse_nsd', MagicMock(return_value=nsd_model))
300 @mock.patch.object(BuildInWorkflowThread, 'run')
301 def test_ns_instantiate_when_succeed_to_enter_workflow(self, mock_run, mock_call_req):
302 config.WORKFLOW_OPTION = "buildin"
303 mock_call_req.side_effect = [
304 [0, self.nsd_model, '200'],
305 [0, self.vnfms, '200'],
306 [0, self.vnfm, '200']
308 resp = self.client.post(self.url % '2', data=self.req_data, format='json')
309 self.assertEqual(status.HTTP_202_ACCEPTED, resp.status_code)
311 @mock.patch.object(InstantNSService, 'do_biz')
312 def test_ns_instantiate_normal(self, mock_do_biz):
313 mock_do_biz.return_value = {'occ_id': "1"}
314 resp = self.client.post(self.url % '2', data=self.req_data, format='json')
315 self.failUnlessEqual(status.HTTP_202_ACCEPTED, resp.status_code)
317 @mock.patch.object(restcall, 'call_req')
318 def test_ns_instantiate_when_fail_to_parse_nsd(self, mock_call_req):
319 mock_call_req.return_value = [1, "Failed to parse nsd", '500']
320 resp = self.client.post(self.url % '2', data=self.req_data, format='json')
321 self.assertEqual(resp.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
323 @mock.patch('lcm.ns.biz.ns_instantiate_flow.post_deal')
324 @mock.patch.object(restcall, 'call_req')
325 @mock.patch('lcm.ns.biz.ns_instantiate_flow.update_job')
326 @mock.patch('lcm.pub.msapi.sdc_run_catalog.parse_nsd', MagicMock(return_value=nsd))
327 @mock.patch('lcm.pub.msapi.extsys.select_vnfm', MagicMock(return_value=vnfminfo))
328 def test_ns_instantiate_with_pnf(self, mock_updata_job, mock_call_req, mock_post_deal):
329 config.WORKFLOW_OPTION = "grapflow"
330 NSInstModel(id="1", name="test_ns", nspackage_id="1", status="created").save()
331 ret = [0, json.JSONEncoder().encode({'jobId': "1", "responseDescriptor": {"progress": 100}}), '200']
332 mock_call_req.side_effect = [ret for i in range(1, 20)]
334 "nsFlavourId": 'default',
335 "additionalParamForNs": {
336 "sdnControllerId": "2"
338 "additionalParamsForVnf": [
340 "vnfProfileId": "zte_ran_cucp_0001",
341 "additionalParams": {"vimId": "3"}
344 "vnfProfileId": "zte_ran_cuup_0001",
345 "additionalParams": {"vimId": "3"}
350 "pnfName": "test_pnf",
351 "pnfdId": "zte_ran_du_0001",
355 response = self.client.post(self.url % '1', data=data, format='json')
356 self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)