7fde5fba3d5e02a71771f04dc0c9f8512052e9b9
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.runtime.monitoring.rest;
22
23 import io.swagger.annotations.ApiOperation;
24 import io.swagger.annotations.ApiParam;
25 import io.swagger.annotations.ApiResponse;
26 import io.swagger.annotations.ApiResponses;
27 import io.swagger.annotations.Authorization;
28 import io.swagger.annotations.Extension;
29 import io.swagger.annotations.ExtensionProperty;
30 import io.swagger.annotations.ResponseHeader;
31 import java.time.Instant;
32 import java.util.UUID;
33 import javax.ws.rs.DefaultValue;
34 import javax.ws.rs.GET;
35 import javax.ws.rs.HeaderParam;
36 import javax.ws.rs.Path;
37 import javax.ws.rs.QueryParam;
38 import javax.ws.rs.core.Response;
39 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatisticsList;
40 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatisticsList;
41 import org.onap.policy.clamp.controlloop.runtime.main.rest.RestController;
42 import org.onap.policy.clamp.controlloop.runtime.monitoring.MonitoringHandler;
43 import org.onap.policy.clamp.controlloop.runtime.monitoring.MonitoringProvider;
44 import org.onap.policy.models.base.PfModelException;
45 import org.onap.policy.models.base.PfModelRuntimeException;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 /**
50  * This class handles REST endpoints for CL Statistics monitoring.
51  */
52 public class MonitoringQueryController extends RestController {
53
54     private static final Logger LOGGER = LoggerFactory.getLogger(MonitoringQueryController.class);
55     private final MonitoringProvider provider;
56
57     /**
58      * Create Monitoring Controller.
59      */
60     public MonitoringQueryController() {
61         this.provider = MonitoringHandler.getInstance().getMonitoringProvider();
62     }
63
64
65     /**
66      * Queries details of control loop participants statistics.
67      *
68      * @param requestId request ID used in ONAP logging
69      * @param name the name of the participant to get, null for all participants statistics
70      * @param version the version of the participant to get, null for all participants with the given name
71      * @param recordCount the record count to be fetched
72      * @param startTime the time from which to get statistics
73      * @param endTime the time to which to get statistics
74      * @return the participant statistics
75      */
76     // @formatter:off
77     @GET
78     @Path("/monitoring/participant")
79     @ApiOperation(value = "Query details of the requested participant stats",
80         notes = "Queries details of the requested participant stats, returning all participant stats",
81         response = ParticipantStatisticsList.class,
82         tags = {
83             "Clamp control loop Monitoring API"
84         },
85         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
86         responseHeaders = {
87             @ResponseHeader(
88                 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
89                 response = String.class),
90             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
91                 response = String.class),
92             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
93                 response = String.class),
94             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
95                 response = UUID.class)},
96         extensions = {
97             @Extension
98                 (
99                     name = EXTENSION_NAME,
100                     properties = {
101                         @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
102                         @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
103                     }
104                 )
105         }
106     )
107     @ApiResponses(
108         value = {
109             @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
110             @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
111             @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
112         }
113     )
114     // @formatter:on
115     public Response queryParticipantStatistics(@HeaderParam(REQUEST_ID_NAME)
116                                                @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
117                                                @ApiParam(value = "Control Loop participant name", required = true)
118                                                @QueryParam("name") final String name,
119                                                @ApiParam(value = "Control Loop participant version", required = true)
120                                                @QueryParam("version") final String version,
121                                                @ApiParam(value = "Record count", required = false) @DefaultValue("0")
122                                                @QueryParam("recordCount") final int recordCount,
123                                                @ApiParam(value = "start time", required = false)
124                                                @QueryParam("startTime") final String startTime,
125                                                @ApiParam(value = "end time", required = false)
126                                                @QueryParam("endTime") final String endTime) {
127
128         try {
129             Instant startTimestamp = null;
130             Instant endTimestamp = null;
131
132             if (startTime != null) {
133                 startTimestamp = Instant.parse(startTime);
134             }
135             if (endTime != null) {
136                 endTimestamp = Instant.parse(endTime);
137             }
138             ParticipantStatisticsList response = provider.fetchFilteredParticipantStatistics(name, version, recordCount,
139                 startTimestamp, endTimestamp);
140             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
141                 .entity(response)
142                 .build();
143
144         } catch (PfModelRuntimeException e) {
145             LOGGER.warn("Monitoring of participants statistics failed", e);
146             return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())),
147                 requestId).build();
148         }
149
150     }
151
152     /**
153      * Queries details of all participant statistics per control loop.
154      *
155      * @param requestId request ID used in ONAP logging
156      * @param name the name of the control loop
157      * @param version version of the control loop
158      * @return the control loop element statistics
159      */
160     // @formatter:off
161     @GET
162     @Path("/monitoring/participants/controlloop")
163     @ApiOperation(value = "Query details of all the participant stats in a control loop",
164         notes = "Queries details of the participant stats, returning all participant stats",
165         response = ClElementStatisticsList.class,
166         tags = {
167             "Clamp control loop Monitoring API"
168         },
169         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
170         responseHeaders = {
171             @ResponseHeader(
172                 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
173                 response = String.class),
174             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
175                 response = String.class),
176             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
177                 response = String.class),
178             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
179                 response = UUID.class)},
180         extensions = {
181             @Extension
182                 (
183                     name = EXTENSION_NAME,
184                     properties = {
185                         @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
186                         @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
187                     }
188                 )
189         })
190     @ApiResponses(
191         value = {
192             @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
193             @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
194             @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
195         }
196     )
197     // @formatter:on
198     public Response queryParticipantStatisticsPerControlLoop(@HeaderParam(REQUEST_ID_NAME)
199                                                              @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
200                                                              @ApiParam(value = "Control Loop name", required = true)
201                                                              @QueryParam("name") final String name,
202                                                              @ApiParam(value = "Control Loop version", required = true)
203                                                              @QueryParam("version") final String version) {
204
205         try {
206             ParticipantStatisticsList response = provider.fetchParticipantStatsPerControlLoop(name, version);
207             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
208                 .entity(response)
209                 .build();
210
211         } catch (PfModelRuntimeException e) {
212             LOGGER.warn("Monitoring of Cl participant statistics failed", e);
213             return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())),
214                 requestId).build();
215         }
216
217     }
218
219
220
221     /**
222      * Queries details of all control loop element statistics per control loop.
223      *
224      * @param requestId request ID used in ONAP logging
225      * @param name the name of the control loop
226      * @param version version of the control loop
227      * @return the control loop element statistics
228      */
229     // @formatter:off
230     @GET
231     @Path("/monitoring/clelements/controlloop")
232     @ApiOperation(value = "Query details of the requested cl element stats in a control loop",
233         notes = "Queries details of the requested cl element stats, returning all clElement stats",
234         response = ClElementStatisticsList.class,
235         tags = {
236             "Clamp control loop Monitoring API"
237         },
238         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
239         responseHeaders = {
240             @ResponseHeader(
241                 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
242                 response = String.class),
243             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
244                 response = String.class),
245             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
246                 response = String.class),
247             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
248                 response = UUID.class)},
249         extensions = {
250             @Extension
251                 (
252                     name = EXTENSION_NAME,
253                     properties = {
254                         @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
255                         @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
256                     }
257                 )
258         })
259     @ApiResponses(
260         value = {
261             @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
262             @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
263             @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
264         }
265     )
266     // @formatter:on
267     public Response queryElementStatisticsPerControlLoop(@HeaderParam(REQUEST_ID_NAME)
268                                                          @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
269                                                          @ApiParam(value = "Control Loop name", required = true)
270                                                          @QueryParam("name") final String name,
271                                                          @ApiParam(value = "Control Loop version", required = true)
272                                                          @QueryParam("version") final String version) {
273
274         try {
275             ClElementStatisticsList response = provider.fetchClElementStatsPerControlLoop(name, version);
276             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
277                 .entity(response)
278                 .build();
279
280         } catch (PfModelRuntimeException e) {
281             LOGGER.warn("Monitoring of Cl Element statistics failed", e);
282             return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())),
283                 requestId).build();
284         }
285
286     }
287
288
289
290
291     /**
292      * Queries details of all control loop element statistics per control loop.
293      *
294      * @param requestId request ID used in ONAP logging
295      * @param name the name of the control loop
296      * @param version version of the control loop
297      * @param id Id of the control loop element
298      * @param recordCount the record count to be fetched
299      * @param startTime the time from which to get statistics
300      * @param endTime the time to which to get statistics
301      * @return the control loop element statistics
302      */
303     // @formatter:off
304     @GET
305     @Path("/monitoring/clelement")
306     @ApiOperation(value = "Query details of the requested cl element stats",
307         notes = "Queries details of the requested cl element stats, returning all clElement stats",
308         response = ClElementStatisticsList.class,
309         tags = {
310             "Clamp control loop Monitoring API"
311         },
312         authorizations = @Authorization(value = AUTHORIZATION_TYPE),
313         responseHeaders = {
314             @ResponseHeader(
315                 name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
316                 response = String.class),
317             @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
318                 response = String.class),
319             @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
320                 response = String.class),
321             @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
322                 response = UUID.class)},
323         extensions = {
324             @Extension
325                 (
326                     name = EXTENSION_NAME,
327                     properties = {
328                         @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
329                         @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
330                     }
331                 )
332         })
333     @ApiResponses(
334         value = {
335             @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
336             @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
337             @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
338         }
339     )
340     // @formatter:on
341     public Response queryElementStatistics(@HeaderParam(REQUEST_ID_NAME)
342                                            @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
343                                            @ApiParam(value = "Participant name", required = true)
344                                            @QueryParam("name") final String name,
345                                            @ApiParam(value = "Participant version", required = true)
346                                            @QueryParam("version") final String version,
347                                            @ApiParam(value = "Record count", required = false)
348                                            @DefaultValue("0") @QueryParam("recordCount") final int recordCount,
349                                            @ApiParam(value = "Control Loop element id", required = false)
350                                            @QueryParam("id") final String id,
351                                            @ApiParam(value = "start time", required = false)
352                                            @QueryParam("startTime") final String startTime,
353                                            @ApiParam(value = "end time", required = false)
354                                            @QueryParam("endTime") final String endTime) {
355
356         try {
357             Instant startTimestamp = null;
358             Instant endTimestamp = null;
359
360             if (startTime != null) {
361                 startTimestamp = Instant.parse(startTime);
362             }
363             if (endTime != null) {
364                 endTimestamp = Instant.parse(endTime);
365             }
366             ClElementStatisticsList response = provider.fetchFilteredClElementStatistics(name, version, id,
367                 startTimestamp, endTimestamp, recordCount);
368             return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
369                 .entity(response)
370                 .build();
371
372         } catch (PfModelRuntimeException | PfModelException e) {
373             LOGGER.warn("Monitoring of Cl Element statistics failed", e);
374             return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())),
375                 requestId).build();
376         }
377
378     }
379
380 }