05f40cc53a7172b3e41b947b346ccdbb51470d80
[policy/xacml-pdp.git] / main / src / main / java / org / onap / policy / pdpx / main / rest / XacmlPdpRestController.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pdpx.main.rest;
23
24 import com.att.research.xacml.api.Request;
25 import java.util.UUID;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.ws.rs.Consumes;
28 import javax.ws.rs.GET;
29 import javax.ws.rs.HeaderParam;
30 import javax.ws.rs.POST;
31 import javax.ws.rs.Path;
32 import javax.ws.rs.Produces;
33 import javax.ws.rs.core.Context;
34 import javax.ws.rs.core.MediaType;
35 import javax.ws.rs.core.Response;
36 import javax.ws.rs.core.Response.ResponseBuilder;
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;
44
45 /**
46  * Class to provide xacml pdp REST services.
47  *
48  */
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;
58
59     @GET
60     @Path("/healthcheck")
61     @Override
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();
66     }
67
68     @GET
69     @Path("/statistics")
70     @Override
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();
75     }
76
77     @POST
78     @Path("/decision")
79     @Override
80     public Response decision(DecisionRequest body, @HeaderParam("X-ONAP-RequestID") UUID requestId) {
81         return decision(body, requestId, request);
82     }
83
84     /**
85      * Our decision entry point.
86      *
87      * @param body Should be a DecisionRequest object
88      * @param requestId Unique request id
89      * @return DecisionResponse or ErrorResponse object
90      */
91     private Response decision(DecisionRequest body,
92             @HeaderParam("X-ONAP-RequestID") UUID requestId,
93             @Context HttpServletRequest request) {
94         try {
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();
103         }
104     }
105
106     /**
107      * Our native decision entry point.
108      *
109      * @param body Should be an Xacml Request object
110      * @param requestId Unique request id
111      * @return Xacml Response or ErrorResponse object
112      */
113     @POST
114     @Path("/xacml")
115     @Produces({XacmlPdpRestController.APPLICATION_XACML_JSON, XacmlPdpRestController.APPLICATION_XACML_XML})
116     @Consumes({XacmlPdpRestController.APPLICATION_XACML_JSON, XacmlPdpRestController.APPLICATION_XACML_XML})
117     @Override
118     public Response xacml(Request body,
119             @HeaderParam("X-ONAP-RequestID") UUID requestId) {
120         try {
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();
129         }
130     }
131
132     private ResponseBuilder addVersionControlHeaders(ResponseBuilder rb) {
133         return rb.header("X-MinorVersion", "0").header("X-PatchVersion", "0").header("X-LatestVersion", "1.0.0");
134     }
135
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());
140         }
141         return rb.header("X-ONAP-RequestID", requestId);
142     }
143
144
145 }