Minor bug fixes for common consumer and updated AWS plugin code with access credentials
[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
26 __author__ = "Wajeeha Hamid"
27 __date__ = "18-September-2017"
28
29 import sys
30 import json
31 from connection import Connection
32 from metric_alarms import MetricAlarm
33 from metrics import Metrics
34 sys.path.append("../../core/message_bus")
35 from producer import KafkaProducer
36 import logging as log
37
38 class plugin_metrics():
39 """Receives Alarm info from MetricAlarm and connects with the consumer/producer """
40 def __init__ (self):
41 self.metric = Metrics()
42 self.producer = KafkaProducer('')
43 #---------------------------------------------------------------------------------------------------------------------------
44 def create_metric_request(self,metric_info):
45 '''Comaptible API using normalized parameters'''
46 metric_resp = self.metric.createMetrics(self.cloudwatch_conn,metric_info)
47 return metric_resp
48 #---------------------------------------------------------------------------------------------------------------------------
49 def update_metric_request(self,updated_info):
50 '''Comaptible API using normalized parameters'''
51 update_resp = self.metric.updateMetrics(self.cloudwatch_conn,updated_info)
52 return update_resp
53 #---------------------------------------------------------------------------------------------------------------------------
54 def delete_metric_request(self,delete_info):
55 '''Comaptible API using normalized parameters'''
56 del_resp = self.metric.deleteMetrics(self.cloudwatch_conn,delete_info)
57 return del_resp
58 #---------------------------------------------------------------------------------------------------------------------------
59 def list_metrics_request(self,list_info):
60 '''Comaptible API using normalized parameters'''
61 list_resp = self.metric.listMetrics(self.cloudwatch_conn,list_info)
62 return list_resp
63 #---------------------------------------------------------------------------------------------------------------------------
64 def read_metrics_data(self,list_info):
65 '''Comaptible API using normalized parameters
66 Read all metric data related to a specified metric'''
67 data_resp=self.metric.metricsData(self.cloudwatch_conn,list_info)
68 return data_resp
69 #---------------------------------------------------------------------------------------------------------------------------
70
71 def metric_calls(self,message,aws_conn):
72 '''Consumer will consume the message from SO,
73 1) parse the message and trigger the methods ac
74 cording to keys and topics provided in request.
75
76 2) The response from plugin is saved in json format.
77
78 3) The producer object then calls the producer response
79 methods to send the response back to message bus
80 '''
81
82 try:
83 self.cloudwatch_conn = aws_conn['cloudwatch_connection']
84 self.ec2_conn = aws_conn['ec2_connection']
85
86 metric_info = json.loads(message.value)
87 metric_response = dict()
88
89 if metric_info['vim_type'] == 'AWS':
90 log.debug ("VIM support : AWS")
91
92 # Check the Functionlity that needs to be performed: topic = 'alarms'/'metrics'/'Access_Credentials'
93 if message.topic == "metric_request":
94 log.info("Action required against: %s" % (message.topic))
95
96 if message.key == "create_metric_request":
97 if self.check_resource(metric_info['metric_create']['resource_uuid']) == True:
98 metric_resp = self.create_metric_request(metric_info['metric_create']) #alarm_info = message.value
99 metric_response['schema_version'] = metric_info['schema_version']
100 metric_response['schema_type'] = "create_metric_response"
101 metric_response['metric_create_response'] = metric_resp
102 payload = json.dumps(metric_response)
103 file = open('../../core/models/create_metric_resp.json','wb').write((payload))
104 self.producer.create_metrics_resp(key='create_metric_response',message=payload,topic = 'metric_response')
105
106 log.info("Metric configured: %s", metric_resp)
107 return metric_response
108
109 elif message.key == "update_metric_request":
110 if self.check_resource(metric_info['metric_create']['resource_uuid']) == True:
111 update_resp = self.update_metric_request(metric_info['metric_create'])
112 metric_response['schema_version'] = metric_info['schema_version']
113 metric_response['schema_type'] = "update_metric_response"
114 metric_response['metric_update_response'] = update_resp
115 payload = json.dumps(metric_response)
116 file = open('../../core/models/update_metric_resp.json','wb').write((payload))
117 self.producer.update_metric_response(key='update_metric_response',message=payload,topic = 'metric_response')
118
119 log.info("Metric Updates: %s",metric_response)
120 return metric_response
121
122 elif message.key == "delete_metric_request":
123 if self.check_resource(metric_info['resource_uuid']) == True:
124 del_resp=self.delete_metric_request(metric_info)
125 payload = json.dumps(del_resp)
126 file = open('../../core/models/delete_metric_resp.json','wb').write((payload))
127 self.producer.delete_metric_response(key='delete_metric_response',message=payload,topic = 'metric_response')
128
129 log.info("Metric Deletion Not supported in AWS : %s",del_resp)
130 return del_resp
131
132 elif message.key == "list_metric_request":
133 if self.check_resource(metric_info['metrics_list_request']['resource_uuid']) == True:
134 list_resp = self.list_metrics_request(metric_info['metrics_list_request'])
135 metric_response['schema_version'] = metric_info['schema_version']
136 metric_response['schema_type'] = "list_metric_response"
137 metric_response['correlation_id'] = metric_info['metrics_list_request']['correlation_id']
138 metric_response['vim_type'] = metric_info['vim_type']
139 metric_response['metrics_list'] = list_resp
140 payload = json.dumps(metric_response)
141 file = open('../../core/models/list_metric_resp.json','wb').write((payload))
142 self.producer.list_metric_response(key='list_metrics_response',message=payload,topic = 'metric_response')
143
144 log.info("Metric List: %s",metric_response)
145 return metric_response
146
147 elif message.key == "read_metric_data_request":
148 if self.check_resource(metric_info['resource_uuid']) == True:
149 data_resp = self.read_metrics_data(metric_info)
150 metric_response['schema_version'] = metric_info['schema_version']
151 metric_response['schema_type'] = "read_metric_data_response"
152 metric_response['metric_name'] = metric_info['metric_name']
153 metric_response['metric_uuid'] = metric_info['metric_uuid']
154 metric_response['correlation_id'] = metric_info['correlation_uuid']
155 metric_response['resource_uuid'] = metric_info['resource_uuid']
156 metric_response['tenant_uuid'] = metric_info['tenant_uuid']
157 metric_response['metrics_data'] = data_resp
158 payload = json.dumps(metric_response)
159 file = open('../../core/models/read_metric_data_resp.json','wb').write((payload))
160 self.producer.read_metric_data_response(key='read_metric_data_response',message=payload,topic = 'metric_response')
161
162 log.info("Metric Data Response: %s",metric_response)
163 return metric_response
164
165 else:
166 log.debug("Unknown key, no action will be performed")
167 else:
168 log.info("Message topic not relevant to this plugin: %s",
169 message.topic)
170
171 except Exception as e:
172 log.error("Consumer exception: %s", str(e))
173
174 #---------------------------------------------------------------------------------------------------------------------------
175 def check_resource(self,resource_uuid):
176
177 '''Checking the resource_uuid is present in EC2 instances'''
178 try:
179 check_resp = dict()
180 instances = self.ec2_conn.get_all_instance_status()
181 status_resource = False
182
183 #resource_id
184 for instance_id in instances:
185 instance_id = str(instance_id).split(':')[1]
186 if instance_id == resource_uuid:
187 check_resp['resource_uuid'] = resource_uuid
188 status_resource = True
189 else:
190 status_resource = False
191
192 #status
193 return status_resource
194
195 except Exception as e:
196 log.error("Error in Plugin Inputs %s",str(e))
197 #---------------------------------------------------------------------------------------------------------------------------
198