1 # -*- coding: utf-8 -*-
3 # Copyright 2018 Whitestack, LLC
4 # *************************************************************
6 # This file is part of OSM Monitoring module
7 # All Rights Reserved to Whitestack, LLC
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
13 # http://www.apache.org/licenses/LICENSE-2.0
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
21 # For those usages not covered by the Apache License, Version 2.0 please
22 # contact: bdiaz@whitestack.com or glavado@whitestack.com
30 from unittest
.mock
import patch
, Mock
32 from kafka
import KafkaProducer
33 from osm_common
.dbmongo
import DbMongo
34 from playhouse
.db_url
import connect
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
.core
.agent
import PolicyModuleAgent
40 from osm_policy_module
.core
.config
import Config
41 from osm_policy_module
.core
.database
import ScalingGroup
, ScalingAlarm
, ScalingPolicy
, ScalingCriteria
, VnfAlarm
, \
44 log
= logging
.getLogger()
45 log
.level
= logging
.INFO
46 stream_handler
= logging
.StreamHandler(sys
.stdout
)
47 log
.addHandler(stream_handler
)
50 "_id": "87776f33-b67c-417a-8119-cb08e4098951",
51 "crete-time": 1535392482.0044956,
52 "operational-status": "running",
53 "ssh-authorized-key": None,
54 "name-ref": "cirros_ns",
56 "name": "cirros_vdu_scaling_ns",
57 "_id": "d7c8bd3c-eb39-4514-8847-19f01345524f",
60 "id": "cirros_nsd_vld1",
61 "name": "cirros_nsd_vld1",
63 "mgmt-network": "true",
64 "vnfd-connection-point-ref": [
66 "vnfd-id-ref": "cirros_vdu_scaling_vnf",
67 "member-vnf-index-ref": 1,
68 "vnfd-connection-point-ref": "eth0"
71 "vnfd-id-ref": "cirros_vdu_scaling_vnf",
72 "member-vnf-index-ref": 2,
73 "vnfd-connection-point-ref": "eth0"
81 "member-vnf-index": "1",
82 "vnfd-id-ref": "cirros_vdu_scaling_vnf"
85 "member-vnf-index": "2",
86 "vnfd-id-ref": "cirros_vdu_scaling_vnf"
90 "id": "cirros_vdu_scaling_ns",
91 "description": "Simple NS example with a cirros_vdu_scaling_vnf",
94 "created": 1535392246.499733,
98 "usageSate": "NOT_IN_USE",
100 "zipfile": "package.tar.gz",
102 "path": "/app/storage/",
103 "folder": "d7c8bd3c-eb39-4514-8847-19f01345524f",
104 "pkg-dir": "cirros_nsd",
105 "descriptor": "cirros_nsd/cirros_vdu_scaling_nsd.yaml"
107 "onboardingState": "ONBOARDED",
108 "modified": 1535392246.499733,
112 "operationalState": "ENABLED",
117 "short-name": "cirros_vdu_scaling_ns"
119 "id": "87776f33-b67c-417a-8119-cb08e4098951",
120 "config-status": "configured",
121 "operational-events": [],
123 "created": 1535392482.0084584,
127 "nsState": "INSTANTIATED",
128 "modified": 1535392482.0084584,
135 "cirros_vdu_scaling_vnf": "7445e347-fe2f-431a-abc2-8b9be3d093c6"
137 "nsd_id": "92c56cf0-f8fa-488c-9afb-9f3d78ae6bbb",
138 "nsr_id": "637e12cd-c201-4c44-8ebd-70fb57a4dcee",
139 "nsr_status": "BUILD"
143 "nsd-ref": "cirros_vdu_scaling_ns",
145 "resource-orchestrator": "osmopenmano",
146 "instantiate_params": {
147 "nsDescription": "default description",
148 "nsdId": "d7c8bd3c-eb39-4514-8847-19f01345524f",
149 "nsr_id": "87776f33-b67c-417a-8119-cb08e4098951",
150 "nsName": "cirros_ns",
151 "vimAccountId": "be48ae31-1d46-4892-a4b4-d69abd55714b"
153 "description": "default description",
154 "constituent-vnfr-ref": [
155 "0d9d06ad-3fc2-418c-9934-465e815fafe2",
156 "3336eb44-77df-4c4f-9881-d2828d259864"
158 "admin-status": "ENABLED",
159 "detailed-status": "done",
160 "datacenter": "be48ae31-1d46-4892-a4b4-d69abd55714b",
161 "orchestration-progress": {
164 "short-name": "cirros_ns",
165 "ns-instance-config-ref": "87776f33-b67c-417a-8119-cb08e4098951",
166 "nsd-name-ref": "cirros_vdu_scaling_ns",
170 "nsr_status": "ACTIVE"
176 vnfr_record_mocks
= [
178 "_id": "0d9d06ad-3fc2-418c-9934-465e815fafe2",
179 "ip-address": "192.168.160.2",
180 "created-time": 1535392482.0044956,
181 "vim-account-id": "be48ae31-1d46-4892-a4b4-d69abd55714b",
186 "mac-address": "fa:16:3e:71:fd:b8",
188 "ip-address": "192.168.160.2"
192 "vim-id": "63a65636-9fc8-4022-b070-980823e6266a",
193 "name": "cirros_ns-1-cirros_vnfd-VM-1",
194 "status-detailed": None,
195 "ip-address": "192.168.160.2",
196 "vdu-id-ref": "cirros_vnfd-VM"
199 "id": "0d9d06ad-3fc2-418c-9934-465e815fafe2",
200 "vnfd-ref": "cirros_vdu_scaling_vnf",
201 "vnfd-id": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
203 "created": 1535392482.0067868,
207 "modified": 1535392482.0067868,
212 "nsr-id-ref": "87776f33-b67c-417a-8119-cb08e4098951",
213 "member-vnf-index-ref": "1",
214 "connection-point": [
218 "connection-point-id": None
223 "_id": "3336eb44-77df-4c4f-9881-d2828d259864",
224 "ip-address": "192.168.160.10",
225 "created-time": 1535392482.0044956,
226 "vim-account-id": "be48ae31-1d46-4892-a4b4-d69abd55714b",
231 "mac-address": "fa:16:3e:1e:76:e8",
233 "ip-address": "192.168.160.10"
237 "vim-id": "a154b8d3-2b10-421a-a51d-4b391d9bd366",
238 "name": "cirros_ns-2-cirros_vnfd-VM-1",
239 "status-detailed": None,
240 "ip-address": "192.168.160.10",
241 "vdu-id-ref": "cirros_vnfd-VM"
244 "id": "3336eb44-77df-4c4f-9881-d2828d259864",
245 "vnfd-ref": "cirros_vdu_scaling_vnf",
246 "vnfd-id": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
248 "created": 1535392482.0076294,
252 "modified": 1535392482.0076294,
257 "nsr-id-ref": "87776f33-b67c-417a-8119-cb08e4098951",
258 "member-vnf-index-ref": "2",
259 "connection-point": [
263 "connection-point-id": None
267 nsd_record_mock
= {'name': 'cirros_vdu_scaling_ns',
269 'short-name': 'cirros_vdu_scaling_ns',
271 'id': 'cirros_vdu_scaling_ns',
272 'description': 'Simple NS example with a cirros_vdu_scaling_vnf',
275 {'name': 'cirros_nsd_vld1',
277 'id': 'cirros_nsd_vld1',
278 'mgmt-network': 'true',
279 'vnfd-connection-point-ref': [
280 {'vnfd-id-ref': 'cirros_vdu_scaling_vnf',
281 'vnfd-connection-point-ref': 'eth0',
282 'member-vnf-index-ref': 1},
283 {'vnfd-id-ref': 'cirros_vdu_scaling_vnf',
284 'vnfd-connection-point-ref': 'eth0',
285 'member-vnf-index-ref': 2}]}],
286 'constituent-vnfd': [{'vnfd-id-ref': 'cirros_vdu_scaling_vnf',
287 'member-vnf-index': '1'},
288 {'vnfd-id-ref': 'cirros_vdu_scaling_vnf',
289 'member-vnf-index': '2'}]}
292 "_id": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
293 "name": "cirros_vdu_scaling_vnf",
297 "name": "cirros_vnfd-VM",
298 "monitoring-param": [
300 "id": "cirros_vnfd-VM_memory_util",
301 "nfvi-metric": "average_memory_utilization"
309 "description": "cirros_vnfd-VM",
311 "id": "cirros_vnfd-VM",
315 "external-connection-point-ref": "eth0",
317 "virtual-interface": {
320 "vpci": "0000:00:0a.0"
324 "image": "cirros034",
329 "insufficient-data": [
331 "url": "localhost:9090"
336 "url": "localhost:9090"
341 "url": "localhost:9090"
345 "alarm-id": "alarm-1",
347 "vnf-monitoring-param-ref": "cirros_vnf_memory_util"
352 "monitoring-param": [
354 "id": "cirros_vnf_memory_util",
355 "name": "cirros_vnf_memory_util",
356 "aggregation-type": "AVERAGE",
357 "vdu-monitoring-param": {
358 "vdu-monitoring-param-ref": "cirros_vnfd-VM_memory_util",
359 "vdu-ref": "cirros_vnfd-VM"
363 "id": "haproxy_users",
364 "name": "haproxy_users",
365 "aggregation-type": "AVERAGE",
367 {"vnf-metric-name-ref": "users"}
370 "vdu-configuration": {
372 "charm": "testmetrics",
379 "description": "Simple VNF example with a cirros and a scaling group descriptor",
380 "id": "cirros_vdu_scaling_vnf",
381 "logo": "cirros-64.png",
383 "connection-point": [
392 "scaling-group-descriptor": [
394 "name": "scale_cirros_vnfd-VM",
395 "min-instance-count": 1,
399 "vdu-id-ref": "cirros_vnfd-VM"
402 "max-instance-count": 10,
405 "name": "auto_memory_util_above_threshold",
406 "scaling-type": "automatic",
408 "threshold-time": 10,
409 "scaling-criteria": [
411 "name": "group1_memory_util_above_threshold",
412 "vnf-monitoring-param-ref": "cirros_vnf_memory_util",
413 "scale-out-threshold": 80,
414 "scale-out-relational-operation": "GT",
415 "scale-in-relational-operation": "LT",
416 "scale-in-threshold": 20
423 "short-name": "cirros_vdu_scaling_vnf",
425 "created": 1535392242.6281035,
426 "modified": 1535392242.6281035,
428 "zipfile": "package.tar.gz",
429 "pkg-dir": "cirros_vnf",
430 "path": "/app/storage/",
431 "folder": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
433 "descriptor": "cirros_vnf/cirros_vdu_scaling_vnfd.yaml"
435 "usageSate": "NOT_IN_USE",
436 "onboardingState": "ONBOARDED",
443 "operationalState": "ENABLED",
450 MODELS
= [ScalingGroup
, ScalingPolicy
, ScalingCriteria
, ScalingAlarm
, VnfAlarm
, AlarmAction
]
453 class PolicyModuleAgentTest(unittest
.TestCase
):
456 database
.db
.initialize(connect('sqlite:///test_db.sqlite'))
457 database
.db
.bind(MODELS
)
458 database
.db
.connect()
459 database
.db
.drop_tables(MODELS
)
460 database
.db
.create_tables(MODELS
)
462 self
.loop
= asyncio
.new_event_loop()
466 os
.remove('test_db.sqlite')
468 @patch.object(DbMongo
, 'db_connect', Mock())
469 @patch.object(KafkaProducer
, '__init__')
470 @patch.object(MonClient
, 'create_alarm')
471 @patch.object(CommonDbClient
, 'get_vnfd')
472 @patch.object(CommonDbClient
, 'get_nsr')
473 @patch.object(CommonDbClient
, 'get_vnfr')
474 def test_configure_scaling_groups(self
, get_vnfr
, get_nsr
, get_vnfd
, create_alarm
, kafka_producer_init
):
475 def _test_configure_scaling_groups_get_vnfr(*args
, **kwargs
):
477 return vnfr_record_mocks
[0]
479 return vnfr_record_mocks
[1]
481 async def _test_configure_scaling_groups_create_alarm(*args
, **kwargs
):
484 kafka_producer_init
.return_value
= None
485 get_vnfr
.side_effect
= _test_configure_scaling_groups_get_vnfr
486 get_nsr
.return_value
= nsr_record_mock
487 get_vnfd
.return_value
= vnfd_record_mock
488 create_alarm
.side_effect
= _test_configure_scaling_groups_create_alarm
490 agent
= PolicyModuleAgent(config
, self
.loop
)
491 self
.loop
.run_until_complete(agent
.autoscaling_service
.configure_scaling_groups("test_nsr_id"))
492 create_alarm
.assert_any_call(metric_name
='cirros_vnf_memory_util',
497 vdu_name
='cirros_ns-1-cirros_vnfd-VM-1',
498 vnf_member_index
='1')
499 create_alarm
.assert_any_call(metric_name
='cirros_vnf_memory_util',
504 vdu_name
='cirros_ns-1-cirros_vnfd-VM-1',
505 vnf_member_index
='1')
506 create_alarm
.assert_any_call(metric_name
='cirros_vnf_memory_util',
511 vdu_name
='cirros_ns-2-cirros_vnfd-VM-1',
512 vnf_member_index
='2')
513 create_alarm
.assert_any_call(metric_name
='cirros_vnf_memory_util',
518 vdu_name
='cirros_ns-2-cirros_vnfd-VM-1',
519 vnf_member_index
='2')
520 scaling_record
= ScalingGroup
.get()
521 self
.assertEqual(scaling_record
.name
, 'scale_cirros_vnfd-VM')
522 self
.assertEqual(scaling_record
.nsr_id
, 'test_nsr_id')
524 @patch.object(DbMongo
, 'db_connect', Mock())
525 @patch.object(KafkaProducer
, '__init__')
526 @patch.object(MonClient
, 'create_alarm')
527 @patch.object(CommonDbClient
, 'get_vnfd')
528 @patch.object(CommonDbClient
, 'get_nsr')
529 @patch.object(CommonDbClient
, 'get_vnfr')
530 def test_configure_vnf_alarms(self
, get_vnfr
, get_nsr
, get_vnfd
, create_alarm
, kafka_producer_init
):
531 def _test_configure_scaling_groups_get_vnfr(*args
, **kwargs
):
533 return vnfr_record_mocks
[0]
535 return vnfr_record_mocks
[1]
537 async def _test_configure_vnf_alarms_create_alarm(*args
, **kwargs
):
540 kafka_producer_init
.return_value
= None
541 get_vnfr
.side_effect
= _test_configure_scaling_groups_get_vnfr
542 get_nsr
.return_value
= nsr_record_mock
543 get_vnfd
.return_value
= vnfd_record_mock
544 create_alarm
.side_effect
= _test_configure_vnf_alarms_create_alarm
546 agent
= PolicyModuleAgent(config
, self
.loop
)
547 self
.loop
.run_until_complete(agent
.alarming_service
.configure_vnf_alarms("test_nsr_id"))
548 create_alarm
.assert_any_call(metric_name
='cirros_vnf_memory_util',
553 vdu_name
='cirros_ns-1-cirros_vnfd-VM-1',
554 vnf_member_index
='1')
555 create_alarm
.assert_any_call(metric_name
='cirros_vnf_memory_util',
560 vdu_name
='cirros_ns-2-cirros_vnfd-VM-1',
561 vnf_member_index
='2')
564 if __name__
== '__main__':