2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pap.main.rest;
23 import io.swagger.annotations.ApiOperation;
24 import io.swagger.annotations.ApiParam;
25 import io.swagger.annotations.ApiResponse;
26 import io.swagger.annotations.ApiResponses;
27 import io.swagger.annotations.Authorization;
28 import io.swagger.annotations.Extension;
29 import io.swagger.annotations.ExtensionProperty;
30 import io.swagger.annotations.ResponseHeader;
31 import java.util.List;
32 import java.util.Optional;
33 import java.util.UUID;
34 import javax.ws.rs.GET;
35 import javax.ws.rs.HeaderParam;
36 import javax.ws.rs.Path;
37 import javax.ws.rs.PathParam;
38 import javax.ws.rs.core.Response;
39 import org.onap.policy.common.utils.services.Registry;
40 import org.onap.policy.models.pap.concepts.PolicyStatus;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
42 import org.onap.policy.pap.main.PapConstants;
43 import org.onap.policy.pap.main.notification.PolicyNotifier;
46 * Class to provide REST end points for PAP component to retrieve the status of deployed
49 public class PolicyStatusControllerV1 extends PapRestControllerV1 {
50 private final PolicyNotifier notifier;
52 public PolicyStatusControllerV1() {
53 this.notifier = Registry.get(PapConstants.REG_POLICY_NOTIFIER, PolicyNotifier.class);
57 * Queries status of all deployed policies.
59 * @param requestId request ID used in ONAP logging
64 @Path("policies/deployed")
65 @ApiOperation(value = "Queries status of all deployed policies",
66 notes = "Queries status of all deployed policies, returning success and failure counts of the PDPs",
67 responseContainer = "List", response = PolicyStatus.class,
68 tags = {"Policy Administration (PAP) API"},
69 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
71 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
72 response = String.class),
73 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
74 response = String.class),
75 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
76 response = String.class),
77 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
78 response = UUID.class)},
79 extensions = {@Extension(name = EXTENSION_NAME,
80 properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
81 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
82 @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
83 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
84 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
87 public Response queryAllDeployedPolicies(
88 @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId) {
90 return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
91 .entity(notifier.getStatus()).build();
96 * Queries status of specific deployed policies.
98 * @param requestId request ID used in ONAP logging
103 @Path("policies/deployed/{name}")
104 @ApiOperation(value = "Queries status of specific deployed policies",
105 notes = "Queries status of specific deployed policies, returning success and failure counts of the PDPs",
106 responseContainer = "List", response = PolicyStatus.class,
107 tags = {"Policy Administration (PAP) API"},
108 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
110 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
111 response = String.class),
112 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
113 response = String.class),
114 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
115 response = String.class),
116 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
117 response = UUID.class)},
118 extensions = {@Extension(name = EXTENSION_NAME,
119 properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
120 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
121 @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
122 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
123 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
126 public Response queryDeployedPolicies(
127 @ApiParam(value = "Policy Id", required = true) @PathParam("name") String name,
128 @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId) {
130 List<PolicyStatus> result = notifier.getStatus(name);
131 if (result.isEmpty()) {
132 return makeNotFoundResponse(requestId);
135 return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
136 .entity(result).build();
142 * Queries status of a specific deployed policy.
144 * @param requestId request ID used in ONAP logging
149 @Path("policies/deployed/{name}/{version}")
150 @ApiOperation(value = "Queries status of a specific deployed policy",
151 notes = "Queries status of a specific deployed policy, returning success and failure counts of the PDPs",
152 response = PolicyStatus.class,
153 tags = {"Policy Administration (PAP) API"},
154 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
156 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
157 response = String.class),
158 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
159 response = String.class),
160 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
161 response = String.class),
162 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
163 response = UUID.class)},
164 extensions = {@Extension(name = EXTENSION_NAME,
165 properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
166 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
167 @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
168 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
169 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
172 public Response queryDeployedPolicy(@ApiParam(value = "Policy Id", required = true) @PathParam("name") String name,
173 @ApiParam(value = "Policy Version", required = true) @PathParam("version") String version,
174 @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId) {
176 ToscaPolicyIdentifier ident = new ToscaPolicyIdentifier(name, version);
177 Optional<PolicyStatus> result = notifier.getStatus(ident);
178 if (result.isPresent()) {
179 return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
180 .entity(result.get()).build();
183 return makeNotFoundResponse(requestId);
189 * Makes a "not found" response.
191 * @param requestId request ID
192 * @return a "not found" response
194 private Response makeNotFoundResponse(final UUID requestId) {
195 return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.NOT_FOUND)), requestId)