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 osm_policy_module
.core
import database
33 from peewee
import SqliteDatabase
35 from osm_policy_module
.common
.mon_client
import MonClient
36 from osm_policy_module
.core
.agent
import PolicyModuleAgent
37 from osm_policy_module
.core
.database
import ScalingRecord
, ScalingAlarm
, BaseModel
39 log
= logging
.getLogger()
40 log
.level
= logging
.INFO
41 stream_handler
= logging
.StreamHandler(sys
.stdout
)
42 log
.addHandler(stream_handler
)
45 "_id": "87776f33-b67c-417a-8119-cb08e4098951",
46 "crete-time": 1535392482.0044956,
47 "operational-status": "running",
48 "ssh-authorized-key": None,
49 "name-ref": "cirros_ns",
51 "name": "cirros_vdu_scaling_ns",
52 "_id": "d7c8bd3c-eb39-4514-8847-19f01345524f",
55 "id": "cirros_nsd_vld1",
56 "name": "cirros_nsd_vld1",
58 "mgmt-network": "true",
59 "vnfd-connection-point-ref": [
61 "vnfd-id-ref": "cirros_vdu_scaling_vnf",
62 "member-vnf-index-ref": 1,
63 "vnfd-connection-point-ref": "eth0"
66 "vnfd-id-ref": "cirros_vdu_scaling_vnf",
67 "member-vnf-index-ref": 2,
68 "vnfd-connection-point-ref": "eth0"
76 "member-vnf-index": "1",
77 "vnfd-id-ref": "cirros_vdu_scaling_vnf"
80 "member-vnf-index": "2",
81 "vnfd-id-ref": "cirros_vdu_scaling_vnf"
85 "id": "cirros_vdu_scaling_ns",
86 "description": "Simple NS example with a cirros_vdu_scaling_vnf",
89 "created": 1535392246.499733,
93 "usageSate": "NOT_IN_USE",
95 "zipfile": "package.tar.gz",
97 "path": "/app/storage/",
98 "folder": "d7c8bd3c-eb39-4514-8847-19f01345524f",
99 "pkg-dir": "cirros_nsd",
100 "descriptor": "cirros_nsd/cirros_vdu_scaling_nsd.yaml"
102 "onboardingState": "ONBOARDED",
103 "modified": 1535392246.499733,
107 "operationalState": "ENABLED",
112 "short-name": "cirros_vdu_scaling_ns"
114 "id": "87776f33-b67c-417a-8119-cb08e4098951",
115 "config-status": "configured",
116 "operational-events": [],
118 "created": 1535392482.0084584,
122 "nsState": "INSTANTIATED",
123 "modified": 1535392482.0084584,
130 "cirros_vdu_scaling_vnf": "7445e347-fe2f-431a-abc2-8b9be3d093c6"
132 "nsd_id": "92c56cf0-f8fa-488c-9afb-9f3d78ae6bbb",
133 "nsr_id": "637e12cd-c201-4c44-8ebd-70fb57a4dcee",
134 "nsr_status": "BUILD"
138 "nsd-ref": "cirros_vdu_scaling_ns",
140 "resource-orchestrator": "osmopenmano",
141 "instantiate_params": {
142 "nsDescription": "default description",
143 "nsdId": "d7c8bd3c-eb39-4514-8847-19f01345524f",
144 "nsr_id": "87776f33-b67c-417a-8119-cb08e4098951",
145 "nsName": "cirros_ns",
146 "vimAccountId": "be48ae31-1d46-4892-a4b4-d69abd55714b"
148 "description": "default description",
149 "constituent-vnfr-ref": [
150 "0d9d06ad-3fc2-418c-9934-465e815fafe2",
151 "3336eb44-77df-4c4f-9881-d2828d259864"
153 "admin-status": "ENABLED",
154 "detailed-status": "done",
155 "datacenter": "be48ae31-1d46-4892-a4b4-d69abd55714b",
156 "orchestration-progress": {
159 "short-name": "cirros_ns",
160 "ns-instance-config-ref": "87776f33-b67c-417a-8119-cb08e4098951",
161 "nsd-name-ref": "cirros_vdu_scaling_ns",
165 "nsr_status": "ACTIVE"
171 vnfr_record_mocks
= [
173 "_id": "0d9d06ad-3fc2-418c-9934-465e815fafe2",
174 "ip-address": "192.168.160.2",
175 "created-time": 1535392482.0044956,
176 "vim-account-id": "be48ae31-1d46-4892-a4b4-d69abd55714b",
181 "mac-address": "fa:16:3e:71:fd:b8",
183 "ip-address": "192.168.160.2"
187 "vim-id": "63a65636-9fc8-4022-b070-980823e6266a",
188 "name": "cirros_ns-1-cirros_vnfd-VM-1",
189 "status-detailed": None,
190 "ip-address": "192.168.160.2",
191 "vdu-id-ref": "cirros_vnfd-VM"
194 "id": "0d9d06ad-3fc2-418c-9934-465e815fafe2",
195 "vnfd-ref": "cirros_vdu_scaling_vnf",
196 "vnfd-id": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
198 "created": 1535392482.0067868,
202 "modified": 1535392482.0067868,
207 "nsr-id-ref": "87776f33-b67c-417a-8119-cb08e4098951",
208 "member-vnf-index-ref": "1",
209 "connection-point": [
213 "connection-point-id": None
218 "_id": "3336eb44-77df-4c4f-9881-d2828d259864",
219 "ip-address": "192.168.160.10",
220 "created-time": 1535392482.0044956,
221 "vim-account-id": "be48ae31-1d46-4892-a4b4-d69abd55714b",
226 "mac-address": "fa:16:3e:1e:76:e8",
228 "ip-address": "192.168.160.10"
232 "vim-id": "a154b8d3-2b10-421a-a51d-4b391d9bd366",
233 "name": "cirros_ns-2-cirros_vnfd-VM-1",
234 "status-detailed": None,
235 "ip-address": "192.168.160.10",
236 "vdu-id-ref": "cirros_vnfd-VM"
239 "id": "3336eb44-77df-4c4f-9881-d2828d259864",
240 "vnfd-ref": "cirros_vdu_scaling_vnf",
241 "vnfd-id": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
243 "created": 1535392482.0076294,
247 "modified": 1535392482.0076294,
252 "nsr-id-ref": "87776f33-b67c-417a-8119-cb08e4098951",
253 "member-vnf-index-ref": "2",
254 "connection-point": [
258 "connection-point-id": None
262 nsd_record_mock
= {'name': 'cirros_vdu_scaling_ns',
264 'short-name': 'cirros_vdu_scaling_ns',
266 'id': 'cirros_vdu_scaling_ns',
267 'description': 'Simple NS example with a cirros_vdu_scaling_vnf',
270 {'name': 'cirros_nsd_vld1',
272 'id': 'cirros_nsd_vld1',
273 'mgmt-network': 'true',
274 'vnfd-connection-point-ref': [
275 {'vnfd-id-ref': 'cirros_vdu_scaling_vnf',
276 'vnfd-connection-point-ref': 'eth0',
277 'member-vnf-index-ref': 1},
278 {'vnfd-id-ref': 'cirros_vdu_scaling_vnf',
279 'vnfd-connection-point-ref': 'eth0',
280 'member-vnf-index-ref': 2}]}],
281 'constituent-vnfd': [{'vnfd-id-ref': 'cirros_vdu_scaling_vnf',
282 'member-vnf-index': '1'},
283 {'vnfd-id-ref': 'cirros_vdu_scaling_vnf',
284 'member-vnf-index': '2'}]}
287 "_id": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
288 "name": "cirros_vdu_scaling_vnf",
292 "name": "cirros_vnfd-VM",
293 "monitoring-param": [
295 "id": "cirros_vnfd-VM_memory_util",
296 "nfvi-metric": "average_memory_utilization"
304 "description": "cirros_vnfd-VM",
306 "id": "cirros_vnfd-VM",
310 "external-connection-point-ref": "eth0",
312 "virtual-interface": {
315 "vpci": "0000:00:0a.0"
322 "monitoring-param": [
324 "id": "cirros_vnf_memory_util",
325 "name": "cirros_vnf_memory_util",
326 "aggregation-type": "AVERAGE",
327 "vdu-monitoring-param-ref": "cirros_vnfd-VM_memory_util",
328 "vdu-ref": "cirros_vnfd-VM"
331 "description": "Simple VNF example with a cirros and a scaling group descriptor",
332 "id": "cirros_vdu_scaling_vnf",
333 "logo": "cirros-64.png",
335 "connection-point": [
344 "scaling-group-descriptor": [
346 "name": "scale_cirros_vnfd-VM",
347 "min-instance-count": 1,
351 "vdu-id-ref": "cirros_vnfd-VM"
354 "max-instance-count": 10,
357 "name": "auto_memory_util_above_threshold",
358 "scaling-type": "automatic",
360 "threshold-time": 10,
361 "scaling-criteria": [
363 "name": "group1_memory_util_above_threshold",
364 "vnf-monitoring-param-ref": "cirros_vnf_memory_util",
365 "scale-out-threshold": 80,
366 "scale-out-relational-operation": "GT",
367 "scale-in-relational-operation": "LT",
368 "scale-in-threshold": 20
375 "short-name": "cirros_vdu_scaling_vnf",
377 "created": 1535392242.6281035,
378 "modified": 1535392242.6281035,
380 "zipfile": "package.tar.gz",
381 "pkg-dir": "cirros_vnf",
382 "path": "/app/storage/",
383 "folder": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
385 "descriptor": "cirros_vnf/cirros_vdu_scaling_vnfd.yaml"
387 "usageSate": "NOT_IN_USE",
388 "onboardingState": "ONBOARDED",
395 "operationalState": "ENABLED",
402 test_db
= SqliteDatabase(':memory:')
404 MODELS
= [ScalingRecord
, ScalingAlarm
]
407 class PolicyModuleAgentTest(unittest
.TestCase
):
410 database
.db
= test_db
413 test_db
.drop_tables(MODELS
)
414 test_db
.create_tables(MODELS
)
419 @patch.object(DbMongo
, 'db_connect', Mock())
420 @patch.object(KafkaProducer
, '__init__')
421 @patch.object(MonClient
, 'create_alarm')
422 @patch.object(PolicyModuleAgent
, '_get_vnfd')
423 @patch.object(PolicyModuleAgent
, '_get_nsr')
424 @patch.object(PolicyModuleAgent
, '_get_vnfr')
425 def test_configure_scaling_groups(self
, get_vnfr
, get_nsr
, get_vnfd
, create_alarm
, kafka_producer_init
):
426 def _test_configure_scaling_groups_get_vnfr(*args
, **kwargs
):
428 return vnfr_record_mocks
[0]
430 return vnfr_record_mocks
[1]
432 def _test_configure_scaling_groups_create_alarm(*args
, **kwargs
):
435 kafka_producer_init
.return_value
= None
436 get_vnfr
.side_effect
= _test_configure_scaling_groups_get_vnfr
437 get_nsr
.return_value
= nsr_record_mock
438 get_vnfd
.return_value
= vnfd_record_mock
439 create_alarm
.side_effect
= _test_configure_scaling_groups_create_alarm
440 agent
= PolicyModuleAgent()
441 agent
._configure
_scaling
_groups
("test_nsr_id")
442 create_alarm
.assert_any_call(metric_name
='average_memory_utilization',
447 vdu_name
='cirros_vnfd-VM',
448 vnf_member_index
='1')
449 scaling_record
= ScalingRecord
.get()
450 self
.assertEqual(scaling_record
.name
, 'scale_cirros_vnfd-VM')
451 self
.assertEqual(scaling_record
.nsr_id
, 'test_nsr_id')
452 self
.assertIsNotNone(scaling_record
)
455 if __name__
== '__main__':