fix nslcm middleware
[vfc/nfvo/lcm.git] / lcm / ns / tests / test_sol_ns_scale_api.py
1 # Copyright 2019 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.pub.database.models import NSInstModel, JobModel, NfInstModel
26 from lcm.pub.exceptions import NSLCMException
27 from lcm.pub.utils.jobutil import JobUtil, JOB_TYPE
28
29 SCALING_JSON = {
30     "scale_options": [
31         {
32             "nsd_id": "ns_ims",
33             "ns_scale_aspect": "TIC_CORE_IMS",
34             "ns_scale_info": [
35                 {
36                     "step": "1",
37                     "scale_list": [
38                         {
39                             "vnfd_id": "zte_ims_cscf",
40                             "vnf_scale_aspect": "mpu",
41                             "numberOfSteps": "1"
42                         },
43                         {
44                             "vnfd_id": "zte_ims_hss",
45                             "vnf_scale_aspect": "fpu",
46                             "numberOfSteps": "3"
47                         }
48                     ]
49                 },
50                 {
51                     "step": "2",
52                     "scale_list": [
53                         {
54                             "vnfd_id": "zte_ims_cscf",
55                             "vnf_scale_aspect": "mpu",
56                             "numberOfSteps": "2"
57                         },
58                         {
59                             "vnfd_id": "zte_ims_hss",
60                             "vnf_scale_aspect": "fpu",
61                             "numberOfSteps": "6"
62                         }
63                     ]
64                 }
65             ]
66         },
67         {
68             "nsd_id": "ns_epc",
69             "ns_scale_aspect": "TIC_EDGE_EPC",
70             "ns_scale_info": [
71                 {
72                     "step": "1",
73                     "scale_list": [
74                         {
75                             "vnfd_id": "zte_epc_spgw",
76                             "vnf_scale_aspect": "gpu",
77                             "numberOfSteps": "1"
78                         },
79                         {
80                             "vnfd_id": "zte_epc_tas",
81                             "vnf_scale_aspect": "fpu",
82                             "numberOfSteps": "2"
83                         }
84                     ]
85                 },
86                 {
87                     "step": "2",
88                     "scale_list": [
89                         {
90                             "vnfd_id": "zte_epc_spgw",
91                             "vnf_scale_aspect": "mpu",
92                             "numberOfSteps": "2"
93                         },
94                         {
95                             "vnfd_id": "zte_epc_tas",
96                             "vnf_scale_aspect": "fpu",
97                             "numberOfSteps": "4"
98                         }
99                     ]
100                 }
101             ]
102         }
103     ]
104 }
105
106
107 class TestScaleNsApi(TestCase):
108     def setUp(self):
109         self.url = "/api/nslcm/v1/ns_instances/%s/scale"
110         self.ns_inst_id = str(uuid.uuid4())
111         self.job_id = JobUtil.create_job(
112             "NS", JOB_TYPE.MANUAL_SCALE_VNF, self.ns_inst_id)
113         self.package_id = "7"
114         self.client = Client()
115         NSInstModel(
116             id=self.ns_inst_id,
117             name="abc",
118             nspackage_id=self.package_id,
119             nsd_id="111").save()
120
121         self.init_scaling_map_json()
122
123     def tearDown(self):
124         NSInstModel.objects.filter().delete()
125         JobModel.objects.filter().delete()
126
127     def init_scaling_map_json(self):
128         curdir_path = os.path.dirname(
129             os.path.dirname(
130                 os.path.dirname(
131                     os.path.abspath(__file__))))
132         filename = curdir_path + "/ns/data/scalemapping.json"
133         self.scaling_map_json = get_json_data(filename)
134
135     def insert_new_ns(self):
136         ns_inst_id = str(uuid.uuid4())
137         job_id = JobUtil.create_job(
138             "NS", JOB_TYPE.MANUAL_SCALE_VNF, self.ns_inst_id)
139         package_id = "23"
140         NSInstModel(
141             id=ns_inst_id,
142             name="abc",
143             nspackage_id=package_id,
144             nsd_id=package_id).save()
145         return ns_inst_id, job_id
146
147     def insert_new_nf(self):
148         # Create a third vnf instance
149         self.nf_name = "name_1"
150         self.vnf_id = "1"
151         self.vnfm_inst_id = "1"
152         nf_inst_id = "233"
153         package_id = "nf_zte_hss"
154         nf_uuid = "ab34-3g5j-de13-ab85-ij93"
155         vnf_model = {
156             "metadata": {
157                 "vnfdId": "1",
158                 "vnfdName": "PGW001",
159                 "vnfProvider": "zte",
160                 "vnfdVersion": "V00001",
161                 "vnfVersion": "V5.10.20",
162                 "productType": "CN",
163                 "vnfType": "PGW",
164                 "description": "PGW VNFD description",
165                 "isShared": True,
166                 "vnfExtendType": "driver"
167             }
168         }
169         NfInstModel.objects.create(
170             nfinstid=nf_inst_id,
171             nf_name=self.nf_name,
172             vnf_id=self.vnf_id,
173             vnfm_inst_id=self.vnfm_inst_id,
174             ns_inst_id=self.ns_inst_id,
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=nf_uuid,
182             package_id=package_id,
183             vnfd_model=str(vnf_model)
184         )
185
186     @mock.patch.object(NSManualScaleService, 'run')
187     def test_ns_scale(self, mock_run):
188         data = {
189             "scaleType": "SCALE_NS",
190             "scaleNsData": [{
191                 "scaleNsByStepsData": [{
192                     "aspectId": "1",
193                     "numberOfSteps": 1,
194                     "scalingDirection": "0"
195                 }]
196             }]
197         }
198         response = self.client.post(self.url % self.ns_inst_id, data=data)
199         self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
200         self.assertIsNotNone(response['Location'])
201
202     @mock.patch.object(NSManualScaleService, 'start')
203     def test_ns_manual_scale_empty_data(self, mock_start):
204         mock_start.side_effect = NSLCMException("NS scale failed.")
205         response = self.client.post(
206             self.url %
207             self.ns_inst_id, data={})
208         self.assertEqual(
209             response.status_code,
210             status.HTTP_500_INTERNAL_SERVER_ERROR)
211
212     @mock.patch.object(NSManualScaleService, 'start')
213     def test_ns_manual_scale_when_ns_not_exist(self, mock_start):
214         mock_start.side_effect = NSLCMException("NS scale failed.")
215         data = {
216             "scaleType": "SCALE_NS",
217             "scaleNsData": [{
218                 "scaleNsByStepsData": [{
219                     "aspectId": "1",
220                     "numberOfSteps": 1,
221                     "scalingDirection": "0"
222                 }]
223             }]
224         }
225         response = self.client.post(self.url % '11', data=data)
226         self.assertEqual(
227             response.status_code,
228             status.HTTP_500_INTERNAL_SERVER_ERROR)
229
230     def test_method_not_allowed(self):
231         response = self.client.put(self.url % '1', data={}, format='json')
232         self.failUnlessEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)
233         response = self.client.patch(self.url % '1', data={}, format='json')
234         self.failUnlessEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)
235         response = self.client.delete(self.url % '1', data={}, format='json')
236         self.failUnlessEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)
237         response = self.client.get(self.url % '1', data={}, format='json')
238         self.failUnlessEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)