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 io.swagger.annotations.ApiOperation;
26 import io.swagger.annotations.ApiParam;
27 import io.swagger.annotations.ApiResponse;
28 import io.swagger.annotations.ApiResponses;
29 import io.swagger.annotations.Authorization;
30 import io.swagger.annotations.Extension;
31 import io.swagger.annotations.ExtensionProperty;
32 import io.swagger.annotations.ResponseHeader;
33 import java.util.Collection;
34 import java.util.UUID;
35 import javax.ws.rs.GET;
36 import javax.ws.rs.HeaderParam;
37 import javax.ws.rs.Path;
38 import javax.ws.rs.PathParam;
39 import javax.ws.rs.core.Response;
40 import org.onap.policy.models.base.PfModelException;
41 import org.onap.policy.models.base.PfModelRuntimeException;
42 import org.onap.policy.models.pap.concepts.PolicyStatus;
43 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 * Class to provide REST end points for PAP component to retrieve the status of deployed
52 public class PolicyStatusControllerV1 extends PapRestControllerV1 {
53 private static final String GET_DEPLOYMENTS_FAILED = "get deployments failed";
55 private static final Logger logger = LoggerFactory.getLogger(PolicyStatusControllerV1.class);
57 private final PolicyStatusProvider provider = new PolicyStatusProvider();
60 * Queries status of all deployed policies.
62 * @param requestId request ID used in ONAP logging
67 @Path("policies/deployed")
68 @ApiOperation(value = "Queries status of all deployed policies",
69 notes = "Queries status of all deployed policies, returning success and failure counts of the PDPs",
70 responseContainer = "List", response = PolicyStatus.class,
71 tags = {"Policy Administration (PAP) API"},
72 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
74 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
75 response = String.class),
76 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
77 response = String.class),
78 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
79 response = String.class),
80 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
81 response = UUID.class)},
82 extensions = {@Extension(name = EXTENSION_NAME,
83 properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
84 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
85 @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
86 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
87 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
90 public Response queryAllDeployedPolicies(
91 @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId) {
94 return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
95 .entity(provider.getStatus()).build();
97 } catch (PfModelException | PfModelRuntimeException e) {
98 logger.warn(GET_DEPLOYMENTS_FAILED, e);
99 return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())),
100 requestId).entity(e.getErrorResponse().getErrorMessage()).build();
106 * Queries status of specific deployed policies.
108 * @param requestId request ID used in ONAP logging
113 @Path("policies/deployed/{name}")
114 @ApiOperation(value = "Queries status of specific deployed policies",
115 notes = "Queries status of specific deployed policies, returning success and failure counts of the PDPs",
116 responseContainer = "List", response = PolicyStatus.class,
117 tags = {"Policy Administration (PAP) API"},
118 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
120 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
121 response = String.class),
122 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
123 response = String.class),
124 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
125 response = String.class),
126 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
127 response = UUID.class)},
128 extensions = {@Extension(name = EXTENSION_NAME,
129 properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
130 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
131 @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
132 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
133 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
136 public Response queryDeployedPolicies(
137 @ApiParam(value = "Policy Id", required = true) @PathParam("name") String name,
138 @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId) {
141 Collection<PolicyStatus> result = provider.getStatus(new ToscaConceptIdentifierOptVersion(name, null));
142 if (result.isEmpty()) {
143 return makeNotFoundResponse(requestId);
146 return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
147 .entity(result).build();
150 } catch (PfModelException | PfModelRuntimeException e) {
151 logger.warn(GET_DEPLOYMENTS_FAILED, e);
152 return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())),
153 requestId).entity(e.getErrorResponse().getErrorMessage()).build();
159 * Queries status of a specific deployed policy.
161 * @param requestId request ID used in ONAP logging
166 @Path("policies/deployed/{name}/{version}")
167 @ApiOperation(value = "Queries status of a specific deployed policy",
168 notes = "Queries status of a specific deployed policy, returning success and failure counts of the PDPs",
169 response = PolicyStatus.class,
170 tags = {"Policy Administration (PAP) API"},
171 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
173 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
174 response = String.class),
175 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
176 response = String.class),
177 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
178 response = String.class),
179 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
180 response = UUID.class)},
181 extensions = {@Extension(name = EXTENSION_NAME,
182 properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
183 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
184 @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
185 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
186 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
189 public Response queryDeployedPolicy(@ApiParam(value = "Policy Id", required = true) @PathParam("name") String name,
190 @ApiParam(value = "Policy Version", required = true) @PathParam("version") String version,
191 @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId) {
194 Collection<PolicyStatus> result = provider.getStatus(new ToscaConceptIdentifierOptVersion(name, version));
195 if (result.isEmpty()) {
196 return makeNotFoundResponse(requestId);
199 return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
200 .entity(result.iterator().next()).build();
203 } catch (PfModelException | PfModelRuntimeException e) {
204 logger.warn(GET_DEPLOYMENTS_FAILED, e);
205 return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())),
206 requestId).entity(e.getErrorResponse().getErrorMessage()).build();
212 * Queries status of all policies.
214 * @param requestId request ID used in ONAP logging
219 @Path("policies/status")
220 @ApiOperation(value = "Queries status of policies in all PdpGroups",
221 notes = "Queries status of policies in all PdpGroups, "
222 + "returning status of policies in all the PDPs belonging to all PdpGroups",
223 responseContainer = "List", response = PdpPolicyStatus.class,
224 tags = {"Policy Administration (PAP) API"},
225 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
227 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
228 response = String.class),
229 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
230 response = String.class),
231 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
232 response = String.class),
233 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
234 response = UUID.class)},
235 extensions = {@Extension(name = EXTENSION_NAME,
236 properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
237 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
238 @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
239 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
240 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
243 public Response getStatusOfAllPolicies(
244 @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId) {
247 return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
248 .entity(provider.getPolicyStatus()).build();
250 } catch (PfModelException | PfModelRuntimeException e) {
251 logger.warn(GET_DEPLOYMENTS_FAILED, e);
252 return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())),
253 requestId).entity(e.getErrorResponse().getErrorMessage()).build();
258 * Queries status of policies in a specific PdpGroup.
260 * @param pdpGroupName name of the PdpGroup
261 * @param requestId request ID used in ONAP logging
266 @Path("policies/status/{pdpGroupName}")
267 @ApiOperation(value = "Queries status of policies in a specific PdpGroup",
268 notes = "Queries status of policies in a specific PdpGroup, "
269 + "returning status of policies in all the PDPs belonging to the PdpGroup",
270 responseContainer = "List", response = PdpPolicyStatus.class,
271 tags = {"Policy Administration (PAP) API"},
272 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
274 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
275 response = String.class),
276 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
277 response = String.class),
278 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
279 response = String.class),
280 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
281 response = UUID.class)},
282 extensions = {@Extension(name = EXTENSION_NAME,
283 properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
284 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
285 @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
286 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
287 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
290 public Response getStatusOfPoliciesByGroup(
291 @ApiParam(value = "PDP Group Name", required = true) @PathParam("pdpGroupName") String pdpGroupName,
292 @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId) {
295 Collection<PdpPolicyStatus> result = provider.getPolicyStatus(pdpGroupName);
296 if (result.isEmpty()) {
297 return makeNotFoundResponse(requestId);
300 return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
301 .entity(result).build();
304 } catch (PfModelException | PfModelRuntimeException e) {
305 logger.warn(GET_DEPLOYMENTS_FAILED, e);
306 return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())),
307 requestId).entity(e.getErrorResponse().getErrorMessage()).build();
312 * Queries status of all versions of a specific policy in a specific PdpGroup.
314 * @param pdpGroupName name of the PdpGroup
315 * @param policyName name of the Policy
316 * @param requestId request ID used in ONAP logging
321 @Path("policies/status/{pdpGroupName}/{policyName}")
322 @ApiOperation(value = "Queries status of all versions of a specific policy in a specific PdpGroup",
323 notes = "Queries status of all versions of a specific policy in a specific PdpGroup,"
324 + " returning status of all versions of the policy in the PDPs belonging to the PdpGroup",
325 responseContainer = "List", response = PdpPolicyStatus.class,
326 tags = {"Policy Administration (PAP) API"},
327 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
329 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
330 response = String.class),
331 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
332 response = String.class),
333 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
334 response = String.class),
335 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
336 response = UUID.class)},
337 extensions = {@Extension(name = EXTENSION_NAME,
338 properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
339 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
340 @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
341 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
342 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
345 public Response getStatusOfPolicies(
346 @ApiParam(value = "PDP Group Name", required = true) @PathParam("pdpGroupName") String pdpGroupName,
347 @ApiParam(value = "Policy Id", required = true) @PathParam("policyName") String policyName,
348 @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId) {
351 Collection<PdpPolicyStatus> result =
352 provider.getPolicyStatus(pdpGroupName, new ToscaConceptIdentifierOptVersion(policyName, null));
353 if (result.isEmpty()) {
354 return makeNotFoundResponse(requestId);
357 return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
358 .entity(result).build();
361 } catch (PfModelException | PfModelRuntimeException e) {
362 logger.warn(GET_DEPLOYMENTS_FAILED, e);
363 return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())),
364 requestId).entity(e.getErrorResponse().getErrorMessage()).build();
370 * Queries status of a specific version of a specific policy in a specific PdpGroup.
372 * @param pdpGroupName name of the PdpGroup
373 * @param policyName name of the Policy
374 * @param policyVersion version of the Policy
375 * @param requestId request ID used in ONAP logging
380 @Path("policies/status/{pdpGroupName}/{policyName}/{policyVersion}")
381 @ApiOperation(value = "Queries status of a specific version of a specific policy in a specific PdpGroup",
382 notes = "Queries status of a specific version of a specific policy in a specific PdpGroup,"
383 + " returning status of the policy in the PDPs belonging to the PdpGroup",
384 response = PdpPolicyStatus.class,
385 tags = {"Policy Administration (PAP) API"},
386 authorizations = @Authorization(value = AUTHORIZATION_TYPE),
388 @ResponseHeader(name = VERSION_MINOR_NAME, description = VERSION_MINOR_DESCRIPTION,
389 response = String.class),
390 @ResponseHeader(name = VERSION_PATCH_NAME, description = VERSION_PATCH_DESCRIPTION,
391 response = String.class),
392 @ResponseHeader(name = VERSION_LATEST_NAME, description = VERSION_LATEST_DESCRIPTION,
393 response = String.class),
394 @ResponseHeader(name = REQUEST_ID_NAME, description = REQUEST_ID_HDR_DESCRIPTION,
395 response = UUID.class)},
396 extensions = {@Extension(name = EXTENSION_NAME,
397 properties = {@ExtensionProperty(name = API_VERSION_NAME, value = API_VERSION),
398 @ExtensionProperty(name = LAST_MOD_NAME, value = LAST_MOD_RELEASE)})})
399 @ApiResponses(value = {@ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE),
400 @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE),
401 @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE)})
404 public Response getStatusOfPolicy(
405 @ApiParam(value = "PDP Group Name", required = true) @PathParam("pdpGroupName") String pdpGroupName,
406 @ApiParam(value = "Policy Id", required = true) @PathParam("policyName") String policyName,
407 @ApiParam(value = "Policy Version", required = true) @PathParam("policyVersion") String policyVersion,
408 @HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId) {
411 Collection<PdpPolicyStatus> result = provider.getPolicyStatus(pdpGroupName,
412 new ToscaConceptIdentifierOptVersion(policyName, policyVersion));
413 if (result.isEmpty()) {
414 return makeNotFoundResponse(requestId);
417 return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
418 .entity(result.iterator().next()).build();
421 } catch (PfModelException | PfModelRuntimeException e) {
422 logger.warn(GET_DEPLOYMENTS_FAILED, e);
423 return addLoggingHeaders(addVersionControlHeaders(Response.status(e.getErrorResponse().getResponseCode())),
424 requestId).entity(e.getErrorResponse().getErrorMessage()).build();
429 * Makes a "not found" response.
431 * @param requestId request ID
432 * @return a "not found" response
434 private Response makeNotFoundResponse(final UUID requestId) {
435 return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.NOT_FOUND)), requestId)