3a82ea72222bb5ab4fc61c5657a3133e65b93929
[osm/POL.git] / osm_policy_module / tests / integration / test_policy_agent.py
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2018 Whitestack, LLC
4 # *************************************************************
5
6 # This file is part of OSM Monitoring module
7 # All Rights Reserved to Whitestack, LLC
8
9 # Licensed under the Apache License, Version 2.0 (the "License"); you may
10 # not use this file except in compliance with the License. You may obtain
11 # a copy of the License at
12
13 # http://www.apache.org/licenses/LICENSE-2.0
14
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18 # License for the specific language governing permissions and limitations
19 # under the License.
20
21 # For those usages not covered by the Apache License, Version 2.0 please
22 # contact: bdiaz@whitestack.com or glavado@whitestack.com
23 ##
24 import logging
25 import sys
26 import unittest
27 import uuid
28 from unittest.mock import patch, Mock
29
30 from kafka import KafkaProducer
31 from osm_common.dbmongo import DbMongo
32 from peewee import SqliteDatabase
33
34 from osm_policy_module.common.mon_client import MonClient
35 from osm_policy_module.core import database
36 from osm_policy_module.core.agent import PolicyModuleAgent
37 from osm_policy_module.core.database import ScalingRecord, ScalingAlarm
38
39 log = logging.getLogger()
40 log.level = logging.INFO
41 stream_handler = logging.StreamHandler(sys.stdout)
42 log.addHandler(stream_handler)
43
44 nsr_record_mock = {
45 "_id": "87776f33-b67c-417a-8119-cb08e4098951",
46 "crete-time": 1535392482.0044956,
47 "operational-status": "running",
48 "ssh-authorized-key": None,
49 "name-ref": "cirros_ns",
50 "nsd": {
51 "name": "cirros_vdu_scaling_ns",
52 "_id": "d7c8bd3c-eb39-4514-8847-19f01345524f",
53 "vld": [
54 {
55 "id": "cirros_nsd_vld1",
56 "name": "cirros_nsd_vld1",
57 "type": "ELAN",
58 "mgmt-network": "true",
59 "vnfd-connection-point-ref": [
60 {
61 "vnfd-id-ref": "cirros_vdu_scaling_vnf",
62 "member-vnf-index-ref": 1,
63 "vnfd-connection-point-ref": "eth0"
64 },
65 {
66 "vnfd-id-ref": "cirros_vdu_scaling_vnf",
67 "member-vnf-index-ref": 2,
68 "vnfd-connection-point-ref": "eth0"
69 }
70 ]
71 }
72 ],
73 "vendor": "OSM",
74 "constituent-vnfd": [
75 {
76 "member-vnf-index": "1",
77 "vnfd-id-ref": "cirros_vdu_scaling_vnf"
78 },
79 {
80 "member-vnf-index": "2",
81 "vnfd-id-ref": "cirros_vdu_scaling_vnf"
82 }
83 ],
84 "version": "1.0",
85 "id": "cirros_vdu_scaling_ns",
86 "description": "Simple NS example with a cirros_vdu_scaling_vnf",
87 "logo": "osm.png",
88 "_admin": {
89 "created": 1535392246.499733,
90 "userDefinedData": {
91
92 },
93 "usageSate": "NOT_IN_USE",
94 "storage": {
95 "zipfile": "package.tar.gz",
96 "fs": "local",
97 "path": "/app/storage/",
98 "folder": "d7c8bd3c-eb39-4514-8847-19f01345524f",
99 "pkg-dir": "cirros_nsd",
100 "descriptor": "cirros_nsd/cirros_vdu_scaling_nsd.yaml"
101 },
102 "onboardingState": "ONBOARDED",
103 "modified": 1535392246.499733,
104 "projects_read": [
105 "admin"
106 ],
107 "operationalState": "ENABLED",
108 "projects_write": [
109 "admin"
110 ]
111 },
112 "short-name": "cirros_vdu_scaling_ns"
113 },
114 "id": "87776f33-b67c-417a-8119-cb08e4098951",
115 "config-status": "configured",
116 "operational-events": [],
117 "_admin": {
118 "created": 1535392482.0084584,
119 "projects_read": [
120 "admin"
121 ],
122 "nsState": "INSTANTIATED",
123 "modified": 1535392482.0084584,
124 "projects_write": [
125 "admin"
126 ],
127 "deployed": {
128 "RO": {
129 "vnfd_id": {
130 "cirros_vdu_scaling_vnf": "7445e347-fe2f-431a-abc2-8b9be3d093c6"
131 },
132 "nsd_id": "92c56cf0-f8fa-488c-9afb-9f3d78ae6bbb",
133 "nsr_id": "637e12cd-c201-4c44-8ebd-70fb57a4dcee",
134 "nsr_status": "BUILD"
135 }
136 }
137 },
138 "nsd-ref": "cirros_vdu_scaling_ns",
139 "name": "cirros_ns",
140 "resource-orchestrator": "osmopenmano",
141 "instantiate_params": {
142 "nsDescription": "default description",
143 "nsdId": "d7c8bd3c-eb39-4514-8847-19f01345524f",
144 "nsr_id": "87776f33-b67c-417a-8119-cb08e4098951",
145 "nsName": "cirros_ns",
146 "vimAccountId": "be48ae31-1d46-4892-a4b4-d69abd55714b"
147 },
148 "description": "default description",
149 "constituent-vnfr-ref": [
150 "0d9d06ad-3fc2-418c-9934-465e815fafe2",
151 "3336eb44-77df-4c4f-9881-d2828d259864"
152 ],
153 "admin-status": "ENABLED",
154 "detailed-status": "done",
155 "datacenter": "be48ae31-1d46-4892-a4b4-d69abd55714b",
156 "orchestration-progress": {
157
158 },
159 "short-name": "cirros_ns",
160 "ns-instance-config-ref": "87776f33-b67c-417a-8119-cb08e4098951",
161 "nsd-name-ref": "cirros_vdu_scaling_ns",
162 "admin": {
163 "deployed": {
164 "RO": {
165 "nsr_status": "ACTIVE"
166 }
167 }
168 }
169 }
170
171 vnfr_record_mocks = [
172 {
173 "_id": "0d9d06ad-3fc2-418c-9934-465e815fafe2",
174 "ip-address": "192.168.160.2",
175 "created-time": 1535392482.0044956,
176 "vim-account-id": "be48ae31-1d46-4892-a4b4-d69abd55714b",
177 "vdur": [
178 {
179 "interfaces": [
180 {
181 "mac-address": "fa:16:3e:71:fd:b8",
182 "name": "eth0",
183 "ip-address": "192.168.160.2"
184 }
185 ],
186 "status": "ACTIVE",
187 "vim-id": "63a65636-9fc8-4022-b070-980823e6266a",
188 "name": "cirros_ns-1-cirros_vnfd-VM-1",
189 "status-detailed": None,
190 "ip-address": "192.168.160.2",
191 "vdu-id-ref": "cirros_vnfd-VM"
192 }
193 ],
194 "id": "0d9d06ad-3fc2-418c-9934-465e815fafe2",
195 "vnfd-ref": "cirros_vdu_scaling_vnf",
196 "vnfd-id": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
197 "_admin": {
198 "created": 1535392482.0067868,
199 "projects_read": [
200 "admin"
201 ],
202 "modified": 1535392482.0067868,
203 "projects_write": [
204 "admin"
205 ]
206 },
207 "nsr-id-ref": "87776f33-b67c-417a-8119-cb08e4098951",
208 "member-vnf-index-ref": "1",
209 "connection-point": [
210 {
211 "name": "eth0",
212 "id": None,
213 "connection-point-id": None
214 }
215 ]
216 },
217 {
218 "_id": "3336eb44-77df-4c4f-9881-d2828d259864",
219 "ip-address": "192.168.160.10",
220 "created-time": 1535392482.0044956,
221 "vim-account-id": "be48ae31-1d46-4892-a4b4-d69abd55714b",
222 "vdur": [
223 {
224 "interfaces": [
225 {
226 "mac-address": "fa:16:3e:1e:76:e8",
227 "name": "eth0",
228 "ip-address": "192.168.160.10"
229 }
230 ],
231 "status": "ACTIVE",
232 "vim-id": "a154b8d3-2b10-421a-a51d-4b391d9bd366",
233 "name": "cirros_ns-2-cirros_vnfd-VM-1",
234 "status-detailed": None,
235 "ip-address": "192.168.160.10",
236 "vdu-id-ref": "cirros_vnfd-VM"
237 }
238 ],
239 "id": "3336eb44-77df-4c4f-9881-d2828d259864",
240 "vnfd-ref": "cirros_vdu_scaling_vnf",
241 "vnfd-id": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
242 "_admin": {
243 "created": 1535392482.0076294,
244 "projects_read": [
245 "admin"
246 ],
247 "modified": 1535392482.0076294,
248 "projects_write": [
249 "admin"
250 ]
251 },
252 "nsr-id-ref": "87776f33-b67c-417a-8119-cb08e4098951",
253 "member-vnf-index-ref": "2",
254 "connection-point": [
255 {
256 "name": "eth0",
257 "id": None,
258 "connection-point-id": None
259 }
260 ]}]
261
262 nsd_record_mock = {'name': 'cirros_vdu_scaling_ns',
263 'version': '1.0',
264 'short-name': 'cirros_vdu_scaling_ns',
265 'logo': 'osm.png',
266 'id': 'cirros_vdu_scaling_ns',
267 'description': 'Simple NS example with a cirros_vdu_scaling_vnf',
268 'vendor': 'OSM',
269 'vld': [
270 {'name': 'cirros_nsd_vld1',
271 'type': 'ELAN',
272 'id': 'cirros_nsd_vld1',
273 'mgmt-network': 'true',
274 'vnfd-connection-point-ref': [
275 {'vnfd-id-ref': 'cirros_vdu_scaling_vnf',
276 'vnfd-connection-point-ref': 'eth0',
277 'member-vnf-index-ref': 1},
278 {'vnfd-id-ref': 'cirros_vdu_scaling_vnf',
279 'vnfd-connection-point-ref': 'eth0',
280 'member-vnf-index-ref': 2}]}],
281 'constituent-vnfd': [{'vnfd-id-ref': 'cirros_vdu_scaling_vnf',
282 'member-vnf-index': '1'},
283 {'vnfd-id-ref': 'cirros_vdu_scaling_vnf',
284 'member-vnf-index': '2'}]}
285
286 vnfd_record_mock = {
287 "_id": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
288 "name": "cirros_vdu_scaling_vnf",
289 "vendor": "OSM",
290 "vdu": [
291 {
292 "name": "cirros_vnfd-VM",
293 "monitoring-param": [
294 {
295 "id": "cirros_vnfd-VM_memory_util",
296 "nfvi-metric": "average_memory_utilization"
297 }
298 ],
299 "vm-flavor": {
300 "vcpu-count": 1,
301 "memory-mb": 256,
302 "storage-gb": 2
303 },
304 "description": "cirros_vnfd-VM",
305 "count": 1,
306 "id": "cirros_vnfd-VM",
307 "interface": [
308 {
309 "name": "eth0",
310 "external-connection-point-ref": "eth0",
311 "type": "EXTERNAL",
312 "virtual-interface": {
313 "bandwidth": "0",
314 "type": "VIRTIO",
315 "vpci": "0000:00:0a.0"
316 }
317 }
318 ],
319 "image": "cirros034"
320 }
321 ],
322 "monitoring-param": [
323 {
324 "id": "cirros_vnf_memory_util",
325 "name": "cirros_vnf_memory_util",
326 "aggregation-type": "AVERAGE",
327 "vdu-monitoring-param-ref": "cirros_vnfd-VM_memory_util",
328 "vdu-ref": "cirros_vnfd-VM"
329 }
330 ],
331 "description": "Simple VNF example with a cirros and a scaling group descriptor",
332 "id": "cirros_vdu_scaling_vnf",
333 "logo": "cirros-64.png",
334 "version": "1.0",
335 "connection-point": [
336 {
337 "name": "eth0",
338 "type": "VPORT"
339 }
340 ],
341 "mgmt-interface": {
342 "cp": "eth0"
343 },
344 "scaling-group-descriptor": [
345 {
346 "name": "scale_cirros_vnfd-VM",
347 "min-instance-count": 1,
348 "vdu": [
349 {
350 "count": 1,
351 "vdu-id-ref": "cirros_vnfd-VM"
352 }
353 ],
354 "max-instance-count": 10,
355 "scaling-policy": [
356 {
357 "name": "auto_memory_util_above_threshold",
358 "scaling-type": "automatic",
359 "cooldown-time": 60,
360 "threshold-time": 10,
361 "scaling-criteria": [
362 {
363 "name": "group1_memory_util_above_threshold",
364 "vnf-monitoring-param-ref": "cirros_vnf_memory_util",
365 "scale-out-threshold": 80,
366 "scale-out-relational-operation": "GT",
367 "scale-in-relational-operation": "LT",
368 "scale-in-threshold": 20
369 }
370 ]
371 }
372 ]
373 }
374 ],
375 "short-name": "cirros_vdu_scaling_vnf",
376 "_admin": {
377 "created": 1535392242.6281035,
378 "modified": 1535392242.6281035,
379 "storage": {
380 "zipfile": "package.tar.gz",
381 "pkg-dir": "cirros_vnf",
382 "path": "/app/storage/",
383 "folder": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
384 "fs": "local",
385 "descriptor": "cirros_vnf/cirros_vdu_scaling_vnfd.yaml"
386 },
387 "usageSate": "NOT_IN_USE",
388 "onboardingState": "ONBOARDED",
389 "userDefinedData": {
390
391 },
392 "projects_read": [
393 "admin"
394 ],
395 "operationalState": "ENABLED",
396 "projects_write": [
397 "admin"
398 ]
399 }
400 }
401
402 test_db = SqliteDatabase(':memory:')
403
404 MODELS = [ScalingRecord, ScalingAlarm]
405
406
407 class PolicyModuleAgentTest(unittest.TestCase):
408 def setUp(self):
409 super()
410 database.db = test_db
411 test_db.bind(MODELS)
412 test_db.connect()
413 test_db.drop_tables(MODELS)
414 test_db.create_tables(MODELS)
415
416 def tearDown(self):
417 super()
418
419 @patch.object(DbMongo, 'db_connect', Mock())
420 @patch.object(KafkaProducer, '__init__')
421 @patch.object(MonClient, 'create_alarm')
422 @patch.object(PolicyModuleAgent, '_get_vnfd')
423 @patch.object(PolicyModuleAgent, '_get_nsr')
424 @patch.object(PolicyModuleAgent, '_get_vnfr')
425 def test_configure_scaling_groups(self, get_vnfr, get_nsr, get_vnfd, create_alarm, kafka_producer_init):
426 def _test_configure_scaling_groups_get_vnfr(*args, **kwargs):
427 if '1' in args[1]:
428 return vnfr_record_mocks[0]
429 if '2' in args[1]:
430 return vnfr_record_mocks[1]
431
432 def _test_configure_scaling_groups_create_alarm(*args, **kwargs):
433 return uuid.uuid4()
434
435 kafka_producer_init.return_value = None
436 get_vnfr.side_effect = _test_configure_scaling_groups_get_vnfr
437 get_nsr.return_value = nsr_record_mock
438 get_vnfd.return_value = vnfd_record_mock
439 create_alarm.side_effect = _test_configure_scaling_groups_create_alarm
440 agent = PolicyModuleAgent()
441 agent._configure_scaling_groups("test_nsr_id")
442 create_alarm.assert_any_call(metric_name='average_memory_utilization',
443 ns_id='test_nsr_id',
444 operation='GT',
445 statistic='AVERAGE',
446 threshold=80,
447 vdu_name='cirros_vnfd-VM',
448 vnf_member_index='1')
449 scaling_record = ScalingRecord.get()
450 self.assertEqual(scaling_record.name, 'scale_cirros_vnfd-VM')
451 self.assertEqual(scaling_record.nsr_id, 'test_nsr_id')
452 self.assertIsNotNone(scaling_record)
453
454
455 if __name__ == '__main__':
456 unittest.main()