965753ae5e11e5fc057a72eb098df2f20063c1a0
[policy/xacml-pdp.git] / main / src / main / java / org / onap / policy / pdpx / main / rest / XacmlPdpRestController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pdpx.main.rest;
22
23 import io.swagger.annotations.Api;
24 import io.swagger.annotations.ApiOperation;
25 import io.swagger.annotations.ApiParam;
26 import io.swagger.annotations.ApiResponse;
27 import io.swagger.annotations.ApiResponses;
28 import io.swagger.annotations.Authorization;
29 import io.swagger.annotations.BasicAuthDefinition;
30 import io.swagger.annotations.Extension;
31 import io.swagger.annotations.ExtensionProperty;
32 import io.swagger.annotations.Info;
33 import io.swagger.annotations.ResponseHeader;
34 import io.swagger.annotations.SecurityDefinition;
35 import io.swagger.annotations.SwaggerDefinition;
36 import java.util.UUID;
37 import javax.ws.rs.Consumes;
38 import javax.ws.rs.GET;
39 import javax.ws.rs.HeaderParam;
40 import javax.ws.rs.POST;
41 import javax.ws.rs.Path;
42 import javax.ws.rs.Produces;
43 import javax.ws.rs.core.MediaType;
44 import javax.ws.rs.core.Response;
45 import javax.ws.rs.core.Response.ResponseBuilder;
46 import org.onap.policy.common.endpoints.report.HealthCheckReport;
47 import org.onap.policy.pdpx.main.rest.model.Decision;
48 import org.onap.policy.pdpx.main.rest.model.StatisticsReport;
49 import org.onap.policy.pdpx.main.rest.provider.DecisionProvider;
50 import org.onap.policy.pdpx.main.rest.provider.HealthCheckProvider;
51 import org.onap.policy.pdpx.main.rest.provider.StatisticsProvider;
52
53 /**
54  * Class to provide xacml pdp REST services.
55  *
56  */
57 @Path("/policy/pdpx/v1")
58 @Api
59 @Produces(MediaType.APPLICATION_JSON)
60 @Consumes(MediaType.APPLICATION_JSON)
61 @SwaggerDefinition(info = @Info(description = "Policy Xacml PDP Service", version = "1.0.0", title = "Policy Xacml PDP",
62         extensions = {@Extension(properties = {@ExtensionProperty(name = "planned-retirement-date", value = "tbd"),
63                 @ExtensionProperty(name = "component", value = "Policy Framework")})}),
64         schemes = {SwaggerDefinition.Scheme.HTTP, SwaggerDefinition.Scheme.HTTPS},
65         securityDefinition = @SecurityDefinition(basicAuthDefinitions = {@BasicAuthDefinition(key = "basicAuth")}))
66 public class XacmlPdpRestController {
67
68     @GET
69     @Path("/healthcheck")
70     @ApiOperation(value = "Perform a system healthcheck",
71             notes = "Provides healthy status of the Policy Xacml PDP component", response = HealthCheckReport.class,
72             responseHeaders = {
73                     @ResponseHeader(name = "X-MinorVersion",
74                             description = "Used to request or communicate a MINOR version back from the client"
75                                     + " to the server, and from the server back to the client",
76                             response = String.class),
77                     @ResponseHeader(name = "X-PatchVersion",
78                             description = "Used only to communicate a PATCH version in a response for"
79                                     + " troubleshooting purposes only, and will not be provided by"
80                                     + " the client on request",
81                             response = String.class),
82                     @ResponseHeader(name = "X-LatestVersion",
83                             description = "Used only to communicate an API's latest version", response = String.class),
84                     @ResponseHeader(name = "X-ONAP-RequestID",
85                             description = "Used to track REST transactions for logging purpose",
86                             response = UUID.class)},
87             authorizations = @Authorization(value = "basicAuth"), tags = {"HealthCheck",},
88             extensions = {@Extension(name = "interface info",
89                     properties = {@ExtensionProperty(name = "pdpx-version", value = "1.0.0"),
90                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")})})
91     @ApiResponses(value = {@ApiResponse(code = 401, message = "Authentication Error"),
92             @ApiResponse(code = 403, message = "Authorization Error"),
93             @ApiResponse(code = 500, message = "Internal Server Error")})
94     public Response healthcheck(
95             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {
96         return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
97                 .entity(new HealthCheckProvider().performHealthCheck()).build();
98     }
99
100     @GET
101     @Path("/statistics")
102     @ApiOperation(value = "Fetch current statistics",
103             notes = "Provides current statistics of the Policy Xacml PDP component", response = StatisticsReport.class,
104             responseHeaders = {
105                     @ResponseHeader(name = "X-MinorVersion",
106                             description = "Used to request or communicate a MINOR version back from the client"
107                                     + " to the server, and from the server back to the client",
108                             response = String.class),
109                     @ResponseHeader(name = "X-PatchVersion",
110                             description = "Used only to communicate a PATCH version in a response for"
111                                     + " troubleshooting purposes only, and will not be provided by"
112                                     + " the client on request",
113                             response = String.class),
114                     @ResponseHeader(name = "X-LatestVersion",
115                             description = "Used only to communicate an API's latest version", response = String.class),
116                     @ResponseHeader(name = "X-ONAP-RequestID",
117                             description = "Used to track REST transactions for logging purpose",
118                             response = UUID.class)},
119             authorizations = @Authorization(value = "basicAuth"), tags = {"Statistics",},
120             extensions = {@Extension(name = "interface info",
121                     properties = {@ExtensionProperty(name = "pdpx-version", value = "1.0.0"),
122                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")})})
123     @ApiResponses(value = {@ApiResponse(code = 401, message = "Authentication Error"),
124             @ApiResponse(code = 403, message = "Authorization Error"),
125             @ApiResponse(code = 500, message = "Internal Server Error")})
126     public Response statistics(
127             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {
128         return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
129                 .entity(new StatisticsProvider().fetchCurrentStatistics()).build();
130     }
131
132     @POST
133     @Path("/decision")
134     @ApiOperation(value = "Fetch the decision using specified decision parameters",
135             notes = "Returns the policy decision from Policy Xacml PDP", response = Decision.class,
136             responseHeaders = {
137                     @ResponseHeader(name = "X-MinorVersion",
138                             description = "Used to request or communicate a MINOR version back from the client"
139                                     + " to the server, and from the server back to the client",
140                             response = String.class),
141                     @ResponseHeader(name = "X-PatchVersion",
142                             description = "Used only to communicate a PATCH version in a response for"
143                                     + " troubleshooting purposes only, and will not be provided by"
144                                     + " the client on request",
145                             response = String.class),
146                     @ResponseHeader(name = "X-LatestVersion",
147                             description = "Used only to communicate an API's latest version", response = String.class),
148                     @ResponseHeader(name = "X-ONAP-RequestID",
149                             description = "Used to track REST transactions for logging purpose",
150                             response = UUID.class)},
151             authorizations = @Authorization(value = "basicAuth"), tags = {"Decision",},
152             extensions = {@Extension(name = "interface info",
153                     properties = {@ExtensionProperty(name = "pdpx-version", value = "1.0.0"),
154                             @ExtensionProperty(name = "last-mod-release", value = "Dublin")})})
155     @ApiResponses(value = {@ApiResponse(code = 400, message = "Bad Request"),
156             @ApiResponse(code = 401, message = "Authentication Error"),
157             @ApiResponse(code = 403, message = "Authorization Error"),
158             @ApiResponse(code = 500, message = "Internal Server Error")})
159     public Response decision(Decision body,
160             @HeaderParam("X-ONAP-RequestID") @ApiParam("RequestID for http transaction") UUID requestId) {
161         return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
162                 .entity(new DecisionProvider().fetchDecision()).build();
163     }
164
165     private ResponseBuilder addVersionControlHeaders(ResponseBuilder rb) {
166         return rb.header("X-MinorVersion", "0").header("X-PatchVersion", "0").header("X-LatestVersion", "1.0.0");
167     }
168
169     private ResponseBuilder addLoggingHeaders(ResponseBuilder rb, UUID requestId) {
170         if (requestId == null) {
171             // Generate a random uuid if client does not embed requestId in rest request
172             return rb.header("X-ONAP-RequestID", UUID.randomUUID());
173         }
174         return rb.header("X-ONAP-RequestID", requestId);
175     }
176 }