1 # Copyright 2017 Intel Research and Development Ireland Limited
2 # *************************************************************
4 # This file is part of OSM Monitoring module
5 # All Rights Reserved to Intel Corporation
7 # Licensed under the Apache License, Version 2.0 (the "License"); you may
8 # not use this file except in compliance with the License. You may obtain
9 # a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16 # License for the specific language governing permissions and limitations
19 # For those usages not covered by the Apache License, Version 2.0 please
20 # contact: helena.mcgough@intel.com or adrian.hoban@intel.com
22 """Generate valid responses to send back to the SO."""
26 log
= logging
.getLogger(__name__
)
28 __author__
= "Helena McGough"
30 schema_version
= "1.0"
33 class OpenStack_Response(object):
34 """Generates responses for SO from OpenStaack plugins."""
37 """Initialize OpenStack Response instance."""
39 def generate_response(self
, key
, **kwargs
):
40 """Make call to appropriate response function."""
41 if key
== "list_alarm_response":
42 message
= self
.alarm_list_response(**kwargs
)
43 elif key
== "create_alarm_response":
44 message
= self
.create_alarm_response(**kwargs
)
45 elif key
== "delete_alarm_response":
46 message
= self
.delete_alarm_response(**kwargs
)
47 elif key
== "update_alarm_response":
48 message
= self
.update_alarm_response(**kwargs
)
49 elif key
== "create_metric_response":
50 message
= self
.metric_create_response(**kwargs
)
51 elif key
== "read_metric_data_response":
52 message
= self
.read_metric_data_response(**kwargs
)
53 elif key
== "delete_metric_response":
54 message
= self
.delete_metric_response(**kwargs
)
55 elif key
== "update_metric_response":
56 message
= self
.update_metric_response(**kwargs
)
57 elif key
== "list_metric_response":
58 message
= self
.list_metric_response(**kwargs
)
59 elif key
== "notify_alarm":
60 message
= self
.notify_alarm(**kwargs
)
62 log
.warn("Failed to generate a valid response message.")
66 def alarm_list_response(self
, **kwargs
):
67 """Generate the response for an alarm list request."""
68 alarm_list_resp
= {"schema_version": schema_version
,
69 "schema_type": "list_alarm_response",
70 "correlation_id": kwargs
['cor_id'],
71 "list_alarm_resp": kwargs
['alarm_list']}
72 return json
.dumps(alarm_list_resp
)
74 def create_alarm_response(self
, **kwargs
):
75 """Generate a response for a create alarm request."""
76 create_alarm_resp
= {"schema_version": schema_version
,
77 "schema_type": "create_alarm_response",
78 "alarm_create_response": {
79 "correlation_id": kwargs
['cor_id'],
80 "alarm_uuid": kwargs
['alarm_id'],
81 "status": kwargs
['status']}}
82 return json
.dumps(create_alarm_resp
)
84 def delete_alarm_response(self
, **kwargs
):
85 """Generate a response for a delete alarm request."""
86 delete_alarm_resp
= {"schema_version": schema_version
,
87 "schema_type": "alarm_deletion_response",
88 "alarm_deletion_response": {
89 "correlation_id": kwargs
['cor_id'],
90 "alarm_uuid": kwargs
['alarm_id'],
91 "status": kwargs
['status']}}
92 return json
.dumps(delete_alarm_resp
)
94 def update_alarm_response(self
, **kwargs
):
95 """Generate a response for an update alarm request."""
96 update_alarm_resp
= {"schema_version": schema_version
,
97 "schema_type": "update_alarm_response",
98 "alarm_update_response": {
99 "correlation_id": kwargs
['cor_id'],
100 "alarm_uuid": kwargs
['alarm_id'],
101 "status": kwargs
['status']}}
102 return json
.dumps(update_alarm_resp
)
104 def metric_create_response(self
, **kwargs
):
105 """Generate a response for a create metric request."""
106 create_metric_resp
= {"schema_version": schema_version
,
107 "schema_type": "create_metric_response",
108 "correlation_id": kwargs
['cor_id'],
109 "metric_create_response": {
110 "metric_uuid": kwargs
['metric_id'],
111 "resource_uuid": kwargs
['r_id'],
112 "status": kwargs
['status']}}
113 return json
.dumps(create_metric_resp
)
115 def read_metric_data_response(self
, **kwargs
):
116 """Generate a response for a read metric data request."""
117 read_metric_data_resp
= {"schema_version": schema_version
,
118 "schema_type": "read_metric_data_response",
119 "metric_name": kwargs
['m_name'],
120 "metric_uuid": kwargs
['m_id'],
121 "resource_uuid": kwargs
['r_id'],
122 "correlation_id": kwargs
['cor_id'],
124 "time_series": kwargs
['times'],
125 "metrics_series": kwargs
['metrics']}}
126 return json
.dumps(read_metric_data_resp
)
128 def delete_metric_response(self
, **kwargs
):
129 """Generate a response for a delete metric request."""
130 delete_metric_resp
= {"schema_version": schema_version
,
131 "schema_type": "delete_metric_response",
132 "metric_name": kwargs
['m_name'],
133 "metric_uuid": kwargs
['m_id'],
134 "resource_uuid": kwargs
['r_id'],
135 "correlation_id": kwargs
['cor_id'],
136 "status": kwargs
['status']}
137 return json
.dumps(delete_metric_resp
)
139 def update_metric_response(self
, **kwargs
):
140 """Generate a repsonse for an update metric request."""
141 update_metric_resp
= {"schema_version": schema_version
,
142 "schema_type": "update_metric_response",
143 "correlation_id": kwargs
['cor_id'],
144 "metric_update_response": {
145 "metric_uuid": kwargs
['m_id'],
146 "status": kwargs
['status'],
147 "resource_uuid": kwargs
['r_id']}}
148 return json
.dumps(update_metric_resp
)
150 def list_metric_response(self
, **kwargs
):
151 """Generate a response for a list metric request."""
152 list_metric_resp
= {"schema_version": schema_version
,
153 "schema_type": "list_metric_response",
154 "correlation_id": kwargs
['cor_id'],
155 "metrics_list": kwargs
['m_list']}
156 return json
.dumps(list_metric_resp
)
158 def notify_alarm(self
, **kwargs
):
159 """Generate a response to send alarm notifications."""
160 notify_alarm_resp
= {"schema_version": schema_version
,
161 "schema_type": "notify_alarm",
163 "alarm_uuid": kwargs
['a_id'],
164 "resource_uuid": kwargs
['r_id'],
165 "vim_type": kwargs
['vim_type'],
166 "severity": kwargs
['sev'],
167 "status": kwargs
['state'],
168 "start_date": kwargs
['date']}}
169 return json
.dumps(notify_alarm_resp
)