2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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=========================================================
20 package org.openecomp.sdc.be.externalapi.servlet;
22 import com.fasterxml.jackson.databind.ObjectMapper;
23 import com.jcabi.aspects.Loggable;
24 import fj.data.Either;
25 import io.swagger.v3.oas.annotations.Operation;
26 import io.swagger.v3.oas.annotations.Parameter;
27 import io.swagger.v3.oas.annotations.responses.ApiResponse;
28 import io.swagger.v3.oas.annotations.servers.Server;
29 import io.swagger.v3.oas.annotations.tags.Tag;
30 import java.io.IOException;
31 import javax.inject.Inject;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.ws.rs.Consumes;
34 import javax.ws.rs.HeaderParam;
35 import javax.ws.rs.POST;
36 import javax.ws.rs.Path;
37 import javax.ws.rs.PathParam;
38 import javax.ws.rs.Produces;
39 import javax.ws.rs.core.Context;
40 import javax.ws.rs.core.MediaType;
41 import javax.ws.rs.core.Response;
42 import org.apache.commons.lang3.StringUtils;
43 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
44 import org.openecomp.sdc.be.components.impl.ResourceImportManager;
45 import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic;
46 import org.openecomp.sdc.be.components.impl.aaf.AafPermission;
47 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
48 import org.openecomp.sdc.be.config.BeEcompErrorManager;
49 import org.openecomp.sdc.be.dao.api.ActionStatus;
50 import org.openecomp.sdc.be.externalapi.servlet.representation.ServiceDistributionReqInfo;
51 import org.openecomp.sdc.be.externalapi.servlet.representation.ServiceDistributionRespInfo;
52 import org.openecomp.sdc.be.impl.ComponentsUtils;
53 import org.openecomp.sdc.be.impl.ServletUtils;
54 import org.openecomp.sdc.be.model.User;
55 import org.openecomp.sdc.be.resources.data.auditing.model.DistributionData;
56 import org.openecomp.sdc.be.servlets.AbstractValidationsServlet;
57 import org.openecomp.sdc.be.servlets.RepresentationUtils;
58 import org.openecomp.sdc.common.api.Constants;
59 import org.openecomp.sdc.common.datastructure.Wrapper;
60 import org.openecomp.sdc.common.log.wrappers.Logger;
61 import org.openecomp.sdc.exception.ResponseFormat;
62 import org.springframework.stereotype.Controller;
65 * Created by chaya on 10/17/2017.
67 @SuppressWarnings("ALL")
68 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
70 @Tag(name = "SDCE-7 APIs")
73 public class ServiceActivationServlet extends AbstractValidationsServlet {
75 private static final Logger log = Logger.getLogger(ServiceActivationServlet.class);
76 private final ServiceBusinessLogic serviceBusinessLogic;
78 private HttpServletRequest request;
81 public ServiceActivationServlet(ComponentInstanceBusinessLogic componentInstanceBL,
82 ComponentsUtils componentsUtils, ServletUtils servletUtils, ResourceImportManager resourceImportManager,
83 ServiceBusinessLogic serviceBusinessLogic) {
84 super(componentInstanceBL, componentsUtils, servletUtils, resourceImportManager);
85 this.serviceBusinessLogic = serviceBusinessLogic;
89 * Activates a service on a specific environment
92 @Path("/services/{serviceUUID}/distribution/{opEnvId}/activate")
93 @Consumes(MediaType.APPLICATION_JSON)
94 @Produces(MediaType.APPLICATION_JSON)
95 @Operation(description = "activate a service", method = "POST", summary = "Activates a service", responses = {
96 @ApiResponse(responseCode = "202", description = "ECOMP component is authenticated and required service may be distributed"),
97 @ApiResponse(responseCode = "400", description = "Missing X-ECOMP-InstanceID HTTP header - POL5001"),
98 @ApiResponse(responseCode = "401", description = "ECOMP component should authenticate itself and to re-send again HTTP request with its Basic Authentication credentials - POL5002"),
99 @ApiResponse(responseCode = "403", description = "ECOMP component is not authorized - POL5003"),
100 @ApiResponse(responseCode = "404", description = "Error: Requested '%1' (uuid) resource was not found - SVC4063"),
101 @ApiResponse(responseCode = "405", description = "Method Not Allowed : Invalid HTTP method type used ( PUT,DELETE,POST will be rejected) - POL4050"),
102 @ApiResponse(responseCode = "500", description = "The request failed either due to internal SDC problem. ECOMP Component should continue the attempts to get the needed information - POL5000"),
103 @ApiResponse(responseCode = "400", description = "Invalid field format. One of the provided fields does not comply with the field rules - SVC4126"),
104 @ApiResponse(responseCode = "400", description = "Missing request body. The post request did not contain the expected body - SVC4500"),
105 @ApiResponse(responseCode = "400", description = "The resource name is missing in the request body - SVC4062"),
106 @ApiResponse(responseCode = "409", description = "Service state is invalid for this action"),
107 @ApiResponse(responseCode = "502", description = "The server was acting as a gateway or proxy and received an invalid response from the upstream server")})
108 @PermissionAllowed({AafPermission.PermNames.WRITE_VALUE})
109 public Response activateServiceExternal(
110 @Parameter(description = "Determines the format of the body of the request", required = true) @HeaderParam(value = Constants.CONTENT_TYPE_HEADER) String contentType,
111 @Parameter(description = "The user id", required = true) @HeaderParam(value = Constants.USER_ID_HEADER) final String userId,
112 @Parameter(description = "X-ECOMP-RequestID header", required = false) @HeaderParam(value = Constants.X_ECOMP_REQUEST_ID_HEADER) String requestId,
113 @Parameter(description = "X-ECOMP-InstanceID header", required = true) @HeaderParam(value = Constants.X_ECOMP_INSTANCE_ID_HEADER) final String instanceIdHeader,
114 @Parameter(description = "Determines the format of the body of the response", required = false) @HeaderParam(value = Constants.ACCEPT_HEADER) String accept,
115 @Parameter(description = "The username and password", required = true) @HeaderParam(value = Constants.AUTHORIZATION_HEADER) String authorization,
116 @Parameter(description = "The serviceUUid to activate", required = true) @PathParam("serviceUUID") final String serviceUUID,
117 @Parameter(description = "The operational environment on which to activate the service on", required = true) @PathParam("opEnvId") final String opEnvId,
118 String data) throws IOException {
120 ResponseFormat responseFormat = null;
121 String requestURI = request.getRequestURI();
122 String url = request.getMethod() + " " + requestURI;
123 log.debug("Start handle request of {}", url);
124 User modifier = new User();
126 Wrapper<ResponseFormat> responseWrapper = validateRequestHeaders(instanceIdHeader, userId);
127 if (responseWrapper.isEmpty()) {
128 modifier.setUserId(userId);
129 log.debug("modifier id is {}", userId);
130 ServiceDistributionReqInfo reqMetadata = convertJsonToActivationMetadata(data);
131 Either<String, ResponseFormat> distResponse = serviceBusinessLogic
132 .activateServiceOnTenantEnvironment(serviceUUID, opEnvId, modifier, reqMetadata);
133 if (distResponse.isRight()) {
134 log.debug("failed to activate service distribution");
135 responseFormat = distResponse.right().value();
136 return buildErrorResponse(responseFormat);
138 String distributionId = distResponse.left().value();
139 Object result = RepresentationUtils.toRepresentation(new ServiceDistributionRespInfo(distributionId));
140 responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.ACCEPTED);
141 return buildOkResponse(responseFormat, result);
143 log.debug("request instanceId/userId header validation failed");
144 responseFormat = responseWrapper.getInnerElement();
145 return buildErrorResponse(responseFormat);
147 } catch (Exception e) {
148 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Activate Distribution");
149 log.debug("activate distribution failed with exception", e);
153 .auditExternalActivateService(responseFormat, new DistributionData(instanceIdHeader, requestURI), requestId, serviceUUID, modifier);
157 private Wrapper<ResponseFormat> validateRequestHeaders(String instanceIdHeader, String userId) {
158 Wrapper<ResponseFormat> responseWrapper = new Wrapper<>();
159 if (responseWrapper.isEmpty()) {
160 validateXECOMPInstanceIDHeader(instanceIdHeader, responseWrapper);
162 if (responseWrapper.isEmpty()) {
163 validateHttpCspUserIdHeader(userId, responseWrapper);
165 return responseWrapper;
168 private ServiceDistributionReqInfo convertJsonToActivationMetadata(String data) {
169 ObjectMapper mapper = new ObjectMapper();
171 return mapper.readValue(data, ServiceDistributionReqInfo.class);
172 } catch (IOException e) {
173 log.error("#convertJsonToActivationMetadata - json deserialization failed with error: ", e);
174 return new ServiceDistributionReqInfo(null);
179 protected void validateHttpCspUserIdHeader(String header, Wrapper<ResponseFormat> responseWrapper) {
180 ResponseFormat responseFormat;
181 if (StringUtils.isEmpty(header)) {
182 log.debug("MissingUSER_ID");
183 responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.AUTH_FAILED);
184 responseWrapper.setInnerElement(responseFormat);