cdd5dfc9eb8b8b24b8307a20fdfa39daa5b0015c
[osm/MON.git] / policy_module / osm_policy_module / core / 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 json
25 import logging
26 from typing import Dict, List
27
28 import yaml
29 from kafka import KafkaConsumer
30 from osm_policy_module.common.alarm_config import AlarmConfig
31 from osm_policy_module.common.lcm_client import LcmClient
32 from osm_policy_module.common.mon_client import MonClient
33 from osm_policy_module.core.config import Config
34 from osm_policy_module.core.database import ScalingRecord, ScalingAlarm
35
36 log = logging.getLogger(__name__)
37
38
39 class PolicyModuleAgent:
40 def run(self):
41 cfg = Config.instance()
42 # Initialize servers
43 kafka_server = '{}:{}'.format(cfg.get('policy_module', 'kafka_server_host'),
44 cfg.get('policy_module', 'kafka_server_port'))
45
46 # Initialize Kafka consumer
47 log.info("Connecting to Kafka server at %s", kafka_server)
48 consumer = KafkaConsumer(bootstrap_servers=kafka_server,
49 key_deserializer=bytes.decode,
50 value_deserializer=bytes.decode,
51 group_id="pm-consumer")
52 consumer.subscribe(['lcm_pm', 'alarm_response'])
53
54 for message in consumer:
55 log.info("Message arrived: %s", message)
56 try:
57 if message.key == 'configure_scaling':
58 try:
59 content = json.loads(message.value)
60 except:
61 content = yaml.safe_load(message.value)
62 log.info("Creating scaling record in DB")
63 # TODO: Use transactions: http://docs.peewee-orm.com/en/latest/peewee/transactions.html
64 scaling_record = ScalingRecord.create(
65 nsr_id=content['ns_id'],
66 name=content['scaling_group_descriptor']['name'],
67 content=json.dumps(content)
68 )
69 log.info("Created scaling record in DB : nsr_id=%s, name=%s, content=%s",
70 scaling_record.nsr_id,
71 scaling_record.name,
72 scaling_record.content)
73 alarm_configs = self._get_alarm_configs(content)
74 for config in alarm_configs:
75 mon_client = MonClient()
76 log.info("Creating alarm record in DB")
77 alarm_uuid = mon_client.create_alarm(
78 metric_name=config.metric_name,
79 ns_id=scaling_record.nsr_id,
80 vdu_name=config.vdu_name,
81 vnf_member_index=config.vnf_member_index,
82 threshold=config.threshold,
83 operation=config.operation,
84 statistic=config.statistic
85 )
86 ScalingAlarm.create(
87 alarm_id=alarm_uuid,
88 action=config.action,
89 scaling_record=scaling_record
90 )
91 if message.key == 'notify_alarm':
92 content = json.loads(message.value)
93 alarm_id = content['notify_details']['alarm_uuid']
94 metric_name = content['notify_details']['metric_name']
95 operation = content['notify_details']['operation']
96 threshold = content['notify_details']['threshold_value']
97 vdu_name = content['notify_details']['vdu_name']
98 vnf_member_index = content['notify_details']['vnf_member_index']
99 ns_id = content['notify_details']['ns_id']
100 log.info(
101 "Received alarm notification for alarm %s, \
102 metric %s, \
103 operation %s, \
104 threshold %s, \
105 vdu_name %s, \
106 vnf_member_index %s, \
107 ns_id %s ",
108 alarm_id, metric_name, operation, threshold, vdu_name, vnf_member_index, ns_id)
109 try:
110 alarm = ScalingAlarm.select().where(ScalingAlarm.alarm_id == alarm_id).get()
111 lcm_client = LcmClient()
112 log.info("Sending scaling action message for ns: %s", alarm_id)
113 lcm_client.scale(alarm.scaling_record.nsr_id, alarm.scaling_record.name, alarm.action)
114 except ScalingAlarm.DoesNotExist:
115 log.info("There is no action configured for alarm %s.", alarm_id)
116 except Exception:
117 log.exception("Error consuming message: ")
118
119 def _get_alarm_configs(self, message_content: Dict) -> List[AlarmConfig]:
120 scaling_criterias = message_content['scaling_group_descriptor']['scaling_policy']['scaling_criteria']
121 alarm_configs = []
122 for criteria in scaling_criterias:
123 metric_name = ''
124 scale_out_threshold = criteria['scale_out_threshold']
125 scale_in_threshold = criteria['scale_in_threshold']
126 scale_out_operation = criteria['scale_out_relational_operation']
127 scale_in_operation = criteria['scale_in_relational_operation']
128 statistic = criteria['monitoring_param']['aggregation_type']
129 vdu_name = ''
130 vnf_member_index = ''
131 if 'vdu_monitoring_param' in criteria['monitoring_param']:
132 vdu_name = criteria['monitoring_param']['vdu_monitoring_param']['vdu_name']
133 vnf_member_index = criteria['monitoring_param']['vdu_monitoring_param']['vnf_member_index']
134 metric_name = criteria['monitoring_param']['vdu_monitoring_param']['name']
135 if 'vnf_metric' in criteria['monitoring_param']:
136 # TODO vnf_metric
137 continue
138 if 'vdu_metric' in criteria['monitoring_param']:
139 # TODO vdu_metric
140 continue
141 scale_out_alarm_config = AlarmConfig(metric_name,
142 vdu_name,
143 vnf_member_index,
144 scale_out_threshold,
145 scale_out_operation,
146 statistic,
147 'scale_out')
148 scale_in_alarm_config = AlarmConfig(metric_name,
149 vdu_name,
150 vnf_member_index,
151 scale_in_threshold,
152 scale_in_operation,
153 statistic,
154 'scale_in')
155 alarm_configs.append(scale_in_alarm_config)
156 alarm_configs.append(scale_out_alarm_config)
157 return alarm_configs