Code Coverage

Cobertura Coverage Report > osm_policy_module.core >

agent.py

Trend

Classes100%
 
Lines51%
   
Conditionals100%
 

File Coverage summary

NameClassesLinesConditionals
agent.py
100%
1/1
51%
40/79
100%
0/0

Coverage Breakdown by Class

NameLinesConditionals
agent.py
51%
40/79
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
27 1 import peewee
28
29 1 from osm_policy_module.alarming.service import AlarmingService
30 1 from osm_policy_module.autoscaling.service import AutoscalingService
31 1 from osm_policy_module.common.common_db_client import CommonDbClient
32 1 from osm_policy_module.common.message_bus_client import MessageBusClient
33 1 from osm_policy_module.core.config import Config
34
35 1 log = logging.getLogger(__name__)
36
37 1 ALLOWED_KAFKA_KEYS = ['instantiated', 'scaled', 'terminated', 'notify_alarm']
38
39
40 1 class PolicyModuleAgent:
41 1     def __init__(self, config: Config, loop=None):
42 1         self.conf = config
43 1         if not loop:
44 0             loop = asyncio.get_event_loop()
45 1         self.loop = loop
46 1         self.msg_bus = MessageBusClient(config)
47 1         self.db_client = CommonDbClient(config)
48 1         self.autoscaling_service = AutoscalingService(config, loop)
49 1         self.alarming_service = AlarmingService(config, loop)
50
51 1     def run(self):
52 0         self.loop.run_until_complete(self.start())
53
54 1     async def start(self):
55 0         topics = [
56             "ns",
57             "alarm_response"
58         ]
59 0         await self.msg_bus.aioread(topics, self._process_msg)
60 0         log.critical("Exiting...")
61
62 1     async def _process_msg(self, topic, key, msg):
63 0         log.debug("_process_msg topic=%s key=%s msg=%s", topic, key, msg)
64 0         try:
65 0             if key in ALLOWED_KAFKA_KEYS:
66
67 0                 if key == 'instantiated':
68 0                     await self._handle_instantiated(msg)
69
70 0                 if key == 'scaled':
71 0                     await self._handle_scaled(msg)
72
73 0                 if key == 'terminated':
74 0                     await self._handle_terminated(msg)
75
76 0                 if key == 'notify_alarm':
77 0                     await self._handle_alarm_notification(msg)
78             else:
79 0                 log.debug("Key %s is not in ALLOWED_KAFKA_KEYS", key)
80 0         except peewee.PeeweeException:
81 0             log.exception("Database error consuming message: ")
82 0             raise
83 0         except Exception:
84 0             log.exception("Error consuming message: ")
85
86 1     async def _handle_alarm_notification(self, content):
87 1         log.debug("_handle_alarm_notification: %s", content)
88 1         alarm_uuid = content['notify_details']['alarm_uuid']
89 1         status = content['notify_details']['status']
90 1         await self.autoscaling_service.handle_alarm(alarm_uuid, status)
91 1         await self.alarming_service.handle_alarm(alarm_uuid, status, content)
92
93 1     async def _handle_instantiated(self, content):
94 1         log.debug("_handle_instantiated: %s", content)
95 1         nslcmop_id = content['nslcmop_id']
96 1         nslcmop = self.db_client.get_nslcmop(nslcmop_id)
97 1         if nslcmop['operationState'] == 'COMPLETED' or nslcmop['operationState'] == 'PARTIALLY_COMPLETED':
98 1             nsr_id = nslcmop['nsInstanceId']
99 1             log.info("Configuring nsr_id: %s", nsr_id)
100 1             await self.autoscaling_service.configure_scaling_groups(nsr_id)
101 1             await self.alarming_service.configure_vnf_alarms(nsr_id)
102         else:
103 1             log.info(
104                 "Network_service is not in COMPLETED or PARTIALLY_COMPLETED state. "
105                 "Current state is %s. Skipping...",
106                 nslcmop['operationState'])
107
108 1     async def _handle_scaled(self, content):
109 0         log.debug("_handle_scaled: %s", content)
110 0         nslcmop_id = content['nslcmop_id']
111 0         nslcmop = self.db_client.get_nslcmop(nslcmop_id)
112 0         if nslcmop['operationState'] == 'COMPLETED' or nslcmop['operationState'] == 'PARTIALLY_COMPLETED':
113 0             nsr_id = nslcmop['nsInstanceId']
114 0             log.info("Configuring scaled service with nsr_id: %s", nsr_id)
115 0             await self.autoscaling_service.configure_scaling_groups(nsr_id)
116 0             await self.autoscaling_service.delete_orphaned_alarms(nsr_id)
117 0             await self.alarming_service.configure_vnf_alarms(nsr_id)
118         else:
119 0             log.debug(
120                 "Network service is not in COMPLETED or PARTIALLY_COMPLETED state. "
121                 "Current state is %s. Skipping...",
122                 nslcmop['operationState'])
123
124 1     async def _handle_terminated(self, content):
125 0         log.debug("_handle_deleted: %s", content)
126 0         nsr_id = content['nsr_id']
127 0         if content['operationState'] == 'COMPLETED' or content['operationState'] == 'PARTIALLY_COMPLETED':
128 0             log.info("Deleting scaling groups and alarms for network autoscaling_service with nsr_id: %s", nsr_id)
129 0             await self.autoscaling_service.delete_scaling_groups(nsr_id)
130 0             await self.alarming_service.delete_vnf_alarms(nsr_id)
131         else:
132 0             log.info(
133                 "Network service is not in COMPLETED or PARTIALLY_COMPLETED state. "
134                 "Current state is %s. Skipping...",
135                 content['operationState'])