e1772142e3b39c1045dcbc90f630cec2698c53dd
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / rest / PolicyStatusControllerV1.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP PAP
4  * ================================================================================
5  * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2021-2023 Nordix Foundation.
7  * Modifications Copyright (C) 2021-2022 Bell Canada. All rights reserved.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.pap.main.rest;
24
25 import com.google.re2j.PatternSyntaxException;
26 import java.util.Collection;
27 import java.util.UUID;
28 import lombok.RequiredArgsConstructor;
29 import org.onap.policy.models.base.PfModelRuntimeException;
30 import org.onap.policy.models.pap.concepts.PolicyStatus;
31 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
32 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.http.HttpStatus;
36 import org.springframework.http.ResponseEntity;
37 import org.springframework.web.bind.annotation.RestController;
38
39 /**
40  * Class to provide REST end points for PAP component to retrieve the status of deployed
41  * policies.
42  */
43 @RestController
44 @RequiredArgsConstructor
45 public class PolicyStatusControllerV1 extends PapRestControllerV1 implements PolicyStatusControllerV1Api {
46     private static final String EMPTY_REGEX_ERROR_MESSAGE = "An empty string passed as a regex is not allowed";
47     private static final String EMPTY_REGEX_WARNING = ". Empty string passed as Regex.";
48     private static final String GET_DEPLOYMENTS_FAILED = "get deployments failed";
49
50     private static final Logger logger = LoggerFactory.getLogger(PolicyStatusControllerV1.class);
51
52     private final PolicyStatusProvider provider;
53
54     /**
55      * Queries status of all deployed policies. If regex is not null or empty, the function will only return
56      * policies that match regex
57      *
58      * @param requestId request ID used in ONAP logging
59      * @param regex regex for a policy name
60      * @return a response
61      */
62     @Override
63     public ResponseEntity<Object> queryAllDeployedPolicies(UUID requestId, String regex) {
64         try {
65             final Collection<PolicyStatus> result;
66             if (regex == null) {
67                 result = provider.getStatus();
68             } else if (regex.isBlank()) {
69                 return makeRegexNotFoundResponse(requestId);
70             } else {
71                 result = provider.getByRegex(regex);
72             }
73             return makeListOrNotFoundResponse(requestId, result);
74
75         } catch (PfModelRuntimeException e) {
76             logger.warn(GET_DEPLOYMENTS_FAILED, e);
77             return addLoggingHeaders(
78                 addVersionControlHeaders(ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode())),
79                 requestId).body(e.getErrorResponse().getErrorMessage());
80         } catch (PatternSyntaxException e) {
81             logger.warn(GET_DEPLOYMENTS_FAILED, e);
82             return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.status(HttpStatus.BAD_REQUEST)), requestId)
83                 .body(e.getMessage());
84         }
85     }
86
87     /**
88      * Queries status of specific deployed policies.
89      *
90      * @param requestId request ID used in ONAP logging
91      * @return a response
92      */
93     @Override
94     public ResponseEntity<Object> queryDeployedPolicies(String name, UUID requestId) {
95
96         try {
97             Collection<PolicyStatus> result = provider.getStatus(new ToscaConceptIdentifierOptVersion(name, null));
98             if (result.isEmpty()) {
99                 return makeNotFoundResponse(requestId);
100
101             } else {
102                 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId).body(result);
103             }
104
105         } catch (PfModelRuntimeException e) {
106             logger.warn(GET_DEPLOYMENTS_FAILED, e);
107             return addLoggingHeaders(
108                 addVersionControlHeaders(ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode())),
109                 requestId).body(e.getErrorResponse().getErrorMessage());
110         }
111     }
112
113
114     /**
115      * Queries status of a specific deployed policy.
116      *
117      * @param requestId request ID used in ONAP logging
118      * @return a response
119      */
120     @Override
121     public ResponseEntity<Object> queryDeployedPolicy(String name, String version, UUID requestId) {
122
123         try {
124             Collection<PolicyStatus> result = provider.getStatus(new ToscaConceptIdentifierOptVersion(name, version));
125             if (result.isEmpty()) {
126                 return makeNotFoundResponse(requestId);
127
128             } else {
129                 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
130                     .body(result.iterator().next());
131             }
132
133         } catch (PfModelRuntimeException e) {
134             logger.warn(GET_DEPLOYMENTS_FAILED, e);
135             return addLoggingHeaders(
136                 addVersionControlHeaders(ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode())),
137                 requestId).body(e.getErrorResponse().getErrorMessage());
138         }
139     }
140
141
142     /**
143      * Queries status of all policies.
144      *
145      * @param requestId request ID used in ONAP logging
146      * @return a response
147      */
148     @Override
149     public ResponseEntity<Object> getStatusOfAllPolicies(UUID requestId) {
150
151         try {
152             return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
153                 .body(provider.getPolicyStatus());
154
155         } catch (PfModelRuntimeException e) {
156             logger.warn(GET_DEPLOYMENTS_FAILED, e);
157             return addLoggingHeaders(
158                 addVersionControlHeaders(ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode())),
159                 requestId).body(e.getErrorResponse().getErrorMessage());
160         }
161     }
162
163     /**
164      * Queries status of policies in a specific PdpGroup. if regex is not null or empty, the function will only return
165      * policies that match regex
166      *
167      * @param pdpGroupName name of the PdpGroup
168      * @param requestId request ID used in ONAP logging
169      * @param regex regex for a policy name
170      * @return a response
171      */
172     @Override
173     public ResponseEntity<Object> getStatusOfPoliciesByGroup(
174         String pdpGroupName,
175         UUID requestId,
176         String regex) {
177
178         try {
179             final Collection<PdpPolicyStatus> result;
180             if (regex == null) {
181                 result = provider.getPolicyStatus(pdpGroupName);
182             } else if (regex.isBlank()) {
183                 return makeRegexNotFoundResponse(requestId);
184             } else {
185                 result = provider.getPolicyStatusByRegex(pdpGroupName, regex);
186             }
187             return makeListOrNotFoundResponse(requestId, result);
188
189         } catch (PfModelRuntimeException e) {
190             logger.warn(GET_DEPLOYMENTS_FAILED, e);
191             return addLoggingHeaders(
192                 addVersionControlHeaders(ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode())),
193                 requestId).body(e.getErrorResponse().getErrorMessage());
194         } catch (PatternSyntaxException e) {
195             logger.warn(GET_DEPLOYMENTS_FAILED, e);
196             return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.status(HttpStatus.BAD_REQUEST)), requestId)
197                 .body(e.getMessage());
198         }
199     }
200
201     /**
202      * Queries status of all versions of a specific policy in a specific PdpGroup.
203      *
204      * @param pdpGroupName name of the PdpGroup
205      * @param policyName name of the Policy
206      * @param requestId request ID used in ONAP logging
207      * @return a response
208      */
209     @Override
210     public ResponseEntity<Object> getStatusOfPolicies(
211         String pdpGroupName,
212         String policyName,
213         UUID requestId) {
214
215         try {
216             Collection<PdpPolicyStatus> result =
217                 provider.getPolicyStatus(pdpGroupName, new ToscaConceptIdentifierOptVersion(policyName, null));
218             if (result.isEmpty()) {
219                 return makeNotFoundResponse(requestId);
220
221             } else {
222                 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
223                     .body(result);
224             }
225
226         } catch (PfModelRuntimeException e) {
227             logger.warn(GET_DEPLOYMENTS_FAILED, e);
228             return addLoggingHeaders(
229                 addVersionControlHeaders(ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode())),
230                 requestId).body(e.getErrorResponse().getErrorMessage());
231         }
232     }
233
234
235     /**
236      * Queries status of a specific version of a specific policy in a specific PdpGroup.
237      *
238      * @param pdpGroupName name of the PdpGroup
239      * @param policyName name of the Policy
240      * @param policyVersion version of the Policy
241      * @param requestId request ID used in ONAP logging
242      * @return a response
243      */
244
245     @Override
246     public ResponseEntity<Object> getStatusOfPolicy(
247             String pdpGroupName,
248             String policyName,
249             String policyVersion,
250             UUID requestId) {
251
252         try {
253             Collection<PdpPolicyStatus> result = provider.getPolicyStatus(pdpGroupName,
254                 new ToscaConceptIdentifierOptVersion(policyName, policyVersion));
255             if (result.isEmpty()) {
256                 return makeNotFoundResponse(requestId);
257
258             } else {
259                 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
260                     .body(result.iterator().next());
261             }
262
263         } catch (PfModelRuntimeException e) {
264             logger.warn(GET_DEPLOYMENTS_FAILED, e);
265             return addLoggingHeaders(
266                 addVersionControlHeaders(ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode())),
267                 requestId).body(e.getErrorResponse().getErrorMessage());
268         }
269     }
270
271     /**
272      * Makes a "not found" response.
273      *
274      * @param requestId request ID
275      * @return a "not found" response
276      */
277     private ResponseEntity<Object> makeNotFoundResponse(final UUID requestId) {
278         return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.status(HttpStatus.NOT_FOUND)), requestId)
279                         .build();
280     }
281
282     private ResponseEntity<Object> makeRegexNotFoundResponse(UUID requestId) {
283         logger.warn(GET_DEPLOYMENTS_FAILED + EMPTY_REGEX_WARNING);
284         return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.status(HttpStatus.BAD_REQUEST)),
285             requestId).body(EMPTY_REGEX_ERROR_MESSAGE);
286     }
287
288     private ResponseEntity<Object> makeListOrNotFoundResponse(UUID requestId, Collection<?> result) {
289         if (result.isEmpty()) {
290             return makeNotFoundResponse(requestId);
291         } else {
292             return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId).body(result);
293         }
294     }
295 }