74b8b5fc8b29819d5c0016af700396af6754ee43
[pti/o2.git] / o2ims / views / alarm_route.py
1 # Copyright (C) 2021 Wind River Systems, Inc.
2 #
3 #  Licensed under the Apache License, Version 2.0 (the "License");
4 #  you may not use this file except in compliance with the License.
5 #  You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 #  Unless required by applicable law or agreed to in writing, software
10 #  distributed under the License is distributed on an "AS IS" BASIS,
11 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 #  See the License for the specific language governing permissions and
13 #  limitations under the License.
14
15 from flask import request
16 from flask_restx import Resource, reqparse
17
18 from o2common.service.messagebus import MessageBus
19 from o2common.views.pagination_route import link_header, PAGE_PARAM
20 from o2ims.views import alarm_view
21 from o2ims.views.api_ns import api_ims_monitoring as api_monitoring_v1
22 from o2ims.views.alarm_dto import AlarmDTO, SubscriptionDTO
23
24 from o2common.helper import o2logging
25 logger = o2logging.get_logger(__name__)
26
27
28 def configure_api_route():
29     # Set global bus for resource
30     global bus
31     bus = MessageBus.get_instance()
32
33
34 # ----------  API versions ---------- #
35 @api_monitoring_v1.route("/v1/api_versions")
36 class VersionRouter(Resource):
37     def get(self):
38         return {
39             'uriPrefix': request.base_url.rsplit('/', 1)[0],
40             'apiVersions': [{
41                 'version': '1.0.0',
42                 # 'isDeprecated': 'False',
43                 # 'retirementDate': ''
44             }]
45         }
46
47
48 # ----------  Alarm Event Record ---------- #
49 @api_monitoring_v1.route("/v1/alarms")
50 @api_monitoring_v1.param(PAGE_PARAM,
51                          'Page number of the results to fetch.' +
52                          ' Default: 1',
53                          _in='query', default=1)
54 @api_monitoring_v1.param(
55     'all_fields',
56     'Set any value for show all fields. This value will cover "fields" ' +
57     'and "all_fields".',
58     _in='query')
59 @api_monitoring_v1.param(
60     'fields',
61     'Set fields to show, split by comma, "/" for parent and children.' +
62     ' Like "name,parent/children". This value will cover' +
63     ' "exculde_fields".',
64     _in='query')
65 @api_monitoring_v1.param(
66     'exclude_fields',
67     'Set fields to exclude showing, split by comma, "/" for parent and ' +
68     'children. Like "name,parent/children". This value will cover ' +
69     '"exclude_default".',
70     _in='query')
71 @api_monitoring_v1.param(
72     'exclude_default',
73     'Exclude showing all default fields, Set "true" to enable.',
74     _in='query')
75 @api_monitoring_v1.param(
76     'filter',
77     'Filter of the query.',
78     _in='query')
79 class AlarmListRouter(Resource):
80
81     model = AlarmDTO.alarm_event_record_get
82
83     @api_monitoring_v1.marshal_list_with(model)
84     def get(self):
85         parser = reqparse.RequestParser()
86         parser.add_argument(PAGE_PARAM, location='args')
87         parser.add_argument('filter', location='args')
88         args = parser.parse_args()
89         kwargs = {}
90         if args.nextpage_opaque_marker is not None:
91             kwargs['page'] = args.nextpage_opaque_marker
92         kwargs['filter'] = args.filter if args.filter is not None else ''
93
94         ret = alarm_view.alarm_event_records(bus.uow, **kwargs)
95         return link_header(request.full_path, ret)
96
97
98 @api_monitoring_v1.route("/v1/alarms/<alarmEventRecordId>")
99 @api_monitoring_v1.param('alarmEventRecordId', 'ID of the alarm event record')
100 @api_monitoring_v1.response(404, 'Alarm Event Record not found')
101 @api_monitoring_v1.param(
102     'all_fields',
103     'Set any value for show all fields. This value will cover "fields" ' +
104     'and "all_fields".',
105     _in='query')
106 @api_monitoring_v1.param(
107     'fields',
108     'Set fields to show, split by comma, "/" for parent and children.' +
109     ' Like "name,parent/children". This value will cover' +
110     ' "exculde_fields".',
111     _in='query')
112 @api_monitoring_v1.param(
113     'exclude_fields',
114     'Set fields to exclude showing, split by comma, "/" for parent and ' +
115     'children. Like "name,parent/children". This value will cover ' +
116     '"exclude_default".',
117     _in='query')
118 @api_monitoring_v1.param(
119     'exclude_default',
120     'Exclude showing all default fields, Set "true" to enable.',
121     _in='query')
122 class AlarmGetRouter(Resource):
123
124     model = AlarmDTO.alarm_event_record_get
125
126     @api_monitoring_v1.doc('Get resource type')
127     @api_monitoring_v1.marshal_with(model)
128     def get(self, alarmEventRecordId):
129         result = alarm_view.alarm_event_record_one(alarmEventRecordId, bus.uow)
130         if result is not None:
131             return result
132         api_monitoring_v1.abort(
133             404, "Resource type {} doesn't exist".format(alarmEventRecordId))
134
135
136 # ----------  Alarm Subscriptions ---------- #
137 @api_monitoring_v1.route("/v1/alarmSubscriptions")
138 class SubscriptionsListRouter(Resource):
139
140     model = SubscriptionDTO.subscription_get
141     expect = SubscriptionDTO.subscription
142     post_resp = SubscriptionDTO.subscription_post_resp
143
144     @api_monitoring_v1.doc('List alarm subscriptions')
145     @api_monitoring_v1.marshal_list_with(model)
146     @api_monitoring_v1.param(
147         PAGE_PARAM,
148         'Page number of the results to fetch. Default: 1',
149         _in='query', default=1)
150     @api_monitoring_v1.param(
151         'all_fields',
152         'Set any value for show all fields. This value will cover "fields" ' +
153         'and "all_fields".',
154         _in='query')
155     @api_monitoring_v1.param(
156         'fields',
157         'Set fields to show, split by comma, "/" for parent and children.' +
158         ' Like "name,parent/children". This value will cover' +
159         ' "exculde_fields".',
160         _in='query')
161     @api_monitoring_v1.param(
162         'exclude_fields',
163         'Set fields to exclude showing, split by comma, "/" for parent and ' +
164         'children. Like "name,parent/children". This value will cover ' +
165         '"exclude_default".',
166         _in='query')
167     @api_monitoring_v1.param(
168         'exclude_default',
169         'Exclude showing all default fields, Set "true" to enable.',
170         _in='query')
171     @api_monitoring_v1.param(
172         'filter',
173         'Filter of the query.',
174         _in='query')
175     def get(self):
176         parser = reqparse.RequestParser()
177         parser.add_argument(PAGE_PARAM, location='args')
178         parser.add_argument('filter', location='args')
179         args = parser.parse_args()
180         kwargs = {}
181         if args.nextpage_opaque_marker is not None:
182             kwargs['page'] = args.nextpage_opaque_marker
183         kwargs['filter'] = args.filter if args.filter is not None else ''
184
185         ret = alarm_view.subscriptions(bus.uow, **kwargs)
186         return link_header(request.full_path, ret)
187
188     @api_monitoring_v1.doc('Create a alarm subscription')
189     @api_monitoring_v1.expect(expect)
190     @api_monitoring_v1.marshal_with(post_resp, code=201)
191     def post(self):
192         data = api_monitoring_v1.payload
193         result = alarm_view.subscription_create(data, bus.uow)
194         return result, 201
195
196
197 @api_monitoring_v1.route("/v1/alarmSubscriptions/<alarmSubscriptionID>")
198 @api_monitoring_v1.param('alarmSubscriptionID', 'ID of the Alarm Subscription')
199 @api_monitoring_v1.response(404, 'Alarm Subscription not found')
200 class SubscriptionGetDelRouter(Resource):
201
202     model = SubscriptionDTO.subscription_get
203
204     @api_monitoring_v1.doc('Get Alarm Subscription by ID')
205     @api_monitoring_v1.marshal_with(model)
206     @api_monitoring_v1.param(
207         'all_fields',
208         'Set any value for show all fields. This value will cover "fields" ' +
209         'and "all_fields".',
210         _in='query')
211     @api_monitoring_v1.param(
212         'fields',
213         'Set fields to show, split by comma, "/" for parent and children.' +
214         ' Like "name,parent/children". This value will cover' +
215         ' "exculde_fields".',
216         _in='query')
217     @api_monitoring_v1.param(
218         'exclude_fields',
219         'Set fields to exclude showing, split by comma, "/" for parent and ' +
220         'children. Like "name,parent/children". This value will cover ' +
221         '"exclude_default".',
222         _in='query')
223     @api_monitoring_v1.param(
224         'exclude_default',
225         'Exclude showing all default fields, Set "true" to enable.',
226         _in='query')
227     def get(self, alarmSubscriptionID):
228         result = alarm_view.subscription_one(
229             alarmSubscriptionID, bus.uow)
230         if result is not None:
231             return result
232         api_monitoring_v1.abort(404, "Subscription {} doesn't exist".format(
233             alarmSubscriptionID))
234
235     @api_monitoring_v1.doc('Delete subscription by ID')
236     @api_monitoring_v1.response(204, 'Subscription deleted')
237     def delete(self, alarmSubscriptionID):
238         result = alarm_view.subscription_delete(alarmSubscriptionID, bus.uow)
239         return result, 204