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
28 from unittest
.mock
import patch
, Mock
30 from kafka
import KafkaProducer
31 from osm_common
.dbmongo
import DbMongo
32 from peewee
import SqliteDatabase
34 from osm_policy_module
.common
.db_client
import DbClient
35 from osm_policy_module
.common
.mon_client
import MonClient
36 from osm_policy_module
.core
import database
37 from osm_policy_module
.core
.agent
import PolicyModuleAgent
38 from osm_policy_module
.core
.database
import ScalingGroup
, ScalingAlarm
, ScalingPolicy
, ScalingCriteria
40 log
= logging
.getLogger()
41 log
.level
= logging
.INFO
42 stream_handler
= logging
.StreamHandler(sys
.stdout
)
43 log
.addHandler(stream_handler
)
46 "_id": "87776f33-b67c-417a-8119-cb08e4098951",
47 "crete-time": 1535392482.0044956,
48 "operational-status": "running",
49 "ssh-authorized-key": None,
50 "name-ref": "cirros_ns",
52 "name": "cirros_vdu_scaling_ns",
53 "_id": "d7c8bd3c-eb39-4514-8847-19f01345524f",
56 "id": "cirros_nsd_vld1",
57 "name": "cirros_nsd_vld1",
59 "mgmt-network": "true",
60 "vnfd-connection-point-ref": [
62 "vnfd-id-ref": "cirros_vdu_scaling_vnf",
63 "member-vnf-index-ref": 1,
64 "vnfd-connection-point-ref": "eth0"
67 "vnfd-id-ref": "cirros_vdu_scaling_vnf",
68 "member-vnf-index-ref": 2,
69 "vnfd-connection-point-ref": "eth0"
77 "member-vnf-index": "1",
78 "vnfd-id-ref": "cirros_vdu_scaling_vnf"
81 "member-vnf-index": "2",
82 "vnfd-id-ref": "cirros_vdu_scaling_vnf"
86 "id": "cirros_vdu_scaling_ns",
87 "description": "Simple NS example with a cirros_vdu_scaling_vnf",
90 "created": 1535392246.499733,
94 "usageSate": "NOT_IN_USE",
96 "zipfile": "package.tar.gz",
98 "path": "/app/storage/",
99 "folder": "d7c8bd3c-eb39-4514-8847-19f01345524f",
100 "pkg-dir": "cirros_nsd",
101 "descriptor": "cirros_nsd/cirros_vdu_scaling_nsd.yaml"
103 "onboardingState": "ONBOARDED",
104 "modified": 1535392246.499733,
108 "operationalState": "ENABLED",
113 "short-name": "cirros_vdu_scaling_ns"
115 "id": "87776f33-b67c-417a-8119-cb08e4098951",
116 "config-status": "configured",
117 "operational-events": [],
119 "created": 1535392482.0084584,
123 "nsState": "INSTANTIATED",
124 "modified": 1535392482.0084584,
131 "cirros_vdu_scaling_vnf": "7445e347-fe2f-431a-abc2-8b9be3d093c6"
133 "nsd_id": "92c56cf0-f8fa-488c-9afb-9f3d78ae6bbb",
134 "nsr_id": "637e12cd-c201-4c44-8ebd-70fb57a4dcee",
135 "nsr_status": "BUILD"
139 "nsd-ref": "cirros_vdu_scaling_ns",
141 "resource-orchestrator": "osmopenmano",
142 "instantiate_params": {
143 "nsDescription": "default description",
144 "nsdId": "d7c8bd3c-eb39-4514-8847-19f01345524f",
145 "nsr_id": "87776f33-b67c-417a-8119-cb08e4098951",
146 "nsName": "cirros_ns",
147 "vimAccountId": "be48ae31-1d46-4892-a4b4-d69abd55714b"
149 "description": "default description",
150 "constituent-vnfr-ref": [
151 "0d9d06ad-3fc2-418c-9934-465e815fafe2",
152 "3336eb44-77df-4c4f-9881-d2828d259864"
154 "admin-status": "ENABLED",
155 "detailed-status": "done",
156 "datacenter": "be48ae31-1d46-4892-a4b4-d69abd55714b",
157 "orchestration-progress": {
160 "short-name": "cirros_ns",
161 "ns-instance-config-ref": "87776f33-b67c-417a-8119-cb08e4098951",
162 "nsd-name-ref": "cirros_vdu_scaling_ns",
166 "nsr_status": "ACTIVE"
172 vnfr_record_mocks
= [
174 "_id": "0d9d06ad-3fc2-418c-9934-465e815fafe2",
175 "ip-address": "192.168.160.2",
176 "created-time": 1535392482.0044956,
177 "vim-account-id": "be48ae31-1d46-4892-a4b4-d69abd55714b",
182 "mac-address": "fa:16:3e:71:fd:b8",
184 "ip-address": "192.168.160.2"
188 "vim-id": "63a65636-9fc8-4022-b070-980823e6266a",
189 "name": "cirros_ns-1-cirros_vnfd-VM-1",
190 "status-detailed": None,
191 "ip-address": "192.168.160.2",
192 "vdu-id-ref": "cirros_vnfd-VM"
195 "id": "0d9d06ad-3fc2-418c-9934-465e815fafe2",
196 "vnfd-ref": "cirros_vdu_scaling_vnf",
197 "vnfd-id": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
199 "created": 1535392482.0067868,
203 "modified": 1535392482.0067868,
208 "nsr-id-ref": "87776f33-b67c-417a-8119-cb08e4098951",
209 "member-vnf-index-ref": "1",
210 "connection-point": [
214 "connection-point-id": None
219 "_id": "3336eb44-77df-4c4f-9881-d2828d259864",
220 "ip-address": "192.168.160.10",
221 "created-time": 1535392482.0044956,
222 "vim-account-id": "be48ae31-1d46-4892-a4b4-d69abd55714b",
227 "mac-address": "fa:16:3e:1e:76:e8",
229 "ip-address": "192.168.160.10"
233 "vim-id": "a154b8d3-2b10-421a-a51d-4b391d9bd366",
234 "name": "cirros_ns-2-cirros_vnfd-VM-1",
235 "status-detailed": None,
236 "ip-address": "192.168.160.10",
237 "vdu-id-ref": "cirros_vnfd-VM"
240 "id": "3336eb44-77df-4c4f-9881-d2828d259864",
241 "vnfd-ref": "cirros_vdu_scaling_vnf",
242 "vnfd-id": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
244 "created": 1535392482.0076294,
248 "modified": 1535392482.0076294,
253 "nsr-id-ref": "87776f33-b67c-417a-8119-cb08e4098951",
254 "member-vnf-index-ref": "2",
255 "connection-point": [
259 "connection-point-id": None
263 nsd_record_mock
= {'name': 'cirros_vdu_scaling_ns',
265 'short-name': 'cirros_vdu_scaling_ns',
267 'id': 'cirros_vdu_scaling_ns',
268 'description': 'Simple NS example with a cirros_vdu_scaling_vnf',
271 {'name': 'cirros_nsd_vld1',
273 'id': 'cirros_nsd_vld1',
274 'mgmt-network': 'true',
275 'vnfd-connection-point-ref': [
276 {'vnfd-id-ref': 'cirros_vdu_scaling_vnf',
277 'vnfd-connection-point-ref': 'eth0',
278 'member-vnf-index-ref': 1},
279 {'vnfd-id-ref': 'cirros_vdu_scaling_vnf',
280 'vnfd-connection-point-ref': 'eth0',
281 'member-vnf-index-ref': 2}]}],
282 'constituent-vnfd': [{'vnfd-id-ref': 'cirros_vdu_scaling_vnf',
283 'member-vnf-index': '1'},
284 {'vnfd-id-ref': 'cirros_vdu_scaling_vnf',
285 'member-vnf-index': '2'}]}
288 "_id": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
289 "name": "cirros_vdu_scaling_vnf",
293 "name": "cirros_vnfd-VM",
294 "monitoring-param": [
296 "id": "cirros_vnfd-VM_memory_util",
297 "nfvi-metric": "average_memory_utilization"
305 "description": "cirros_vnfd-VM",
307 "id": "cirros_vnfd-VM",
311 "external-connection-point-ref": "eth0",
313 "virtual-interface": {
316 "vpci": "0000:00:0a.0"
323 "monitoring-param": [
325 "id": "cirros_vnf_memory_util",
326 "name": "cirros_vnf_memory_util",
327 "aggregation-type": "AVERAGE",
328 "vdu-monitoring-param-ref": "cirros_vnfd-VM_memory_util",
329 "vdu-ref": "cirros_vnfd-VM"
332 "description": "Simple VNF example with a cirros and a scaling group descriptor",
333 "id": "cirros_vdu_scaling_vnf",
334 "logo": "cirros-64.png",
336 "connection-point": [
345 "scaling-group-descriptor": [
347 "name": "scale_cirros_vnfd-VM",
348 "min-instance-count": 1,
352 "vdu-id-ref": "cirros_vnfd-VM"
355 "max-instance-count": 10,
358 "name": "auto_memory_util_above_threshold",
359 "scaling-type": "automatic",
361 "threshold-time": 10,
362 "scaling-criteria": [
364 "name": "group1_memory_util_above_threshold",
365 "vnf-monitoring-param-ref": "cirros_vnf_memory_util",
366 "scale-out-threshold": 80,
367 "scale-out-relational-operation": "GT",
368 "scale-in-relational-operation": "LT",
369 "scale-in-threshold": 20
376 "short-name": "cirros_vdu_scaling_vnf",
378 "created": 1535392242.6281035,
379 "modified": 1535392242.6281035,
381 "zipfile": "package.tar.gz",
382 "pkg-dir": "cirros_vnf",
383 "path": "/app/storage/",
384 "folder": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
386 "descriptor": "cirros_vnf/cirros_vdu_scaling_vnfd.yaml"
388 "usageSate": "NOT_IN_USE",
389 "onboardingState": "ONBOARDED",
396 "operationalState": "ENABLED",
403 test_db
= SqliteDatabase(':memory:')
405 MODELS
= [ScalingGroup
, ScalingPolicy
, ScalingCriteria
, ScalingAlarm
]
408 class PolicyModuleAgentTest(unittest
.TestCase
):
411 database
.db
= test_db
414 test_db
.drop_tables(MODELS
)
415 test_db
.create_tables(MODELS
)
420 @patch.object(DbMongo
, 'db_connect', Mock())
421 @patch.object(KafkaProducer
, '__init__')
422 @patch.object(MonClient
, 'create_alarm')
423 @patch.object(DbClient
, 'get_vnfd')
424 @patch.object(DbClient
, 'get_nsr')
425 @patch.object(DbClient
, 'get_vnfr')
426 def test_configure_scaling_groups(self
, get_vnfr
, get_nsr
, get_vnfd
, create_alarm
, kafka_producer_init
):
427 def _test_configure_scaling_groups_get_vnfr(*args
, **kwargs
):
429 return vnfr_record_mocks
[0]
431 return vnfr_record_mocks
[1]
433 def _test_configure_scaling_groups_create_alarm(*args
, **kwargs
):
436 kafka_producer_init
.return_value
= None
437 get_vnfr
.side_effect
= _test_configure_scaling_groups_get_vnfr
438 get_nsr
.return_value
= nsr_record_mock
439 get_vnfd
.return_value
= vnfd_record_mock
440 create_alarm
.side_effect
= _test_configure_scaling_groups_create_alarm
441 agent
= PolicyModuleAgent()
442 agent
._configure
_scaling
_groups
("test_nsr_id")
443 create_alarm
.assert_any_call(metric_name
='average_memory_utilization',
448 vdu_name
='cirros_ns-1-cirros_vnfd-VM-1',
449 vnf_member_index
='1')
450 create_alarm
.assert_any_call(metric_name
='average_memory_utilization',
455 vdu_name
='cirros_ns-1-cirros_vnfd-VM-1',
456 vnf_member_index
='1')
457 create_alarm
.assert_any_call(metric_name
='average_memory_utilization',
462 vdu_name
='cirros_ns-2-cirros_vnfd-VM-1',
463 vnf_member_index
='2')
464 create_alarm
.assert_any_call(metric_name
='average_memory_utilization',
469 vdu_name
='cirros_ns-2-cirros_vnfd-VM-1',
470 vnf_member_index
='2')
471 scaling_record
= ScalingGroup
.get()
472 self
.assertEqual(scaling_record
.name
, 'scale_cirros_vnfd-VM')
473 self
.assertEqual(scaling_record
.nsr_id
, 'test_nsr_id')
476 if __name__
== '__main__':