Set all cross references of policy/pap
[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.context.annotation.Profile;
36 import org.springframework.http.HttpStatus;
37 import org.springframework.http.ResponseEntity;
38 import org.springframework.web.bind.annotation.RestController;
39
40 /**
41  * Class to provide REST end points for PAP component to retrieve the status of deployed
42  * policies.
43  */
44 @RestController
45 @RequiredArgsConstructor
46 @Profile("default")
47 public class PolicyStatusControllerV1 extends PapRestControllerV1 implements PolicyStatusControllerV1Api {
48     private static final String EMPTY_REGEX_ERROR_MESSAGE = "An empty string passed as a regex is not allowed";
49     private static final String EMPTY_REGEX_WARNING = ". Empty string passed as Regex.";
50     private static final String GET_DEPLOYMENTS_FAILED = "get deployments failed";
51
52     private static final Logger logger = LoggerFactory.getLogger(PolicyStatusControllerV1.class);
53
54     private final PolicyStatusProvider provider;
55
56     /**
57      * Queries status of all deployed policies. If regex is not null or empty, the function will only return
58      * policies that match regex
59      *
60      * @param requestId request ID used in ONAP logging
61      * @param regex regex for a policy name
62      * @return a response
63      */
64     @Override
65     public ResponseEntity<Object> queryAllDeployedPolicies(UUID requestId, String regex) {
66         try {
67             final Collection<PolicyStatus> result;
68             if (regex == null) {
69                 result = provider.getStatus();
70             } else if (regex.isBlank()) {
71                 return makeRegexNotFoundResponse(requestId);
72             } else {
73                 result = provider.getByRegex(regex);
74             }
75             return makeListOrNotFoundResponse(requestId, result);
76
77         } catch (PfModelRuntimeException e) {
78             logger.warn(GET_DEPLOYMENTS_FAILED, e);
79             return addLoggingHeaders(
80                 addVersionControlHeaders(ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode())),
81                 requestId).body(e.getErrorResponse().getErrorMessage());
82         } catch (PatternSyntaxException e) {
83             logger.warn(GET_DEPLOYMENTS_FAILED, e);
84             return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.status(HttpStatus.BAD_REQUEST)), requestId)
85                 .body(e.getMessage());
86         }
87     }
88
89     /**
90      * Queries status of specific deployed policies.
91      *
92      * @param requestId request ID used in ONAP logging
93      * @return a response
94      */
95     @Override
96     public ResponseEntity<Object> queryDeployedPolicies(String name, UUID requestId) {
97
98         try {
99             Collection<PolicyStatus> result = provider.getStatus(new ToscaConceptIdentifierOptVersion(name, null));
100             if (result.isEmpty()) {
101                 return makeNotFoundResponse(requestId);
102
103             } else {
104                 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId).body(result);
105             }
106
107         } catch (PfModelRuntimeException e) {
108             logger.warn(GET_DEPLOYMENTS_FAILED, e);
109             return addLoggingHeaders(
110                 addVersionControlHeaders(ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode())),
111                 requestId).body(e.getErrorResponse().getErrorMessage());
112         }
113     }
114
115
116     /**
117      * Queries status of a specific deployed policy.
118      *
119      * @param requestId request ID used in ONAP logging
120      * @return a response
121      */
122     @Override
123     public ResponseEntity<Object> queryDeployedPolicy(String name, String version, UUID requestId) {
124
125         try {
126             Collection<PolicyStatus> result = provider.getStatus(new ToscaConceptIdentifierOptVersion(name, version));
127             if (result.isEmpty()) {
128                 return makeNotFoundResponse(requestId);
129
130             } else {
131                 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
132                     .body(result.iterator().next());
133             }
134
135         } catch (PfModelRuntimeException e) {
136             logger.warn(GET_DEPLOYMENTS_FAILED, e);
137             return addLoggingHeaders(
138                 addVersionControlHeaders(ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode())),
139                 requestId).body(e.getErrorResponse().getErrorMessage());
140         }
141     }
142
143
144     /**
145      * Queries status of all policies.
146      *
147      * @param requestId request ID used in ONAP logging
148      * @return a response
149      */
150     @Override
151     public ResponseEntity<Object> getStatusOfAllPolicies(UUID requestId) {
152
153         try {
154             return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
155                 .body(provider.getPolicyStatus());
156
157         } catch (PfModelRuntimeException e) {
158             logger.warn(GET_DEPLOYMENTS_FAILED, e);
159             return addLoggingHeaders(
160                 addVersionControlHeaders(ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode())),
161                 requestId).body(e.getErrorResponse().getErrorMessage());
162         }
163     }
164
165     /**
166      * Queries status of policies in a specific PdpGroup. if regex is not null or empty, the function will only return
167      * policies that match regex
168      *
169      * @param pdpGroupName name of the PdpGroup
170      * @param requestId request ID used in ONAP logging
171      * @param regex regex for a policy name
172      * @return a response
173      */
174     @Override
175     public ResponseEntity<Object> getStatusOfPoliciesByGroup(
176         String pdpGroupName,
177         UUID requestId,
178         String regex) {
179
180         try {
181             final Collection<PdpPolicyStatus> result;
182             if (regex == null) {
183                 result = provider.getPolicyStatus(pdpGroupName);
184             } else if (regex.isBlank()) {
185                 return makeRegexNotFoundResponse(requestId);
186             } else {
187                 result = provider.getPolicyStatusByRegex(pdpGroupName, regex);
188             }
189             return makeListOrNotFoundResponse(requestId, result);
190
191         } catch (PfModelRuntimeException e) {
192             logger.warn(GET_DEPLOYMENTS_FAILED, e);
193             return addLoggingHeaders(
194                 addVersionControlHeaders(ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode())),
195                 requestId).body(e.getErrorResponse().getErrorMessage());
196         } catch (PatternSyntaxException e) {
197             logger.warn(GET_DEPLOYMENTS_FAILED, e);
198             return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.status(HttpStatus.BAD_REQUEST)), requestId)
199                 .body(e.getMessage());
200         }
201     }
202
203     /**
204      * Queries status of all versions of a specific policy in a specific PdpGroup.
205      *
206      * @param pdpGroupName name of the PdpGroup
207      * @param policyName name of the Policy
208      * @param requestId request ID used in ONAP logging
209      * @return a response
210      */
211     @Override
212     public ResponseEntity<Object> getStatusOfPolicies(
213         String pdpGroupName,
214         String policyName,
215         UUID requestId) {
216
217         try {
218             Collection<PdpPolicyStatus> result =
219                 provider.getPolicyStatus(pdpGroupName, new ToscaConceptIdentifierOptVersion(policyName, null));
220             if (result.isEmpty()) {
221                 return makeNotFoundResponse(requestId);
222
223             } else {
224                 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
225                     .body(result);
226             }
227
228         } catch (PfModelRuntimeException e) {
229             logger.warn(GET_DEPLOYMENTS_FAILED, e);
230             return addLoggingHeaders(
231                 addVersionControlHeaders(ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode())),
232                 requestId).body(e.getErrorResponse().getErrorMessage());
233         }
234     }
235
236
237     /**
238      * Queries status of a specific version of a specific policy in a specific PdpGroup.
239      *
240      * @param pdpGroupName name of the PdpGroup
241      * @param policyName name of the Policy
242      * @param policyVersion version of the Policy
243      * @param requestId request ID used in ONAP logging
244      * @return a response
245      */
246
247     @Override
248     public ResponseEntity<Object> getStatusOfPolicy(
249             String pdpGroupName,
250             String policyName,
251             String policyVersion,
252             UUID requestId) {
253
254         try {
255             Collection<PdpPolicyStatus> result = provider.getPolicyStatus(pdpGroupName,
256                 new ToscaConceptIdentifierOptVersion(policyName, policyVersion));
257             if (result.isEmpty()) {
258                 return makeNotFoundResponse(requestId);
259
260             } else {
261                 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
262                     .body(result.iterator().next());
263             }
264
265         } catch (PfModelRuntimeException e) {
266             logger.warn(GET_DEPLOYMENTS_FAILED, e);
267             return addLoggingHeaders(
268                 addVersionControlHeaders(ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode())),
269                 requestId).body(e.getErrorResponse().getErrorMessage());
270         }
271     }
272
273     /**
274      * Makes a "not found" response.
275      *
276      * @param requestId request ID
277      * @return a "not found" response
278      */
279     private ResponseEntity<Object> makeNotFoundResponse(final UUID requestId) {
280         return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.status(HttpStatus.NOT_FOUND)), requestId)
281                         .build();
282     }
283
284     private ResponseEntity<Object> makeRegexNotFoundResponse(UUID requestId) {
285         logger.warn(GET_DEPLOYMENTS_FAILED + EMPTY_REGEX_WARNING);
286         return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.status(HttpStatus.BAD_REQUEST)),
287             requestId).body(EMPTY_REGEX_ERROR_MESSAGE);
288     }
289
290     private ResponseEntity<Object> makeListOrNotFoundResponse(UUID requestId, Collection<?> result) {
291         if (result.isEmpty()) {
292             return makeNotFoundResponse(requestId);
293         } else {
294             return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId).body(result);
295         }
296     }
297 }