2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
4 * Modifications Copyright (C) 2021-2023 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.pdpx.main.rest;
24 import com.att.research.xacml.api.Request;
25 import jakarta.servlet.http.HttpServletRequest;
26 import jakarta.ws.rs.Consumes;
27 import jakarta.ws.rs.GET;
28 import jakarta.ws.rs.HeaderParam;
29 import jakarta.ws.rs.POST;
30 import jakarta.ws.rs.Path;
31 import jakarta.ws.rs.Produces;
32 import jakarta.ws.rs.core.Context;
33 import jakarta.ws.rs.core.MediaType;
34 import jakarta.ws.rs.core.Response;
35 import jakarta.ws.rs.core.Response.ResponseBuilder;
36 import java.util.UUID;
37 import org.onap.policy.models.decisions.concepts.DecisionException;
38 import org.onap.policy.models.decisions.concepts.DecisionRequest;
39 import org.onap.policy.pdpx.main.rest.provider.DecisionProvider;
40 import org.onap.policy.pdpx.main.rest.provider.HealthCheckProvider;
41 import org.onap.policy.pdpx.main.rest.provider.StatisticsProvider;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * Class to provide xacml pdp REST services.
49 @Path("/policy/pdpx/v1")
50 @Produces({MediaType.APPLICATION_JSON, XacmlPdpRestController.APPLICATION_YAML})
51 @Consumes({MediaType.APPLICATION_JSON, XacmlPdpRestController.APPLICATION_YAML})
52 public class XacmlPdpRestController implements HealthcheckApi, StatisticsApi, DecisionApi, XacmlApi {
53 private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpRestController.class);
54 public static final String APPLICATION_YAML = "application/yaml";
55 public static final String APPLICATION_XACML_JSON = "application/xacml+json";
56 public static final String APPLICATION_XACML_XML = "application/xacml+xml";
57 @Context private HttpServletRequest request;
62 public Response healthcheck(
63 @HeaderParam("X-ONAP-RequestID") UUID requestId) {
64 return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
65 .entity(new HealthCheckProvider().performHealthCheck()).build();
71 public Response statistics(
72 @HeaderParam("X-ONAP-RequestID") UUID requestId) {
73 return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
74 .entity(new StatisticsProvider().fetchCurrentStatistics()).build();
80 public Response decision(DecisionRequest body, @HeaderParam("X-ONAP-RequestID") UUID requestId) {
81 return decision(body, requestId, request);
85 * Our decision entry point.
87 * @param body Should be a DecisionRequest object
88 * @param requestId Unique request id
89 * @return DecisionResponse or ErrorResponse object
91 private Response decision(DecisionRequest body,
92 @HeaderParam("X-ONAP-RequestID") UUID requestId,
93 @Context HttpServletRequest request) {
95 return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
96 .entity(new DecisionProvider().fetchDecision(body, request.getParameterMap())).build();
97 } catch (DecisionException e) {
98 LOGGER.error("Decision exception", e);
99 XacmlPdpStatisticsManager.getCurrent().updateErrorCount();
100 return addLoggingHeaders(
101 addVersionControlHeaders(Response.status((e.getErrorResponse().getResponseCode()))), requestId)
102 .entity(e.getErrorResponse()).build();
107 * Our native decision entry point.
109 * @param body Should be an Xacml Request object
110 * @param requestId Unique request id
111 * @return Xacml Response or ErrorResponse object
115 @Produces({XacmlPdpRestController.APPLICATION_XACML_JSON, XacmlPdpRestController.APPLICATION_XACML_XML})
116 @Consumes({XacmlPdpRestController.APPLICATION_XACML_JSON, XacmlPdpRestController.APPLICATION_XACML_XML})
118 public Response xacml(Request body,
119 @HeaderParam("X-ONAP-RequestID") UUID requestId) {
121 return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
122 .entity(new DecisionProvider().fetchNativeDecision(body)).build();
123 } catch (DecisionException e) {
124 LOGGER.error("Decision exception", e);
125 XacmlPdpStatisticsManager.getCurrent().updateErrorCount();
126 return addLoggingHeaders(
127 addVersionControlHeaders(Response.status((e.getErrorResponse().getResponseCode()))), requestId)
128 .entity(e.getErrorResponse()).build();
132 private ResponseBuilder addVersionControlHeaders(ResponseBuilder rb) {
133 return rb.header("X-MinorVersion", "0").header("X-PatchVersion", "0").header("X-LatestVersion", "1.0.0");
136 private ResponseBuilder addLoggingHeaders(ResponseBuilder rb, UUID requestId) {
137 if (requestId == null) {
138 // Generate a random uuid if client does not embed requestId in rest request
139 return rb.header("X-ONAP-RequestID", UUID.randomUUID());
141 return rb.header("X-ONAP-RequestID", requestId);