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
29 from kafka
import KafkaProducer
, KafkaConsumer
31 from osm_policy_module
.core
.config
import Config
33 log
= logging
.getLogger(__name__
)
38 cfg
= Config
.instance()
39 self
.kafka_server
= '{}:{}'.format(cfg
.get('policy_module', 'kafka_server_host'),
40 cfg
.get('policy_module', 'kafka_server_port'))
41 self
.producer
= KafkaProducer(bootstrap_servers
=self
.kafka_server
,
42 key_serializer
=str.encode
,
43 value_serializer
=str.encode
)
45 def create_alarm(self
, metric_name
, resource_uuid
, vim_uuid
, threshold
, statistic
, operation
):
46 cor_id
= random
.randint(1, 1000000)
47 msg
= self
._create
_alarm
_payload
(cor_id
, metric_name
, resource_uuid
, vim_uuid
, threshold
, statistic
, operation
)
48 log
.info("Sending create_alarm_request %s", msg
)
49 future
= self
.producer
.send(topic
='alarm_request', key
='create_alarm_request', value
=json
.dumps(msg
))
50 future
.get(timeout
=60)
51 consumer
= KafkaConsumer(bootstrap_servers
=self
.kafka_server
,
52 key_deserializer
=bytes
.decode
,
53 value_deserializer
=bytes
.decode
,
54 consumer_timeout_ms
=10000)
55 consumer
.subscribe(['alarm_response'])
56 for message
in consumer
:
57 if message
.key
== 'create_alarm_response':
58 content
= json
.loads(message
.value
)
59 log
.info("Received create_alarm_response %s", content
)
60 if self
._is
_alarm
_response
_correlation
_id
_eq
(cor_id
, content
):
61 alarm_uuid
= content
['alarm_create_response']['alarm_uuid']
62 # TODO Handle error response
65 raise ValueError('Timeout: No alarm creation response from MON. Is MON up?')
67 def _create_alarm_payload(self
, cor_id
, metric_name
, resource_uuid
, vim_uuid
, threshold
, statistic
, operation
):
68 alarm_create_request
= {
69 'correlation_id': cor_id
,
70 'alarm_name': str(uuid
.uuid4()),
71 'metric_name': metric_name
,
72 'resource_uuid': resource_uuid
,
73 'operation': operation
,
74 'severity': 'critical',
75 'threshold_value': threshold
,
76 'statistic': statistic
79 'alarm_create_request': alarm_create_request
,
84 def _is_alarm_response_correlation_id_eq(self
, cor_id
, message_content
):
85 return message_content
['alarm_create_response']['correlation_id'] == cor_id