Add yaml support to pap rest server
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / rest / PapRestControllerV1.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
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.pap.main.rest;
23
24 import io.swagger.annotations.Api;
25 import io.swagger.annotations.BasicAuthDefinition;
26 import io.swagger.annotations.Info;
27 import io.swagger.annotations.SecurityDefinition;
28 import io.swagger.annotations.SwaggerDefinition;
29 import io.swagger.annotations.Tag;
30 import java.net.HttpURLConnection;
31 import java.util.UUID;
32 import javax.ws.rs.Path;
33 import javax.ws.rs.Produces;
34 import javax.ws.rs.core.MediaType;
35 import javax.ws.rs.core.Response.ResponseBuilder;
36 import org.onap.policy.models.base.PfModelException;
37
38 /**
39  * Version v1 common superclass to provide REST endpoints for PAP component.
40  *
41  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
42  */
43 // @formatter:off
44 @Path("/policy/pap/v1")
45 @Api(value = "Policy Administration (PAP) API")
46 @Produces({MediaType.APPLICATION_JSON, PapRestControllerV1.APPLICATION_YAML})
47 @SwaggerDefinition(
48     info = @Info(description =
49                     "Policy Administration is responsible for the deployment life cycle of policies as well as "
50                     + "interworking with the mechanisms required to orchestrate the nodes and containers on which "
51                     + "policies run. It is also responsible for the administration of policies at run time;"
52                     + " ensuring that policies are available to users, that policies are executing correctly,"
53                     + " and that the state and status of policies is monitored", version = "v1.0",
54                     title = "Policy Administration"),
55     consumes = {MediaType.APPLICATION_JSON, PapRestControllerV1.APPLICATION_YAML},
56     produces = {MediaType.APPLICATION_JSON, PapRestControllerV1.APPLICATION_YAML},
57     schemes = {SwaggerDefinition.Scheme.HTTP, SwaggerDefinition.Scheme.HTTPS},
58     tags = {@Tag(name = "policy-administration", description = "Policy Administration Service Operations")},
59     securityDefinition = @SecurityDefinition(basicAuthDefinitions = {@BasicAuthDefinition(key = "basicAuth")}))
60 // @formatter:on
61 public class PapRestControllerV1 {
62     public static final String APPLICATION_YAML = "application/yaml";
63
64     public static final String EXTENSION_NAME = "interface info";
65
66     public static final String API_VERSION_NAME = "api-version";
67     public static final String API_VERSION = "1.0.0";
68
69     public static final String LAST_MOD_NAME = "last-mod-release";
70     public static final String LAST_MOD_RELEASE = "Dublin";
71
72     public static final String VERSION_MINOR_NAME = "X-MinorVersion";
73     public static final String VERSION_MINOR_DESCRIPTION =
74                     "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
77     public static final String VERSION_PATCH_NAME = "X-PatchVersion";
78     public static final String VERSION_PATCH_DESCRIPTION = "Used only to communicate a PATCH version in a response for"
79                     + " troubleshooting purposes only, and will not be provided by" + " the client on request";
80
81     public static final String VERSION_LATEST_NAME = "X-LatestVersion";
82     public static final String VERSION_LATEST_DESCRIPTION = "Used only to communicate an API's latest version";
83
84     public static final String REQUEST_ID_NAME = "X-ONAP-RequestID";
85     public static final String REQUEST_ID_HDR_DESCRIPTION = "Used to track REST transactions for logging purpose";
86     public static final String REQUEST_ID_PARAM_DESCRIPTION = "RequestID for http transaction";
87
88     public static final String AUTHORIZATION_TYPE = "basicAuth";
89
90     public static final int AUTHENTICATION_ERROR_CODE = HttpURLConnection.HTTP_UNAUTHORIZED;
91     public static final int AUTHORIZATION_ERROR_CODE = HttpURLConnection.HTTP_FORBIDDEN;
92     public static final int SERVER_ERROR_CODE = HttpURLConnection.HTTP_INTERNAL_ERROR;
93
94     public static final String AUTHENTICATION_ERROR_MESSAGE = "Authentication Error";
95     public static final String AUTHORIZATION_ERROR_MESSAGE = "Authorization Error";
96     public static final String SERVER_ERROR_MESSAGE = "Internal Server Error";
97
98     /**
99      * Adds version headers to the response.
100      *
101      * @param respBuilder response builder
102      * @return the response builder, with version headers
103      */
104     public ResponseBuilder addVersionControlHeaders(ResponseBuilder respBuilder) {
105         return respBuilder.header(VERSION_MINOR_NAME, "0").header(VERSION_PATCH_NAME, "0").header(VERSION_LATEST_NAME,
106                         API_VERSION);
107     }
108
109     /**
110      * Adds logging headers to the response.
111      *
112      * @param respBuilder response builder
113      * @return the response builder, with version logging
114      */
115     public ResponseBuilder addLoggingHeaders(ResponseBuilder respBuilder, UUID requestId) {
116         if (requestId == null) {
117             // Generate a random uuid if client does not embed requestId in rest request
118             return respBuilder.header(REQUEST_ID_NAME, UUID.randomUUID());
119         }
120
121         return respBuilder.header(REQUEST_ID_NAME, requestId);
122     }
123
124     /**
125      * Functions that throw {@link PfModelException}.
126      */
127     @FunctionalInterface
128     public static interface RunnableWithPfEx {
129         public void run() throws PfModelException;
130     }
131 }