Docs: Update all API docs with example
[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 o2common.views.route_exception import NotFoundException, \
21     BadRequestException
22 from o2ims.views import alarm_view
23 from o2ims.views.api_ns import api_ims_monitoring as api_monitoring_v1
24 from o2ims.views.alarm_dto import AlarmDTO, SubscriptionDTO, \
25     MonitoringApiV1DTO
26
27 from o2common.helper import o2logging
28 logger = o2logging.get_logger(__name__)
29
30
31 def configure_api_route():
32     # Set global bus for resource
33     global bus
34     bus = MessageBus.get_instance()
35
36
37 # ----------  API versions ---------- #
38 @api_monitoring_v1.route("/v1/api_versions")
39 class VersionRouter(Resource):
40     model = MonitoringApiV1DTO.api_version_info_get
41
42     @api_monitoring_v1.doc('Get Monitoring API version')
43     @api_monitoring_v1.marshal_list_with(model)
44     def get(self):
45         return {
46             'uriPrefix': request.base_url.rsplit('/', 1)[0],
47             'apiVersions': [{
48                 'version': '1.0.0',
49                 # 'isDeprecated': 'False',
50                 # 'retirementDate': ''
51             }]
52         }
53
54
55 # ----------  Alarm Event Record ---------- #
56 @api_monitoring_v1.route("/v1/alarms")
57 @api_monitoring_v1.param(PAGE_PARAM,
58                          'Page number of the results to fetch.' +
59                          ' Default: 1',
60                          _in='query', default=1)
61 @api_monitoring_v1.param(
62     'all_fields',
63     'Set any value for show all fields. This value will cover "fields" ' +
64     'and "all_fields".',
65     _in='query')
66 @api_monitoring_v1.param(
67     'fields',
68     'Set fields to show, split by comma, "/" for parent and children.' +
69     ' Like "name,parent/children". This value will cover' +
70     ' "exculde_fields".',
71     _in='query')
72 @api_monitoring_v1.param(
73     'exclude_fields',
74     'Set fields to exclude showing, split by comma, "/" for parent and ' +
75     'children. Like "name,parent/children". This value will cover ' +
76     '"exclude_default".',
77     _in='query')
78 @api_monitoring_v1.param(
79     'exclude_default',
80     'Exclude showing all default fields, Set "true" to enable.',
81     _in='query')
82 @api_monitoring_v1.param(
83     'filter',
84     'Filter of the query.',
85     _in='query')
86 class AlarmListRouter(Resource):
87
88     model = AlarmDTO.alarm_event_record_get
89
90     @api_monitoring_v1.doc('Get Alarm Event Record List')
91     @api_monitoring_v1.marshal_list_with(model)
92     def get(self):
93         parser = reqparse.RequestParser()
94         parser.add_argument(PAGE_PARAM, location='args')
95         parser.add_argument('filter', location='args')
96         args = parser.parse_args()
97         kwargs = {}
98         if args.nextpage_opaque_marker is not None:
99             kwargs['page'] = args.nextpage_opaque_marker
100         kwargs['filter'] = args.filter if args.filter is not None else ''
101
102         ret = alarm_view.alarm_event_records(bus.uow, **kwargs)
103         return link_header(request.full_path, ret)
104
105
106 @api_monitoring_v1.route("/v1/alarms/<alarmEventRecordId>")
107 @api_monitoring_v1.param('alarmEventRecordId', 'ID of the alarm event record')
108 @api_monitoring_v1.response(404, 'Alarm Event Record not found')
109 @api_monitoring_v1.param(
110     'all_fields',
111     'Set any value for show all fields. This value will cover "fields" ' +
112     'and "all_fields".',
113     _in='query')
114 @api_monitoring_v1.param(
115     'fields',
116     'Set fields to show, split by comma, "/" for parent and children.' +
117     ' Like "name,parent/children". This value will cover' +
118     ' "exculde_fields".',
119     _in='query')
120 @api_monitoring_v1.param(
121     'exclude_fields',
122     'Set fields to exclude showing, split by comma, "/" for parent and ' +
123     'children. Like "name,parent/children". This value will cover ' +
124     '"exclude_default".',
125     _in='query')
126 @api_monitoring_v1.param(
127     'exclude_default',
128     'Exclude showing all default fields, Set "true" to enable.',
129     _in='query')
130 class AlarmGetRouter(Resource):
131
132     model = AlarmDTO.alarm_event_record_get
133
134     @api_monitoring_v1.doc('Get Alarm Event Record Information')
135     @api_monitoring_v1.marshal_with(model)
136     def get(self, alarmEventRecordId):
137         result = alarm_view.alarm_event_record_one(alarmEventRecordId, bus.uow)
138         if result is not None:
139             return result
140         raise NotFoundException(
141             "Alarm Event Record {} doesn't exist".format(alarmEventRecordId))
142
143
144 # ----------  Alarm Subscriptions ---------- #
145 @api_monitoring_v1.route("/v1/alarmSubscriptions")
146 class SubscriptionsListRouter(Resource):
147
148     model = SubscriptionDTO.subscription_get
149     expect = SubscriptionDTO.subscription_create
150
151     @api_monitoring_v1.doc('Get Alarm Subscription List')
152     @api_monitoring_v1.marshal_list_with(model)
153     @api_monitoring_v1.param(
154         PAGE_PARAM,
155         'Page number of the results to fetch. Default: 1',
156         _in='query', default=1)
157     @api_monitoring_v1.param(
158         'all_fields',
159         'Set any value for show all fields. This value will cover "fields" ' +
160         'and "all_fields".',
161         _in='query')
162     @api_monitoring_v1.param(
163         'fields',
164         'Set fields to show, split by comma, "/" for parent and children.' +
165         ' Like "name,parent/children". This value will cover' +
166         ' "exculde_fields".',
167         _in='query')
168     @api_monitoring_v1.param(
169         'exclude_fields',
170         'Set fields to exclude showing, split by comma, "/" for parent and ' +
171         'children. Like "name,parent/children". This value will cover ' +
172         '"exclude_default".',
173         _in='query')
174     @api_monitoring_v1.param(
175         'exclude_default',
176         'Exclude showing all default fields, Set "true" to enable.',
177         _in='query')
178     @api_monitoring_v1.param(
179         'filter',
180         'Filter of the query.',
181         _in='query')
182     def get(self):
183         parser = reqparse.RequestParser()
184         parser.add_argument(PAGE_PARAM, location='args')
185         parser.add_argument('filter', location='args')
186         args = parser.parse_args()
187         kwargs = {}
188         if args.nextpage_opaque_marker is not None:
189             kwargs['page'] = args.nextpage_opaque_marker
190         kwargs['filter'] = args.filter if args.filter is not None else ''
191
192         ret = alarm_view.subscriptions(bus.uow, **kwargs)
193         return link_header(request.full_path, ret)
194
195     @api_monitoring_v1.doc('Create a Alarm Subscription')
196     @api_monitoring_v1.expect(expect)
197     @api_monitoring_v1.marshal_with(
198         model, code=201,
199         mask='{alarmSubscriptionId,callback,consumerSubscriptionId,filter}')
200     def post(self):
201         data = api_monitoring_v1.payload
202         callback = data.get('callback', None)
203         if not callback:
204             raise BadRequestException('The callback parameter is required')
205
206         result = alarm_view.subscription_create(data, bus.uow)
207         return result, 201
208
209
210 @api_monitoring_v1.route("/v1/alarmSubscriptions/<alarmSubscriptionID>")
211 @api_monitoring_v1.param('alarmSubscriptionID', 'ID of the Alarm Subscription')
212 @api_monitoring_v1.response(404, 'Alarm Subscription not found')
213 class SubscriptionGetDelRouter(Resource):
214
215     model = SubscriptionDTO.subscription_get
216
217     @api_monitoring_v1.doc('Get Alarm Subscription Information')
218     @api_monitoring_v1.marshal_with(model)
219     @api_monitoring_v1.param(
220         'all_fields',
221         'Set any value for show all fields. This value will cover "fields" ' +
222         'and "all_fields".',
223         _in='query')
224     @api_monitoring_v1.param(
225         'fields',
226         'Set fields to show, split by comma, "/" for parent and children.' +
227         ' Like "name,parent/children". This value will cover' +
228         ' "exculde_fields".',
229         _in='query')
230     @api_monitoring_v1.param(
231         'exclude_fields',
232         'Set fields to exclude showing, split by comma, "/" for parent and ' +
233         'children. Like "name,parent/children". This value will cover ' +
234         '"exclude_default".',
235         _in='query')
236     @api_monitoring_v1.param(
237         'exclude_default',
238         'Exclude showing all default fields, Set "true" to enable.',
239         _in='query')
240     def get(self, alarmSubscriptionID):
241         result = alarm_view.subscription_one(
242             alarmSubscriptionID, bus.uow)
243         if result is not None:
244             return result
245         raise NotFoundException(
246             "Subscription {} doesn't exist".format(alarmSubscriptionID))
247
248     @api_monitoring_v1.doc('Delete an Alarm Subscription')
249     @api_monitoring_v1.response(200, 'Subscription deleted')
250     def delete(self, alarmSubscriptionID):
251         result = alarm_view.subscription_delete(alarmSubscriptionID, bus.uow)
252         return result, 200