bed6eb53be77d7e7873669ccaa922341a461d260
[osm/POL.git] / osm_policy_module / tests / integration / test_policy_agent.py
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2018 Whitestack, LLC
4 # *************************************************************
5
6 # This file is part of OSM Monitoring module
7 # All Rights Reserved to Whitestack, LLC
8
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
12
13 # http://www.apache.org/licenses/LICENSE-2.0
14
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
19 # under the License.
20
21 # For those usages not covered by the Apache License, Version 2.0 please
22 # contact: bdiaz@whitestack.com or glavado@whitestack.com
23 ##
24 import logging
25 import sys
26 import unittest
27 import uuid
28 from unittest.mock import patch, Mock
29
30 from kafka import KafkaProducer
31 from osm_common.dbmongo import DbMongo
32 from peewee import SqliteDatabase
33
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
39
40 log = logging.getLogger()
41 log.level = logging.INFO
42 stream_handler = logging.StreamHandler(sys.stdout)
43 log.addHandler(stream_handler)
44
45 nsr_record_mock = {
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",
51 "nsd": {
52 "name": "cirros_vdu_scaling_ns",
53 "_id": "d7c8bd3c-eb39-4514-8847-19f01345524f",
54 "vld": [
55 {
56 "id": "cirros_nsd_vld1",
57 "name": "cirros_nsd_vld1",
58 "type": "ELAN",
59 "mgmt-network": "true",
60 "vnfd-connection-point-ref": [
61 {
62 "vnfd-id-ref": "cirros_vdu_scaling_vnf",
63 "member-vnf-index-ref": 1,
64 "vnfd-connection-point-ref": "eth0"
65 },
66 {
67 "vnfd-id-ref": "cirros_vdu_scaling_vnf",
68 "member-vnf-index-ref": 2,
69 "vnfd-connection-point-ref": "eth0"
70 }
71 ]
72 }
73 ],
74 "vendor": "OSM",
75 "constituent-vnfd": [
76 {
77 "member-vnf-index": "1",
78 "vnfd-id-ref": "cirros_vdu_scaling_vnf"
79 },
80 {
81 "member-vnf-index": "2",
82 "vnfd-id-ref": "cirros_vdu_scaling_vnf"
83 }
84 ],
85 "version": "1.0",
86 "id": "cirros_vdu_scaling_ns",
87 "description": "Simple NS example with a cirros_vdu_scaling_vnf",
88 "logo": "osm.png",
89 "_admin": {
90 "created": 1535392246.499733,
91 "userDefinedData": {
92
93 },
94 "usageSate": "NOT_IN_USE",
95 "storage": {
96 "zipfile": "package.tar.gz",
97 "fs": "local",
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"
102 },
103 "onboardingState": "ONBOARDED",
104 "modified": 1535392246.499733,
105 "projects_read": [
106 "admin"
107 ],
108 "operationalState": "ENABLED",
109 "projects_write": [
110 "admin"
111 ]
112 },
113 "short-name": "cirros_vdu_scaling_ns"
114 },
115 "id": "87776f33-b67c-417a-8119-cb08e4098951",
116 "config-status": "configured",
117 "operational-events": [],
118 "_admin": {
119 "created": 1535392482.0084584,
120 "projects_read": [
121 "admin"
122 ],
123 "nsState": "INSTANTIATED",
124 "modified": 1535392482.0084584,
125 "projects_write": [
126 "admin"
127 ],
128 "deployed": {
129 "RO": {
130 "vnfd_id": {
131 "cirros_vdu_scaling_vnf": "7445e347-fe2f-431a-abc2-8b9be3d093c6"
132 },
133 "nsd_id": "92c56cf0-f8fa-488c-9afb-9f3d78ae6bbb",
134 "nsr_id": "637e12cd-c201-4c44-8ebd-70fb57a4dcee",
135 "nsr_status": "BUILD"
136 }
137 }
138 },
139 "nsd-ref": "cirros_vdu_scaling_ns",
140 "name": "cirros_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"
148 },
149 "description": "default description",
150 "constituent-vnfr-ref": [
151 "0d9d06ad-3fc2-418c-9934-465e815fafe2",
152 "3336eb44-77df-4c4f-9881-d2828d259864"
153 ],
154 "admin-status": "ENABLED",
155 "detailed-status": "done",
156 "datacenter": "be48ae31-1d46-4892-a4b4-d69abd55714b",
157 "orchestration-progress": {
158
159 },
160 "short-name": "cirros_ns",
161 "ns-instance-config-ref": "87776f33-b67c-417a-8119-cb08e4098951",
162 "nsd-name-ref": "cirros_vdu_scaling_ns",
163 "admin": {
164 "deployed": {
165 "RO": {
166 "nsr_status": "ACTIVE"
167 }
168 }
169 }
170 }
171
172 vnfr_record_mocks = [
173 {
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",
178 "vdur": [
179 {
180 "interfaces": [
181 {
182 "mac-address": "fa:16:3e:71:fd:b8",
183 "name": "eth0",
184 "ip-address": "192.168.160.2"
185 }
186 ],
187 "status": "ACTIVE",
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"
193 }
194 ],
195 "id": "0d9d06ad-3fc2-418c-9934-465e815fafe2",
196 "vnfd-ref": "cirros_vdu_scaling_vnf",
197 "vnfd-id": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
198 "_admin": {
199 "created": 1535392482.0067868,
200 "projects_read": [
201 "admin"
202 ],
203 "modified": 1535392482.0067868,
204 "projects_write": [
205 "admin"
206 ]
207 },
208 "nsr-id-ref": "87776f33-b67c-417a-8119-cb08e4098951",
209 "member-vnf-index-ref": "1",
210 "connection-point": [
211 {
212 "name": "eth0",
213 "id": None,
214 "connection-point-id": None
215 }
216 ]
217 },
218 {
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",
223 "vdur": [
224 {
225 "interfaces": [
226 {
227 "mac-address": "fa:16:3e:1e:76:e8",
228 "name": "eth0",
229 "ip-address": "192.168.160.10"
230 }
231 ],
232 "status": "ACTIVE",
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"
238 }
239 ],
240 "id": "3336eb44-77df-4c4f-9881-d2828d259864",
241 "vnfd-ref": "cirros_vdu_scaling_vnf",
242 "vnfd-id": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
243 "_admin": {
244 "created": 1535392482.0076294,
245 "projects_read": [
246 "admin"
247 ],
248 "modified": 1535392482.0076294,
249 "projects_write": [
250 "admin"
251 ]
252 },
253 "nsr-id-ref": "87776f33-b67c-417a-8119-cb08e4098951",
254 "member-vnf-index-ref": "2",
255 "connection-point": [
256 {
257 "name": "eth0",
258 "id": None,
259 "connection-point-id": None
260 }
261 ]}]
262
263 nsd_record_mock = {'name': 'cirros_vdu_scaling_ns',
264 'version': '1.0',
265 'short-name': 'cirros_vdu_scaling_ns',
266 'logo': 'osm.png',
267 'id': 'cirros_vdu_scaling_ns',
268 'description': 'Simple NS example with a cirros_vdu_scaling_vnf',
269 'vendor': 'OSM',
270 'vld': [
271 {'name': 'cirros_nsd_vld1',
272 'type': 'ELAN',
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'}]}
286
287 vnfd_record_mock = {
288 "_id": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
289 "name": "cirros_vdu_scaling_vnf",
290 "vendor": "OSM",
291 "vdu": [
292 {
293 "name": "cirros_vnfd-VM",
294 "monitoring-param": [
295 {
296 "id": "cirros_vnfd-VM_memory_util",
297 "nfvi-metric": "average_memory_utilization"
298 }
299 ],
300 "vm-flavor": {
301 "vcpu-count": 1,
302 "memory-mb": 256,
303 "storage-gb": 2
304 },
305 "description": "cirros_vnfd-VM",
306 "count": 1,
307 "id": "cirros_vnfd-VM",
308 "interface": [
309 {
310 "name": "eth0",
311 "external-connection-point-ref": "eth0",
312 "type": "EXTERNAL",
313 "virtual-interface": {
314 "bandwidth": "0",
315 "type": "VIRTIO",
316 "vpci": "0000:00:0a.0"
317 }
318 }
319 ],
320 "image": "cirros034"
321 }
322 ],
323 "monitoring-param": [
324 {
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"
330 }
331 ],
332 "description": "Simple VNF example with a cirros and a scaling group descriptor",
333 "id": "cirros_vdu_scaling_vnf",
334 "logo": "cirros-64.png",
335 "version": "1.0",
336 "connection-point": [
337 {
338 "name": "eth0",
339 "type": "VPORT"
340 }
341 ],
342 "mgmt-interface": {
343 "cp": "eth0"
344 },
345 "scaling-group-descriptor": [
346 {
347 "name": "scale_cirros_vnfd-VM",
348 "min-instance-count": 1,
349 "vdu": [
350 {
351 "count": 1,
352 "vdu-id-ref": "cirros_vnfd-VM"
353 }
354 ],
355 "max-instance-count": 10,
356 "scaling-policy": [
357 {
358 "name": "auto_memory_util_above_threshold",
359 "scaling-type": "automatic",
360 "cooldown-time": 60,
361 "threshold-time": 10,
362 "scaling-criteria": [
363 {
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
370 }
371 ]
372 }
373 ]
374 }
375 ],
376 "short-name": "cirros_vdu_scaling_vnf",
377 "_admin": {
378 "created": 1535392242.6281035,
379 "modified": 1535392242.6281035,
380 "storage": {
381 "zipfile": "package.tar.gz",
382 "pkg-dir": "cirros_vnf",
383 "path": "/app/storage/",
384 "folder": "63f44c41-45ee-456b-b10d-5f08fb1796e0",
385 "fs": "local",
386 "descriptor": "cirros_vnf/cirros_vdu_scaling_vnfd.yaml"
387 },
388 "usageSate": "NOT_IN_USE",
389 "onboardingState": "ONBOARDED",
390 "userDefinedData": {
391
392 },
393 "projects_read": [
394 "admin"
395 ],
396 "operationalState": "ENABLED",
397 "projects_write": [
398 "admin"
399 ]
400 }
401 }
402
403 test_db = SqliteDatabase(':memory:')
404
405 MODELS = [ScalingGroup, ScalingPolicy, ScalingCriteria, ScalingAlarm]
406
407
408 class PolicyModuleAgentTest(unittest.TestCase):
409 def setUp(self):
410 super()
411 database.db = test_db
412 test_db.bind(MODELS)
413 test_db.connect()
414 test_db.drop_tables(MODELS)
415 test_db.create_tables(MODELS)
416
417 def tearDown(self):
418 super()
419
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):
428 if '1' in args[1]:
429 return vnfr_record_mocks[0]
430 if '2' in args[1]:
431 return vnfr_record_mocks[1]
432
433 def _test_configure_scaling_groups_create_alarm(*args, **kwargs):
434 return uuid.uuid4()
435
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',
444 ns_id='test_nsr_id',
445 operation='GT',
446 statistic='AVERAGE',
447 threshold=80,
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',
451 ns_id='test_nsr_id',
452 operation='LT',
453 statistic='AVERAGE',
454 threshold=20,
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',
458 ns_id='test_nsr_id',
459 operation='GT',
460 statistic='AVERAGE',
461 threshold=80,
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',
465 ns_id='test_nsr_id',
466 operation='LT',
467 statistic='AVERAGE',
468 threshold=20,
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')
474
475
476 if __name__ == '__main__':
477 unittest.main()