Fix trailing space in Info.yaml 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-2024 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.pap.concepts.PolicyStatus;
30 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
31 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.context.annotation.Profile;
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 @Profile("default")
46 public class PolicyStatusControllerV1 extends PapRestControllerV1 implements PolicyStatusControllerV1Api {
47     private static final String EMPTY_REGEX_ERROR_MESSAGE = "An empty string passed as a regex is not allowed";
48     private static final String EMPTY_REGEX_WARNING = ". Empty string passed as Regex.";
49     private static final String GET_DEPLOYMENTS_FAILED = "get deployments failed";
50
51     private static final Logger logger = LoggerFactory.getLogger(PolicyStatusControllerV1.class);
52
53     private final PolicyStatusProvider provider;
54
55     /**
56      * Queries status of all deployed policies. If regex is not null or empty, the function will only return
57      * policies that match regex
58      *
59      * @param requestId request ID used in ONAP logging
60      * @param regex     regex for a policy name
61      * @return a response
62      */
63     @Override
64     public ResponseEntity<Object> queryAllDeployedPolicies(UUID requestId, String regex) {
65         try {
66             final Collection<PolicyStatus> result;
67             if (regex == null) {
68                 result = provider.getStatus();
69             } else if (regex.isBlank()) {
70                 return makeRegexNotFoundResponse(requestId);
71             } else {
72                 result = provider.getByRegex(regex);
73             }
74             return makeListOrNotFoundResponse(requestId, result);
75
76         } catch (PatternSyntaxException e) {
77             logger.warn(GET_DEPLOYMENTS_FAILED, e);
78             return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.status(HttpStatus.BAD_REQUEST)), requestId)
79                 .body(e.getMessage());
80         }
81     }
82
83     /**
84      * Queries status of specific deployed policies.
85      *
86      * @param requestId request ID used in ONAP logging
87      * @return a response
88      */
89     @Override
90     public ResponseEntity<Object> queryDeployedPolicies(String name, UUID requestId) {
91         Collection<PolicyStatus> result = provider.getStatus(new ToscaConceptIdentifierOptVersion(name, null));
92         if (result.isEmpty()) {
93             return makeNotFoundResponse(requestId);
94
95         } else {
96             return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId).body(result);
97         }
98     }
99
100
101     /**
102      * Queries status of a specific deployed policy.
103      *
104      * @param requestId request ID used in ONAP logging
105      * @return a response
106      */
107     @Override
108     public ResponseEntity<Object> queryDeployedPolicy(String name, String version, UUID requestId) {
109         Collection<PolicyStatus> result = provider.getStatus(new ToscaConceptIdentifierOptVersion(name, version));
110         if (result.isEmpty()) {
111             return makeNotFoundResponse(requestId);
112
113         } else {
114             return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
115                 .body(result.iterator().next());
116         }
117     }
118
119
120     /**
121      * Queries status of all policies.
122      *
123      * @param requestId request ID used in ONAP logging
124      * @return a response
125      */
126     @Override
127     public ResponseEntity<Object> getStatusOfAllPolicies(UUID requestId) {
128         return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
129             .body(provider.getPolicyStatus());
130     }
131
132     /**
133      * Queries status of policies in a specific PdpGroup. if regex is not null or empty, the function will only return
134      * policies that match regex
135      *
136      * @param pdpGroupName name of the PdpGroup
137      * @param requestId    request ID used in ONAP logging
138      * @param regex        regex for a policy name
139      * @return a response
140      */
141     @Override
142     public ResponseEntity<Object> getStatusOfPoliciesByGroup(String pdpGroupName, UUID requestId, String regex) {
143         try {
144             final Collection<PdpPolicyStatus> result;
145             if (regex == null) {
146                 result = provider.getPolicyStatus(pdpGroupName);
147             } else if (regex.isBlank()) {
148                 return makeRegexNotFoundResponse(requestId);
149             } else {
150                 result = provider.getPolicyStatusByRegex(pdpGroupName, regex);
151             }
152             return makeListOrNotFoundResponse(requestId, result);
153
154         } catch (PatternSyntaxException e) {
155             logger.warn(GET_DEPLOYMENTS_FAILED, e);
156             return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.status(HttpStatus.BAD_REQUEST)), requestId)
157                 .body(e.getMessage());
158         }
159     }
160
161     /**
162      * Queries status of all versions of a specific policy in a specific PdpGroup.
163      *
164      * @param pdpGroupName name of the PdpGroup
165      * @param policyName   name of the Policy
166      * @param requestId    request ID used in ONAP logging
167      * @return a response
168      */
169     @Override
170     public ResponseEntity<Object> getStatusOfPolicies(String pdpGroupName, String policyName, UUID requestId) {
171         Collection<PdpPolicyStatus> result =
172             provider.getPolicyStatus(pdpGroupName, new ToscaConceptIdentifierOptVersion(policyName, null));
173         if (result.isEmpty()) {
174             return makeNotFoundResponse(requestId);
175         } else {
176             return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId).body(result);
177         }
178     }
179
180
181     /**
182      * Queries status of a specific version of a specific policy in a specific PdpGroup.
183      *
184      * @param pdpGroupName  name of the PdpGroup
185      * @param policyName    name of the Policy
186      * @param policyVersion version of the Policy
187      * @param requestId     request ID used in ONAP logging
188      * @return a response
189      */
190
191     @Override
192     public ResponseEntity<Object> getStatusOfPolicy(String pdpGroupName, String policyName, String policyVersion,
193                                                     UUID requestId) {
194         Collection<PdpPolicyStatus> result = provider.getPolicyStatus(pdpGroupName,
195             new ToscaConceptIdentifierOptVersion(policyName, policyVersion));
196         if (result.isEmpty()) {
197             return makeNotFoundResponse(requestId);
198
199         } else {
200             return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
201                 .body(result.iterator().next());
202         }
203     }
204
205     /**
206      * Makes a "not found" response.
207      *
208      * @param requestId request ID
209      * @return a "not found" response
210      */
211     private ResponseEntity<Object> makeNotFoundResponse(final UUID requestId) {
212         return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.status(HttpStatus.NOT_FOUND)), requestId)
213             .build();
214     }
215
216     private ResponseEntity<Object> makeRegexNotFoundResponse(UUID requestId) {
217         logger.warn(GET_DEPLOYMENTS_FAILED + EMPTY_REGEX_WARNING);
218         return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.status(HttpStatus.BAD_REQUEST)),
219             requestId).body(EMPTY_REGEX_ERROR_MESSAGE);
220     }
221
222     private ResponseEntity<Object> makeListOrNotFoundResponse(UUID requestId, Collection<?> result) {
223         if (result.isEmpty()) {
224             return makeNotFoundResponse(requestId);
225         } else {
226             return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId).body(result);
227         }
228     }
229 }