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
29 from unittest
.mock
import patch
, Mock
31 from kafka
import KafkaProducer
32 from osm_common
.dbmongo
import DbMongo
33 from peewee
import SqliteDatabase
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
41 log
= logging
.getLogger()
42 log
.level
= logging
.INFO
43 stream_handler
= logging
.StreamHandler(sys
.stdout
)
44 log
.addHandler(stream_handler
)
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",
53 "name": "cirros_vdu_scaling_ns",
54 "_id": "d7c8bd3c-eb39-4514-8847-19f01345524f",
57 "id": "cirros_nsd_vld1",
58 "name": "cirros_nsd_vld1",
60 "mgmt-network": "true",
61 "vnfd-connection-point-ref": [
63 "vnfd-id-ref": "cirros_vdu_scaling_vnf",
64 "member-vnf-index-ref": 1,
65 "vnfd-connection-point-ref": "eth0"
68 "vnfd-id-ref": "cirros_vdu_scaling_vnf",
69 "member-vnf-index-ref": 2,
70 "vnfd-connection-point-ref": "eth0"
78 "member-vnf-index": "1",
79 "vnfd-id-ref": "cirros_vdu_scaling_vnf"
82 "member-vnf-index": "2",
83 "vnfd-id-ref": "cirros_vdu_scaling_vnf"
87 "id": "cirros_vdu_scaling_ns",
88 "description": "Simple NS example with a cirros_vdu_scaling_vnf",
91 "created": 1535392246.499733,
95 "usageSate": "NOT_IN_USE",
97 "zipfile": "package.tar.gz",
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"
104 "onboardingState": "ONBOARDED",
105 "modified": 1535392246.499733,
109 "operationalState": "ENABLED",
114 "short-name": "cirros_vdu_scaling_ns"
116 "id": "87776f33-b67c-417a-8119-cb08e4098951",
117 "config-status": "configured",
118 "operational-events": [],
120 "created": 1535392482.0084584,
124 "nsState": "INSTANTIATED",
125 "modified": 1535392482.0084584,
132 "cirros_vdu_scaling_vnf": "7445e347-fe2f-431a-abc2-8b9be3d093c6"
134 "nsd_id": "92c56cf0-f8fa-488c-9afb-9f3d78ae6bbb",
135 "nsr_id": "637e12cd-c201-4c44-8ebd-70fb57a4dcee",
136 "nsr_status": "BUILD"
140 "nsd-ref": "cirros_vdu_scaling_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"
150 "description": "default description",
151 "constituent-vnfr-ref": [
152 "0d9d06ad-3fc2-418c-9934-465e815fafe2",
153 "3336eb44-77df-4c4f-9881-d2828d259864"
155 "admin-status": "ENABLED",
156 "detailed-status": "done",
157 "datacenter": "be48ae31-1d46-4892-a4b4-d69abd55714b",
158 "orchestration-progress": {
161 "short-name": "cirros_ns",
162 "ns-instance-config-ref": "87776f33-b67c-417a-8119-cb08e4098951",
163 "nsd-name-ref": "cirros_vdu_scaling_ns",
167 "nsr_status": "ACTIVE"
173 vnfr_record_mocks
= [
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",
183 "mac-address": "fa:16:3e:71:fd:b8",
185 "ip-address": "192.168.160.2"
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"
196 "id": "0d9d06ad-3fc2-418c-9934-465e815fafe2",
197 "vnfd-ref": "cirros_vdu_scaling_vnf",
198 "vnfd-id": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
200 "created": 1535392482.0067868,
204 "modified": 1535392482.0067868,
209 "nsr-id-ref": "87776f33-b67c-417a-8119-cb08e4098951",
210 "member-vnf-index-ref": "1",
211 "connection-point": [
215 "connection-point-id": None
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",
228 "mac-address": "fa:16:3e:1e:76:e8",
230 "ip-address": "192.168.160.10"
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"
241 "id": "3336eb44-77df-4c4f-9881-d2828d259864",
242 "vnfd-ref": "cirros_vdu_scaling_vnf",
243 "vnfd-id": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
245 "created": 1535392482.0076294,
249 "modified": 1535392482.0076294,
254 "nsr-id-ref": "87776f33-b67c-417a-8119-cb08e4098951",
255 "member-vnf-index-ref": "2",
256 "connection-point": [
260 "connection-point-id": None
264 nsd_record_mock
= {'name': 'cirros_vdu_scaling_ns',
266 'short-name': 'cirros_vdu_scaling_ns',
268 'id': 'cirros_vdu_scaling_ns',
269 'description': 'Simple NS example with a cirros_vdu_scaling_vnf',
272 {'name': 'cirros_nsd_vld1',
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'}]}
289 "_id": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
290 "name": "cirros_vdu_scaling_vnf",
294 "name": "cirros_vnfd-VM",
295 "monitoring-param": [
297 "id": "cirros_vnfd-VM_memory_util",
298 "nfvi-metric": "average_memory_utilization"
306 "description": "cirros_vnfd-VM",
308 "id": "cirros_vnfd-VM",
312 "external-connection-point-ref": "eth0",
314 "virtual-interface": {
317 "vpci": "0000:00:0a.0"
324 "monitoring-param": [
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"
335 "id": "haproxy_users",
336 "name": "haproxy_users",
337 "aggregation-type": "AVERAGE",
339 {"vnf-metric-name-ref": "users"}
342 "vdu-configuration": {
344 "charm": "testmetrics",
351 "description": "Simple VNF example with a cirros and a scaling group descriptor",
352 "id": "cirros_vdu_scaling_vnf",
353 "logo": "cirros-64.png",
355 "connection-point": [
364 "scaling-group-descriptor": [
366 "name": "scale_cirros_vnfd-VM",
367 "min-instance-count": 1,
371 "vdu-id-ref": "cirros_vnfd-VM"
374 "max-instance-count": 10,
377 "name": "auto_memory_util_above_threshold",
378 "scaling-type": "automatic",
380 "threshold-time": 10,
381 "scaling-criteria": [
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
395 "short-name": "cirros_vdu_scaling_vnf",
397 "created": 1535392242.6281035,
398 "modified": 1535392242.6281035,
400 "zipfile": "package.tar.gz",
401 "pkg-dir": "cirros_vnf",
402 "path": "/app/storage/",
403 "folder": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
405 "descriptor": "cirros_vnf/cirros_vdu_scaling_vnfd.yaml"
407 "usageSate": "NOT_IN_USE",
408 "onboardingState": "ONBOARDED",
415 "operationalState": "ENABLED",
422 test_db
= SqliteDatabase(':memory:')
424 MODELS
= [ScalingGroup
, ScalingPolicy
, ScalingCriteria
, ScalingAlarm
]
427 class PolicyModuleAgentTest(unittest
.TestCase
):
430 database
.db
= test_db
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)
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
):
450 return vnfr_record_mocks
[0]
452 return vnfr_record_mocks
[1]
454 async def _test_configure_scaling_groups_create_alarm(*args
, **kwargs
):
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',
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',
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',
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',
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')
497 if __name__
== '__main__':