SLAs for async methods
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / rest / PapRestControllerV1.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2022 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
5  *  Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.pap.main.rest;
24
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.Objects;
32 import java.util.UUID;
33 import javax.ws.rs.core.MediaType;
34 import org.onap.policy.models.base.PfModelException;
35 import org.springframework.http.ResponseEntity.BodyBuilder;
36 import org.springframework.security.core.Authentication;
37 import org.springframework.security.core.context.SecurityContextHolder;
38
39 /**
40  * Version v1 common superclass to provide REST endpoints for PAP component.
41  *
42  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
43  */
44 // @formatter:off
45 @SwaggerDefinition(
46     info = @Info(description =
47                     "Policy Administration is responsible for the deployment life cycle of policies as well as "
48                     + "interworking with the mechanisms required to orchestrate the nodes and containers on which "
49                     + "policies run. It is also responsible for the administration of policies at run time;"
50                     + " ensuring that policies are available to users, that policies are executing correctly,"
51                     + " and that the state and status of policies is monitored", version = "v1.0",
52                     title = "Policy Administration"),
53     consumes = {MediaType.APPLICATION_JSON, PapRestControllerV1.APPLICATION_YAML},
54     produces = {MediaType.APPLICATION_JSON, PapRestControllerV1.APPLICATION_YAML},
55     schemes = {SwaggerDefinition.Scheme.HTTP, SwaggerDefinition.Scheme.HTTPS},
56     tags = {@Tag(name = "policy-administration", description = "Policy Administration Service Operations")},
57     securityDefinition = @SecurityDefinition(basicAuthDefinitions = {@BasicAuthDefinition(key = "basicAuth")}))
58 // @formatter:on
59 public class PapRestControllerV1 {
60     public static final String APPLICATION_YAML = "application/yaml";
61
62     public static final String EXTENSION_NAME = "interface info";
63
64     public static final String API_VERSION_NAME = "api-version";
65     public static final String API_VERSION = "1.0.0";
66
67     public static final String LAST_MOD_NAME = "last-mod-release";
68     public static final String LAST_MOD_RELEASE = "Dublin";
69
70     public static final String VERSION_MINOR_NAME = "X-MinorVersion";
71     public static final String VERSION_MINOR_DESCRIPTION =
72             "Used to request or communicate a MINOR version back from the client"
73                     + " to the server, and from the server back to the client";
74
75     public static final String VERSION_PATCH_NAME = "X-PatchVersion";
76     public static final String VERSION_PATCH_DESCRIPTION = "Used only to communicate a PATCH version in a response for"
77             + " troubleshooting purposes only, and will not be provided by" + " the client on request";
78
79     public static final String VERSION_LATEST_NAME = "X-LatestVersion";
80     public static final String VERSION_LATEST_DESCRIPTION = "Used only to communicate an API's latest version";
81
82     public static final String REQUEST_ID_NAME = "X-ONAP-RequestID";
83     public static final String REQUEST_ID_HDR_DESCRIPTION = "Used to track REST transactions for logging purpose";
84     public static final String REQUEST_ID_PARAM_DESCRIPTION = "RequestID for http transaction";
85
86     public static final String AUTHORIZATION_TYPE = "basicAuth";
87
88     public static final int AUTHENTICATION_ERROR_CODE = HttpURLConnection.HTTP_UNAUTHORIZED;
89     public static final int AUTHORIZATION_ERROR_CODE = HttpURLConnection.HTTP_FORBIDDEN;
90     public static final int SERVER_ERROR_CODE = HttpURLConnection.HTTP_INTERNAL_ERROR;
91
92     public static final String AUTHENTICATION_ERROR_MESSAGE = "Authentication Error";
93     public static final String AUTHORIZATION_ERROR_MESSAGE = "Authorization Error";
94     public static final String SERVER_ERROR_MESSAGE = "Internal Server Error";
95
96     /**
97      * Adds version headers to the response.
98      *
99      * @param respBuilder response builder
100      * @return the response builder, with version headers
101      */
102     public static BodyBuilder addVersionControlHeaders(BodyBuilder respBuilder) {
103         return respBuilder.header(VERSION_MINOR_NAME, "0").header(VERSION_PATCH_NAME, "0").header(VERSION_LATEST_NAME,
104                 API_VERSION);
105     }
106
107     /**
108      * Adds logging headers to the response.
109      *
110      * @param respBuilder response builder
111      * @return the response builder, with version logging
112      */
113     public static BodyBuilder addLoggingHeaders(BodyBuilder respBuilder, UUID requestId) {
114         // Generate a random uuid if client does not embed requestId in rest request
115         return respBuilder.header(REQUEST_ID_NAME,
116             Objects.requireNonNullElseGet(requestId, UUID::randomUUID).toString());
117     }
118
119     /**
120      * Get the user principal name from security context.
121      * @return username as {@link String}
122      */
123     public String getPrincipal() {
124         Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
125         if (authentication != null) {
126             return authentication.getName();
127         }
128         return "";
129     }
130
131     /**
132      * Functions that throw {@link PfModelException}.
133      */
134     @FunctionalInterface
135     public interface RunnableWithPfEx {
136         void run() throws PfModelException;
137     }
138 }