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
.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
43 log
= logging
.getLogger()
44 log
.level
= logging
.INFO
45 stream_handler
= logging
.StreamHandler(sys
.stdout
)
46 log
.addHandler(stream_handler
)
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",
55 "name": "cirros_vdu_scaling_ns",
56 "_id": "d7c8bd3c-eb39-4514-8847-19f01345524f",
59 "id": "cirros_nsd_vld1",
60 "name": "cirros_nsd_vld1",
62 "mgmt-network": "true",
63 "vnfd-connection-point-ref": [
65 "vnfd-id-ref": "cirros_vdu_scaling_vnf",
66 "member-vnf-index-ref": 1,
67 "vnfd-connection-point-ref": "eth0"
70 "vnfd-id-ref": "cirros_vdu_scaling_vnf",
71 "member-vnf-index-ref": 2,
72 "vnfd-connection-point-ref": "eth0"
80 "member-vnf-index": "1",
81 "vnfd-id-ref": "cirros_vdu_scaling_vnf"
84 "member-vnf-index": "2",
85 "vnfd-id-ref": "cirros_vdu_scaling_vnf"
89 "id": "cirros_vdu_scaling_ns",
90 "description": "Simple NS example with a cirros_vdu_scaling_vnf",
93 "created": 1535392246.499733,
97 "usageSate": "NOT_IN_USE",
99 "zipfile": "package.tar.gz",
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"
106 "onboardingState": "ONBOARDED",
107 "modified": 1535392246.499733,
111 "operationalState": "ENABLED",
116 "short-name": "cirros_vdu_scaling_ns"
118 "id": "87776f33-b67c-417a-8119-cb08e4098951",
119 "config-status": "configured",
120 "operational-events": [],
122 "created": 1535392482.0084584,
126 "nsState": "INSTANTIATED",
127 "modified": 1535392482.0084584,
134 "cirros_vdu_scaling_vnf": "7445e347-fe2f-431a-abc2-8b9be3d093c6"
136 "nsd_id": "92c56cf0-f8fa-488c-9afb-9f3d78ae6bbb",
137 "nsr_id": "637e12cd-c201-4c44-8ebd-70fb57a4dcee",
138 "nsr_status": "BUILD"
142 "nsd-ref": "cirros_vdu_scaling_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"
152 "description": "default description",
153 "constituent-vnfr-ref": [
154 "0d9d06ad-3fc2-418c-9934-465e815fafe2",
155 "3336eb44-77df-4c4f-9881-d2828d259864"
157 "admin-status": "ENABLED",
158 "detailed-status": "done",
159 "datacenter": "be48ae31-1d46-4892-a4b4-d69abd55714b",
160 "orchestration-progress": {
163 "short-name": "cirros_ns",
164 "ns-instance-config-ref": "87776f33-b67c-417a-8119-cb08e4098951",
165 "nsd-name-ref": "cirros_vdu_scaling_ns",
169 "nsr_status": "ACTIVE"
175 vnfr_record_mocks
= [
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",
185 "mac-address": "fa:16:3e:71:fd:b8",
187 "ip-address": "192.168.160.2"
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"
198 "id": "0d9d06ad-3fc2-418c-9934-465e815fafe2",
199 "vnfd-ref": "cirros_vdu_scaling_vnf",
200 "vnfd-id": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
202 "created": 1535392482.0067868,
206 "modified": 1535392482.0067868,
211 "nsr-id-ref": "87776f33-b67c-417a-8119-cb08e4098951",
212 "member-vnf-index-ref": "1",
213 "connection-point": [
217 "connection-point-id": None
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",
230 "mac-address": "fa:16:3e:1e:76:e8",
232 "ip-address": "192.168.160.10"
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"
243 "id": "3336eb44-77df-4c4f-9881-d2828d259864",
244 "vnfd-ref": "cirros_vdu_scaling_vnf",
245 "vnfd-id": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
247 "created": 1535392482.0076294,
251 "modified": 1535392482.0076294,
256 "nsr-id-ref": "87776f33-b67c-417a-8119-cb08e4098951",
257 "member-vnf-index-ref": "2",
258 "connection-point": [
262 "connection-point-id": None
266 nsd_record_mock
= {'name': 'cirros_vdu_scaling_ns',
268 'short-name': 'cirros_vdu_scaling_ns',
270 'id': 'cirros_vdu_scaling_ns',
271 'description': 'Simple NS example with a cirros_vdu_scaling_vnf',
274 {'name': 'cirros_nsd_vld1',
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'}]}
291 "_id": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
292 "name": "cirros_vdu_scaling_vnf",
296 "name": "cirros_vnfd-VM",
297 "monitoring-param": [
299 "id": "cirros_vnfd-VM_memory_util",
300 "nfvi-metric": "average_memory_utilization"
308 "description": "cirros_vnfd-VM",
310 "id": "cirros_vnfd-VM",
314 "external-connection-point-ref": "eth0",
316 "virtual-interface": {
319 "vpci": "0000:00:0a.0"
326 "monitoring-param": [
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"
337 "id": "haproxy_users",
338 "name": "haproxy_users",
339 "aggregation-type": "AVERAGE",
341 {"vnf-metric-name-ref": "users"}
344 "vdu-configuration": {
346 "charm": "testmetrics",
353 "description": "Simple VNF example with a cirros and a scaling group descriptor",
354 "id": "cirros_vdu_scaling_vnf",
355 "logo": "cirros-64.png",
357 "connection-point": [
366 "scaling-group-descriptor": [
368 "name": "scale_cirros_vnfd-VM",
369 "min-instance-count": 1,
373 "vdu-id-ref": "cirros_vnfd-VM"
376 "max-instance-count": 10,
379 "name": "auto_memory_util_above_threshold",
380 "scaling-type": "automatic",
382 "threshold-time": 10,
383 "scaling-criteria": [
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
397 "short-name": "cirros_vdu_scaling_vnf",
399 "created": 1535392242.6281035,
400 "modified": 1535392242.6281035,
402 "zipfile": "package.tar.gz",
403 "pkg-dir": "cirros_vnf",
404 "path": "/app/storage/",
405 "folder": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
407 "descriptor": "cirros_vnf/cirros_vdu_scaling_vnfd.yaml"
409 "usageSate": "NOT_IN_USE",
410 "onboardingState": "ONBOARDED",
417 "operationalState": "ENABLED",
424 MODELS
= [ScalingGroup
, ScalingPolicy
, ScalingCriteria
, ScalingAlarm
]
427 class PolicyModuleAgentTest(unittest
.TestCase
):
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
)
436 self
.loop
= asyncio
.new_event_loop()
440 os
.remove('test_db.sqlite')
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
):
451 return vnfr_record_mocks
[0]
453 return vnfr_record_mocks
[1]
455 async def _test_configure_scaling_groups_create_alarm(*args
, **kwargs
):
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
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',
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',
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',
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',
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')
499 if __name__
== '__main__':