Use generated PAP interface for Swagger
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / rest / StatisticsRestControllerV1.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2023 Nordix Foundation.
4  *  Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property.
5  *  Modifications Copyright (C) 2021-2022 Bell Canada. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.pap.main.rest;
24
25 import java.time.Instant;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.UUID;
29 import lombok.RequiredArgsConstructor;
30 import org.onap.policy.models.pdp.concepts.PdpStatistics;
31 import org.onap.policy.pap.main.service.PdpStatisticsService;
32 import org.springframework.http.ResponseEntity;
33 import org.springframework.web.bind.annotation.RestController;
34
35 /**
36  * Class to provide REST endpoints for PAP component statistics.
37  *
38  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
39  */
40 @RestController
41 @RequiredArgsConstructor
42 public class StatisticsRestControllerV1 extends PapRestControllerV1
43     implements StatisticsRestControllerV1Api {
44
45     private final PdpStatisticsService pdpStatisticsService;
46
47     /**
48      * get statistics of PAP.
49      *
50      *
51      * @return a response
52      */
53     @Override
54     public ResponseEntity<StatisticsReport> statistics(UUID requestId) {
55         return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
56             .body(pdpStatisticsService.fetchCurrentStatistics());
57     }
58
59     /**
60      * get all statistics of PDP groups.
61      *
62      * @return a response
63      */
64     @Override
65     public ResponseEntity<Map<String, Map<String, List<PdpStatistics>>>> pdpStatistics(
66             UUID requestId,
67             Integer recordCount,
68             Long startTime,
69             Long endTime) {
70         return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId).body(pdpStatisticsService
71             .fetchDatabaseStatistics(recordCount.intValue(), convertEpochtoInstant(startTime),
72                     convertEpochtoInstant(endTime)));
73     }
74
75
76     /**
77      * get all statistics of a PDP group.
78      *
79      * @param groupName name of the PDP group
80      * @return a response
81      */
82     @Override
83     public ResponseEntity<Map<String, Map<String, List<PdpStatistics>>>> pdpGroupStatistics(
84             String groupName,
85             UUID requestId,
86             Integer recordCount,
87             Long startTime,
88             Long endTime) {
89         return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
90             .body(pdpStatisticsService.fetchDatabaseStatistics(groupName, recordCount.intValue(),
91                     convertEpochtoInstant(startTime), convertEpochtoInstant(endTime)));
92     }
93
94     /**
95      * get all statistics of sub PDP group.
96      *
97      * @param groupName name of the PDP group
98      * @param subType type of the sub PDP group
99      * @return a response
100      */
101     @Override
102     public ResponseEntity<Map<String, Map<String, List<PdpStatistics>>>> pdpSubGroupStatistics(
103             String groupName,
104             String subType,
105             UUID requestId,
106             Integer recordCount,
107             Long startTime,
108             Long endTime) {
109         return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
110             .body(pdpStatisticsService.fetchDatabaseStatistics(groupName, subType, recordCount.intValue(),
111                 convertEpochtoInstant(startTime), convertEpochtoInstant(endTime)));
112     }
113
114     /**
115      * get all statistics of one PDP.
116      *
117      * @param groupName name of the PDP group
118      * @param subType type of the sub PDP group
119      * @param pdpName the name of the PDP
120      * @param recordCount the count of the query response, optional, default return all statistics stored
121      * @return a response
122      */
123     @Override
124     public ResponseEntity<Map<String, Map<String, List<PdpStatistics>>>> pdpInstanceStatistics(
125             String groupName,
126             String subType,
127             String pdpName,
128             UUID requestId,
129             Integer recordCount,
130             Long startTime,
131             Long endTime) {
132         return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
133             .body(pdpStatisticsService.fetchDatabaseStatistics(groupName, subType, pdpName, recordCount.intValue(),
134                 convertEpochtoInstant(startTime), convertEpochtoInstant(endTime)));
135     }
136
137     private Instant convertEpochtoInstant(Long epochSecond) {
138         return (epochSecond == null ? null : Instant.ofEpochSecond(epochSecond));
139     }
140
141 }