777723bfd25f57c6a5ad7d2d99f6e9fc2fb798b9
[osm/POL.git] / 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 asyncio
25 import logging
26 from pathlib import Path
27 import os
28
29 import peewee
30
31 from osm_policy_module.alarming.service import AlarmingService
32 from osm_policy_module.autoscaling.service import AutoscalingService
33 from osm_policy_module.common.common_db_client import CommonDbClient
34 from osm_policy_module.common.message_bus_client import MessageBusClient
35 from osm_policy_module.core.config import Config
36
37 log = logging.getLogger(__name__)
38
39 ALLOWED_KAFKA_KEYS = ["instantiated", "scaled", "terminated", "notify_alarm", "policy_updated", "vnf_terminated"]
40
41
42 class PolicyModuleAgent:
43 def __init__(self, config: Config, loop=None):
44 self.conf = config
45 if not loop:
46 loop = asyncio.get_event_loop()
47 self.loop = loop
48 self.msg_bus = MessageBusClient(config)
49 self.db_client = CommonDbClient(config)
50 self.autoscaling_service = AutoscalingService(config, loop)
51 self.alarming_service = AlarmingService(config, loop)
52
53 def run(self):
54 self.loop.run_until_complete(self.start())
55
56 async def start(self):
57 Path("/tmp/osm_pol_agent_health_flag").touch()
58 topics = ["ns", "alarm_response"]
59 await self.msg_bus.aioread(topics, self._process_msg)
60 log.critical("Exiting...")
61 if os.path.exists("/tmp/osm_pol_agent_health_flag"):
62 os.remove("/tmp/osm_pol_agent_health_flag")
63
64 async def _process_msg(self, topic, key, msg):
65 Path("/tmp/osm_pol_agent_health_flag").touch()
66 log.debug("_process_msg topic=%s key=%s msg=%s", topic, key, msg)
67 try:
68 if key in ALLOWED_KAFKA_KEYS:
69
70 if key == "instantiated":
71 await self._handle_instantiated(msg)
72
73 if key == "scaled":
74 await self._handle_scaled(msg)
75
76 if key == "terminated":
77 await self._handle_terminated(msg)
78
79 if key == "notify_alarm":
80 await self._handle_alarm_notification(msg)
81
82 if key == "policy_updated":
83 await self._handle_policy_update(msg)
84
85 if key == "vnf_terminated":
86 await self._handle_vnf_terminated(msg)
87 else:
88 log.debug("Key %s is not in ALLOWED_KAFKA_KEYS", key)
89 except peewee.PeeweeException:
90 log.exception("Database error consuming message: ")
91 raise
92 except Exception:
93 log.exception("Error consuming message: ")
94
95 async def _handle_alarm_notification(self, content):
96 log.debug("_handle_alarm_notification: %s", content)
97 alarm_uuid = content["notify_details"]["alarm_uuid"]
98 status = content["notify_details"]["status"]
99 await self.autoscaling_service.handle_alarm(alarm_uuid, status)
100 await self.alarming_service.handle_alarm(alarm_uuid, status, content)
101
102 async def _handle_instantiated(self, content):
103 log.debug("_handle_instantiated: %s", content)
104 nslcmop_id = content["nslcmop_id"]
105 nslcmop = self.db_client.get_nslcmop(nslcmop_id)
106 if (
107 nslcmop["operationState"] == "COMPLETED"
108 or nslcmop["operationState"] == "PARTIALLY_COMPLETED"
109 ):
110 nsr_id = nslcmop["nsInstanceId"]
111 log.info("Configuring nsr_id: %s", nsr_id)
112 await self.autoscaling_service.configure_scaling_groups(nsr_id)
113 await self.alarming_service.configure_vnf_alarms(nsr_id)
114 else:
115 log.info(
116 "Network_service is not in COMPLETED or PARTIALLY_COMPLETED state. "
117 "Current state is %s. Skipping...",
118 nslcmop["operationState"],
119 )
120
121 async def _handle_scaled(self, content):
122 log.debug("_handle_scaled: %s", content)
123 nslcmop_id = content["nslcmop_id"]
124 nslcmop = self.db_client.get_nslcmop(nslcmop_id)
125 if (
126 nslcmop["operationState"] == "COMPLETED"
127 or nslcmop["operationState"] == "PARTIALLY_COMPLETED"
128 ):
129 nsr_id = nslcmop["nsInstanceId"]
130 log.info("Configuring scaled service with nsr_id: %s", nsr_id)
131 await self.autoscaling_service.configure_scaling_groups(nsr_id)
132 await self.autoscaling_service.delete_orphaned_alarms(nsr_id)
133 await self.alarming_service.configure_vnf_alarms(nsr_id)
134 else:
135 log.debug(
136 "Network service is not in COMPLETED or PARTIALLY_COMPLETED state. "
137 "Current state is %s. Skipping...",
138 nslcmop["operationState"],
139 )
140
141 async def _handle_terminated(self, content):
142 log.debug("_handle_deleted: %s", content)
143 nsr_id = content["nsr_id"]
144 if (
145 content["operationState"] == "COMPLETED"
146 or content["operationState"] == "PARTIALLY_COMPLETED"
147 ):
148 log.info(
149 "Deleting scaling groups and alarms for network autoscaling_service with nsr_id: %s",
150 nsr_id,
151 )
152 await self.autoscaling_service.delete_scaling_groups(nsr_id)
153 await self.alarming_service.delete_vnf_alarms(nsr_id)
154 else:
155 log.info(
156 "Network service is not in COMPLETED or PARTIALLY_COMPLETED state. "
157 "Current state is %s. Skipping...",
158 content["operationState"],
159 )
160
161 async def _handle_policy_update(self, content):
162 log.info("_handle_policy_update: %s", content)
163 nsr_id = content['nsr_id']
164 vnf_member_index = content['vnf_member_index']
165 if (
166 content["operationState"] == "COMPLETED"
167 or content["operationState"] == "PARTIALLY_COMPLETED"
168 ):
169 log.info(
170 "Updating policies of VNF with nsr_id: %s and vnf-member-index: %s"
171 % (nsr_id, vnf_member_index))
172 await self.autoscaling_service.delete_scaling_groups(nsr_id, vnf_member_index)
173 await self.alarming_service.delete_vnf_alarms(nsr_id, vnf_member_index)
174 await self.autoscaling_service.configure_scaling_groups(nsr_id, vnf_member_index)
175 await self.alarming_service.configure_vnf_alarms(nsr_id, vnf_member_index)
176 else:
177 log.info(
178 "Network service is not in COMPLETED or PARTIALLY_COMPLETED state. "
179 "Current state is %s. Skipping...",
180 content['operationState'])
181
182 async def _handle_vnf_terminated(self, content):
183 nsr_id = content['nsr_id']
184 vnf_member_index = content['vnf_member_index']
185 if (
186 content["operationState"] == "COMPLETED"
187 or content["operationState"] == "PARTIALLY_COMPLETED"
188 ):
189 log.info(
190 "Deleting policies of VNF with nsr_id: %s and vnf-member-index: %s"
191 % (nsr_id, vnf_member_index))
192 await self.autoscaling_service.delete_scaling_groups(nsr_id, vnf_member_index)
193 await self.alarming_service.delete_vnf_alarms(nsr_id, vnf_member_index)
194 else:
195 log.info(
196 "Network service is not in COMPLETED or PARTIALLY_COMPLETED state. "
197 "Current state is %s. Skipping...",
198 content['operationState'])