SOL003 API Align
[vfc/nfvo/lcm.git] / lcm / ns / tests / test_ns_manual_scale.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 os
16 import uuid
17
18 import mock
19 from django.test import Client
20 from django.test import TestCase
21 from lcm.ns.biz.scaleaspect import get_json_data
22 from rest_framework import status
23
24 from lcm.ns.biz.ns_manual_scale import NSManualScaleService
25 from lcm.ns.const import NS_INST_STATUS
26 from lcm.pub.database.models import NSInstModel, JobModel, NfInstModel
27 from lcm.pub.exceptions import NSLCMException
28 from lcm.pub.msapi import catalog
29 from lcm.pub.utils import restcall
30 from lcm.pub.utils.jobutil import JobUtil, JOB_TYPE, JOB_MODEL_STATUS
31
32 SCALING_JSON = {
33     "scale_options": [
34         {
35             "nsd_id": "ns_ims",
36             "ns_scale_aspect": "TIC_CORE_IMS",
37             "ns_scale_info": [
38                 {
39                     "step": "1",
40                     "scale_list": [
41                         {
42                             "vnfd_id": "zte_ims_cscf",
43                             "vnf_scale_aspect": "mpu",
44                             "numberOfSteps": "1"
45                         },
46                         {
47                             "vnfd_id": "zte_ims_hss",
48                             "vnf_scale_aspect": "fpu",
49                             "numberOfSteps": "3"
50                         }
51                     ]
52                 },
53                 {
54                     "step": "2",
55                     "scale_list": [
56                         {
57                             "vnfd_id": "zte_ims_cscf",
58                             "vnf_scale_aspect": "mpu",
59                             "numberOfSteps": "2"
60                         },
61                         {
62                             "vnfd_id": "zte_ims_hss",
63                             "vnf_scale_aspect": "fpu",
64                             "numberOfSteps": "6"
65                         }
66                     ]
67                 }
68             ]
69         },
70         {
71             "nsd_id": "ns_epc",
72             "ns_scale_aspect": "TIC_EDGE_EPC",
73             "ns_scale_info": [
74                 {
75                     "step": "1",
76                     "scale_list": [
77                         {
78                             "vnfd_id": "zte_epc_spgw",
79                             "vnf_scale_aspect": "gpu",
80                             "numberOfSteps": "1"
81                         },
82                         {
83                             "vnfd_id": "zte_epc_tas",
84                             "vnf_scale_aspect": "fpu",
85                             "numberOfSteps": "2"
86                         }
87                     ]
88                 },
89                 {
90                     "step": "2",
91                     "scale_list": [
92                         {
93                             "vnfd_id": "zte_epc_spgw",
94                             "vnf_scale_aspect": "mpu",
95                             "numberOfSteps": "2"
96                         },
97                         {
98                             "vnfd_id": "zte_epc_tas",
99                             "vnf_scale_aspect": "fpu",
100                             "numberOfSteps": "4"
101                         }
102                     ]
103                 }
104             ]
105         }
106     ]
107 }
108
109
110 class TestNsManualScale(TestCase):
111     def setUp(self):
112         self.ns_inst_id = str(uuid.uuid4())
113         self.job_id = JobUtil.create_job(
114             "NS", JOB_TYPE.MANUAL_SCALE_VNF, self.ns_inst_id)
115         self.package_id = "7"
116         self.client = Client()
117         NSInstModel(
118             id=self.ns_inst_id,
119             name="abc",
120             nspackage_id=self.package_id,
121             nsd_id="111").save()
122
123         self.init_scaling_map_json()
124
125     def tearDown(self):
126         NSInstModel.objects.filter().delete()
127         JobModel.objects.filter().delete()
128
129     def init_scaling_map_json(self):
130         curdir_path = os.path.dirname(
131             os.path.dirname(
132                 os.path.dirname(
133                     os.path.abspath(__file__))))
134         filename = curdir_path + "/ns/data/scalemapping.json"
135         self.scaling_map_json = get_json_data(filename)
136
137     def insert_new_ns(self):
138         ns_inst_id = str(uuid.uuid4())
139         job_id = JobUtil.create_job(
140             "NS", JOB_TYPE.MANUAL_SCALE_VNF, self.ns_inst_id)
141         package_id = "23"
142         NSInstModel(
143             id=ns_inst_id,
144             name="abc",
145             nspackage_id=package_id,
146             nsd_id=package_id).save()
147         return ns_inst_id, job_id
148
149     def insert_new_nf(self):
150         # Create a third vnf instance
151         self.nf_name = "name_1"
152         self.vnf_id = "1"
153         self.vnfm_inst_id = "1"
154         nf_inst_id = "233"
155         package_id = "nf_zte_hss"
156         nf_uuid = "ab34-3g5j-de13-ab85-ij93"
157
158         NfInstModel.objects.create(
159             nfinstid=nf_inst_id,
160             nf_name=self.nf_name,
161             vnf_id=self.vnf_id,
162             vnfm_inst_id=self.vnfm_inst_id,
163             ns_inst_id=self.ns_inst_id,
164             max_cpu='14',
165             max_ram='12296',
166             max_hd='101',
167             max_shd="20",
168             max_net=10,
169             status='active',
170             mnfinstid=nf_uuid,
171             package_id=package_id,
172             vnfd_model='{"metadata": {"vnfdId": "1","vnfdName": "PGW001",'
173                        '"vnfProvider": "zte","vnfdVersion": "V00001","vnfVersion": "V5.10.20",'
174                        '"productType": "CN","vnfType": "PGW",'
175                        '"description": "PGW VNFD description",'
176                        '"isShared":true,"vnfExtendType":"driver"}}')
177
178     @mock.patch.object(NSManualScaleService, 'run')
179     def test_ns_manual_scale(self, mock_run):
180         data = {
181             "scaleType": "SCALE_NS",
182             "scaleNsData": [{
183                 "scaleNsByStepsData": [{
184                     "aspectId": "1",
185                     "numberOfSteps": 1,
186                     "scalingDirection": "0"
187                 }]
188             }]
189         }
190         response = self.client.post(
191             "/api/nslcm/v1/ns/%s/scale" %
192             self.ns_inst_id, data=data)
193         self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
194
195     def test_ns_manual_scale_error_scaletype(self):
196         data = {
197             "scaleType": "SCALE_ERR",
198             "scaleNsData": [{
199                 "scaleNsByStepsData": [{
200                     "aspectId": "sss_zte",
201                     "numberOfSteps": 1,
202                     "scalingDirection": "0"
203                 }]
204             }]
205         }
206         NSManualScaleService(self.ns_inst_id, data, self.job_id).run()
207         jobs = JobModel.objects.filter(jobid=self.job_id)
208         self.assertEqual(255, jobs[0].progress)
209
210     def test_ns_manual_scale_error_nsd_id(self):
211         data = {
212             "scaleType": "SCALE_NS",
213             "scaleNsData": [{
214                 "scaleNsByStepsData": [{
215                     "aspectId": "sss_zte",
216                     "numberOfSteps": 1,
217                     "scalingDirection": "0"
218                 }]
219             }]
220         }
221         NSManualScaleService(self.ns_inst_id, data, self.job_id).run()
222         jobs = JobModel.objects.filter(jobid=self.job_id)
223         self.assertEqual(255, jobs[0].progress)
224
225     def test_ns_manual_scale_error_aspect(self):
226         data = {
227             "scaleType": "SCALE_NS",
228             "scaleNsData": [{
229                 "scaleNsByStepsData": [{
230                     "aspectId": "sss_zte",
231                     "numberOfSteps": 1,
232                     "scalingDirection": "0"
233                 }]
234             }]
235         }
236         ns_inst_id, job_id = self.insert_new_ns()
237         job_id = JobUtil.create_job(
238             "NS", JOB_TYPE.MANUAL_SCALE_VNF, ns_inst_id)
239         NSManualScaleService(ns_inst_id, data, job_id).run()
240         jobs = JobModel.objects.filter(jobid=job_id)
241         self.assertEqual(255, jobs[0].progress)
242
243     @mock.patch.object(catalog, 'get_scalingmap_json_package')
244     @mock.patch.object(NSManualScaleService, 'do_vnfs_scale')
245     def test_ns_manual_scale_success(self, mock_do_vnfs_scale, mock_get_scalingmap_json_package):
246         data = {
247             "scaleType": "SCALE_NS",
248             "scaleNsData": [{
249                 "scaleNsByStepsData": [{
250                     "aspectId": "TIC_EDGE_IMS",
251                     "numberOfSteps": "1",
252                     "scalingDirection": "0"
253                 }]
254             }]
255         }
256         mock_get_scalingmap_json_package.return_value = self.scaling_map_json
257         mock_do_vnfs_scale.return_value = JOB_MODEL_STATUS.FINISHED
258         ns_inst_id, job_id = self.insert_new_ns()
259         job_id = JobUtil.create_job(
260             "NS", JOB_TYPE.MANUAL_SCALE_VNF, ns_inst_id)
261         self.insert_new_nf()
262         NSManualScaleService(ns_inst_id, data, job_id).run()
263         jobs = JobModel.objects.filter(jobid=job_id)
264         self.assertEqual(255, jobs[0].progress)
265
266     @mock.patch.object(restcall, 'call_req')
267     def test_ns_manual_scale_thread(self, mock_call):
268         data = {
269             "scaleType": "SCALE_NS",
270             "scaleNsData": [{
271                 "scaleNsByStepsData": [{
272                     "aspectId": "1",
273                     "numberOfSteps": 1,
274                     "scalingDirection": "0"
275                 }]
276             }]
277         }
278         NSManualScaleService(self.ns_inst_id, data, self.job_id).run()
279         self.assertTrue(
280             NSInstModel.objects.get(
281                 id=self.ns_inst_id).status,
282             NS_INST_STATUS.ACTIVE)
283
284     # def test_swagger_ok(self):
285     # resp = self.client.get("/api/nslcm/v1/swagger.json", format='json')
286     # self.assertEqual(resp.status_code, status.HTTP_200_OK)
287
288     @mock.patch.object(NSManualScaleService, 'start')
289     def test_ns_manual_scale_empty_data(self, mock_start):
290         mock_start.side_effect = NSLCMException("NS scale failed.")
291         response = self.client.post(
292             "/api/nslcm/v1/ns/%s/scale" %
293             self.ns_inst_id, data={})
294         self.assertEqual(
295             response.status_code,
296             status.HTTP_500_INTERNAL_SERVER_ERROR)
297         self.assertIn("error", response.data)
298
299     @mock.patch.object(NSManualScaleService, 'start')
300     def test_ns_manual_scale_when_ns_not_exist(self, mock_start):
301         mock_start.side_effect = NSLCMException("NS scale failed.")
302         data = {
303             "scaleType": "SCALE_NS",
304             "scaleNsData": [{
305                 "scaleNsByStepsData": [{
306                     "aspectId": "1",
307                     "numberOfSteps": 1,
308                     "scalingDirection": "0"
309                 }]
310             }]
311         }
312         response = self.client.post("/api/nslcm/v1/ns/11/scale", data=data)
313         self.assertEqual(
314             response.status_code,
315             status.HTTP_500_INTERNAL_SERVER_ERROR)
316         self.assertIn("error", response.data)