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