4db07c953f8ea45bab25fd872b995e821a4b89ed
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021 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.clamp.acm.participant.simulator.main.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.core.MediaType;
32 import lombok.AccessLevel;
33 import lombok.Getter;
34 import org.onap.policy.clamp.acm.participant.simulator.simulation.SimulationProvider;
35 import org.springframework.http.HttpHeaders;
36 import org.springframework.web.bind.annotation.RequestMapping;
37
38 /**
39  * Common superclass to provide REST endpoints for the participant simulator.
40  */
41 // @formatter:off
42 @RequestMapping(
43     value = "/v2",
44     produces = {
45         MediaType.APPLICATION_JSON,
46         AbstractRestController.APPLICATION_YAML
47     }
48 )
49 @Api(value = "Participant Simulator API")
50 @SwaggerDefinition(
51     info = @Info(
52         description = "Participant Simulator",
53         version = "v1.0",
54         title = "Participant Simulator"
55     ),
56     consumes = {MediaType.APPLICATION_JSON, AbstractRestController.APPLICATION_YAML},
57     produces = {MediaType.APPLICATION_JSON, AbstractRestController.APPLICATION_YAML},
58     schemes = {SwaggerDefinition.Scheme.HTTP, SwaggerDefinition.Scheme.HTTPS},
59     tags = {
60         @Tag(name = "participantsim", description = "Participant Simulator")
61     },
62     securityDefinition = @SecurityDefinition(basicAuthDefinitions = {@BasicAuthDefinition(key = "basicAuth")}))
63 // @formatter:on
64 public abstract class AbstractRestController {
65     public static final String APPLICATION_YAML = "application/yaml";
66
67     public static final String EXTENSION_NAME = "interface info";
68
69     public static final String API_VERSION_NAME = "api-version";
70     public static final String API_VERSION = "1.0.0";
71
72     public static final String LAST_MOD_NAME = "last-mod-release";
73     public static final String LAST_MOD_RELEASE = "Dublin";
74
75     public static final String VERSION_MINOR_NAME = "X-MinorVersion";
76     public static final String VERSION_MINOR_DESCRIPTION =
77             "Used to request or communicate a MINOR version back from the client"
78                     + " to the server, and from the server back to the client";
79
80     public static final String VERSION_PATCH_NAME = "X-PatchVersion";
81     public static final String VERSION_PATCH_DESCRIPTION = "Used only to communicate a PATCH version in a response for"
82             + " troubleshooting purposes only, and will not be provided by" + " the client on request";
83
84     public static final String VERSION_LATEST_NAME = "X-LatestVersion";
85     public static final String VERSION_LATEST_DESCRIPTION = "Used only to communicate an API's latest version";
86
87     public static final String REQUEST_ID_NAME = "X-ONAP-RequestID";
88     public static final String REQUEST_ID_HDR_DESCRIPTION = "Used to track REST transactions for logging purpose";
89     public static final String REQUEST_ID_PARAM_DESCRIPTION = "RequestID for http transaction";
90
91     public static final String AUTHORIZATION_TYPE = "basicAuth";
92
93     public static final int AUTHENTICATION_ERROR_CODE = HttpURLConnection.HTTP_UNAUTHORIZED;
94     public static final int AUTHORIZATION_ERROR_CODE = HttpURLConnection.HTTP_FORBIDDEN;
95     public static final int SERVER_ERROR_CODE = HttpURLConnection.HTTP_INTERNAL_ERROR;
96
97     public static final String AUTHENTICATION_ERROR_MESSAGE = "Authentication Error";
98     public static final String AUTHORIZATION_ERROR_MESSAGE = "Authorization Error";
99     public static final String SERVER_ERROR_MESSAGE = "Internal Server Error";
100
101     // The provider for simulation requests
102     @Getter(AccessLevel.PROTECTED)
103     private SimulationProvider simulationProvider;
104
105     /**
106      * create a Rest Controller.
107      *
108      * @param simulationProvider the provider for the simulation participant
109      */
110     protected AbstractRestController(SimulationProvider simulationProvider) {
111         this.simulationProvider = simulationProvider;
112     }
113
114     /**
115      * Get the common headers for responses.
116      *
117      * @param requestId  the request ID
118      *
119      * @return the headers
120      */
121     protected HttpHeaders getCommonHeaders(UUID requestId) {
122         HttpHeaders commonHeaders = new HttpHeaders();
123         commonHeaders.add(VERSION_MINOR_NAME, API_VERSION.split("\\.")[1]);
124         commonHeaders.add(VERSION_PATCH_NAME, API_VERSION.split("\\.")[2]);
125         commonHeaders.add(VERSION_LATEST_NAME, API_VERSION);
126         commonHeaders.add(REQUEST_ID_NAME, (requestId != null ? requestId.toString() : null));
127
128         return commonHeaders;
129     }
130 }