ed8909b800d8b68dd3cb4cde8dd15d2bf3d618f8
[policy/models.git] / models-sim / models-sim-dmaap / src / main / java / org / onap / policy / models / sim / dmaap / rest / BaseRestControllerV1.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
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.models.sim.dmaap.rest;
22
23 import io.swagger.annotations.Api;
24 import io.swagger.annotations.BasicAuthDefinition;
25 import io.swagger.annotations.Info;
26 import io.swagger.annotations.SecurityDefinition;
27 import io.swagger.annotations.SwaggerDefinition;
28 import io.swagger.annotations.Tag;
29 import java.net.HttpURLConnection;
30 import java.util.UUID;
31 import javax.ws.rs.Consumes;
32 import javax.ws.rs.Produces;
33 import javax.ws.rs.core.MediaType;
34 import javax.ws.rs.core.Response.ResponseBuilder;
35
36 /**
37  * Version v1 common superclass to provide DMaaP endpoints for the DMaaP simulator component.
38  */
39 // @formatter:off
40 @Api(value = "DMaaP Simulator API")
41 @Produces("application/json")
42 @Consumes({"application/cambria", "application/json"})
43 @SwaggerDefinition(
44     info = @Info(description =
45                     "Simulator for DMaaP, follows API as described at "
46                   + "\"https://onap.readthedocs.io/en/amsterdam/submodules/dmaap/messagerouter/messageservice.git/"
47                   + "docs/message-router/message-router.html", version = "v1.0",
48                     title = "Policy Administration"),
49     consumes = {MediaType.APPLICATION_JSON},
50     produces = {MediaType.APPLICATION_JSON},
51     schemes = {SwaggerDefinition.Scheme.HTTP, SwaggerDefinition.Scheme.HTTPS},
52     tags = {@Tag(name = "dmaap-simulator", description = "DMaaP simulation")},
53     securityDefinition = @SecurityDefinition(basicAuthDefinitions = {@BasicAuthDefinition(key = "basicAuth")}))
54 // @formatter:on
55 public class BaseRestControllerV1 {
56     public static final String EXTENSION_NAME = "interface info";
57
58     public static final String API_VERSION_NAME = "api-version";
59     public static final String API_VERSION = "1.0.0";
60
61     public static final String LAST_MOD_NAME = "last-mod-release";
62     public static final String LAST_MOD_RELEASE = "Dublin";
63
64     public static final String VERSION_MINOR_NAME = "X-MinorVersion";
65     public static final String VERSION_MINOR_DESCRIPTION =
66             "Used to request or communicate a MINOR version back from the client"
67                     + " to the server, and from the server back to the client";
68
69     public static final String VERSION_PATCH_NAME = "X-PatchVersion";
70     public static final String VERSION_PATCH_DESCRIPTION = "Used only to communicate a PATCH version in a response for"
71             + " troubleshooting purposes only, and will not be provided by" + " the client on request";
72
73     public static final String VERSION_LATEST_NAME = "X-LatestVersion";
74     public static final String VERSION_LATEST_DESCRIPTION = "Used only to communicate an API's latest version";
75
76     public static final String REQUEST_ID_NAME = "X-ONAP-RequestID";
77     public static final String REQUEST_ID_HDR_DESCRIPTION = "Used to track REST transactions for logging purpose";
78     public static final String REQUEST_ID_PARAM_DESCRIPTION = "RequestID for http transaction";
79
80     public static final String AUTHORIZATION_TYPE = "basicAuth";
81
82     public static final int AUTHENTICATION_ERROR_CODE = HttpURLConnection.HTTP_UNAUTHORIZED;
83     public static final int AUTHORIZATION_ERROR_CODE = HttpURLConnection.HTTP_FORBIDDEN;
84     public static final int SERVER_ERROR_CODE = HttpURLConnection.HTTP_INTERNAL_ERROR;
85
86     public static final String AUTHENTICATION_ERROR_MESSAGE = "Authentication Error";
87     public static final String AUTHORIZATION_ERROR_MESSAGE = "Authorization Error";
88     public static final String SERVER_ERROR_MESSAGE = "Internal Server Error";
89
90     /**
91      * Adds version headers to the response.
92      *
93      * @param respBuilder response builder
94      * @return the response builder, with version headers
95      */
96     public ResponseBuilder addVersionControlHeaders(ResponseBuilder respBuilder) {
97         return respBuilder.header(VERSION_MINOR_NAME, "0").header(VERSION_PATCH_NAME, "0").header(VERSION_LATEST_NAME,
98                 API_VERSION);
99     }
100
101     /**
102      * Adds logging headers to the response.
103      *
104      * @param respBuilder response builder
105      * @return the response builder, with version logging
106      */
107     public ResponseBuilder addLoggingHeaders(ResponseBuilder respBuilder, UUID requestId) {
108         if (requestId == null) {
109             // Generate a random uuid if client does not embed requestId in rest request
110             return respBuilder.header(REQUEST_ID_NAME, UUID.randomUUID());
111         }
112
113         return respBuilder.header(REQUEST_ID_NAME, requestId);
114     }
115 }