2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2021 Nordix Foundation.
7 * Modifications Copyright (C) 2021 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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.policy.pap.main.rest;
25 import com.google.re2j.PatternSyntaxException;
26 import io.swagger.annotations.ApiOperation;
27 import io.swagger.annotations.ApiParam;
28 import io.swagger.annotations.ApiResponse;
29 import io.swagger.annotations.ApiResponses;
30 import io.swagger.annotations.Authorization;
31 import io.swagger.annotations.Extension;
32 import io.swagger.annotations.ExtensionProperty;
33 import io.swagger.annotations.ResponseHeader;
34 import java.util.Collection;
35 import java.util.UUID;
36 import lombok.RequiredArgsConstructor;
37 import org.onap.policy.models.base.PfModelException;
38 import org.onap.policy.models.base.PfModelRuntimeException;
39 import org.onap.policy.models.pap.concepts.PolicyStatus;
40 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.http.HttpStatus;
45 import org.springframework.http.ResponseEntity;
46 import org.springframework.web.bind.annotation.GetMapping;
47 import org.springframework.web.bind.annotation.PathVariable;
48 import org.springframework.web.bind.annotation.RequestHeader;
49 import org.springframework.web.bind.annotation.RequestMapping;
50 import org.springframework.web.bind.annotation.RequestParam;
51 import org.springframework.web.bind.annotation.RestController;
54 * Class to provide REST end points for PAP component to retrieve the status of deployed
58 @RequestMapping(path = "/policy/pap/v1")
59 @RequiredArgsConstructor
60 public class PolicyStatusControllerV1 extends PapRestControllerV1 {
61 private static final String EMPTY_REGEX_ERROR_MESSAGE = "An empty string passed as a regex is not allowed";
62 private static final String EMPTY_REGEX_WARNING = ". Empty string passed as Regex.";
63 private static final String GET_DEPLOYMENTS_FAILED = "get deployments failed";
65 private static final Logger logger = LoggerFactory.getLogger(PolicyStatusControllerV1.class);
67 private final PolicyStatusProvider provider;
70 * Queries status of all deployed policies. If regex is not null or empty, the function will only return
71 * policies that match regex
73 * @param requestId request ID used in ONAP logging
74 * @param regex regex for a policy name
78 @GetMapping("policies/deployed")
79 @ApiOperation(value = "Queries status of all deployed policies",
80 notes = "Queries status of all deployed policies, returning success and failure counts of the PDPs",
81 responseContainer = "List", response = PolicyStatus.class,
82 tags = {"Policy Deployment Status"},
83 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
85 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
86 response = String.class),
87 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
88 response = String.class),
89 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
90 response = String.class),
91 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
92 response = UUID.class)},
94 @Extension(name = EXTENSION_NAME,
96 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
97 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
100 @ApiResponses(value = {
101 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
102 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
103 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
107 public ResponseEntity<Object> queryAllDeployedPolicies(
108 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
110 value = REQUEST_ID_NAME) final UUID requestId,
111 @ApiParam(value = "Regex for a policy name") @RequestParam(required = false, value = "regex") String regex) {
113 final Collection<PolicyStatus> result;
115 result = provider.getStatus();
116 } else if (regex.isBlank()) {
117 return makeRegexNotFoundResponse(requestId);
119 result = provider.getByRegex(regex);
121 return makeListOrNotFoundResponse(requestId, result);
123 } catch (PfModelException | PfModelRuntimeException e) {
124 logger.warn(GET_DEPLOYMENTS_FAILED, e);
125 return addLoggingHeaders(
126 addVersionControlHeaders(ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode())),
127 requestId).body(e.getErrorResponse().getErrorMessage());
128 } catch (PatternSyntaxException e) {
129 logger.warn(GET_DEPLOYMENTS_FAILED, e);
130 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.status(HttpStatus.BAD_REQUEST)), requestId)
131 .body(e.getMessage());
136 * Queries status of specific deployed policies.
138 * @param requestId request ID used in ONAP logging
142 @GetMapping("policies/deployed/{name}")
143 @ApiOperation(value = "Queries status of specific deployed policies",
144 notes = "Queries status of specific deployed policies, returning success and failure counts of the PDPs",
145 responseContainer = "List", response = PolicyStatus.class,
146 tags = {"Policy Deployment Status"},
147 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
149 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
150 response = String.class),
151 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
152 response = String.class),
153 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
154 response = String.class),
155 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
156 response = UUID.class)},
158 @Extension(name = EXTENSION_NAME,
160 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
161 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
164 @ApiResponses(value = {
165 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
166 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
167 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
171 public ResponseEntity<Object> queryDeployedPolicies(
172 @ApiParam(value = "Policy Id") @PathVariable("name") String name,
173 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
175 value = REQUEST_ID_NAME) final UUID requestId) {
178 Collection<PolicyStatus> result = provider.getStatus(new ToscaConceptIdentifierOptVersion(name, null));
179 if (result.isEmpty()) {
180 return makeNotFoundResponse(requestId);
183 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId).body(result);
186 } catch (PfModelException | PfModelRuntimeException e) {
187 logger.warn(GET_DEPLOYMENTS_FAILED, e);
188 return addLoggingHeaders(
189 addVersionControlHeaders(ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode())),
190 requestId).body(e.getErrorResponse().getErrorMessage());
196 * Queries status of a specific deployed policy.
198 * @param requestId request ID used in ONAP logging
202 @GetMapping("policies/deployed/{name}/{version}")
203 @ApiOperation(value = "Queries status of a specific deployed policy",
204 notes = "Queries status of a specific deployed policy, returning success and failure counts of the PDPs",
205 response = PolicyStatus.class,
206 tags = {"Policy Deployment Status"},
207 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
209 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
210 response = String.class),
211 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
212 response = String.class),
213 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
214 response = String.class),
215 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
216 response = UUID.class)},
218 @Extension(name = EXTENSION_NAME,
220 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
221 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
224 @ApiResponses(value = {
225 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
226 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
227 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
231 public ResponseEntity<Object> queryDeployedPolicy(@ApiParam(value = "Policy Id") @PathVariable("name") String name,
232 @ApiParam(value = "Policy Version") @PathVariable("version") String version,
233 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
235 value = REQUEST_ID_NAME) final UUID requestId) {
238 Collection<PolicyStatus> result = provider.getStatus(new ToscaConceptIdentifierOptVersion(name, version));
239 if (result.isEmpty()) {
240 return makeNotFoundResponse(requestId);
243 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
244 .body(result.iterator().next());
247 } catch (PfModelException | PfModelRuntimeException e) {
248 logger.warn(GET_DEPLOYMENTS_FAILED, e);
249 return addLoggingHeaders(
250 addVersionControlHeaders(ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode())),
251 requestId).body(e.getErrorResponse().getErrorMessage());
257 * Queries status of all policies.
259 * @param requestId request ID used in ONAP logging
263 @GetMapping("policies/status")
264 @ApiOperation(value = "Queries status of policies in all PdpGroups",
265 notes = "Queries status of policies in all PdpGroups, "
266 + "returning status of policies in all the PDPs belonging to all PdpGroups",
267 responseContainer = "List", response = PdpPolicyStatus.class,
268 tags = {"Policy Status"},
269 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
271 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
272 response = String.class),
273 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
274 response = String.class),
275 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
276 response = String.class),
277 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
278 response = UUID.class)},
280 @Extension(name = EXTENSION_NAME,
282 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
283 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
286 @ApiResponses(value = {
287 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
288 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
289 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
293 public ResponseEntity<Object> getStatusOfAllPolicies(
294 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
296 value = REQUEST_ID_NAME) final UUID requestId) {
299 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
300 .body(provider.getPolicyStatus());
302 } catch (PfModelException | PfModelRuntimeException e) {
303 logger.warn(GET_DEPLOYMENTS_FAILED, e);
304 return addLoggingHeaders(
305 addVersionControlHeaders(ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode())),
306 requestId).body(e.getErrorResponse().getErrorMessage());
311 * Queries status of policies in a specific PdpGroup. if regex is not null or empty, the function will only return
312 * policies that match regex
314 * @param pdpGroupName name of the PdpGroup
315 * @param requestId request ID used in ONAP logging
316 * @param regex regex for a policy name
320 @GetMapping("policies/status/{pdpGroupName}")
321 @ApiOperation(value = "Queries status of policies in a specific PdpGroup",
322 notes = "Queries status of policies in a specific PdpGroup, "
323 + "returning status of policies in all the PDPs belonging to the PdpGroup",
324 responseContainer = "List", response = PdpPolicyStatus.class,
325 tags = {"Policy Status"},
326 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
328 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
329 response = String.class),
330 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
331 response = String.class),
332 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
333 response = String.class),
334 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
335 response = UUID.class)},
337 @Extension(name = EXTENSION_NAME,
339 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
340 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
343 @ApiResponses(value = {
344 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
345 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
346 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
350 public ResponseEntity<Object> getStatusOfPoliciesByGroup(
351 @ApiParam(value = "PDP Group Name") @PathVariable("pdpGroupName") String pdpGroupName,
352 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
354 value = REQUEST_ID_NAME) final UUID requestId,
355 @ApiParam(value = "Regex for a policy name") @RequestParam(required = false, value = "regex") String regex) {
358 final Collection<PdpPolicyStatus> result;
360 result = provider.getPolicyStatus(pdpGroupName);
361 } else if (regex.isBlank()) {
362 return makeRegexNotFoundResponse(requestId);
364 result = provider.getPolicyStatusByRegex(pdpGroupName, regex);
366 return makeListOrNotFoundResponse(requestId, result);
368 } catch (PfModelException | PfModelRuntimeException e) {
369 logger.warn(GET_DEPLOYMENTS_FAILED, e);
370 return addLoggingHeaders(
371 addVersionControlHeaders(ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode())),
372 requestId).body(e.getErrorResponse().getErrorMessage());
373 } catch (PatternSyntaxException e) {
374 logger.warn(GET_DEPLOYMENTS_FAILED, e);
375 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.status(HttpStatus.BAD_REQUEST)), requestId)
376 .body(e.getMessage());
381 * Queries status of all versions of a specific policy in a specific PdpGroup.
383 * @param pdpGroupName name of the PdpGroup
384 * @param policyName name of the Policy
385 * @param requestId request ID used in ONAP logging
389 @GetMapping("policies/status/{pdpGroupName}/{policyName}")
390 @ApiOperation(value = "Queries status of all versions of a specific policy in a specific PdpGroup",
391 notes = "Queries status of all versions of a specific policy in a specific PdpGroup,"
392 + " returning status of all versions of the policy in the PDPs belonging to the PdpGroup",
393 responseContainer = "List", response = PdpPolicyStatus.class,
394 tags = {"Policy Administration (PAP) API"},
395 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
397 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
398 response = String.class),
399 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
400 response = String.class),
401 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
402 response = String.class),
403 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
404 response = UUID.class)},
406 @Extension(name = EXTENSION_NAME,
408 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
409 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
412 @ApiResponses(value = {
413 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
414 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
415 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
419 public ResponseEntity<Object> getStatusOfPolicies(
420 @ApiParam(value = "PDP Group Name") @PathVariable("pdpGroupName") String pdpGroupName,
421 @ApiParam(value = "Policy Id") @PathVariable("policyName") String policyName,
422 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
424 value = REQUEST_ID_NAME) final UUID requestId) {
427 Collection<PdpPolicyStatus> result =
428 provider.getPolicyStatus(pdpGroupName, new ToscaConceptIdentifierOptVersion(policyName, null));
429 if (result.isEmpty()) {
430 return makeNotFoundResponse(requestId);
433 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
437 } catch (PfModelException | PfModelRuntimeException e) {
438 logger.warn(GET_DEPLOYMENTS_FAILED, e);
439 return addLoggingHeaders(
440 addVersionControlHeaders(ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode())),
441 requestId).body(e.getErrorResponse().getErrorMessage());
447 * Queries status of a specific version of a specific policy in a specific PdpGroup.
449 * @param pdpGroupName name of the PdpGroup
450 * @param policyName name of the Policy
451 * @param policyVersion version of the Policy
452 * @param requestId request ID used in ONAP logging
456 @GetMapping("policies/status/{pdpGroupName}/{policyName}/{policyVersion}")
457 @ApiOperation(value = "Queries status of a specific version of a specific policy in a specific PdpGroup",
458 notes = "Queries status of a specific version of a specific policy in a specific PdpGroup,"
459 + " returning status of the policy in the PDPs belonging to the PdpGroup",
460 response = PdpPolicyStatus.class,
461 tags = {"Policy Administration (PAP) API"},
462 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
464 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
465 response = String.class),
466 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
467 response = String.class),
468 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
469 response = String.class),
470 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
471 response = UUID.class)},
473 @Extension(name = EXTENSION_NAME,
475 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
476 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
479 @ApiResponses(value = {
480 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
481 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
482 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
486 public ResponseEntity<Object> getStatusOfPolicy(
487 @ApiParam(value = "PDP Group Name") @PathVariable("pdpGroupName") String pdpGroupName,
488 @ApiParam(value = "Policy Id") @PathVariable("policyName") String policyName,
489 @ApiParam(value = "Policy Version") @PathVariable("policyVersion") String policyVersion,
490 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
492 value = REQUEST_ID_NAME) final UUID requestId) {
495 Collection<PdpPolicyStatus> result = provider.getPolicyStatus(pdpGroupName,
496 new ToscaConceptIdentifierOptVersion(policyName, policyVersion));
497 if (result.isEmpty()) {
498 return makeNotFoundResponse(requestId);
501 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
502 .body(result.iterator().next());
505 } catch (PfModelException | PfModelRuntimeException e) {
506 logger.warn(GET_DEPLOYMENTS_FAILED, e);
507 return addLoggingHeaders(
508 addVersionControlHeaders(ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode())),
509 requestId).body(e.getErrorResponse().getErrorMessage());
514 * Makes a "not found" response.
516 * @param requestId request ID
517 * @return a "not found" response
519 private ResponseEntity<Object> makeNotFoundResponse(final UUID requestId) {
520 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.status(HttpStatus.NOT_FOUND)), requestId)
524 private ResponseEntity<Object> makeRegexNotFoundResponse(UUID requestId) {
525 logger.warn(GET_DEPLOYMENTS_FAILED + EMPTY_REGEX_WARNING);
526 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.status(HttpStatus.BAD_REQUEST)),
527 requestId).body(EMPTY_REGEX_ERROR_MESSAGE);
530 private ResponseEntity<Object> makeListOrNotFoundResponse(UUID requestId, Collection<?> result) {
531 if (result.isEmpty()) {
532 return makeNotFoundResponse(requestId);
534 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId).body(result);