5d2416e7f3845847e589f3c1cbf7a02ee35ad4ca
[osm/POL.git] / osm_policy_module / common / mon_client.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 import random
27 import uuid
28
29 from kafka import KafkaProducer, KafkaConsumer
30
31 from osm_policy_module.core.config import Config
32
33 log = logging.getLogger(__name__)
34
35
36 class MonClient:
37 def __init__(self):
38 cfg = Config.instance()
39 self.kafka_server = '{}:{}'.format(cfg.OSMPOL_MESSAGE_HOST,
40 cfg.OSMPOL_MESSAGE_PORT)
41 self.producer = KafkaProducer(bootstrap_servers=self.kafka_server,
42 key_serializer=str.encode,
43 value_serializer=str.encode)
44
45 def create_alarm(self, metric_name: str, ns_id: str, vdu_name: str, vnf_member_index: int, threshold: int,
46 statistic: str, operation: str):
47 cor_id = random.randint(1, 1000000)
48 msg = self._create_alarm_payload(cor_id, metric_name, ns_id, vdu_name, vnf_member_index, threshold, statistic,
49 operation)
50 log.info("Sending create_alarm_request %s", msg)
51 self.producer.send(topic='alarm_request', key='create_alarm_request', value=json.dumps(msg))
52 self.producer.flush()
53 consumer = KafkaConsumer(bootstrap_servers=self.kafka_server,
54 key_deserializer=bytes.decode,
55 value_deserializer=bytes.decode,
56 consumer_timeout_ms=10000,
57 group_id='mon-client-' + str(uuid.uuid4()))
58 consumer.subscribe(['alarm_response'])
59 for message in consumer:
60 if message.key == 'create_alarm_response':
61 content = json.loads(message.value)
62 log.info("Received create_alarm_response %s", content)
63 if self._is_alarm_response_correlation_id_eq(cor_id, content):
64 alarm_uuid = content['alarm_create_response']['alarm_uuid']
65 # TODO Handle error response
66 return alarm_uuid
67
68 raise ValueError('Timeout: No alarm creation response from MON. Is MON up?')
69
70 def _create_alarm_payload(self, cor_id: int, metric_name: str, ns_id: str, vdu_name: str, vnf_member_index: int,
71 threshold: int, statistic: str, operation: str):
72 alarm_create_request = {
73 'correlation_id': cor_id,
74 'alarm_name': str(uuid.uuid4()),
75 'metric_name': metric_name,
76 'ns_id': ns_id,
77 'vdu_name': vdu_name,
78 'vnf_member_index': vnf_member_index,
79 'operation': operation,
80 'severity': 'critical',
81 'threshold_value': threshold,
82 'statistic': statistic
83 }
84 msg = {
85 'alarm_create_request': alarm_create_request,
86 }
87 return msg
88
89 def _is_alarm_response_correlation_id_eq(self, cor_id, message_content):
90 return message_content['alarm_create_response']['correlation_id'] == cor_id