Refactors codebase
[osm/MON.git] / osm_mon / plugins / CloudWatch / plugin_metric.py
1 ##
2 # Copyright 2017 xFlow Research Pvt. Ltd
3 # This file is part of MON module
4 # All Rights Reserved.
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License"); you may
7 # not use this file except in compliance with the License. You may obtain
8 # a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15 # License for the specific language governing permissions and limitations
16 # under the License.
17 #
18 # For those usages not covered by the Apache License, Version 2.0 please
19 # contact with: wajeeha.hamid@xflowresearch.com
20 ##
21
22 """
23 AWS-Plugin implements all the methods of MON to interact with AWS using the BOTO client
24 """
25 from io import UnsupportedOperation
26
27 from osm_mon.core.settings import Config
28 from osm_mon.plugins.CloudWatch.metrics import Metrics
29
30 __author__ = "Wajeeha Hamid"
31 __date__ = "18-September-2017"
32
33 import logging
34
35 log = logging.getLogger(__name__)
36
37
38 class plugin_metrics:
39 """Receives Alarm info from MetricAlarm and connects with the consumer/producer """
40
41 def __init__(self):
42 self._cfg = Config.instance()
43 self.metric = Metrics()
44
45 def create_metric_request(self, metric_info):
46 """Compatible API using normalized parameters"""
47 metric_resp = self.metric.createMetrics(self.cloudwatch_conn, metric_info)
48 return metric_resp
49
50 def update_metric_request(self, updated_info):
51 """Compatible API using normalized parameters"""
52 update_resp = self.metric.updateMetrics(self.cloudwatch_conn, updated_info)
53 return update_resp
54
55 def delete_metric_request(self, delete_info):
56 """Compatible API using normalized parameters"""
57 del_resp = self.metric.deleteMetrics(self.cloudwatch_conn, delete_info)
58 return del_resp
59
60 def list_metrics_request(self, list_info):
61 """Compatible API using normalized parameters"""
62 list_resp = self.metric.listMetrics(self.cloudwatch_conn, list_info)
63 return list_resp
64
65 def read_metrics_data(self, list_info):
66 """Compatible API using normalized parameters
67 Read all metric data related to a specified metric"""
68 data_resp = self.metric.metricsData(self.cloudwatch_conn, list_info)
69 return data_resp
70
71 def metric_calls(self, key: str, metric_info: dict, aws_conn: dict):
72 """Gets the message from the common consumer"""
73
74 try:
75 self.cloudwatch_conn = aws_conn['cloudwatch_connection']
76 self.ec2_conn = aws_conn['ec2_connection']
77
78 metric_response = dict()
79
80 log.debug("VIM support : AWS")
81
82 if key == "create_metric_request":
83 if self.check_resource(metric_info['metric_create_request']['resource_uuid']):
84 metric_resp = self.create_metric_request(
85 metric_info['metric_create_request']) # alarm_info = message.value
86 metric_response['schema_version'] = metric_info['schema_version']
87 metric_response['schema_type'] = "create_metric_response"
88 metric_response['metric_create_response'] = metric_resp
89 log.info("Metric configured: %s", metric_resp)
90 return metric_response
91
92 elif key == "update_metric_request":
93 if self.check_resource(metric_info['metric_create_request']['resource_uuid']):
94 update_resp = self.update_metric_request(metric_info['metric_create_request'])
95 metric_response['schema_version'] = metric_info['schema_version']
96 metric_response['schema_type'] = "update_metric_response"
97 metric_response['metric_update_response'] = update_resp
98 log.info("Metric Updates: %s", metric_response)
99 return metric_response
100
101 elif key == "delete_metric_request":
102 if self.check_resource(metric_info['resource_uuid']):
103 del_resp = self.delete_metric_request(metric_info)
104 log.info("Metric Deletion Not supported in AWS : %s", del_resp)
105 return del_resp
106
107 elif key == "list_metric_request":
108 if self.check_resource(metric_info['metrics_list_request']['resource_uuid']):
109 list_resp = self.list_metrics_request(metric_info['metrics_list_request'])
110 metric_response['schema_version'] = metric_info['schema_version']
111 metric_response['schema_type'] = "list_metric_response"
112 metric_response['correlation_id'] = metric_info['metrics_list_request']['correlation_id']
113 metric_response['vim_type'] = metric_info['vim_type']
114 metric_response['metrics_list'] = list_resp
115 log.info("Metric List: %s", metric_response)
116 return metric_response
117
118 elif key == "read_metric_data_request":
119 if self.check_resource(metric_info['resource_uuid']):
120 data_resp = self.read_metrics_data(metric_info)
121 metric_response['schema_version'] = metric_info['schema_version']
122 metric_response['schema_type'] = "read_metric_data_response"
123 metric_response['metric_name'] = metric_info['metric_name']
124 metric_response['metric_uuid'] = metric_info['metric_uuid']
125 metric_response['correlation_id'] = metric_info['correlation_uuid']
126 metric_response['resource_uuid'] = metric_info['resource_uuid']
127 metric_response['tenant_uuid'] = metric_info['tenant_uuid']
128 metric_response['metrics_data'] = data_resp
129 log.info("Metric Data Response: %s", metric_response)
130 return metric_response
131
132 else:
133 raise UnsupportedOperation("Unknown key, no action will be performed")
134
135 except Exception as e:
136 log.error("Consumer exception: %s", str(e))
137
138 def check_resource(self, resource_uuid):
139
140 """Checking the resource_uuid is present in EC2 instances"""
141 try:
142 check_resp = dict()
143 instances = self.ec2_conn.get_all_instance_status()
144 status_resource = False
145
146 # resource_id
147 for instance_id in instances:
148 instance_id = str(instance_id).split(':')[1]
149 if instance_id == resource_uuid:
150 check_resp['resource_uuid'] = resource_uuid
151 status_resource = True
152 else:
153 status_resource = False
154
155 # status
156 return status_resource
157
158 except Exception as e:
159 log.error("Error in Plugin Inputs %s", str(e))