1 # Copyright (C) 2021 Wind River Systems, Inc.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 from flask import request
16 from flask_restx import Resource, reqparse
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_monitoring_v1
22 from o2ims.views.alarm_dto import AlarmDTO, SubscriptionDTO
24 from o2common.helper import o2logging
25 logger = o2logging.get_logger(__name__)
28 def configure_api_route():
29 # Set global bus for resource
31 bus = MessageBus.get_instance()
34 # ---------- Alarm Event Record ---------- #
35 @api_monitoring_v1.route("/alarms")
36 @api_monitoring_v1.param(PAGE_PARAM,
37 'Page number of the results to fetch.' +
39 _in='query', default=1)
40 @api_monitoring_v1.param(
42 'Set any value for show all fields. This value will cover "fields" ' +
45 @api_monitoring_v1.param(
47 'Set fields to show, split by comma, "/" for parent and children.' +
48 ' Like "name,parent/children". This value will cover' +
51 @api_monitoring_v1.param(
53 'Set fields to exclude showing, split by comma, "/" for parent and ' +
54 'children. Like "name,parent/children". This value will cover ' +
57 @api_monitoring_v1.param(
59 'Exclude showing all default fields, Set "true" to enable.',
61 @api_monitoring_v1.param(
63 'Exclude showing all default fields, Set "true" to enable.',
65 class AlarmListRouter(Resource):
67 model = AlarmDTO.alarm_event_record_get
69 @api_monitoring_v1.marshal_list_with(model)
71 parser = reqparse.RequestParser()
72 parser.add_argument(PAGE_PARAM, location='args')
73 parser.add_argument('filter', location='args')
74 args = parser.parse_args()
76 if args.nextpage_opaque_marker is not None:
77 kwargs['page'] = args.nextpage_opaque_marker
78 kwargs['filter'] = args.filter if args.filter is not None else ''
80 ret = alarm_view.alarm_event_records(bus.uow, **kwargs)
81 return link_header(request.full_path, ret)
84 @api_monitoring_v1.route("/alarms/<alarmEventRecordId>")
85 @api_monitoring_v1.param('alarmEventRecordId', 'ID of the alarm event record')
86 @api_monitoring_v1.response(404, 'Alarm Event Record not found')
87 @api_monitoring_v1.param(
89 'Set any value for show all fields. This value will cover "fields" ' +
92 @api_monitoring_v1.param(
94 'Set fields to show, split by comma, "/" for parent and children.' +
95 ' Like "name,parent/children". This value will cover' +
98 @api_monitoring_v1.param(
100 'Set fields to exclude showing, split by comma, "/" for parent and ' +
101 'children. Like "name,parent/children". This value will cover ' +
102 '"exclude_default".',
104 @api_monitoring_v1.param(
106 'Exclude showing all default fields, Set "true" to enable.',
108 class AlarmGetRouter(Resource):
110 model = AlarmDTO.alarm_event_record_get
112 @api_monitoring_v1.doc('Get resource type')
113 @api_monitoring_v1.marshal_with(model)
114 def get(self, alarmEventRecordId):
115 result = alarm_view.alarm_event_record_one(alarmEventRecordId, bus.uow)
116 if result is not None:
118 api_monitoring_v1.abort(
119 404, "Resource type {} doesn't exist".format(alarmEventRecordId))
122 # ---------- Alarm Subscriptions ---------- #
123 @api_monitoring_v1.route("/alarmSubscriptions")
124 class SubscriptionsListRouter(Resource):
126 model = SubscriptionDTO.subscription_get
127 expect = SubscriptionDTO.subscription
128 post_resp = SubscriptionDTO.subscription_post_resp
130 @api_monitoring_v1.doc('List alarm subscriptions')
131 @api_monitoring_v1.marshal_list_with(model)
132 @api_monitoring_v1.param(
134 'Page number of the results to fetch. Default: 1',
135 _in='query', default=1)
136 @api_monitoring_v1.param(
138 'Set any value for show all fields. This value will cover "fields" ' +
141 @api_monitoring_v1.param(
143 'Set fields to show, split by comma, "/" for parent and children.' +
144 ' Like "name,parent/children". This value will cover' +
145 ' "exculde_fields".',
147 @api_monitoring_v1.param(
149 'Set fields to exclude showing, split by comma, "/" for parent and ' +
150 'children. Like "name,parent/children". This value will cover ' +
151 '"exclude_default".',
153 @api_monitoring_v1.param(
155 'Exclude showing all default fields, Set "true" to enable.',
157 @api_monitoring_v1.param(
159 'Exclude showing all default fields, Set "true" to enable.',
162 parser = reqparse.RequestParser()
163 parser.add_argument(PAGE_PARAM, location='args')
164 parser.add_argument('filter', location='args')
165 args = parser.parse_args()
167 if args.nextpage_opaque_marker is not None:
168 kwargs['page'] = args.nextpage_opaque_marker
169 kwargs['filter'] = args.filter if args.filter is not None else ''
171 ret = alarm_view.subscriptions(bus.uow, **kwargs)
172 return link_header(request.full_path, ret)
174 @api_monitoring_v1.doc('Create a alarm subscription')
175 @api_monitoring_v1.expect(expect)
176 @api_monitoring_v1.marshal_with(post_resp, code=201)
178 data = api_monitoring_v1.payload
179 result = alarm_view.subscription_create(data, bus.uow)
183 @api_monitoring_v1.route("/alarmSubscriptions/<alarmSubscriptionID>")
184 @api_monitoring_v1.param('alarmSubscriptionID', 'ID of the Alarm Subscription')
185 @api_monitoring_v1.response(404, 'Alarm Subscription not found')
186 class SubscriptionGetDelRouter(Resource):
188 model = SubscriptionDTO.subscription_get
190 @api_monitoring_v1.doc('Get Alarm Subscription by ID')
191 @api_monitoring_v1.marshal_with(model)
192 @api_monitoring_v1.param(
194 'Set any value for show all fields. This value will cover "fields" ' +
197 @api_monitoring_v1.param(
199 'Set fields to show, split by comma, "/" for parent and children.' +
200 ' Like "name,parent/children". This value will cover' +
201 ' "exculde_fields".',
203 @api_monitoring_v1.param(
205 'Set fields to exclude showing, split by comma, "/" for parent and ' +
206 'children. Like "name,parent/children". This value will cover ' +
207 '"exclude_default".',
209 @api_monitoring_v1.param(
211 'Exclude showing all default fields, Set "true" to enable.',
213 def get(self, alarmSubscriptionID):
214 result = alarm_view.subscription_one(
215 alarmSubscriptionID, bus.uow)
216 if result is not None:
218 api_monitoring_v1.abort(404, "Subscription {} doesn't exist".format(
219 alarmSubscriptionID))
221 @api_monitoring_v1.doc('Delete subscription by ID')
222 @api_monitoring_v1.response(204, 'Subscription deleted')
223 def delete(self, alarmSubscriptionID):
224 result = alarm_view.subscription_delete(alarmSubscriptionID, bus.uow)