Code Coverage

Cobertura Coverage Report > osm_policy_module.core >

agent.py

Trend

Classes100%
 
Lines57%
   
Conditionals100%
 

File Coverage summary

NameClassesLinesConditionals
agent.py
100%
1/1
57%
65/115
100%
0/0

Coverage Breakdown by Class

NameLinesConditionals
agent.py
57%
65/115
N/A

Source

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