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-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
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.PfModelRuntimeException;
38 import org.onap.policy.models.pap.concepts.PolicyStatus;
39 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.http.HttpStatus;
44 import org.springframework.http.ResponseEntity;
45 import org.springframework.web.bind.annotation.GetMapping;
46 import org.springframework.web.bind.annotation.PathVariable;
47 import org.springframework.web.bind.annotation.RequestHeader;
48 import org.springframework.web.bind.annotation.RequestMapping;
49 import org.springframework.web.bind.annotation.RequestParam;
50 import org.springframework.web.bind.annotation.RestController;
53 * Class to provide REST end points for PAP component to retrieve the status of deployed
57 @RequestMapping(path = "/policy/pap/v1")
58 @RequiredArgsConstructor
59 public class PolicyStatusControllerV1 extends PapRestControllerV1 {
60 private static final String EMPTY_REGEX_ERROR_MESSAGE = "An empty string passed as a regex is not allowed";
61 private static final String EMPTY_REGEX_WARNING = ". Empty string passed as Regex.";
62 private static final String GET_DEPLOYMENTS_FAILED = "get deployments failed";
64 private static final Logger logger = LoggerFactory.getLogger(PolicyStatusControllerV1.class);
66 private final PolicyStatusProvider provider;
69 * Queries status of all deployed policies. If regex is not null or empty, the function will only return
70 * policies that match regex
72 * @param requestId request ID used in ONAP logging
73 * @param regex regex for a policy name
77 @GetMapping("policies/deployed")
78 @ApiOperation(value = "Queries status of all deployed policies",
79 notes = "Queries status of all deployed policies, returning success and failure counts of the PDPs",
80 responseContainer = "List", response = PolicyStatus.class,
81 tags = {"Policy Deployment Status"},
82 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
84 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
85 response = String.class),
86 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
87 response = String.class),
88 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
89 response = String.class),
90 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
91 response = UUID.class)},
93 @Extension(name = EXTENSION_NAME,
95 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
96 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
99 @ApiResponses(value = {
100 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
101 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
102 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
106 public ResponseEntity<Object> queryAllDeployedPolicies(
107 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
109 value = REQUEST_ID_NAME) final UUID requestId,
110 @ApiParam(value = "Regex for a policy name") @RequestParam(required = false, value = "regex") String regex) {
112 final Collection<PolicyStatus> result;
114 result = provider.getStatus();
115 } else if (regex.isBlank()) {
116 return makeRegexNotFoundResponse(requestId);
118 result = provider.getByRegex(regex);
120 return makeListOrNotFoundResponse(requestId, result);
122 } catch (PfModelRuntimeException e) {
123 logger.warn(GET_DEPLOYMENTS_FAILED, e);
124 return addLoggingHeaders(
125 addVersionControlHeaders(ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode())),
126 requestId).body(e.getErrorResponse().getErrorMessage());
127 } catch (PatternSyntaxException e) {
128 logger.warn(GET_DEPLOYMENTS_FAILED, e);
129 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.status(HttpStatus.BAD_REQUEST)), requestId)
130 .body(e.getMessage());
135 * Queries status of specific deployed policies.
137 * @param requestId request ID used in ONAP logging
141 @GetMapping("policies/deployed/{name}")
142 @ApiOperation(value = "Queries status of specific deployed policies",
143 notes = "Queries status of specific deployed policies, returning success and failure counts of the PDPs",
144 responseContainer = "List", response = PolicyStatus.class,
145 tags = {"Policy Deployment Status"},
146 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
148 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
149 response = String.class),
150 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
151 response = String.class),
152 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
153 response = String.class),
154 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
155 response = UUID.class)},
157 @Extension(name = EXTENSION_NAME,
159 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
160 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
163 @ApiResponses(value = {
164 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
165 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
166 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
170 public ResponseEntity<Object> queryDeployedPolicies(
171 @ApiParam(value = "Policy Id") @PathVariable("name") String name,
172 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
174 value = REQUEST_ID_NAME) final UUID requestId) {
177 Collection<PolicyStatus> result = provider.getStatus(new ToscaConceptIdentifierOptVersion(name, null));
178 if (result.isEmpty()) {
179 return makeNotFoundResponse(requestId);
182 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId).body(result);
185 } catch (PfModelRuntimeException e) {
186 logger.warn(GET_DEPLOYMENTS_FAILED, e);
187 return addLoggingHeaders(
188 addVersionControlHeaders(ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode())),
189 requestId).body(e.getErrorResponse().getErrorMessage());
195 * Queries status of a specific deployed policy.
197 * @param requestId request ID used in ONAP logging
201 @GetMapping("policies/deployed/{name}/{version}")
202 @ApiOperation(value = "Queries status of a specific deployed policy",
203 notes = "Queries status of a specific deployed policy, returning success and failure counts of the PDPs",
204 response = PolicyStatus.class,
205 tags = {"Policy Deployment Status"},
206 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
208 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
209 response = String.class),
210 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
211 response = String.class),
212 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
213 response = String.class),
214 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
215 response = UUID.class)},
217 @Extension(name = EXTENSION_NAME,
219 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
220 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
223 @ApiResponses(value = {
224 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
225 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
226 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
230 public ResponseEntity<Object> queryDeployedPolicy(@ApiParam(value = "Policy Id") @PathVariable("name") String name,
231 @ApiParam(value = "Policy Version") @PathVariable("version") String version,
232 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
234 value = REQUEST_ID_NAME) final UUID requestId) {
237 Collection<PolicyStatus> result = provider.getStatus(new ToscaConceptIdentifierOptVersion(name, version));
238 if (result.isEmpty()) {
239 return makeNotFoundResponse(requestId);
242 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
243 .body(result.iterator().next());
246 } catch (PfModelRuntimeException e) {
247 logger.warn(GET_DEPLOYMENTS_FAILED, e);
248 return addLoggingHeaders(
249 addVersionControlHeaders(ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode())),
250 requestId).body(e.getErrorResponse().getErrorMessage());
256 * Queries status of all policies.
258 * @param requestId request ID used in ONAP logging
262 @GetMapping("policies/status")
263 @ApiOperation(value = "Queries status of policies in all PdpGroups",
264 notes = "Queries status of policies in all PdpGroups, "
265 + "returning status of policies in all the PDPs belonging to all PdpGroups",
266 responseContainer = "List", response = PdpPolicyStatus.class,
267 tags = {"Policy Status"},
268 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
270 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
271 response = String.class),
272 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
273 response = String.class),
274 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
275 response = String.class),
276 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
277 response = UUID.class)},
279 @Extension(name = EXTENSION_NAME,
281 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
282 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
285 @ApiResponses(value = {
286 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
287 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
288 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
292 public ResponseEntity<Object> getStatusOfAllPolicies(
293 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
295 value = REQUEST_ID_NAME) final UUID requestId) {
298 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
299 .body(provider.getPolicyStatus());
301 } catch (PfModelRuntimeException e) {
302 logger.warn(GET_DEPLOYMENTS_FAILED, e);
303 return addLoggingHeaders(
304 addVersionControlHeaders(ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode())),
305 requestId).body(e.getErrorResponse().getErrorMessage());
310 * Queries status of policies in a specific PdpGroup. if regex is not null or empty, the function will only return
311 * policies that match regex
313 * @param pdpGroupName name of the PdpGroup
314 * @param requestId request ID used in ONAP logging
315 * @param regex regex for a policy name
319 @GetMapping("policies/status/{pdpGroupName}")
320 @ApiOperation(value = "Queries status of policies in a specific PdpGroup",
321 notes = "Queries status of policies in a specific PdpGroup, "
322 + "returning status of policies in all the PDPs belonging to the PdpGroup",
323 responseContainer = "List", response = PdpPolicyStatus.class,
324 tags = {"Policy Status"},
325 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
327 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
328 response = String.class),
329 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
330 response = String.class),
331 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
332 response = String.class),
333 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
334 response = UUID.class)},
336 @Extension(name = EXTENSION_NAME,
338 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
339 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
342 @ApiResponses(value = {
343 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
344 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
345 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
349 public ResponseEntity<Object> getStatusOfPoliciesByGroup(
350 @ApiParam(value = "PDP Group Name") @PathVariable("pdpGroupName") String pdpGroupName,
351 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
353 value = REQUEST_ID_NAME) final UUID requestId,
354 @ApiParam(value = "Regex for a policy name") @RequestParam(required = false, value = "regex") String regex) {
357 final Collection<PdpPolicyStatus> result;
359 result = provider.getPolicyStatus(pdpGroupName);
360 } else if (regex.isBlank()) {
361 return makeRegexNotFoundResponse(requestId);
363 result = provider.getPolicyStatusByRegex(pdpGroupName, regex);
365 return makeListOrNotFoundResponse(requestId, result);
367 } catch (PfModelRuntimeException e) {
368 logger.warn(GET_DEPLOYMENTS_FAILED, e);
369 return addLoggingHeaders(
370 addVersionControlHeaders(ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode())),
371 requestId).body(e.getErrorResponse().getErrorMessage());
372 } catch (PatternSyntaxException e) {
373 logger.warn(GET_DEPLOYMENTS_FAILED, e);
374 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.status(HttpStatus.BAD_REQUEST)), requestId)
375 .body(e.getMessage());
380 * Queries status of all versions of a specific policy in a specific PdpGroup.
382 * @param pdpGroupName name of the PdpGroup
383 * @param policyName name of the Policy
384 * @param requestId request ID used in ONAP logging
388 @GetMapping("policies/status/{pdpGroupName}/{policyName}")
389 @ApiOperation(value = "Queries status of all versions of a specific policy in a specific PdpGroup",
390 notes = "Queries status of all versions of a specific policy in a specific PdpGroup,"
391 + " returning status of all versions of the policy in the PDPs belonging to the PdpGroup",
392 responseContainer = "List", response = PdpPolicyStatus.class,
393 tags = {"Policy Administration (PAP) API"},
394 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
396 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
397 response = String.class),
398 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
399 response = String.class),
400 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
401 response = String.class),
402 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
403 response = UUID.class)},
405 @Extension(name = EXTENSION_NAME,
407 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
408 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
411 @ApiResponses(value = {
412 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
413 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
414 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
418 public ResponseEntity<Object> getStatusOfPolicies(
419 @ApiParam(value = "PDP Group Name") @PathVariable("pdpGroupName") String pdpGroupName,
420 @ApiParam(value = "Policy Id") @PathVariable("policyName") String policyName,
421 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
423 value = REQUEST_ID_NAME) final UUID requestId) {
426 Collection<PdpPolicyStatus> result =
427 provider.getPolicyStatus(pdpGroupName, new ToscaConceptIdentifierOptVersion(policyName, null));
428 if (result.isEmpty()) {
429 return makeNotFoundResponse(requestId);
432 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
436 } catch (PfModelRuntimeException e) {
437 logger.warn(GET_DEPLOYMENTS_FAILED, e);
438 return addLoggingHeaders(
439 addVersionControlHeaders(ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode())),
440 requestId).body(e.getErrorResponse().getErrorMessage());
446 * Queries status of a specific version of a specific policy in a specific PdpGroup.
448 * @param pdpGroupName name of the PdpGroup
449 * @param policyName name of the Policy
450 * @param policyVersion version of the Policy
451 * @param requestId request ID used in ONAP logging
455 @GetMapping("policies/status/{pdpGroupName}/{policyName}/{policyVersion}")
456 @ApiOperation(value = "Queries status of a specific version of a specific policy in a specific PdpGroup",
457 notes = "Queries status of a specific version of a specific policy in a specific PdpGroup,"
458 + " returning status of the policy in the PDPs belonging to the PdpGroup",
459 response = PdpPolicyStatus.class,
460 tags = {"Policy Administration (PAP) API"},
461 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
463 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
464 response = String.class),
465 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
466 response = String.class),
467 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
468 response = String.class),
469 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
470 response = UUID.class)},
472 @Extension(name = EXTENSION_NAME,
474 @ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
475 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)
478 @ApiResponses(value = {
479 @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
480 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
481 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)
485 public ResponseEntity<Object> getStatusOfPolicy(
486 @ApiParam(value = "PDP Group Name") @PathVariable("pdpGroupName") String pdpGroupName,
487 @ApiParam(value = "Policy Id") @PathVariable("policyName") String policyName,
488 @ApiParam(value = "Policy Version") @PathVariable("policyVersion") String policyVersion,
489 @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) @RequestHeader(
491 value = REQUEST_ID_NAME) final UUID requestId) {
494 Collection<PdpPolicyStatus> result = provider.getPolicyStatus(pdpGroupName,
495 new ToscaConceptIdentifierOptVersion(policyName, policyVersion));
496 if (result.isEmpty()) {
497 return makeNotFoundResponse(requestId);
500 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId)
501 .body(result.iterator().next());
504 } catch (PfModelRuntimeException e) {
505 logger.warn(GET_DEPLOYMENTS_FAILED, e);
506 return addLoggingHeaders(
507 addVersionControlHeaders(ResponseEntity.status(e.getErrorResponse().getResponseCode().getStatusCode())),
508 requestId).body(e.getErrorResponse().getErrorMessage());
513 * Makes a "not found" response.
515 * @param requestId request ID
516 * @return a "not found" response
518 private ResponseEntity<Object> makeNotFoundResponse(final UUID requestId) {
519 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.status(HttpStatus.NOT_FOUND)), requestId)
523 private ResponseEntity<Object> makeRegexNotFoundResponse(UUID requestId) {
524 logger.warn(GET_DEPLOYMENTS_FAILED + EMPTY_REGEX_WARNING);
525 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.status(HttpStatus.BAD_REQUEST)),
526 requestId).body(EMPTY_REGEX_ERROR_MESSAGE);
529 private ResponseEntity<Object> makeListOrNotFoundResponse(UUID requestId, Collection<?> result) {
530 if (result.isEmpty()) {
531 return makeNotFoundResponse(requestId);
533 return addLoggingHeaders(addVersionControlHeaders(ResponseEntity.ok()), requestId).body(result);