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