1 |
|
# -*- coding: utf-8 -*- |
2 |
|
# pylint: disable=no-member |
3 |
|
|
4 |
|
# Copyright 2018 Whitestack, LLC |
5 |
|
# ************************************************************* |
6 |
|
|
7 |
|
# This file is part of OSM Monitoring module |
8 |
|
# All Rights Reserved to Whitestack, LLC |
9 |
|
|
10 |
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
11 |
|
# not use this file except in compliance with the License. You may obtain |
12 |
|
# a copy of the License at |
13 |
|
|
14 |
|
# http://www.apache.org/licenses/LICENSE-2.0 |
15 |
|
|
16 |
|
# Unless required by applicable law or agreed to in writing, software |
17 |
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
18 |
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
19 |
|
# License for the specific language governing permissions and limitations |
20 |
|
# under the License. |
21 |
|
|
22 |
|
# For those usages not covered by the Apache License, Version 2.0 please |
23 |
|
# contact: bdiaz@whitestack.com or glavado@whitestack.com |
24 |
|
## |
25 |
1 |
import datetime |
26 |
1 |
import json |
27 |
1 |
import logging |
28 |
1 |
import operator |
29 |
1 |
import functools |
30 |
|
|
31 |
1 |
from osm_policy_module.common.common_db_client import CommonDbClient |
32 |
1 |
from osm_policy_module.common.lcm_client import LcmClient |
33 |
1 |
from osm_policy_module.common.mon_client import MonClient |
34 |
1 |
from osm_policy_module.core import database |
35 |
1 |
from osm_policy_module.core.config import Config |
36 |
1 |
from osm_policy_module.core.database import ( |
37 |
|
ScalingGroup, |
38 |
|
ScalingAlarm, |
39 |
|
ScalingPolicy, |
40 |
|
ScalingCriteria, |
41 |
|
ScalingAlarmRepository, |
42 |
|
ScalingGroupRepository, |
43 |
|
ScalingPolicyRepository, |
44 |
|
ScalingCriteriaRepository, |
45 |
|
) |
46 |
1 |
from osm_policy_module.core.exceptions import VdurNotFound |
47 |
|
|
48 |
1 |
log = logging.getLogger(__name__) |
49 |
|
|
50 |
|
|
51 |
1 |
class AutoscalingService: |
52 |
1 |
def __init__(self, config: Config): |
53 |
1 |
self.conf = config |
54 |
1 |
self.db_client = CommonDbClient(config) |
55 |
1 |
self.mon_client = MonClient(config) |
56 |
1 |
self.lcm_client = LcmClient(config) |
57 |
|
|
58 |
1 |
async def configure_scaling_groups(self, nsr_id: str, vnf_member_index=None): |
59 |
|
""" |
60 |
|
Configures scaling groups for a network service. Creates records in DB. Creates alarms in MON. |
61 |
|
:param nsr_id: Network service record id |
62 |
|
:return: |
63 |
|
""" |
64 |
1 |
log.info( |
65 |
|
"Configuring scaling groups for network service with nsr_id: %s", nsr_id |
66 |
|
) |
67 |
1 |
alarms_created = [] |
68 |
1 |
database.db.connect() |
69 |
1 |
try: |
70 |
1 |
with database.db.atomic() as tx: |
71 |
1 |
try: |
72 |
1 |
if vnf_member_index is None: |
73 |
1 |
vnfrs = self.db_client.get_vnfrs(nsr_id) |
74 |
|
else: |
75 |
0 |
vnfrs = [] |
76 |
0 |
vnfr = self.db_client.get_vnfr(nsr_id, vnf_member_index) |
77 |
0 |
vnfrs.append(vnfr) |
78 |
|
# vnfrs = self.db_client.get_vnfrs(nsr_id) |
79 |
1 |
for vnfr in vnfrs: |
80 |
1 |
log.debug("Processing vnfr: %s", vnfr) |
81 |
1 |
vnfd = self.db_client.get_vnfd(vnfr["vnfd-id"]) |
82 |
|
# TODO: Change for multiple DF support |
83 |
1 |
df = vnfd.get("df", [{}])[0] |
84 |
1 |
if "scaling-aspect" not in df: |
85 |
0 |
log.debug("No scaling aspect present in vnfd") |
86 |
0 |
continue |
87 |
|
# TODO: Change for multiple instantiation levels support |
88 |
1 |
scaling_aspects = df["scaling-aspect"] |
89 |
1 |
all_vnfd_monitoring_params = ( |
90 |
|
self._get_all_vnfd_monitoring_params(vnfd) |
91 |
|
) |
92 |
1 |
for scaling_aspect in scaling_aspects: |
93 |
1 |
scaling_group_record = self._get_or_create_scaling_group( |
94 |
|
nsr_id, vnfr["member-vnf-index-ref"], scaling_aspect |
95 |
|
) |
96 |
1 |
vdurs = self._get_monitored_vdurs( |
97 |
|
scaling_aspect, vnfr["vdur"] |
98 |
|
) |
99 |
1 |
for scaling_policy in scaling_aspect.get( |
100 |
|
"scaling-policy", () |
101 |
|
): |
102 |
1 |
if scaling_policy["scaling-type"] != "automatic": |
103 |
0 |
continue |
104 |
1 |
scaling_policy_record = ( |
105 |
|
self._get_or_create_scaling_policy( |
106 |
|
nsr_id, scaling_policy, scaling_group_record |
107 |
|
) |
108 |
|
) |
109 |
|
|
110 |
1 |
for scaling_criteria in scaling_policy[ |
111 |
|
"scaling-criteria" |
112 |
|
]: |
113 |
1 |
scaling_criteria_record = ( |
114 |
|
self._get_or_create_scaling_criteria( |
115 |
|
nsr_id, |
116 |
|
scaling_criteria, |
117 |
|
scaling_policy_record, |
118 |
|
) |
119 |
|
) |
120 |
1 |
monitoring_param_ref = scaling_criteria.get( |
121 |
|
"vnf-monitoring-param-ref" |
122 |
|
) |
123 |
1 |
vnf_monitoring_param = all_vnfd_monitoring_params[ |
124 |
|
monitoring_param_ref |
125 |
|
] |
126 |
|
|
127 |
1 |
for vdur in vdurs: |
128 |
1 |
vdu_id = vdur["vdu-id-ref"] |
129 |
1 |
log.debug("Creating alarm for vdur %s ", vdur) |
130 |
1 |
try: |
131 |
1 |
ScalingAlarmRepository.get( |
132 |
|
ScalingAlarm.vdu_name == vdur["name"], |
133 |
|
ScalingCriteria.name |
134 |
|
== scaling_criteria["name"], |
135 |
|
ScalingPolicy.name |
136 |
|
== scaling_policy["name"], |
137 |
|
ScalingGroup.nsr_id == nsr_id, |
138 |
|
join_classes=[ |
139 |
|
ScalingCriteria, |
140 |
|
ScalingPolicy, |
141 |
|
ScalingGroup, |
142 |
|
], |
143 |
|
) |
144 |
0 |
log.debug( |
145 |
|
"vdu %s already has an alarm configured", |
146 |
|
vdur["name"], |
147 |
|
) |
148 |
0 |
continue |
149 |
1 |
except ScalingAlarm.DoesNotExist: |
150 |
1 |
pass |
151 |
1 |
metric_name = self._get_metric_name( |
152 |
|
vnf_monitoring_param |
153 |
|
) |
154 |
|
|
155 |
1 |
db_nsr = self.db_client.get_nsr(nsr_id) |
156 |
1 |
nb_scale_op = 0 |
157 |
1 |
if db_nsr["_admin"].get("scaling-group"): |
158 |
0 |
db_nsr_admin = db_nsr["_admin"][ |
159 |
|
"scaling-group" |
160 |
|
] |
161 |
0 |
for ( |
162 |
|
admin_scale_index, |
163 |
|
admin_scale_info, |
164 |
|
) in enumerate(db_nsr_admin): |
165 |
0 |
if ( |
166 |
|
admin_scale_info["name"] |
167 |
|
== scaling_aspect["name"] |
168 |
|
): |
169 |
0 |
nb_scale_op = admin_scale_info.get( |
170 |
|
"nb-scale-op", 0 |
171 |
|
) |
172 |
0 |
break |
173 |
1 |
min_instance_count = 1 |
174 |
1 |
for vdu_profile in df.get("vdu-profile", ()): |
175 |
1 |
if vdu_profile.get("id") == vdu_id: |
176 |
1 |
min_instance_count = int( |
177 |
|
vdu_profile.get( |
178 |
|
"min-number-of-instances ", 1 |
179 |
|
) |
180 |
|
) |
181 |
1 |
break |
182 |
1 |
if nb_scale_op >= min_instance_count: |
183 |
0 |
alarm_uuid = ( |
184 |
|
await self.mon_client.create_alarm( |
185 |
|
metric_name=metric_name, |
186 |
|
ns_id=nsr_id, |
187 |
|
vdu_name=vdur["name"], |
188 |
|
vnf_member_index=vnfr[ |
189 |
|
"member-vnf-index-ref" |
190 |
|
], |
191 |
|
threshold=scaling_criteria[ |
192 |
|
"scale-in-threshold" |
193 |
|
], |
194 |
|
operation=scaling_criteria[ |
195 |
|
"scale-in-relational-operation" |
196 |
|
], |
197 |
|
action="scale_in", |
198 |
|
vnfr=vnfr, |
199 |
|
vnfd=vnfd, |
200 |
|
) |
201 |
|
) |
202 |
0 |
alarm = ScalingAlarmRepository.create( |
203 |
|
alarm_uuid=alarm_uuid, |
204 |
|
action="scale_in", |
205 |
|
vnf_member_index=vnfr[ |
206 |
|
"member-vnf-index-ref" |
207 |
|
], |
208 |
|
vdu_name=vdur["name"], |
209 |
|
scaling_criteria=scaling_criteria_record, |
210 |
|
) |
211 |
0 |
alarms_created.append(alarm) |
212 |
1 |
alarm_uuid = await self.mon_client.create_alarm( |
213 |
|
metric_name=metric_name, |
214 |
|
ns_id=nsr_id, |
215 |
|
vdu_name=vdur["name"], |
216 |
|
vnf_member_index=vnfr[ |
217 |
|
"member-vnf-index-ref" |
218 |
|
], |
219 |
|
threshold=scaling_criteria[ |
220 |
|
"scale-out-threshold" |
221 |
|
], |
222 |
|
operation=scaling_criteria[ |
223 |
|
"scale-out-relational-operation" |
224 |
|
], |
225 |
|
action="scale_out", |
226 |
|
vnfr=vnfr, |
227 |
|
vnfd=vnfd, |
228 |
|
) |
229 |
1 |
alarm = ScalingAlarmRepository.create( |
230 |
|
alarm_uuid=alarm_uuid, |
231 |
|
action="scale_out", |
232 |
|
vnf_member_index=vnfr[ |
233 |
|
"member-vnf-index-ref" |
234 |
|
], |
235 |
|
vdu_name=vdur["name"], |
236 |
|
scaling_criteria=scaling_criteria_record, |
237 |
|
) |
238 |
1 |
alarms_created.append(alarm) |
239 |
|
|
240 |
0 |
except Exception as e: |
241 |
0 |
log.exception("Error configuring scaling groups:") |
242 |
0 |
tx.rollback() |
243 |
0 |
if len(alarms_created) > 0: |
244 |
0 |
log.info("Cleaning alarm resources in MON") |
245 |
0 |
for alarm in alarms_created: |
246 |
0 |
await self.mon_client.delete_alarm( |
247 |
|
alarm.scaling_criteria.scaling_policy.scaling_group.nsr_id, |
248 |
|
alarm.vnf_member_index, |
249 |
|
alarm.vdu_name, |
250 |
|
alarm.alarm_uuid, |
251 |
|
) |
252 |
0 |
raise e |
253 |
|
finally: |
254 |
1 |
database.db.close() |
255 |
|
|
256 |
1 |
async def delete_scaling_groups(self, nsr_id: str, vnf_member_index=None): |
257 |
0 |
log.debug("Deleting scaling groups for network service %s", nsr_id) |
258 |
0 |
database.db.connect() |
259 |
0 |
try: |
260 |
0 |
with database.db.atomic() as tx: |
261 |
0 |
try: |
262 |
0 |
if vnf_member_index is None: |
263 |
0 |
scale_conditions = ScalingGroup.nsr_id == nsr_id |
264 |
|
else: |
265 |
0 |
query_list = [ |
266 |
|
ScalingGroup.nsr_id == nsr_id, |
267 |
|
ScalingGroup.vnf_member_index == vnf_member_index, |
268 |
|
] |
269 |
0 |
scale_conditions = functools.reduce(operator.and_, query_list) |
270 |
0 |
for scaling_group in ScalingGroupRepository.list(scale_conditions): |
271 |
0 |
for scaling_policy in scaling_group.scaling_policies: |
272 |
0 |
for scaling_criteria in scaling_policy.scaling_criterias: |
273 |
0 |
for alarm in scaling_criteria.scaling_alarms: |
274 |
0 |
try: |
275 |
0 |
await self.mon_client.delete_alarm( |
276 |
|
alarm.scaling_criteria.scaling_policy.scaling_group.nsr_id, |
277 |
|
alarm.vnf_member_index, |
278 |
|
alarm.vdu_name, |
279 |
|
alarm.alarm_uuid, |
280 |
|
) |
281 |
0 |
except ValueError: |
282 |
0 |
log.exception( |
283 |
|
"Error deleting alarm in MON %s", |
284 |
|
alarm.alarm_uuid, |
285 |
|
) |
286 |
0 |
alarm.delete_instance() |
287 |
0 |
scaling_criteria.delete_instance() |
288 |
0 |
scaling_policy.delete_instance() |
289 |
0 |
scaling_group.delete_instance() |
290 |
|
|
291 |
0 |
except Exception as e: |
292 |
0 |
log.exception("Error deleting scaling groups and alarms:") |
293 |
0 |
tx.rollback() |
294 |
0 |
raise e |
295 |
|
finally: |
296 |
0 |
database.db.close() |
297 |
|
|
298 |
1 |
async def delete_orphaned_alarms(self, nsr_id): |
299 |
0 |
log.info("Deleting orphaned scaling alarms for network service %s", nsr_id) |
300 |
0 |
database.db.connect() |
301 |
0 |
try: |
302 |
0 |
with database.db.atomic() as tx: |
303 |
0 |
try: |
304 |
0 |
for scaling_group in ScalingGroupRepository.list( |
305 |
|
ScalingGroup.nsr_id == nsr_id |
306 |
|
): |
307 |
0 |
for scaling_policy in scaling_group.scaling_policies: |
308 |
0 |
for scaling_criteria in scaling_policy.scaling_criterias: |
309 |
0 |
for alarm in scaling_criteria.scaling_alarms: |
310 |
0 |
try: |
311 |
0 |
self.db_client.get_vdur( |
312 |
|
nsr_id, |
313 |
|
alarm.vnf_member_index, |
314 |
|
alarm.vdu_name, |
315 |
|
) |
316 |
0 |
except VdurNotFound: |
317 |
0 |
log.debug( |
318 |
|
"Deleting orphaned scaling alarm %s", |
319 |
|
alarm.alarm_uuid, |
320 |
|
) |
321 |
0 |
try: |
322 |
0 |
await self.mon_client.delete_alarm( |
323 |
|
alarm.scaling_criteria.scaling_policy.scaling_group.nsr_id, |
324 |
|
alarm.vnf_member_index, |
325 |
|
alarm.vdu_name, |
326 |
|
alarm.alarm_uuid, |
327 |
|
) |
328 |
0 |
except ValueError: |
329 |
0 |
log.exception( |
330 |
|
"Error deleting alarm in MON %s", |
331 |
|
alarm.alarm_uuid, |
332 |
|
) |
333 |
0 |
alarm.delete_instance() |
334 |
|
|
335 |
0 |
except Exception as e: |
336 |
0 |
log.exception("Error deleting orphaned alarms:") |
337 |
0 |
tx.rollback() |
338 |
0 |
raise e |
339 |
|
finally: |
340 |
0 |
database.db.close() |
341 |
|
|
342 |
1 |
async def handle_alarm(self, alarm_uuid: str, status: str): |
343 |
0 |
await self.update_alarm_status(alarm_uuid, status) |
344 |
0 |
await self.evaluate_policy(alarm_uuid) |
345 |
|
|
346 |
1 |
async def update_alarm_status(self, alarm_uuid: str, status: str): |
347 |
1 |
database.db.connect() |
348 |
1 |
try: |
349 |
1 |
with database.db.atomic(): |
350 |
1 |
alarm = ScalingAlarmRepository.get( |
351 |
|
ScalingAlarm.alarm_uuid == alarm_uuid |
352 |
|
) |
353 |
1 |
alarm.last_status = status |
354 |
1 |
alarm.save() |
355 |
0 |
except ScalingAlarm.DoesNotExist: |
356 |
0 |
log.debug( |
357 |
|
"There is no autoscaling action configured for alarm %s.", alarm_uuid |
358 |
|
) |
359 |
|
finally: |
360 |
1 |
database.db.close() |
361 |
|
|
362 |
1 |
async def evaluate_policy(self, alarm_uuid): |
363 |
1 |
database.db.connect() |
364 |
1 |
try: |
365 |
1 |
if self.conf.get("autoscale", "enabled") == "True": |
366 |
0 |
with database.db.atomic(): |
367 |
0 |
alarm = ScalingAlarmRepository.get( |
368 |
|
ScalingAlarm.alarm_uuid == alarm_uuid |
369 |
|
) |
370 |
0 |
vnf_member_index = alarm.vnf_member_index |
371 |
0 |
action = alarm.action |
372 |
0 |
scaling_policy = alarm.scaling_criteria.scaling_policy |
373 |
0 |
if not scaling_policy.enabled: |
374 |
0 |
return |
375 |
0 |
if action == "scale_in": |
376 |
0 |
operation = scaling_policy.scale_in_operation |
377 |
0 |
elif action == "scale_out": |
378 |
0 |
operation = scaling_policy.scale_out_operation |
379 |
|
else: |
380 |
0 |
raise Exception("Unknown alarm action {}".format(alarm.action)) |
381 |
0 |
alarms = ScalingAlarmRepository.list( |
382 |
|
ScalingAlarm.scaling_criteria == alarm.scaling_criteria, |
383 |
|
ScalingAlarm.action == alarm.action, |
384 |
|
ScalingAlarm.vnf_member_index == vnf_member_index, |
385 |
|
ScalingAlarm.vdu_name == alarm.vdu_name, |
386 |
|
) |
387 |
0 |
statuses = [] |
388 |
0 |
for alarm in alarms: |
389 |
0 |
statuses.append(alarm.last_status) |
390 |
0 |
if (operation == "AND" and set(statuses) == {"alarm"}) or ( |
391 |
|
operation == "OR" and "alarm" in statuses |
392 |
|
): |
393 |
0 |
delta = datetime.datetime.now() - scaling_policy.last_scale |
394 |
0 |
if delta.total_seconds() > scaling_policy.cooldown_time: |
395 |
0 |
log.info( |
396 |
|
"Sending %s action message for ns: %s", |
397 |
|
alarm.action, |
398 |
|
scaling_policy.scaling_group.nsr_id, |
399 |
|
) |
400 |
0 |
await self.lcm_client.scale( |
401 |
|
scaling_policy.scaling_group.nsr_id, |
402 |
|
scaling_policy.scaling_group.name, |
403 |
|
vnf_member_index, |
404 |
|
action, |
405 |
|
) |
406 |
0 |
scaling_policy.last_scale = datetime.datetime.now() |
407 |
0 |
scaling_policy.save() |
408 |
|
|
409 |
0 |
except ScalingAlarm.DoesNotExist: |
410 |
0 |
log.debug( |
411 |
|
"There is no autoscaling action configured for alarm %s.", alarm_uuid |
412 |
|
) |
413 |
|
finally: |
414 |
1 |
database.db.close() |
415 |
|
|
416 |
1 |
def _get_all_vnfd_monitoring_params(self, vnfd): |
417 |
1 |
all_monitoring_params = {} |
418 |
1 |
for ivld in vnfd.get("int-virtual-link-desc", ()): |
419 |
0 |
for mp in ivld.get("monitoring-parameters", ()): |
420 |
0 |
all_monitoring_params[mp.get("id")] = mp |
421 |
|
|
422 |
1 |
for vdu in vnfd.get("vdu", ()): |
423 |
1 |
for mp in vdu.get("monitoring-parameter", ()): |
424 |
1 |
all_monitoring_params[mp.get("id")] = mp |
425 |
|
|
426 |
1 |
for df in vnfd.get("df", ()): |
427 |
1 |
for mp in df.get("monitoring-parameter", ()): |
428 |
0 |
all_monitoring_params[mp.get("id")] = mp |
429 |
|
|
430 |
1 |
return all_monitoring_params |
431 |
|
|
432 |
1 |
def _get_or_create_scaling_group( |
433 |
|
self, nsr_id: str, vnf_member_index: str, scaling_aspect: dict |
434 |
|
): |
435 |
1 |
try: |
436 |
1 |
scaling_group_record = ScalingGroupRepository.get( |
437 |
|
ScalingGroup.nsr_id == nsr_id, |
438 |
|
ScalingGroup.vnf_member_index == vnf_member_index, |
439 |
|
ScalingGroup.name == scaling_aspect["name"], |
440 |
|
) |
441 |
0 |
log.debug("Found existing scaling group record in DB...") |
442 |
1 |
except ScalingGroup.DoesNotExist: |
443 |
1 |
log.debug("Creating scaling group record in DB...") |
444 |
1 |
scaling_group_record = ScalingGroupRepository.create( |
445 |
|
nsr_id=nsr_id, |
446 |
|
vnf_member_index=vnf_member_index, |
447 |
|
name=scaling_aspect["name"], |
448 |
|
content=json.dumps(scaling_aspect), |
449 |
|
) |
450 |
1 |
log.debug( |
451 |
|
"Created scaling group record in DB : nsr_id=%s, vnf_member_index=%s, name=%s", |
452 |
|
scaling_group_record.nsr_id, |
453 |
|
scaling_group_record.vnf_member_index, |
454 |
|
scaling_group_record.name, |
455 |
|
) |
456 |
1 |
return scaling_group_record |
457 |
|
|
458 |
1 |
def _get_or_create_scaling_policy( |
459 |
|
self, nsr_id: str, scaling_policy: dict, scaling_group_record: ScalingGroup |
460 |
|
): |
461 |
1 |
try: |
462 |
1 |
scaling_policy_record = ScalingPolicyRepository.get( |
463 |
|
ScalingPolicy.name == scaling_policy["name"], |
464 |
|
ScalingGroup.id == scaling_group_record.id, |
465 |
|
join_classes=[ScalingGroup], |
466 |
|
) |
467 |
0 |
log.debug("Found existing scaling policy record in DB...") |
468 |
1 |
except ScalingPolicy.DoesNotExist: |
469 |
1 |
log.debug("Creating scaling policy record in DB...") |
470 |
1 |
scaling_policy_record = ScalingPolicyRepository.create( |
471 |
|
nsr_id=nsr_id, |
472 |
|
name=scaling_policy["name"], |
473 |
|
cooldown_time=scaling_policy["cooldown-time"], |
474 |
|
scaling_group=scaling_group_record, |
475 |
|
) |
476 |
1 |
if "scale-in-operation-type" in scaling_policy: |
477 |
0 |
scaling_policy_record.scale_in_operation = scaling_policy[ |
478 |
|
"scale-in-operation-type" |
479 |
|
] |
480 |
1 |
if "scale-out-operation-type" in scaling_policy: |
481 |
0 |
scaling_policy_record.scale_out_operation = scaling_policy[ |
482 |
|
"scale-out-operation-type" |
483 |
|
] |
484 |
1 |
if "enabled" in scaling_policy: |
485 |
0 |
scaling_policy_record.enabled = scaling_policy["enabled"] |
486 |
1 |
scaling_policy_record.save() |
487 |
1 |
log.debug( |
488 |
|
"Created scaling policy record in DB : name=%s, scaling_group.name=%s", |
489 |
|
scaling_policy_record.name, |
490 |
|
scaling_policy_record.scaling_group.name, |
491 |
|
) |
492 |
1 |
return scaling_policy_record |
493 |
|
|
494 |
1 |
def _get_or_create_scaling_criteria( |
495 |
|
self, nsr_id: str, scaling_criteria: dict, scaling_policy_record: ScalingPolicy |
496 |
|
): |
497 |
1 |
try: |
498 |
1 |
scaling_criteria_record = ScalingCriteriaRepository.get( |
499 |
|
ScalingPolicy.id == scaling_policy_record.id, |
500 |
|
ScalingCriteria.name == scaling_criteria["name"], |
501 |
|
join_classes=[ScalingPolicy], |
502 |
|
) |
503 |
0 |
log.debug("Found existing scaling criteria record in DB...") |
504 |
1 |
except ScalingCriteria.DoesNotExist: |
505 |
1 |
log.debug("Creating scaling criteria record in DB...") |
506 |
1 |
scaling_criteria_record = ScalingCriteriaRepository.create( |
507 |
|
nsr_id=nsr_id, |
508 |
|
name=scaling_criteria["name"], |
509 |
|
scaling_policy=scaling_policy_record, |
510 |
|
) |
511 |
1 |
log.debug( |
512 |
|
"Created scaling criteria record in DB : name=%s, scaling_policy.name=%s", |
513 |
|
scaling_criteria_record.name, |
514 |
|
scaling_criteria_record.scaling_policy.name, |
515 |
|
) |
516 |
1 |
return scaling_criteria_record |
517 |
|
|
518 |
1 |
def _get_monitored_vdurs(self, scaling_aspect: dict, vdurs): |
519 |
1 |
all_monitored_vdus = set() |
520 |
1 |
for delta in scaling_aspect.get("aspect-delta-details", {}).get("deltas", ()): |
521 |
1 |
for vdu_delta in delta.get("vdu-delta", ()): |
522 |
1 |
all_monitored_vdus.add(vdu_delta.get("id")) |
523 |
|
|
524 |
1 |
monitored_vdurs = list( |
525 |
|
filter(lambda vdur: vdur["vdu-id-ref"] in all_monitored_vdus, vdurs) |
526 |
|
) |
527 |
|
|
528 |
1 |
if not monitored_vdurs: |
529 |
0 |
log.warning( |
530 |
|
"Scaling criteria is referring to a vnf-monitoring-param that does not " |
531 |
|
"contain a reference to a vdu or vnf metric." |
532 |
|
) |
533 |
1 |
return monitored_vdurs |
534 |
|
|
535 |
1 |
def _get_metric_name(self, vnf_monitoring_param: dict): |
536 |
1 |
if "performance-metric" in vnf_monitoring_param: |
537 |
1 |
return vnf_monitoring_param["performance-metric"] |
538 |
0 |
raise ValueError( |
539 |
|
"No metric name found for vnf_monitoring_param %s" |
540 |
|
% vnf_monitoring_param["id"] |
541 |
|
) |