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 playhouse
.db_url
import connect
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
.config
import Config
40 from osm_policy_module
.core
.database
import ScalingGroup
, ScalingAlarm
, ScalingPolicy
, ScalingCriteria
42 log
= logging
.getLogger()
43 log
.level
= logging
.INFO
44 stream_handler
= logging
.StreamHandler(sys
.stdout
)
45 log
.addHandler(stream_handler
)
48 "_id": "87776f33-b67c-417a-8119-cb08e4098951",
49 "crete-time": 1535392482.0044956,
50 "operational-status": "running",
51 "ssh-authorized-key": None,
52 "name-ref": "cirros_ns",
54 "name": "cirros_vdu_scaling_ns",
55 "_id": "d7c8bd3c-eb39-4514-8847-19f01345524f",
58 "id": "cirros_nsd_vld1",
59 "name": "cirros_nsd_vld1",
61 "mgmt-network": "true",
62 "vnfd-connection-point-ref": [
64 "vnfd-id-ref": "cirros_vdu_scaling_vnf",
65 "member-vnf-index-ref": 1,
66 "vnfd-connection-point-ref": "eth0"
69 "vnfd-id-ref": "cirros_vdu_scaling_vnf",
70 "member-vnf-index-ref": 2,
71 "vnfd-connection-point-ref": "eth0"
79 "member-vnf-index": "1",
80 "vnfd-id-ref": "cirros_vdu_scaling_vnf"
83 "member-vnf-index": "2",
84 "vnfd-id-ref": "cirros_vdu_scaling_vnf"
88 "id": "cirros_vdu_scaling_ns",
89 "description": "Simple NS example with a cirros_vdu_scaling_vnf",
92 "created": 1535392246.499733,
96 "usageSate": "NOT_IN_USE",
98 "zipfile": "package.tar.gz",
100 "path": "/app/storage/",
101 "folder": "d7c8bd3c-eb39-4514-8847-19f01345524f",
102 "pkg-dir": "cirros_nsd",
103 "descriptor": "cirros_nsd/cirros_vdu_scaling_nsd.yaml"
105 "onboardingState": "ONBOARDED",
106 "modified": 1535392246.499733,
110 "operationalState": "ENABLED",
115 "short-name": "cirros_vdu_scaling_ns"
117 "id": "87776f33-b67c-417a-8119-cb08e4098951",
118 "config-status": "configured",
119 "operational-events": [],
121 "created": 1535392482.0084584,
125 "nsState": "INSTANTIATED",
126 "modified": 1535392482.0084584,
133 "cirros_vdu_scaling_vnf": "7445e347-fe2f-431a-abc2-8b9be3d093c6"
135 "nsd_id": "92c56cf0-f8fa-488c-9afb-9f3d78ae6bbb",
136 "nsr_id": "637e12cd-c201-4c44-8ebd-70fb57a4dcee",
137 "nsr_status": "BUILD"
141 "nsd-ref": "cirros_vdu_scaling_ns",
143 "resource-orchestrator": "osmopenmano",
144 "instantiate_params": {
145 "nsDescription": "default description",
146 "nsdId": "d7c8bd3c-eb39-4514-8847-19f01345524f",
147 "nsr_id": "87776f33-b67c-417a-8119-cb08e4098951",
148 "nsName": "cirros_ns",
149 "vimAccountId": "be48ae31-1d46-4892-a4b4-d69abd55714b"
151 "description": "default description",
152 "constituent-vnfr-ref": [
153 "0d9d06ad-3fc2-418c-9934-465e815fafe2",
154 "3336eb44-77df-4c4f-9881-d2828d259864"
156 "admin-status": "ENABLED",
157 "detailed-status": "done",
158 "datacenter": "be48ae31-1d46-4892-a4b4-d69abd55714b",
159 "orchestration-progress": {
162 "short-name": "cirros_ns",
163 "ns-instance-config-ref": "87776f33-b67c-417a-8119-cb08e4098951",
164 "nsd-name-ref": "cirros_vdu_scaling_ns",
168 "nsr_status": "ACTIVE"
174 vnfr_record_mocks
= [
176 "_id": "0d9d06ad-3fc2-418c-9934-465e815fafe2",
177 "ip-address": "192.168.160.2",
178 "created-time": 1535392482.0044956,
179 "vim-account-id": "be48ae31-1d46-4892-a4b4-d69abd55714b",
184 "mac-address": "fa:16:3e:71:fd:b8",
186 "ip-address": "192.168.160.2"
190 "vim-id": "63a65636-9fc8-4022-b070-980823e6266a",
191 "name": "cirros_ns-1-cirros_vnfd-VM-1",
192 "status-detailed": None,
193 "ip-address": "192.168.160.2",
194 "vdu-id-ref": "cirros_vnfd-VM"
197 "id": "0d9d06ad-3fc2-418c-9934-465e815fafe2",
198 "vnfd-ref": "cirros_vdu_scaling_vnf",
199 "vnfd-id": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
201 "created": 1535392482.0067868,
205 "modified": 1535392482.0067868,
210 "nsr-id-ref": "87776f33-b67c-417a-8119-cb08e4098951",
211 "member-vnf-index-ref": "1",
212 "connection-point": [
216 "connection-point-id": None
221 "_id": "3336eb44-77df-4c4f-9881-d2828d259864",
222 "ip-address": "192.168.160.10",
223 "created-time": 1535392482.0044956,
224 "vim-account-id": "be48ae31-1d46-4892-a4b4-d69abd55714b",
229 "mac-address": "fa:16:3e:1e:76:e8",
231 "ip-address": "192.168.160.10"
235 "vim-id": "a154b8d3-2b10-421a-a51d-4b391d9bd366",
236 "name": "cirros_ns-2-cirros_vnfd-VM-1",
237 "status-detailed": None,
238 "ip-address": "192.168.160.10",
239 "vdu-id-ref": "cirros_vnfd-VM"
242 "id": "3336eb44-77df-4c4f-9881-d2828d259864",
243 "vnfd-ref": "cirros_vdu_scaling_vnf",
244 "vnfd-id": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
246 "created": 1535392482.0076294,
250 "modified": 1535392482.0076294,
255 "nsr-id-ref": "87776f33-b67c-417a-8119-cb08e4098951",
256 "member-vnf-index-ref": "2",
257 "connection-point": [
261 "connection-point-id": None
265 nsd_record_mock
= {'name': 'cirros_vdu_scaling_ns',
267 'short-name': 'cirros_vdu_scaling_ns',
269 'id': 'cirros_vdu_scaling_ns',
270 'description': 'Simple NS example with a cirros_vdu_scaling_vnf',
273 {'name': 'cirros_nsd_vld1',
275 'id': 'cirros_nsd_vld1',
276 'mgmt-network': 'true',
277 'vnfd-connection-point-ref': [
278 {'vnfd-id-ref': 'cirros_vdu_scaling_vnf',
279 'vnfd-connection-point-ref': 'eth0',
280 'member-vnf-index-ref': 1},
281 {'vnfd-id-ref': 'cirros_vdu_scaling_vnf',
282 'vnfd-connection-point-ref': 'eth0',
283 'member-vnf-index-ref': 2}]}],
284 'constituent-vnfd': [{'vnfd-id-ref': 'cirros_vdu_scaling_vnf',
285 'member-vnf-index': '1'},
286 {'vnfd-id-ref': 'cirros_vdu_scaling_vnf',
287 'member-vnf-index': '2'}]}
290 "_id": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
291 "name": "cirros_vdu_scaling_vnf",
295 "name": "cirros_vnfd-VM",
296 "monitoring-param": [
298 "id": "cirros_vnfd-VM_memory_util",
299 "nfvi-metric": "average_memory_utilization"
307 "description": "cirros_vnfd-VM",
309 "id": "cirros_vnfd-VM",
313 "external-connection-point-ref": "eth0",
315 "virtual-interface": {
318 "vpci": "0000:00:0a.0"
325 "monitoring-param": [
327 "id": "cirros_vnf_memory_util",
328 "name": "cirros_vnf_memory_util",
329 "aggregation-type": "AVERAGE",
330 "vdu-monitoring-param": {
331 "vdu-monitoring-param-ref": "cirros_vnfd-VM_memory_util",
332 "vdu-ref": "cirros_vnfd-VM"
336 "id": "haproxy_users",
337 "name": "haproxy_users",
338 "aggregation-type": "AVERAGE",
340 {"vnf-metric-name-ref": "users"}
343 "vdu-configuration": {
345 "charm": "testmetrics",
352 "description": "Simple VNF example with a cirros and a scaling group descriptor",
353 "id": "cirros_vdu_scaling_vnf",
354 "logo": "cirros-64.png",
356 "connection-point": [
365 "scaling-group-descriptor": [
367 "name": "scale_cirros_vnfd-VM",
368 "min-instance-count": 1,
372 "vdu-id-ref": "cirros_vnfd-VM"
375 "max-instance-count": 10,
378 "name": "auto_memory_util_above_threshold",
379 "scaling-type": "automatic",
381 "threshold-time": 10,
382 "scaling-criteria": [
384 "name": "group1_memory_util_above_threshold",
385 "vnf-monitoring-param-ref": "cirros_vnf_memory_util",
386 "scale-out-threshold": 80,
387 "scale-out-relational-operation": "GT",
388 "scale-in-relational-operation": "LT",
389 "scale-in-threshold": 20
396 "short-name": "cirros_vdu_scaling_vnf",
398 "created": 1535392242.6281035,
399 "modified": 1535392242.6281035,
401 "zipfile": "package.tar.gz",
402 "pkg-dir": "cirros_vnf",
403 "path": "/app/storage/",
404 "folder": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
406 "descriptor": "cirros_vnf/cirros_vdu_scaling_vnfd.yaml"
408 "usageSate": "NOT_IN_USE",
409 "onboardingState": "ONBOARDED",
416 "operationalState": "ENABLED",
423 MODELS
= [ScalingGroup
, ScalingPolicy
, ScalingCriteria
, ScalingAlarm
]
426 class PolicyModuleAgentTest(unittest
.TestCase
):
429 database
.db
.initialize(connect('sqlite://'))
430 database
.db
.bind(MODELS
)
431 database
.db
.connect()
432 database
.db
.drop_tables(MODELS
)
433 database
.db
.create_tables(MODELS
)
434 self
.loop
= asyncio
.new_event_loop()
439 @patch.object(DbMongo
, 'db_connect', Mock())
440 @patch.object(KafkaProducer
, '__init__')
441 @patch.object(MonClient
, 'create_alarm')
442 @patch.object(CommonDbClient
, 'get_vnfd')
443 @patch.object(CommonDbClient
, 'get_nsr')
444 @patch.object(CommonDbClient
, 'get_vnfr')
445 def test_configure_scaling_groups(self
, get_vnfr
, get_nsr
, get_vnfd
, create_alarm
, kafka_producer_init
):
446 def _test_configure_scaling_groups_get_vnfr(*args
, **kwargs
):
448 return vnfr_record_mocks
[0]
450 return vnfr_record_mocks
[1]
452 async def _test_configure_scaling_groups_create_alarm(*args
, **kwargs
):
455 kafka_producer_init
.return_value
= None
456 get_vnfr
.side_effect
= _test_configure_scaling_groups_get_vnfr
457 get_nsr
.return_value
= nsr_record_mock
458 get_vnfd
.return_value
= vnfd_record_mock
459 create_alarm
.side_effect
= _test_configure_scaling_groups_create_alarm
461 agent
= PolicyModuleAgent(config
, self
.loop
)
462 self
.loop
.run_until_complete(agent
._configure
_scaling
_groups
("test_nsr_id"))
463 create_alarm
.assert_any_call(metric_name
='cirros_vnf_memory_util',
468 vdu_name
='cirros_ns-1-cirros_vnfd-VM-1',
469 vnf_member_index
='1')
470 create_alarm
.assert_any_call(metric_name
='cirros_vnf_memory_util',
475 vdu_name
='cirros_ns-1-cirros_vnfd-VM-1',
476 vnf_member_index
='1')
477 create_alarm
.assert_any_call(metric_name
='cirros_vnf_memory_util',
482 vdu_name
='cirros_ns-2-cirros_vnfd-VM-1',
483 vnf_member_index
='2')
484 create_alarm
.assert_any_call(metric_name
='cirros_vnf_memory_util',
489 vdu_name
='cirros_ns-2-cirros_vnfd-VM-1',
490 vnf_member_index
='2')
491 scaling_record
= ScalingGroup
.get()
492 self
.assertEqual(scaling_record
.name
, 'scale_cirros_vnfd-VM')
493 self
.assertEqual(scaling_record
.nsr_id
, 'test_nsr_id')
496 if __name__
== '__main__':