2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.distribution.servlet;
23 import com.jcabi.aspects.Loggable;
24 import fj.data.Either;
25 import io.swagger.annotations.*;
26 import org.openecomp.sdc.be.config.BeEcompErrorManager;
27 import org.openecomp.sdc.be.dao.api.ActionStatus;
28 import org.openecomp.sdc.be.distribution.AuditHandler;
29 import org.openecomp.sdc.be.distribution.DistributionBusinessLogic;
30 import org.openecomp.sdc.be.distribution.api.client.RegistrationRequest;
31 import org.openecomp.sdc.be.distribution.api.client.ServerListResponse;
32 import org.openecomp.sdc.be.distribution.api.client.TopicRegistrationResponse;
33 import org.openecomp.sdc.be.distribution.api.client.TopicUnregistrationResponse;
34 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
35 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
36 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
37 import org.openecomp.sdc.be.servlets.BeGenericServlet;
38 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
39 import org.openecomp.sdc.common.api.Constants;
40 import org.openecomp.sdc.common.config.EcompErrorName;
41 import org.openecomp.sdc.common.datastructure.Wrapper;
42 import org.openecomp.sdc.common.util.HttpUtil;
43 import org.openecomp.sdc.exception.ResponseFormat;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.springframework.web.context.WebApplicationContext;
48 import javax.annotation.Resource;
49 import javax.inject.Singleton;
50 import javax.servlet.ServletContext;
51 import javax.servlet.http.HttpServletRequest;
53 import javax.ws.rs.core.Context;
54 import javax.ws.rs.core.MediaType;
55 import javax.ws.rs.core.Response;
58 * This Servlet serves external users for distribution purposes.
64 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
66 @Api(value = "Distribution Servlet", description = "This Servlet serves external users for distribution purposes.")
68 public class DistributionServlet extends BeGenericServlet {
70 private static final Logger log = LoggerFactory.getLogger(DistributionServlet.class);
72 private DistributionBusinessLogic distributionLogic;
74 private HttpServletRequest request;
81 * @param authorization
85 @Path("/distributionUebCluster")
86 @Consumes(MediaType.APPLICATION_JSON)
87 @Produces(MediaType.APPLICATION_JSON)
88 @ApiOperation(value = "UEB Server List", httpMethod = "GET", notes = "return the available UEB Server List",
89 //TODO Tal G fix response headers
91 @ResponseHeader(name = Constants.CONTENT_TYPE_HEADER, description = "Determines the format of the response body", response = String.class),
92 @ResponseHeader(name = "Content-Length", description = "Length of the response body", response = String.class)})
93 @ApiResponses(value = {
94 @ApiResponse(code = 200, message = "ECOMP component is authenticated and list of Cambria API server’s FQDNs is returned", response = ServerListResponse.class),
95 @ApiResponse(code = 400, message = "Missing 'X-ECOMP-InstanceID' HTTP header - POL5001"),
96 @ApiResponse(code = 401, message = "ECOMP component should authenticate itself and to re-send again HTTP request with its credentials for Basic Authentication - POL5002"),
97 @ApiResponse(code = 403, message = "ECOMP component is not authorized - POL5003"),
98 @ApiResponse(code = 405, message = "Method Not Allowed: Invalid HTTP method type used ( PUT,DELETE,POST will be rejected) - POL4050"),
99 @ApiResponse(code = 500, message = "The GET request failed either due to internal SDC problem or Cambria Service failure. ECOMP Component should continue the attempts to get the needed information - POL5000")})
100 public Response getUebServerList(
101 @ApiParam(value = "X-ECOMP-RequestID header", required = false)@HeaderParam(value = Constants.X_ECOMP_REQUEST_ID_HEADER) String requestId,
102 @ApiParam(value = "X-ECOMP-InstanceID header", required = true)@HeaderParam(value = Constants.X_ECOMP_INSTANCE_ID_HEADER) String instanceId,
103 @ApiParam(value = "Determines the format of the body of the response", required = false)@HeaderParam(value = Constants.ACCEPT_HEADER) String accept,
104 @ApiParam(value = "The username and password", required = true)@HeaderParam(value = Constants.AUTHORIZATION_HEADER) String authorization) {
107 String url = request.getMethod() + " " + request.getRequestURI();
108 log.debug("Start handle request of {}", url);
109 Response response = null;
110 ResponseFormat responseFormat = null;
112 if (instanceId == null) {
113 responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID);
114 response = buildErrorResponse(responseFormat);
115 getComponentsUtils().auditMissingInstanceId(AuditingActionEnum.GET_UEB_CLUSTER, responseFormat.getStatus().toString(), responseFormat.getFormattedMessage());
120 Either<ServerListResponse, ResponseFormat> actionResponse = distributionLogic.getUebServerList();
122 if (actionResponse.isRight()) {
123 responseFormat = actionResponse.right().value();
124 response = buildErrorResponse(responseFormat);
126 responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
127 response = buildOkResponse(responseFormat, actionResponse.left().value());
130 getComponentsUtils().auditGetUebCluster(AuditingActionEnum.GET_UEB_CLUSTER, instanceId, null, responseFormat.getStatus().toString(), responseFormat.getFormattedMessage());
133 } catch (Exception e) {
134 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("failed to get ueb serbver list from cofiguration");
135 log.debug("failed to get ueb serbver list from cofiguration", e);
136 responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
137 getComponentsUtils().auditGetUebCluster(AuditingActionEnum.GET_UEB_CLUSTER, instanceId, null, responseFormat.getStatus().toString(), responseFormat.getFormattedMessage());
138 response = buildErrorResponse(responseFormat);
150 * @param contenLength
151 * @param authorization
156 @Path("/registerForDistribution")
157 @Consumes(MediaType.APPLICATION_JSON)
158 @Produces(MediaType.APPLICATION_JSON)
159 @ApiOperation(value = "Subscription status", httpMethod = "POST", notes = "Subscribes for distribution notifications")
160 @ApiResponses(value = {
161 @ApiResponse(code = 200, message = "ECOMP component is successfully registered for distribution", response = TopicRegistrationResponse.class),
162 @ApiResponse(code = 400, message = "Missing 'X-ECOMP-InstanceID' HTTP header - POL5001"),
163 @ApiResponse(code = 400, message = "Missing Body - POL4500"),
164 @ApiResponse(code = 400, message = "Invalid Body : missing mandatory parameter 'apiPublicKey' - POL4501"),
165 @ApiResponse(code = 400, message = "Invalid Body : missing mandatory parameter 'distrEnvName' - POL4502"),
166 @ApiResponse(code = 400, message = "Invalid Body : Specified 'distrEnvName' doesn’t exist - POL4137"),
167 @ApiResponse(code = 401, message = "ECOMP component should authenticate itself and to re-send again HTTP request with its Basic Authentication credentials - POL5002"),
168 @ApiResponse(code = 403, message = "ECOMP component is not authorized - POL5003"),
169 @ApiResponse(code = 405, message = "Method Not Allowed : Invalid HTTP method type used to register for distribution ( PUT,DELETE,GET will be rejected) - POL4050"),
170 @ApiResponse(code = 500, message = "The registration failed due to internal SDC problem or Cambria Service failure ECOMP Component should continue the attempts to register for distribution - POL5000")})
171 //TODO Tal G fix response headers and to check missing header validations with Michael L
172 @ApiImplicitParams({@ApiImplicitParam(required = true, dataType = "org.openecomp.sdc.be.distribution.api.client.RegistrationRequest", paramType = "body", value = "json describe the artifact")})
173 public Response registerForDistribution(
174 @ApiParam(value = "X-ECOMP-RequestID header", required = false)@HeaderParam(value = Constants.X_ECOMP_REQUEST_ID_HEADER) String requestId,
175 @ApiParam(value = "X-ECOMP-InstanceID header", required = true)@HeaderParam(value = Constants.X_ECOMP_INSTANCE_ID_HEADER) String instanceId,
176 @ApiParam(value = "Determines the format of the body of the response", required = false)@HeaderParam(value = Constants.ACCEPT_HEADER) String accept,
177 @ApiParam(value = "Determines the format of the body of the request", required = true)@HeaderParam(value = Constants.CONTENT_TYPE_HEADER) String contenType,
178 @ApiParam(value = "Length of the request body", required = true)@HeaderParam(value = Constants.CONTENT_LENGTH_HEADER) String contenLength,
179 @ApiParam(value = "The username and password", required = true)@HeaderParam(value = Constants.AUTHORIZATION_HEADER) String authorization,
180 String requestJson) {
181 String url = request.getMethod() + " " + request.getRequestURI();
182 log.debug("Start handle request of {}", url);
185 Wrapper<Response> responseWrapper = new Wrapper<>();
186 Wrapper<RegistrationRequest> registrationRequestWrapper = new Wrapper<>();
188 validateHeaders(responseWrapper, request, AuditingActionEnum.ADD_KEY_TO_TOPIC_ACL);
190 if (responseWrapper.isEmpty()) {
191 validateJson(responseWrapper, registrationRequestWrapper, requestJson);
193 if (responseWrapper.isEmpty()) {
194 validateEnv(responseWrapper, registrationRequestWrapper.getInnerElement().getDistrEnvName());
197 if (responseWrapper.isEmpty()) {
198 distributionLogic.handleRegistration(responseWrapper, registrationRequestWrapper.getInnerElement(), buildAuditHandler(request, registrationRequestWrapper.getInnerElement()));
200 BeEcompErrorManager.getInstance().logBeDistributionEngineSystemError(DistributionBusinessLogic.REGISTER_IN_DISTRIBUTION_ENGINE, "registration validation failed");
203 return responseWrapper.getInnerElement();
207 * Returns list of valid artifact types for validation done in the distribution client.<br>
208 * The list is the representation of the values of the enum ArtifactTypeEnum.
212 * @param authorization
217 @Path("/artifactTypes")
218 @Consumes(MediaType.APPLICATION_JSON)
219 @Produces(MediaType.APPLICATION_JSON)
220 @ApiOperation(value = "Artifact types list", httpMethod = "GET", notes = "Fetches available artifact types list")
221 @ApiResponses(value = {
222 @ApiResponse(code = 200, message = "Artifact types list fetched successfully", response = String.class),
223 @ApiResponse(code = 400, message = "Missing 'X-ECOMP-InstanceID' HTTP header - POL5001"),
224 @ApiResponse(code = 401, message = "ECOMP component should authenticate itself and to re-send again HTTP request with its Basic Authentication credentials - POL5002"),
225 @ApiResponse(code = 403, message = "ECOMP component is not authorized - POL5003"),
226 @ApiResponse(code = 405, message = "Method Not Allowed : Invalid HTTP method type used to register for distribution ( POST,PUT,DELETE will be rejected) - POL4050"),
227 @ApiResponse(code = 500, message = "The registration failed due to internal SDC problem or Cambria Service failure ECOMP Component should continue the attempts to register for distribution - POL5000")})
228 public Response getValidArtifactTypes(
229 @ApiParam(value = "X-ECOMP-RequestID header", required = false)@HeaderParam(value = Constants.X_ECOMP_REQUEST_ID_HEADER) String requestId,
230 @ApiParam(value = "X-ECOMP-InstanceID header", required = true)@HeaderParam(value = Constants.X_ECOMP_INSTANCE_ID_HEADER) String instanceId,
231 @ApiParam(value = "The username and password", required = true)@HeaderParam(value = Constants.AUTHORIZATION_HEADER) String authorization,
232 @ApiParam(value = "The username and password", required = true)@HeaderParam(value = Constants.ACCEPT_HEADER) String accept) {
234 String url = request.getMethod() + " " + request.getRequestURI();
235 log.debug("Start handle request of {}", url);
236 Response response = null;
238 Wrapper<Response> responseWrapper = new Wrapper<>();
240 validateHeaders(responseWrapper, request, AuditingActionEnum.GET_VALID_ARTIFACT_TYPES);
241 if (responseWrapper.isEmpty()) {
242 response = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), ArtifactTypeEnum.values());
244 response = responseWrapper.getInnerElement();
250 * Removes from subscription for distribution notifications
256 * @param contenLength
257 * @param authorization
262 @Path("/unRegisterForDistribution")
263 @Consumes(MediaType.APPLICATION_JSON)
264 @Produces(MediaType.APPLICATION_JSON)
265 @ApiOperation(value = "Subscription status", httpMethod = "POST", notes = "Removes from subscription for distribution notifications")
266 //TODO Edit the responses
267 @ApiResponses(value = {
268 @ApiResponse(code = 204, message = "ECOMP component is successfully unregistered", response = TopicUnregistrationResponse.class),
269 @ApiResponse(code = 400, message = "Missing 'X-ECOMP-InstanceID' HTTP header - POL5001"),
270 @ApiResponse(code = 400, message = "Missing Body - POL4500"),
271 @ApiResponse(code = 400, message = "Invalid Body : missing mandatory parameter 'apiPublicKey' - POL4501"),
272 @ApiResponse(code = 400, message = "Invalid Body : missing mandatory parameter 'distrEnvName' - SVC4506"),
273 @ApiResponse(code = 400, message = "Invalid Body : Specified 'distrEnvName' doesn’t exist - POL4137"),
274 @ApiResponse(code = 401, message = "ECOMP component should authenticate itself and to re-send again HTTP request with its Basic Authentication credentials - POL5002"),
275 @ApiResponse(code = 403, message = "ECOMP component is not authorized - POL5003"),
276 @ApiResponse(code = 405, message = "Method Not Allowed : Invalid HTTP method type used to register for distribution ( PUT,DELETE,GET will be rejected) - POL4050"),
277 @ApiResponse(code = 500, message = "The registration failed due to internal SDC problem or Cambria Service failure ECOMP Component should continue the attempts to register for distribution - POL5000")})
278 @ApiImplicitParams({@ApiImplicitParam(required = true, dataType = "org.openecomp.sdc.be.distribution.api.client.RegistrationRequest", paramType = "body", value = "json describe the artifact")})
279 public Response unRegisterForDistribution(
280 @ApiParam(value = "X-ECOMP-RequestID header", required = false)@HeaderParam(value = Constants.X_ECOMP_REQUEST_ID_HEADER) String requestId,
281 @ApiParam(value = "X-ECOMP-InstanceID header", required = true)@HeaderParam(value = Constants.X_ECOMP_INSTANCE_ID_HEADER) String instanceId,
282 @ApiParam(value = "Determines the format of the body of the response", required = false)@HeaderParam(value = Constants.ACCEPT_HEADER) String accept,
283 @ApiParam(value = "Determines the format of the body of the request", required = true)@HeaderParam(value = Constants.CONTENT_TYPE_HEADER) String contenType,
284 @ApiParam(value = "Length of the request body", required = true)@HeaderParam(value = Constants.CONTENT_LENGTH_HEADER) String contenLength,
285 @ApiParam(value = "The username and password", required = true)@HeaderParam(value = Constants.AUTHORIZATION_HEADER) String authorization,
286 String requestJson) {
288 String url = request.getMethod() + " " + request.getRequestURI();
289 log.debug("Start handle request of {}", url);
292 Wrapper<Response> responseWrapper = new Wrapper<>();
293 Wrapper<RegistrationRequest> unRegistrationRequestWrapper = new Wrapper<>();
295 validateHeaders(responseWrapper, request, AuditingActionEnum.REMOVE_KEY_FROM_TOPIC_ACL);
297 if (responseWrapper.isEmpty()) {
298 validateJson(responseWrapper, unRegistrationRequestWrapper, requestJson);
300 if (responseWrapper.isEmpty()) {
301 validateEnv(responseWrapper, unRegistrationRequestWrapper.getInnerElement().getDistrEnvName());
303 if (responseWrapper.isEmpty()) {
304 distributionLogic.handleUnRegistration(responseWrapper, unRegistrationRequestWrapper.getInnerElement(), buildAuditHandler(request, unRegistrationRequestWrapper.getInnerElement()));
306 BeEcompErrorManager.getInstance().logBeDistributionEngineSystemError(DistributionBusinessLogic.UN_REGISTER_IN_DISTRIBUTION_ENGINE, "unregistration validation failed");
309 return responseWrapper.getInnerElement();
312 private void validateEnv(Wrapper<Response> responseWrapper, String distrEnvName) {
315 StorageOperationStatus environmentStatus = distributionLogic.getDistributionEngine().isEnvironmentAvailable();
316 if (environmentStatus != StorageOperationStatus.OK) {
317 if (environmentStatus == StorageOperationStatus.DISTR_ENVIRONMENT_NOT_FOUND) {
318 Response missingHeaderResponse = buildErrorResponse(distributionLogic.getResponseFormatManager().getResponseFormat(ActionStatus.DISTRIBUTION_ENV_DOES_NOT_EXIST));
319 responseWrapper.setInnerElement(missingHeaderResponse);
321 Response missingHeaderResponse = buildErrorResponse(distributionLogic.getResponseFormatManager().getResponseFormat(ActionStatus.GENERAL_ERROR));
322 responseWrapper.setInnerElement(missingHeaderResponse);
328 private void init(HttpServletRequest request) {
329 if (distributionLogic == null) {
330 distributionLogic = getDistributionBL(request.getSession().getServletContext());
334 private void validateHeaders(Wrapper<Response> responseWrapper, HttpServletRequest request, AuditingActionEnum auditingAction) {
335 if (request.getHeader(Constants.X_ECOMP_INSTANCE_ID_HEADER) == null) {
336 Response missingHeaderResponse = buildErrorResponse(distributionLogic.getResponseFormatManager().getResponseFormat(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID));
337 responseWrapper.setInnerElement(missingHeaderResponse);
339 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID);
340 getComponentsUtils().auditMissingInstanceId(auditingAction, responseFormat.getStatus().toString(), responseFormat.getFormattedMessage());
346 private void validateJson(Wrapper<Response> responseWrapper, Wrapper<RegistrationRequest> registrationRequestWrapper, String requestJson) {
347 if (requestJson == null || requestJson.isEmpty()) {
348 Response missingBodyResponse = buildErrorResponse(distributionLogic.getResponseFormatManager().getResponseFormat(ActionStatus.MISSING_BODY));
349 responseWrapper.setInnerElement(missingBodyResponse);
351 Either<RegistrationRequest, Exception> eitherRegistration = HttpUtil.convertJsonStringToObject(requestJson, RegistrationRequest.class);
352 if (eitherRegistration.isLeft()) {
353 RegistrationRequest registrationRequest = eitherRegistration.left().value();
354 if (registrationRequest.getApiPublicKey() == null) {
355 Response missingBodyResponse = buildErrorResponse(distributionLogic.getResponseFormatManager().getResponseFormat(ActionStatus.MISSING_PUBLIC_KEY));
356 responseWrapper.setInnerElement(missingBodyResponse);
358 } else if (registrationRequest.getDistrEnvName() == null) {
359 Response missingBodyResponse = buildErrorResponse(distributionLogic.getResponseFormatManager().getResponseFormat(ActionStatus.MISSING_ENV_NAME));
360 responseWrapper.setInnerElement(missingBodyResponse);
362 registrationRequestWrapper.setInnerElement(registrationRequest);
365 Response missingBodyResponse = buildErrorResponse(distributionLogic.getResponseFormatManager().getResponseFormat(ActionStatus.MISSING_BODY));
366 responseWrapper.setInnerElement(missingBodyResponse);
372 private DistributionBusinessLogic getDistributionBL(ServletContext context) {
373 WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
374 WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
375 return webApplicationContext.getBean(DistributionBusinessLogic.class);
378 private AuditHandler buildAuditHandler(HttpServletRequest request, RegistrationRequest registrationRequest) {
379 return new AuditHandler(getComponentsUtils(), request.getHeader(Constants.X_ECOMP_INSTANCE_ID_HEADER), registrationRequest);