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
27 from json
import JSONDecodeError
30 from kafka
import KafkaConsumer
32 from osm_policy_module
.common
.db_client
import DbClient
33 from osm_policy_module
.common
.lcm_client
import LcmClient
34 from osm_policy_module
.common
.mon_client
import MonClient
35 from osm_policy_module
.core
import database
36 from osm_policy_module
.core
.config
import Config
37 from osm_policy_module
.core
.database
import ScalingGroup
, ScalingAlarm
, ScalingPolicy
, ScalingCriteria
39 log
= logging
.getLogger(__name__
)
41 ALLOWED_KAFKA_KEYS
= ['instantiated', 'scaled', 'notify_alarm']
44 class PolicyModuleAgent
:
46 cfg
= Config
.instance()
47 self
.db_client
= DbClient()
48 self
.mon_client
= MonClient()
49 self
.lcm_client
= LcmClient()
50 self
.kafka_server
= '{}:{}'.format(cfg
.OSMPOL_MESSAGE_HOST
,
51 cfg
.OSMPOL_MESSAGE_PORT
)
54 consumer
= KafkaConsumer(bootstrap_servers
=self
.kafka_server
,
55 key_deserializer
=bytes
.decode
,
56 value_deserializer
=bytes
.decode
,
57 group_id
='pol-consumer')
58 consumer
.subscribe(["ns", "alarm_response"])
60 for message
in consumer
:
61 t
= threading
.Thread(target
=self
._process
_msg
, args
=(message
.topic
, message
.key
, message
.value
,))
64 def _process_msg(self
, topic
, key
, msg
):
66 log
.debug("Message arrived with topic: %s, key: %s, msg: %s", topic
, key
, msg
)
67 if key
in ALLOWED_KAFKA_KEYS
:
69 content
= json
.loads(msg
)
70 except JSONDecodeError
:
71 content
= yaml
.safe_load(msg
)
73 if key
== 'instantiated' or key
== 'scaled':
74 self
._handle
_instantiated
_or
_scaled
(content
)
76 if key
== 'notify_alarm':
77 self
._handle
_alarm
_notification
(content
)
79 log
.debug("Key %s is not in ALLOWED_KAFKA_KEYS", key
)
81 log
.exception("Error consuming message: ")
83 def _handle_alarm_notification(self
, content
):
84 alarm_id
= content
['notify_details']['alarm_uuid']
85 metric_name
= content
['notify_details']['metric_name']
86 operation
= content
['notify_details']['operation']
87 threshold
= content
['notify_details']['threshold_value']
88 vdu_name
= content
['notify_details']['vdu_name']
89 vnf_member_index
= content
['notify_details']['vnf_member_index']
90 ns_id
= content
['notify_details']['ns_id']
92 "Received alarm notification for alarm %s, \
97 vnf_member_index %s, \
99 alarm_id
, metric_name
, operation
, threshold
, vdu_name
, vnf_member_index
, ns_id
)
101 alarm
= ScalingAlarm
.select().where(ScalingAlarm
.alarm_id
== alarm_id
).get()
102 log
.info("Sending scaling action message for ns: %s", alarm_id
)
103 self
.lcm_client
.scale(alarm
.scaling_criteria
.scaling_policy
.scaling_group
.nsr_id
,
104 alarm
.scaling_criteria
.scaling_policy
.scaling_group
.name
,
105 alarm
.vnf_member_index
,
107 except ScalingAlarm
.DoesNotExist
:
108 log
.info("There is no action configured for alarm %s.", alarm_id
)
110 def _handle_instantiated_or_scaled(self
, content
):
111 nslcmop_id
= content
['nslcmop_id']
112 nslcmop
= self
.db_client
.get_nslcmop(nslcmop_id
)
113 if nslcmop
['operationState'] == 'COMPLETED' or nslcmop
['operationState'] == 'PARTIALLY_COMPLETED':
114 nsr_id
= nslcmop
['nsInstanceId']
115 log
.info("Configuring scaling groups for network service with nsr_id: %s", nsr_id
)
116 self
._configure
_scaling
_groups
(nsr_id
)
119 "Network service is not in COMPLETED or PARTIALLY_COMPLETED state. "
120 "Current state is %s. Skipping...",
121 nslcmop
['operationState'])
123 def _configure_scaling_groups(self
, nsr_id
: str):
124 # TODO(diazb): Check for alarm creation on exception and clean resources if needed.
125 # TODO: Add support for non-nfvi metrics
126 with database
.db
.atomic():
127 vnfrs
= self
.db_client
.get_vnfrs(nsr_id
)
128 log
.info("Checking %s vnfrs...", len(vnfrs
))
130 vnfd
= self
.db_client
.get_vnfd(vnfr
['vnfd-id'])
131 log
.info("Looking for vnfd %s", vnfr
['vnfd-id'])
132 scaling_groups
= vnfd
['scaling-group-descriptor']
133 vnf_monitoring_params
= vnfd
['monitoring-param']
134 for scaling_group
in scaling_groups
:
136 scaling_group_record
= ScalingGroup
.select().where(
137 ScalingGroup
.nsr_id
== nsr_id
,
138 ScalingGroup
.name
== scaling_group
['name']
140 except ScalingGroup
.DoesNotExist
:
141 log
.info("Creating scaling group record in DB...")
142 scaling_group_record
= ScalingGroup
.create(
144 name
=scaling_group
['name'],
145 content
=json
.dumps(scaling_group
)
147 log
.info("Created scaling group record in DB : nsr_id=%s, name=%s, content=%s",
148 scaling_group_record
.nsr_id
,
149 scaling_group_record
.name
,
150 scaling_group_record
.content
)
151 for scaling_policy
in scaling_group
['scaling-policy']:
152 if scaling_policy
['scaling-type'] != 'automatic':
155 scaling_policy_record
= ScalingPolicy
.select().join(ScalingGroup
).where(
156 ScalingPolicy
.name
== scaling_policy
['name'],
157 ScalingGroup
.id == scaling_group_record
.id
159 except ScalingPolicy
.DoesNotExist
:
160 log
.info("Creating scaling policy record in DB...")
161 scaling_policy_record
= ScalingPolicy
.create(
163 name
=scaling_policy
['name'],
164 scaling_group
=scaling_group_record
166 log
.info("Created scaling policy record in DB : name=%s, scaling_group.name=%s",
167 scaling_policy_record
.name
,
168 scaling_policy_record
.scaling_group
.name
)
170 for scaling_criteria
in scaling_policy
['scaling-criteria']:
172 scaling_criteria_record
= ScalingCriteria
.select().join(ScalingPolicy
).where(
173 ScalingPolicy
.id == scaling_policy_record
.id,
174 ScalingCriteria
.name
== scaling_criteria
['name']
176 except ScalingCriteria
.DoesNotExist
:
177 log
.info("Creating scaling criteria record in DB...")
178 scaling_criteria_record
= ScalingCriteria
.create(
180 name
=scaling_criteria
['name'],
181 scaling_policy
=scaling_policy_record
184 "Created scaling criteria record in DB : name=%s, scaling_policy.name=%s",
185 scaling_criteria_record
.name
,
186 scaling_criteria_record
.scaling_policy
.name
)
188 for vdu_ref
in scaling_group
['vdu']:
189 vnf_monitoring_param
= next(
190 filter(lambda param
: param
['id'] == scaling_criteria
['vnf-monitoring-param-ref'],
191 vnf_monitoring_params
))
192 if not vdu_ref
['vdu-id-ref'] == vnf_monitoring_param
['vdu-ref']:
195 filter(lambda vdu
: vdu
['id'] == vdu_ref
['vdu-id-ref'], vnfd
['vdu'])
197 vdu_monitoring_params
= vdu
['monitoring-param']
198 vdu_monitoring_param
= next(
200 lambda param
: param
['id'] == vnf_monitoring_param
['vdu-monitoring-param-ref'],
201 vdu_monitoring_params
))
202 vdurs
= list(filter(lambda vdur
: vdur
['vdu-id-ref'] == vnf_monitoring_param
['vdu-ref'],
206 ScalingAlarm
.select().join(ScalingCriteria
).where(
207 ScalingAlarm
.vdu_name
== vdur
['name'],
208 ScalingCriteria
.name
== scaling_criteria
['name']
210 log
.debug("VDU %s already has an alarm configured", vdur
['name'])
212 except ScalingAlarm
.DoesNotExist
:
214 alarm_uuid
= self
.mon_client
.create_alarm(
215 metric_name
=vdu_monitoring_param
['nfvi-metric'],
217 vdu_name
=vdur
['name'],
218 vnf_member_index
=vnfr
['member-vnf-index-ref'],
219 threshold
=scaling_criteria
['scale-in-threshold'],
220 operation
=scaling_criteria
['scale-in-relational-operation'],
221 statistic
=vnf_monitoring_param
['aggregation-type']
226 vnf_member_index
=int(vnfr
['member-vnf-index-ref']),
227 vdu_name
=vdur
['name'],
228 scaling_criteria
=scaling_criteria_record
230 alarm_uuid
= self
.mon_client
.create_alarm(
231 metric_name
=vdu_monitoring_param
['nfvi-metric'],
233 vdu_name
=vdur
['name'],
234 vnf_member_index
=vnfr
['member-vnf-index-ref'],
235 threshold
=scaling_criteria
['scale-out-threshold'],
236 operation
=scaling_criteria
['scale-out-relational-operation'],
237 statistic
=vnf_monitoring_param
['aggregation-type']
242 vnf_member_index
=int(vnfr
['member-vnf-index-ref']),
243 vdu_name
=vdur
['name'],
244 scaling_criteria
=scaling_criteria_record