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 javax.annotation.Resource;
24 import javax.inject.Singleton;
25 import javax.servlet.ServletContext;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.ws.rs.Consumes;
28 import javax.ws.rs.GET;
29 import javax.ws.rs.HeaderParam;
30 import javax.ws.rs.POST;
31 import javax.ws.rs.Path;
32 import javax.ws.rs.Produces;
33 import javax.ws.rs.core.Context;
34 import javax.ws.rs.core.MediaType;
35 import javax.ws.rs.core.Response;
37 import org.openecomp.sdc.be.config.BeEcompErrorManager;
38 import org.openecomp.sdc.be.dao.api.ActionStatus;
39 import org.openecomp.sdc.be.distribution.AuditHandler;
40 import org.openecomp.sdc.be.distribution.DistributionBusinessLogic;
41 import org.openecomp.sdc.be.distribution.api.client.RegistrationRequest;
42 import org.openecomp.sdc.be.distribution.api.client.ServerListResponse;
43 import org.openecomp.sdc.be.distribution.api.client.TopicRegistrationResponse;
44 import org.openecomp.sdc.be.distribution.api.client.TopicUnregistrationResponse;
45 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
46 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
47 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
48 import org.openecomp.sdc.be.servlets.BeGenericServlet;
49 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
50 import org.openecomp.sdc.common.api.Constants;
51 import org.openecomp.sdc.common.config.EcompErrorName;
52 import org.openecomp.sdc.common.datastructure.Wrapper;
53 import org.openecomp.sdc.common.util.HttpUtil;
54 import org.openecomp.sdc.exception.ResponseFormat;
55 import org.slf4j.Logger;
56 import org.slf4j.LoggerFactory;
57 import org.springframework.web.context.WebApplicationContext;
59 import com.jcabi.aspects.Loggable;
61 import fj.data.Either;
62 import io.swagger.annotations.Api;
63 import io.swagger.annotations.ApiImplicitParam;
64 import io.swagger.annotations.ApiImplicitParams;
65 import io.swagger.annotations.ApiOperation;
66 import io.swagger.annotations.ApiParam;
67 import io.swagger.annotations.ApiResponse;
68 import io.swagger.annotations.ApiResponses;
69 import io.swagger.annotations.ResponseHeader;
72 * This Servlet serves external users for distribution purposes.
78 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
80 @Api(value = "Distribution Servlet", description = "This Servlet serves external users for distribution purposes.")
82 public class DistributionServlet extends BeGenericServlet {
84 private static Logger log = LoggerFactory.getLogger(DistributionServlet.class.getName());
86 private DistributionBusinessLogic distributionLogic;
88 private HttpServletRequest request;
95 * @param authorization
99 @Path("/distributionUebCluster")
100 @Consumes(MediaType.APPLICATION_JSON)
101 @Produces(MediaType.APPLICATION_JSON)
102 @ApiOperation(value = "UEB Server List", httpMethod = "GET", notes = "return the available UEB Server List",
103 //TODO Tal G fix response headers
105 @ResponseHeader(name = Constants.CONTENT_TYPE_HEADER, description = "Determines the format of the response body", response = String.class),
106 @ResponseHeader(name = "Content-Length", description = "Length of the response body", response = String.class)})
107 @ApiResponses(value = {
108 @ApiResponse(code = 200, message = "ECOMP component is authenticated and list of Cambria API server’s FQDNs is returned", response = ServerListResponse.class),
109 @ApiResponse(code = 400, message = "Missing 'X-ECOMP-InstanceID' HTTP header - POL5001"),
110 @ApiResponse(code = 401, message = "ECOMP component should authenticate itself and to re-send again HTTP request with its credentials for Basic Authentication - POL5002"),
111 @ApiResponse(code = 403, message = "ECOMP component is not authorized - POL5003"),
112 @ApiResponse(code = 405, message = "Method Not Allowed: Invalid HTTP method type used ( PUT,DELETE,POST will be rejected) - POL4050"),
113 @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")})
114 public Response getUebServerList(
115 @ApiParam(value = "X-ECOMP-RequestID header", required = false)@HeaderParam(value = Constants.X_ECOMP_REQUEST_ID_HEADER) String requestId,
116 @ApiParam(value = "X-ECOMP-InstanceID header", required = true)@HeaderParam(value = Constants.X_ECOMP_INSTANCE_ID_HEADER) String instanceId,
117 @ApiParam(value = "Determines the format of the body of the response", required = false)@HeaderParam(value = Constants.ACCEPT_HEADER) String accept,
118 @ApiParam(value = "The username and password", required = true)@HeaderParam(value = Constants.AUTHORIZATION_HEADER) String authorization) {
121 String url = request.getMethod() + " " + request.getRequestURI();
122 log.debug("Start handle request of {}", url);
123 Response response = null;
124 ResponseFormat responseFormat = null;
126 if (instanceId == null) {
127 responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID);
128 response = buildErrorResponse(responseFormat);
129 getComponentsUtils().auditMissingInstanceId(AuditingActionEnum.GET_UEB_CLUSTER, responseFormat.getStatus().toString(), responseFormat.getFormattedMessage());
134 Either<ServerListResponse, ResponseFormat> actionResponse = distributionLogic.getUebServerList();
136 if (actionResponse.isRight()) {
137 responseFormat = actionResponse.right().value();
138 response = buildErrorResponse(responseFormat);
140 responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
141 response = buildOkResponse(responseFormat, actionResponse.left().value());
144 getComponentsUtils().auditGetUebCluster(AuditingActionEnum.GET_UEB_CLUSTER, instanceId, null, responseFormat.getStatus().toString(), responseFormat.getFormattedMessage());
147 } catch (Exception e) {
148 BeEcompErrorManager.getInstance().processEcompError(EcompErrorName.BeRestApiGeneralError, "failed to get ueb serbver list from cofiguration");
149 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("failed to get ueb serbver list from cofiguration");
150 log.debug("failed to get ueb serbver list from cofiguration", e);
151 responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
152 getComponentsUtils().auditGetUebCluster(AuditingActionEnum.GET_UEB_CLUSTER, instanceId, null, responseFormat.getStatus().toString(), responseFormat.getFormattedMessage());
153 response = buildErrorResponse(responseFormat);
165 * @param contenLength
166 * @param authorization
171 @Path("/registerForDistribution")
172 @Consumes(MediaType.APPLICATION_JSON)
173 @Produces(MediaType.APPLICATION_JSON)
174 @ApiOperation(value = "Subscription status", httpMethod = "POST", notes = "Subscribes for distribution notifications")
175 @ApiResponses(value = {
176 @ApiResponse(code = 200, message = "ECOMP component is successfully registered for distribution", response = TopicRegistrationResponse.class),
177 @ApiResponse(code = 400, message = "Missing 'X-ECOMP-InstanceID' HTTP header - POL5001"),
178 @ApiResponse(code = 400, message = "Missing Body - POL4500"),
179 @ApiResponse(code = 400, message = "Invalid Body : missing mandatory parameter 'apiPublicKey' - POL4501"),
180 @ApiResponse(code = 400, message = "Invalid Body : missing mandatory parameter 'distrEnvName' - POL4502"),
181 @ApiResponse(code = 400, message = "Invalid Body : Specified 'distrEnvName' doesn’t exist - POL4137"),
182 @ApiResponse(code = 401, message = "ECOMP component should authenticate itself and to re-send again HTTP request with its Basic Authentication credentials - POL5002"),
183 @ApiResponse(code = 403, message = "ECOMP component is not authorized - POL5003"),
184 @ApiResponse(code = 405, message = "Method Not Allowed : Invalid HTTP method type used to register for distribution ( PUT,DELETE,GET will be rejected) - POL4050"),
185 @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")})
186 //TODO Tal G fix response headers and to check missing header validations with Michael L
187 @ApiImplicitParam(required = true, dataType = "org.openecomp.sdc.be.distribution.api.client.RegistrationRequest", paramType = "body", value = "json describe the artifact")
188 public Response registerForDistribution(
189 @ApiParam(value = "X-ECOMP-RequestID header", required = false)@HeaderParam(value = Constants.X_ECOMP_REQUEST_ID_HEADER) String requestId,
190 @ApiParam(value = "X-ECOMP-InstanceID header", required = true)@HeaderParam(value = Constants.X_ECOMP_INSTANCE_ID_HEADER) String instanceId,
191 @ApiParam(value = "Determines the format of the body of the response", required = false)@HeaderParam(value = Constants.ACCEPT_HEADER) String accept,
192 @ApiParam(value = "Determines the format of the body of the request", required = true)@HeaderParam(value = Constants.CONTENT_TYPE_HEADER) String contenType,
193 @ApiParam(value = "Length of the request body", required = true)@HeaderParam(value = Constants.CONTENT_LENGTH_HEADER) String contenLength,
194 @ApiParam(value = "The username and password", required = true)@HeaderParam(value = Constants.AUTHORIZATION_HEADER) String authorization,
195 String requestJson) {
196 String url = request.getMethod() + " " + request.getRequestURI();
197 log.debug("Start handle request of {}", url);
200 Wrapper<Response> responseWrapper = new Wrapper<>();
201 Wrapper<RegistrationRequest> registrationRequestWrapper = new Wrapper<>();
203 validateHeaders(responseWrapper, request, AuditingActionEnum.ADD_KEY_TO_TOPIC_ACL);
205 if (responseWrapper.isEmpty()) {
206 validateJson(responseWrapper, registrationRequestWrapper, requestJson);
208 if (responseWrapper.isEmpty()) {
209 validateEnv(responseWrapper, registrationRequestWrapper.getInnerElement().getDistrEnvName());
212 if (responseWrapper.isEmpty()) {
213 distributionLogic.handleRegistration(responseWrapper, registrationRequestWrapper.getInnerElement(), buildAuditHandler(request, registrationRequestWrapper.getInnerElement()));
215 BeEcompErrorManager.getInstance().processEcompError(EcompErrorName.BeDistributionEngineSystemError, DistributionBusinessLogic.REGISTER_IN_DISTRIBUTION_ENGINE, "registration validation failed");
216 BeEcompErrorManager.getInstance().logBeDistributionEngineSystemError(DistributionBusinessLogic.REGISTER_IN_DISTRIBUTION_ENGINE, "registration validation failed");
219 return responseWrapper.getInnerElement();
223 * Returns list of valid artifact types for validation done in the distribution client.<br>
224 * The list is the representation of the values of the enum ArtifactTypeEnum.
228 * @param authorization
233 @Path("/artifactTypes")
234 @Consumes(MediaType.APPLICATION_JSON)
235 @Produces(MediaType.APPLICATION_JSON)
236 @ApiOperation(value = "Artifact types list", httpMethod = "GET", notes = "Fetches available artifact types list")
237 @ApiResponses(value = {
238 @ApiResponse(code = 200, message = "Artifact types list fetched successfully", response = String.class),
239 @ApiResponse(code = 400, message = "Missing 'X-ECOMP-InstanceID' HTTP header - POL5001"),
240 @ApiResponse(code = 401, message = "ECOMP component should authenticate itself and to re-send again HTTP request with its Basic Authentication credentials - POL5002"),
241 @ApiResponse(code = 403, message = "ECOMP component is not authorized - POL5003"),
242 @ApiResponse(code = 405, message = "Method Not Allowed : Invalid HTTP method type used to register for distribution ( POST,PUT,DELETE will be rejected) - POL4050"),
243 @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")})
244 public Response getValidArtifactTypes(
245 @ApiParam(value = "X-ECOMP-RequestID header", required = false)@HeaderParam(value = Constants.X_ECOMP_REQUEST_ID_HEADER) String requestId,
246 @ApiParam(value = "X-ECOMP-InstanceID header", required = true)@HeaderParam(value = Constants.X_ECOMP_INSTANCE_ID_HEADER) String instanceId,
247 @ApiParam(value = "The username and password", required = true)@HeaderParam(value = Constants.AUTHORIZATION_HEADER) String authorization,
248 @ApiParam(value = "The username and password", required = true)@HeaderParam(value = Constants.ACCEPT_HEADER) String accept) {
250 String url = request.getMethod() + " " + request.getRequestURI();
251 log.debug("Start handle request of {}", url);
252 Response response = null;
254 Wrapper<Response> responseWrapper = new Wrapper<>();
256 validateHeaders(responseWrapper, request, AuditingActionEnum.GET_VALID_ARTIFACT_TYPES);
257 if (responseWrapper.isEmpty()) {
258 response = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), ArtifactTypeEnum.values());
260 response = responseWrapper.getInnerElement();
266 * Removes from subscription for distribution notifications
272 * @param contenLength
273 * @param authorization
278 @Path("/unRegisterForDistribution")
279 @Consumes(MediaType.APPLICATION_JSON)
280 @Produces(MediaType.APPLICATION_JSON)
281 @ApiOperation(value = "Subscription status", httpMethod = "POST", notes = "Removes from subscription for distribution notifications")
282 //TODO Edit the responses
283 @ApiResponses(value = {
284 @ApiResponse(code = 204, message = "ECOMP component is successfully unregistered", response = TopicUnregistrationResponse.class),
285 @ApiResponse(code = 400, message = "Missing 'X-ECOMP-InstanceID' HTTP header - POL5001"),
286 @ApiResponse(code = 400, message = "Missing Body - POL4500"),
287 @ApiResponse(code = 400, message = "Invalid Body : missing mandatory parameter 'apiPublicKey' - POL4501"),
288 @ApiResponse(code = 400, message = "Invalid Body : missing mandatory parameter 'distrEnvName' - SVC4506"),
289 @ApiResponse(code = 400, message = "Invalid Body : Specified 'distrEnvName' doesn’t exist - POL4137"),
290 @ApiResponse(code = 401, message = "ECOMP component should authenticate itself and to re-send again HTTP request with its Basic Authentication credentials - POL5002"),
291 @ApiResponse(code = 403, message = "ECOMP component is not authorized - POL5003"),
292 @ApiResponse(code = 405, message = "Method Not Allowed : Invalid HTTP method type used to register for distribution ( PUT,DELETE,GET will be rejected) - POL4050"),
293 @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")})
294 @ApiImplicitParam(required = true, dataType = "org.openecomp.sdc.be.distribution.api.client.RegistrationRequest", paramType = "body", value = "json describe the artifact")
295 public Response unRegisterForDistribution(
296 @ApiParam(value = "X-ECOMP-RequestID header", required = false)@HeaderParam(value = Constants.X_ECOMP_REQUEST_ID_HEADER) String requestId,
297 @ApiParam(value = "X-ECOMP-InstanceID header", required = true)@HeaderParam(value = Constants.X_ECOMP_INSTANCE_ID_HEADER) String instanceId,
298 @ApiParam(value = "Determines the format of the body of the response", required = false)@HeaderParam(value = Constants.ACCEPT_HEADER) String accept,
299 @ApiParam(value = "Determines the format of the body of the request", required = true)@HeaderParam(value = Constants.CONTENT_TYPE_HEADER) String contenType,
300 @ApiParam(value = "Length of the request body", required = true)@HeaderParam(value = Constants.CONTENT_LENGTH_HEADER) String contenLength,
301 @ApiParam(value = "The username and password", required = true)@HeaderParam(value = Constants.AUTHORIZATION_HEADER) String authorization,
302 String requestJson) {
304 String url = request.getMethod() + " " + request.getRequestURI();
305 log.debug("Start handle request of {}", url);
308 Wrapper<Response> responseWrapper = new Wrapper<>();
309 Wrapper<RegistrationRequest> unRegistrationRequestWrapper = new Wrapper<>();
311 validateHeaders(responseWrapper, request, AuditingActionEnum.REMOVE_KEY_FROM_TOPIC_ACL);
313 if (responseWrapper.isEmpty()) {
314 validateJson(responseWrapper, unRegistrationRequestWrapper, requestJson);
316 if (responseWrapper.isEmpty()) {
317 validateEnv(responseWrapper, unRegistrationRequestWrapper.getInnerElement().getDistrEnvName());
319 if (responseWrapper.isEmpty()) {
320 distributionLogic.handleUnRegistration(responseWrapper, unRegistrationRequestWrapper.getInnerElement(), buildAuditHandler(request, unRegistrationRequestWrapper.getInnerElement()));
322 BeEcompErrorManager.getInstance().processEcompError(EcompErrorName.BeDistributionEngineSystemError, DistributionBusinessLogic.UN_REGISTER_IN_DISTRIBUTION_ENGINE, "unregistration validation failed");
323 BeEcompErrorManager.getInstance().logBeDistributionEngineSystemError(DistributionBusinessLogic.UN_REGISTER_IN_DISTRIBUTION_ENGINE, "unregistration validation failed");
326 return responseWrapper.getInnerElement();
329 private void validateEnv(Wrapper<Response> responseWrapper, String distrEnvName) {
332 StorageOperationStatus environmentStatus = distributionLogic.getDistributionEngine().isEnvironmentAvailable();
334 // StorageOperationStatus environmentStatus =
335 // distributionLogic.getDistributionEngine().isEnvironmentAvailable(distrEnvName);
336 if (environmentStatus != StorageOperationStatus.OK) {
337 if (environmentStatus == StorageOperationStatus.DISTR_ENVIRONMENT_NOT_FOUND) {
338 Response missingHeaderResponse = buildErrorResponse(distributionLogic.getResponseFormatManager().getResponseFormat(ActionStatus.DISTRIBUTION_ENV_DOES_NOT_EXIST));
339 responseWrapper.setInnerElement(missingHeaderResponse);
341 Response missingHeaderResponse = buildErrorResponse(distributionLogic.getResponseFormatManager().getResponseFormat(ActionStatus.GENERAL_ERROR));
342 responseWrapper.setInnerElement(missingHeaderResponse);
348 private void init(HttpServletRequest request) {
349 if (distributionLogic == null) {
350 distributionLogic = getDistributionBL(request.getSession().getServletContext());
354 private void validateHeaders(Wrapper<Response> responseWrapper, HttpServletRequest request, AuditingActionEnum auditingAction) {
355 if (request.getHeader(Constants.X_ECOMP_INSTANCE_ID_HEADER) == null) {
356 Response missingHeaderResponse = buildErrorResponse(distributionLogic.getResponseFormatManager().getResponseFormat(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID));
357 responseWrapper.setInnerElement(missingHeaderResponse);
359 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID);
360 getComponentsUtils().auditMissingInstanceId(auditingAction, responseFormat.getStatus().toString(), responseFormat.getFormattedMessage());
366 private void validateJson(Wrapper<Response> responseWrapper, Wrapper<RegistrationRequest> registrationRequestWrapper, String requestJson) {
367 if (requestJson == null || requestJson.isEmpty()) {
368 Response missingBodyResponse = buildErrorResponse(distributionLogic.getResponseFormatManager().getResponseFormat(ActionStatus.MISSING_BODY));
369 responseWrapper.setInnerElement(missingBodyResponse);
371 Either<RegistrationRequest, Exception> eitherRegistration = HttpUtil.convertJsonStringToObject(requestJson, RegistrationRequest.class);
372 if (eitherRegistration.isLeft()) {
373 RegistrationRequest registrationRequest = eitherRegistration.left().value();
374 if (registrationRequest.getApiPublicKey() == null) {
375 Response missingBodyResponse = buildErrorResponse(distributionLogic.getResponseFormatManager().getResponseFormat(ActionStatus.MISSING_PUBLIC_KEY));
376 responseWrapper.setInnerElement(missingBodyResponse);
378 } else if (registrationRequest.getDistrEnvName() == null) {
379 Response missingBodyResponse = buildErrorResponse(distributionLogic.getResponseFormatManager().getResponseFormat(ActionStatus.MISSING_ENV_NAME));
380 responseWrapper.setInnerElement(missingBodyResponse);
382 registrationRequestWrapper.setInnerElement(registrationRequest);
385 Response missingBodyResponse = buildErrorResponse(distributionLogic.getResponseFormatManager().getResponseFormat(ActionStatus.MISSING_BODY));
386 responseWrapper.setInnerElement(missingBodyResponse);
392 private DistributionBusinessLogic getDistributionBL(ServletContext context) {
393 WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
394 WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
395 return webApplicationContext.getBean(DistributionBusinessLogic.class);
398 private AuditHandler buildAuditHandler(HttpServletRequest request, RegistrationRequest registrationRequest) {
399 return new AuditHandler(getComponentsUtils(), request.getHeader(Constants.X_ECOMP_INSTANCE_ID_HEADER), registrationRequest);