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
26 from pathlib
import Path
31 from osm_policy_module
.alarming
.service
import AlarmingService
32 from osm_policy_module
.autoscaling
.service
import AutoscalingService
33 from osm_policy_module
.healing
.service
import HealingService
34 from osm_policy_module
.common
.common_db_client
import CommonDbClient
35 from osm_policy_module
.common
.message_bus_client
import MessageBusClient
36 from osm_policy_module
.core
.config
import Config
38 log
= logging
.getLogger(__name__
)
40 ALLOWED_KAFKA_KEYS
= [
50 class PolicyModuleAgent
:
51 def __init__(self
, config
: Config
, loop
=None):
54 loop
= asyncio
.get_event_loop()
56 self
.msg_bus
= MessageBusClient(config
)
57 self
.db_client
= CommonDbClient(config
)
58 self
.autoscaling_service
= AutoscalingService(config
, loop
)
59 self
.alarming_service
= AlarmingService(config
, loop
)
60 self
.healing_service
= HealingService(config
, loop
)
63 self
.loop
.run_until_complete(self
.start())
65 async def start(self
):
66 Path("/tmp/osm_pol_agent_health_flag").touch()
67 topics
= ["ns", "alarm_response"]
68 await self
.msg_bus
.aioread(topics
, self
._process
_msg
)
69 log
.critical("Exiting...")
70 if os
.path
.exists("/tmp/osm_pol_agent_health_flag"):
71 os
.remove("/tmp/osm_pol_agent_health_flag")
73 async def _process_msg(self
, topic
, key
, msg
):
74 Path("/tmp/osm_pol_agent_health_flag").touch()
75 log
.debug("_process_msg topic=%s key=%s msg=%s", topic
, key
, msg
)
77 if key
in ALLOWED_KAFKA_KEYS
:
79 if key
== "instantiated":
80 await self
._handle
_instantiated
(msg
)
83 await self
._handle
_scaled
(msg
)
85 if key
== "terminated":
86 await self
._handle
_terminated
(msg
)
88 if key
== "notify_alarm":
89 await self
._handle
_alarm
_notification
(msg
)
91 if key
== "policy_updated":
92 await self
._handle
_policy
_update
(msg
)
94 if key
== "vnf_terminated":
95 await self
._handle
_vnf
_terminated
(msg
)
97 log
.debug("Key %s is not in ALLOWED_KAFKA_KEYS", key
)
98 except peewee
.PeeweeException
:
99 log
.exception("Database error consuming message: ")
102 log
.exception("Error consuming message: ")
104 async def _handle_alarm_notification(self
, content
):
105 log
.debug("_handle_alarm_notification: %s", content
)
106 alarm_uuid
= content
["notify_details"]["alarm_uuid"]
107 status
= content
["notify_details"]["status"]
108 await self
.autoscaling_service
.handle_alarm(alarm_uuid
, status
)
109 await self
.alarming_service
.handle_alarm(alarm_uuid
, status
, content
)
110 await self
.healing_service
.handle_alarm(alarm_uuid
, status
)
112 async def _handle_instantiated(self
, content
):
113 log
.debug("_handle_instantiated: %s", content
)
114 nslcmop_id
= content
["nslcmop_id"]
115 nslcmop
= self
.db_client
.get_nslcmop(nslcmop_id
)
117 nslcmop
["operationState"] == "COMPLETED"
118 or nslcmop
["operationState"] == "PARTIALLY_COMPLETED"
120 nsr_id
= nslcmop
["nsInstanceId"]
121 log
.info("Configuring nsr_id: %s", nsr_id
)
122 await self
.autoscaling_service
.configure_scaling_groups(nsr_id
)
123 await self
.alarming_service
.configure_vnf_alarms(nsr_id
)
124 await self
.healing_service
.configure_healing_alarms(nsr_id
)
127 "Network_service is not in COMPLETED or PARTIALLY_COMPLETED state. "
128 "Current state is %s. Skipping...",
129 nslcmop
["operationState"],
132 async def _handle_scaled(self
, content
):
133 log
.debug("_handle_scaled: %s", content
)
134 nslcmop_id
= content
["nslcmop_id"]
135 nslcmop
= self
.db_client
.get_nslcmop(nslcmop_id
)
137 nslcmop
["operationState"] == "COMPLETED"
138 or nslcmop
["operationState"] == "PARTIALLY_COMPLETED"
140 nsr_id
= nslcmop
["nsInstanceId"]
141 log
.info("Configuring scaled service with nsr_id: %s", nsr_id
)
142 await self
.autoscaling_service
.configure_scaling_groups(nsr_id
)
143 await self
.autoscaling_service
.delete_orphaned_alarms(nsr_id
)
144 await self
.alarming_service
.configure_vnf_alarms(nsr_id
)
145 await self
.healing_service
.configure_healing_alarms(nsr_id
)
146 await self
.healing_service
.delete_orphaned_healing_alarms(nsr_id
)
149 "Network service is not in COMPLETED or PARTIALLY_COMPLETED state. "
150 "Current state is %s. Skipping...",
151 nslcmop
["operationState"],
154 async def _handle_terminated(self
, content
):
155 log
.debug("_handle_deleted: %s", content
)
156 nsr_id
= content
["nsr_id"]
158 content
["operationState"] == "COMPLETED"
159 or content
["operationState"] == "PARTIALLY_COMPLETED"
162 "Deleting scaling groups and alarms for network autoscaling_service with nsr_id: %s",
165 await self
.autoscaling_service
.delete_scaling_groups(nsr_id
)
166 await self
.alarming_service
.delete_vnf_alarms(nsr_id
)
167 await self
.healing_service
.delete_healing_alarms(nsr_id
)
170 "Network service is not in COMPLETED or PARTIALLY_COMPLETED state. "
171 "Current state is %s. Skipping...",
172 content
["operationState"],
175 async def _handle_policy_update(self
, content
):
176 log
.info("_handle_policy_update: %s", content
)
177 nsr_id
= content
["nsr_id"]
178 vnf_member_index
= content
["vnf_member_index"]
180 content
["operationState"] == "COMPLETED"
181 or content
["operationState"] == "PARTIALLY_COMPLETED"
184 "Updating policies of VNF with nsr_id: %s and vnf-member-index: %s"
185 % (nsr_id
, vnf_member_index
)
187 await self
.autoscaling_service
.delete_scaling_groups(
188 nsr_id
, vnf_member_index
190 await self
.alarming_service
.delete_vnf_alarms(nsr_id
, vnf_member_index
)
191 await self
.autoscaling_service
.configure_scaling_groups(
192 nsr_id
, vnf_member_index
194 await self
.alarming_service
.configure_vnf_alarms(nsr_id
, vnf_member_index
)
197 "Network service is not in COMPLETED or PARTIALLY_COMPLETED state. "
198 "Current state is %s. Skipping...",
199 content
["operationState"],
202 async def _handle_vnf_terminated(self
, content
):
203 nsr_id
= content
["nsr_id"]
204 vnf_member_index
= content
["vnf_member_index"]
206 content
["operationState"] == "COMPLETED"
207 or content
["operationState"] == "PARTIALLY_COMPLETED"
210 "Deleting policies of VNF with nsr_id: %s and vnf-member-index: %s"
211 % (nsr_id
, vnf_member_index
)
213 await self
.autoscaling_service
.delete_scaling_groups(
214 nsr_id
, vnf_member_index
216 await self
.alarming_service
.delete_vnf_alarms(nsr_id
, vnf_member_index
)
219 "Network service is not in COMPLETED or PARTIALLY_COMPLETED state. "
220 "Current state is %s. Skipping...",
221 content
["operationState"],