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
28 from json
import JSONDecodeError
31 from aiokafka
import AIOKafkaProducer
, AIOKafkaConsumer
33 from osm_policy_module
.core
.config
import Config
35 log
= logging
.getLogger(__name__
)
39 def __init__(self
, config
: Config
, loop
=None):
40 self
.kafka_server
= "{}:{}".format(
41 config
.get("message", "host"), config
.get("message", "port")
44 loop
= asyncio
.get_event_loop()
47 async def create_alarm(
52 vnf_member_index
: str,
55 statistic
: str = "AVERAGE",
58 cor_id
= random
.randint(1, 10e7
)
59 msg
= self
._build
_create
_alarm
_payload
(
70 log
.debug("Sending create_alarm_request %s", msg
)
71 producer
= AIOKafkaProducer(
73 bootstrap_servers
=self
.kafka_server
,
74 key_serializer
=str.encode
,
75 value_serializer
=str.encode
,
77 await producer
.start()
79 await producer
.send_and_wait(
80 "alarm_request", key
="create_alarm_request", value
=json
.dumps(msg
)
84 log
.debug("Waiting for create_alarm_response...")
85 consumer
= AIOKafkaConsumer(
86 "alarm_response_" + str(cor_id
),
88 bootstrap_servers
=self
.kafka_server
,
89 key_deserializer
=bytes
.decode
,
90 value_deserializer
=bytes
.decode
,
91 auto_offset_reset
="earliest",
93 await consumer
.start()
96 async for message
in consumer
:
98 content
= json
.loads(message
.value
)
99 except JSONDecodeError
:
100 content
= yaml
.safe_load(message
.value
)
101 log
.debug("Received create_alarm_response %s", content
)
102 if content
["alarm_create_response"]["correlation_id"] == cor_id
:
103 if not content
["alarm_create_response"]["status"]:
104 raise ValueError("Error creating alarm in MON")
105 alarm_uuid
= content
["alarm_create_response"]["alarm_uuid"]
108 await consumer
.stop()
110 raise ValueError("No alarm deletion response from MON. Is MON up?")
113 async def delete_alarm(
114 self
, ns_id
: str, vnf_member_index
: str, vdu_name
: str, alarm_uuid
: str
116 cor_id
= random
.randint(1, 10e7
)
117 msg
= self
._build
_delete
_alarm
_payload
(
118 cor_id
, ns_id
, vdu_name
, vnf_member_index
, alarm_uuid
120 log
.debug("Sending delete_alarm_request %s", msg
)
121 producer
= AIOKafkaProducer(
123 bootstrap_servers
=self
.kafka_server
,
124 key_serializer
=str.encode
,
125 value_serializer
=str.encode
,
127 await producer
.start()
129 await producer
.send_and_wait(
130 "alarm_request", key
="delete_alarm_request", value
=json
.dumps(msg
)
133 await producer
.stop()
134 log
.debug("Waiting for delete_alarm_response...")
135 consumer
= AIOKafkaConsumer(
136 "alarm_response_" + str(cor_id
),
138 bootstrap_servers
=self
.kafka_server
,
139 key_deserializer
=bytes
.decode
,
140 value_deserializer
=bytes
.decode
,
141 auto_offset_reset
="earliest",
143 await consumer
.start()
146 async for message
in consumer
:
148 content
= json
.loads(message
.value
)
149 except JSONDecodeError
:
150 content
= yaml
.safe_load(message
.value
)
151 if content
["alarm_delete_response"]["correlation_id"] == cor_id
:
152 log
.debug("Received delete_alarm_response %s", content
)
153 if not content
["alarm_delete_response"]["status"]:
155 "Error deleting alarm in MON. Response status is False."
157 alarm_uuid
= content
["alarm_delete_response"]["alarm_uuid"]
160 await consumer
.stop()
162 raise ValueError("No alarm deletion response from MON. Is MON up?")
165 def _build_create_alarm_payload(
171 vnf_member_index
: str,
178 alarm_create_request
= {
179 "correlation_id": cor_id
,
180 "alarm_name": "osm_alarm_{}_{}_{}_{}".format(
181 ns_id
, vnf_member_index
, vdu_name
, metric_name
183 "metric_name": metric_name
,
184 "operation": operation
,
185 "severity": "critical",
186 "threshold_value": threshold
,
187 "statistic": statistic
,
191 "vdu_name": vdu_name
,
192 "vnf_member_index": vnf_member_index
,
196 "alarm_create_request": alarm_create_request
,
200 def _build_delete_alarm_payload(
205 vnf_member_index
: str,
208 alarm_delete_request
= {
209 "correlation_id": cor_id
,
210 "alarm_uuid": alarm_uuid
,
213 "vdu_name": vdu_name
,
214 "vnf_member_index": vnf_member_index
,
218 "alarm_delete_request": alarm_delete_request
,