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.datastructure.Wrapper;
52 import org.openecomp.sdc.common.util.HttpUtil;
53 import org.openecomp.sdc.exception.ResponseFormat;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56 import org.springframework.web.context.WebApplicationContext;
58 import com.jcabi.aspects.Loggable;
60 import fj.data.Either;
61 import io.swagger.annotations.Api;
62 import io.swagger.annotations.ApiImplicitParam;
63 import io.swagger.annotations.ApiImplicitParams;
64 import io.swagger.annotations.ApiOperation;
65 import io.swagger.annotations.ApiParam;
66 import io.swagger.annotations.ApiResponse;
67 import io.swagger.annotations.ApiResponses;
68 import io.swagger.annotations.ResponseHeader;
71 * This Servlet serves external users for distribution purposes.
77 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
79 @Api(value = "Distribution Servlet", description = "This Servlet serves external users for distribution purposes.")
81 public class DistributionServlet extends BeGenericServlet {
83 private static final Logger log = LoggerFactory.getLogger(DistributionServlet.class);
85 private DistributionBusinessLogic distributionLogic;
87 private HttpServletRequest request;
94 * @param authorization
98 @Path("/distributionUebCluster")
99 @Consumes(MediaType.APPLICATION_JSON)
100 @Produces(MediaType.APPLICATION_JSON)
101 @ApiOperation(value = "UEB Server List", httpMethod = "GET", notes = "return the available UEB Server List",
102 //TODO Tal G fix response headers
104 @ResponseHeader(name = Constants.CONTENT_TYPE_HEADER, description = "Determines the format of the response body", response = String.class),
105 @ResponseHeader(name = "Content-Length", description = "Length of the response body", response = String.class)})
106 @ApiResponses(value = {
107 @ApiResponse(code = 200, message = "ECOMP component is authenticated and list of Cambria API server’s FQDNs is returned", response = ServerListResponse.class),
108 @ApiResponse(code = 400, message = "Missing 'X-ECOMP-InstanceID' HTTP header - POL5001"),
109 @ApiResponse(code = 401, message = "ECOMP component should authenticate itself and to re-send again HTTP request with its credentials for Basic Authentication - POL5002"),
110 @ApiResponse(code = 403, message = "ECOMP component is not authorized - POL5003"),
111 @ApiResponse(code = 405, message = "Method Not Allowed: Invalid HTTP method type used ( PUT,DELETE,POST will be rejected) - POL4050"),
112 @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")})
113 public Response getUebServerList(
114 @ApiParam(value = "X-ECOMP-RequestID header", required = false)@HeaderParam(value = Constants.X_ECOMP_REQUEST_ID_HEADER) String requestId,
115 @ApiParam(value = "X-ECOMP-InstanceID header", required = true)@HeaderParam(value = Constants.X_ECOMP_INSTANCE_ID_HEADER) String instanceId,
116 @ApiParam(value = "Determines the format of the body of the response", required = false)@HeaderParam(value = Constants.ACCEPT_HEADER) String accept,
117 @ApiParam(value = "The username and password", required = true)@HeaderParam(value = Constants.AUTHORIZATION_HEADER) String authorization) {
120 String url = request.getMethod() + " " + request.getRequestURI();
121 log.debug("Start handle request of {}", url);
122 Response response = null;
123 ResponseFormat responseFormat = null;
125 if (instanceId == null) {
126 responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID);
127 response = buildErrorResponse(responseFormat);
128 getComponentsUtils().auditMissingInstanceId(AuditingActionEnum.GET_UEB_CLUSTER, responseFormat.getStatus().toString(), responseFormat.getFormattedMessage());
133 Either<ServerListResponse, ResponseFormat> actionResponse = distributionLogic.getUebServerList();
135 if (actionResponse.isRight()) {
136 responseFormat = actionResponse.right().value();
137 response = buildErrorResponse(responseFormat);
139 responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
140 response = buildOkResponse(responseFormat, actionResponse.left().value());
143 getComponentsUtils().auditGetUebCluster(AuditingActionEnum.GET_UEB_CLUSTER, instanceId, null, responseFormat.getStatus().toString(), responseFormat.getFormattedMessage());
146 } catch (Exception e) {
147 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("failed to get ueb serbver list from cofiguration");
148 log.debug("failed to get ueb serbver list from cofiguration", e);
149 responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
150 getComponentsUtils().auditGetUebCluster(AuditingActionEnum.GET_UEB_CLUSTER, instanceId, null, responseFormat.getStatus().toString(), responseFormat.getFormattedMessage());
151 response = buildErrorResponse(responseFormat);
163 * @param contenLength
164 * @param authorization
169 @Path("/registerForDistribution")
170 @Consumes(MediaType.APPLICATION_JSON)
171 @Produces(MediaType.APPLICATION_JSON)
172 @ApiOperation(value = "Subscription status", httpMethod = "POST", notes = "Subscribes for distribution notifications")
173 @ApiResponses(value = {
174 @ApiResponse(code = 200, message = "ECOMP component is successfully registered for distribution", response = TopicRegistrationResponse.class),
175 @ApiResponse(code = 400, message = "Missing 'X-ECOMP-InstanceID' HTTP header - POL5001"),
176 @ApiResponse(code = 400, message = "Missing Body - POL4500"),
177 @ApiResponse(code = 400, message = "Invalid Body : missing mandatory parameter 'apiPublicKey' - POL4501"),
178 @ApiResponse(code = 400, message = "Invalid Body : missing mandatory parameter 'distrEnvName' - POL4502"),
179 @ApiResponse(code = 400, message = "Invalid Body : Specified 'distrEnvName' doesn’t exist - POL4137"),
180 @ApiResponse(code = 401, message = "ECOMP component should authenticate itself and to re-send again HTTP request with its Basic Authentication credentials - POL5002"),
181 @ApiResponse(code = 403, message = "ECOMP component is not authorized - POL5003"),
182 @ApiResponse(code = 405, message = "Method Not Allowed : Invalid HTTP method type used to register for distribution ( PUT,DELETE,GET will be rejected) - POL4050"),
183 @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")})
184 @ApiImplicitParams({@ApiImplicitParam(name = "requestJson", required = true, dataType = "org.openecomp.sdc.be.distribution.api.client.RegistrationRequest", paramType = "body", value = "json describe the artifact")})
185 public Response registerForDistribution(
186 @ApiParam(value = "X-ECOMP-RequestID header", required = false)@HeaderParam(value = Constants.X_ECOMP_REQUEST_ID_HEADER) String requestId,
187 @ApiParam(value = "X-ECOMP-InstanceID header", required = true)@HeaderParam(value = Constants.X_ECOMP_INSTANCE_ID_HEADER) String instanceId,
188 @ApiParam(value = "Determines the format of the body of the response", required = false)@HeaderParam(value = Constants.ACCEPT_HEADER) String accept,
189 @ApiParam(value = "Determines the format of the body of the request", required = true)@HeaderParam(value = Constants.CONTENT_TYPE_HEADER) String contenType,
190 @ApiParam(value = "Length of the request body", required = true)@HeaderParam(value = Constants.CONTENT_LENGTH_HEADER) String contenLength,
191 @ApiParam(value = "The username and password", required = true)@HeaderParam(value = Constants.AUTHORIZATION_HEADER) String authorization,
192 @ApiParam( hidden = true) String requestJson) {
193 String url = request.getMethod() + " " + request.getRequestURI();
194 log.debug("Start handle request of {}", url);
197 Wrapper<Response> responseWrapper = new Wrapper<>();
198 Wrapper<RegistrationRequest> registrationRequestWrapper = new Wrapper<>();
200 validateHeaders(responseWrapper, request, AuditingActionEnum.ADD_KEY_TO_TOPIC_ACL);
202 if (responseWrapper.isEmpty()) {
203 validateJson(responseWrapper, registrationRequestWrapper, requestJson);
205 if (responseWrapper.isEmpty()) {
206 validateEnv(responseWrapper, registrationRequestWrapper.getInnerElement().getDistrEnvName());
209 if (responseWrapper.isEmpty()) {
210 distributionLogic.handleRegistration(responseWrapper, registrationRequestWrapper.getInnerElement(), buildAuditHandler(request, registrationRequestWrapper.getInnerElement()));
212 BeEcompErrorManager.getInstance().logBeDistributionEngineSystemError(DistributionBusinessLogic.REGISTER_IN_DISTRIBUTION_ENGINE, "registration validation failed");
215 return responseWrapper.getInnerElement();
219 * Returns list of valid artifact types for validation done in the distribution client.<br>
220 * The list is the representation of the values of the enum ArtifactTypeEnum.
224 * @param authorization
229 @Path("/artifactTypes")
230 @Consumes(MediaType.APPLICATION_JSON)
231 @Produces(MediaType.APPLICATION_JSON)
232 @ApiOperation(value = "Artifact types list", httpMethod = "GET", notes = "Fetches available artifact types list")
233 @ApiResponses(value = {
234 @ApiResponse(code = 200, message = "Artifact types list fetched successfully", response = String.class),
235 @ApiResponse(code = 400, message = "Missing 'X-ECOMP-InstanceID' HTTP header - POL5001"),
236 @ApiResponse(code = 401, message = "ECOMP component should authenticate itself and to re-send again HTTP request with its Basic Authentication credentials - POL5002"),
237 @ApiResponse(code = 403, message = "ECOMP component is not authorized - POL5003"),
238 @ApiResponse(code = 405, message = "Method Not Allowed : Invalid HTTP method type used to register for distribution ( POST,PUT,DELETE will be rejected) - POL4050"),
239 @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")})
240 public Response getValidArtifactTypes(
241 @ApiParam(value = "X-ECOMP-RequestID header", required = false)@HeaderParam(value = Constants.X_ECOMP_REQUEST_ID_HEADER) String requestId,
242 @ApiParam(value = "X-ECOMP-InstanceID header", required = true)@HeaderParam(value = Constants.X_ECOMP_INSTANCE_ID_HEADER) String instanceId,
243 @ApiParam(value = "The username and password", required = true)@HeaderParam(value = Constants.AUTHORIZATION_HEADER) String authorization,
244 @ApiParam(value = "The username and password", required = true)@HeaderParam(value = Constants.ACCEPT_HEADER) String accept) {
246 String url = request.getMethod() + " " + request.getRequestURI();
247 log.debug("Start handle request of {}", url);
248 Response response = null;
250 Wrapper<Response> responseWrapper = new Wrapper<>();
252 validateHeaders(responseWrapper, request, AuditingActionEnum.GET_VALID_ARTIFACT_TYPES);
253 if (responseWrapper.isEmpty()) {
254 response = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), ArtifactTypeEnum.values());
256 response = responseWrapper.getInnerElement();
262 * Removes from subscription for distribution notifications
268 * @param contenLength
269 * @param authorization
274 @Path("/unRegisterForDistribution")
275 @Consumes(MediaType.APPLICATION_JSON)
276 @Produces(MediaType.APPLICATION_JSON)
277 @ApiOperation(value = "Subscription status", httpMethod = "POST", notes = "Removes from subscription for distribution notifications")
278 //TODO Edit the responses
279 @ApiResponses(value = {
280 @ApiResponse(code = 204, message = "ECOMP component is successfully unregistered", response = TopicUnregistrationResponse.class),
281 @ApiResponse(code = 400, message = "Missing 'X-ECOMP-InstanceID' HTTP header - POL5001"),
282 @ApiResponse(code = 400, message = "Missing Body - POL4500"),
283 @ApiResponse(code = 400, message = "Invalid Body : missing mandatory parameter 'apiPublicKey' - POL4501"),
284 @ApiResponse(code = 400, message = "Invalid Body : missing mandatory parameter 'distrEnvName' - SVC4506"),
285 @ApiResponse(code = 400, message = "Invalid Body : Specified 'distrEnvName' doesn’t exist - POL4137"),
286 @ApiResponse(code = 401, message = "ECOMP component should authenticate itself and to re-send again HTTP request with its Basic Authentication credentials - POL5002"),
287 @ApiResponse(code = 403, message = "ECOMP component is not authorized - POL5003"),
288 @ApiResponse(code = 405, message = "Method Not Allowed : Invalid HTTP method type used to register for distribution ( PUT,DELETE,GET will be rejected) - POL4050"),
289 @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")})
290 @ApiImplicitParams({@ApiImplicitParam(name = "requestJson", required = true, dataType = "org.openecomp.sdc.be.distribution.api.client.RegistrationRequest", paramType = "body", value = "json describe the artifact")})
291 public Response unRegisterForDistribution(
292 @ApiParam(value = "X-ECOMP-RequestID header", required = false)@HeaderParam(value = Constants.X_ECOMP_REQUEST_ID_HEADER) String requestId,
293 @ApiParam(value = "X-ECOMP-InstanceID header", required = true)@HeaderParam(value = Constants.X_ECOMP_INSTANCE_ID_HEADER) String instanceId,
294 @ApiParam(value = "Determines the format of the body of the response", required = false)@HeaderParam(value = Constants.ACCEPT_HEADER) String accept,
295 @ApiParam(value = "Determines the format of the body of the request", required = true)@HeaderParam(value = Constants.CONTENT_TYPE_HEADER) String contenType,
296 @ApiParam(value = "Length of the request body", required = true)@HeaderParam(value = Constants.CONTENT_LENGTH_HEADER) String contenLength,
297 @ApiParam(value = "The username and password", required = true)@HeaderParam(value = Constants.AUTHORIZATION_HEADER) String authorization,
298 @ApiParam( hidden = true) String requestJson) {
300 String url = request.getMethod() + " " + request.getRequestURI();
301 log.debug("Start handle request of {}", url);
304 Wrapper<Response> responseWrapper = new Wrapper<>();
305 Wrapper<RegistrationRequest> unRegistrationRequestWrapper = new Wrapper<>();
307 validateHeaders(responseWrapper, request, AuditingActionEnum.REMOVE_KEY_FROM_TOPIC_ACL);
309 if (responseWrapper.isEmpty()) {
310 validateJson(responseWrapper, unRegistrationRequestWrapper, requestJson);
312 if (responseWrapper.isEmpty()) {
313 validateEnv(responseWrapper, unRegistrationRequestWrapper.getInnerElement().getDistrEnvName());
315 if (responseWrapper.isEmpty()) {
316 distributionLogic.handleUnRegistration(responseWrapper, unRegistrationRequestWrapper.getInnerElement(), buildAuditHandler(request, unRegistrationRequestWrapper.getInnerElement()));
318 BeEcompErrorManager.getInstance().logBeDistributionEngineSystemError(DistributionBusinessLogic.UN_REGISTER_IN_DISTRIBUTION_ENGINE, "unregistration validation failed");
321 return responseWrapper.getInnerElement();
324 private void validateEnv(Wrapper<Response> responseWrapper, String distrEnvName) {
327 StorageOperationStatus environmentStatus = distributionLogic.getDistributionEngine().isEnvironmentAvailable();
328 if (environmentStatus != StorageOperationStatus.OK) {
329 if (environmentStatus == StorageOperationStatus.DISTR_ENVIRONMENT_NOT_FOUND) {
330 Response missingHeaderResponse = buildErrorResponse(distributionLogic.getResponseFormatManager().getResponseFormat(ActionStatus.DISTRIBUTION_ENV_DOES_NOT_EXIST));
331 responseWrapper.setInnerElement(missingHeaderResponse);
333 Response missingHeaderResponse = buildErrorResponse(distributionLogic.getResponseFormatManager().getResponseFormat(ActionStatus.GENERAL_ERROR));
334 responseWrapper.setInnerElement(missingHeaderResponse);
340 private void init(HttpServletRequest request) {
341 if (distributionLogic == null) {
342 distributionLogic = getDistributionBL(request.getSession().getServletContext());
346 private void validateHeaders(Wrapper<Response> responseWrapper, HttpServletRequest request, AuditingActionEnum auditingAction) {
347 if (request.getHeader(Constants.X_ECOMP_INSTANCE_ID_HEADER) == null) {
348 Response missingHeaderResponse = buildErrorResponse(distributionLogic.getResponseFormatManager().getResponseFormat(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID));
349 responseWrapper.setInnerElement(missingHeaderResponse);
351 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID);
352 getComponentsUtils().auditMissingInstanceId(auditingAction, responseFormat.getStatus().toString(), responseFormat.getFormattedMessage());
358 private void validateJson(Wrapper<Response> responseWrapper, Wrapper<RegistrationRequest> registrationRequestWrapper, String requestJson) {
359 if (requestJson == null || requestJson.isEmpty()) {
360 Response missingBodyResponse = buildErrorResponse(distributionLogic.getResponseFormatManager().getResponseFormat(ActionStatus.MISSING_BODY));
361 responseWrapper.setInnerElement(missingBodyResponse);
363 Either<RegistrationRequest, Exception> eitherRegistration = HttpUtil.convertJsonStringToObject(requestJson, RegistrationRequest.class);
364 if (eitherRegistration.isLeft()) {
365 RegistrationRequest registrationRequest = eitherRegistration.left().value();
366 if (registrationRequest.getApiPublicKey() == null) {
367 Response missingBodyResponse = buildErrorResponse(distributionLogic.getResponseFormatManager().getResponseFormat(ActionStatus.MISSING_PUBLIC_KEY));
368 responseWrapper.setInnerElement(missingBodyResponse);
370 } else if (registrationRequest.getDistrEnvName() == null) {
371 Response missingBodyResponse = buildErrorResponse(distributionLogic.getResponseFormatManager().getResponseFormat(ActionStatus.MISSING_ENV_NAME));
372 responseWrapper.setInnerElement(missingBodyResponse);
374 registrationRequestWrapper.setInnerElement(registrationRequest);
377 Response missingBodyResponse = buildErrorResponse(distributionLogic.getResponseFormatManager().getResponseFormat(ActionStatus.MISSING_BODY));
378 responseWrapper.setInnerElement(missingBodyResponse);
384 private DistributionBusinessLogic getDistributionBL(ServletContext context) {
385 WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
386 WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
387 return webApplicationContext.getBean(DistributionBusinessLogic.class);
390 private AuditHandler buildAuditHandler(HttpServletRequest request, RegistrationRequest registrationRequest) {
391 return new AuditHandler(getComponentsUtils(), request.getHeader(Constants.X_ECOMP_INSTANCE_ID_HEADER), registrationRequest);