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.servlets;
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import com.jcabi.aspects.Loggable;
25 import fj.data.Either;
26 import io.swagger.annotations.*;
27 import javax.inject.Inject;
28 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
29 import org.openecomp.sdc.be.components.impl.GroupBusinessLogic;
30 import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic;
31 import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoBase;
32 import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction;
33 import org.openecomp.sdc.be.config.BeEcompErrorManager;
34 import org.openecomp.sdc.be.dao.api.ActionStatus;
35 import org.openecomp.sdc.be.datamodel.utils.UiComponentDataConverter;
36 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
37 import org.openecomp.sdc.be.impl.ComponentsUtils;
38 import org.openecomp.sdc.be.model.Component;
39 import org.openecomp.sdc.be.model.LifeCycleTransitionEnum;
40 import org.openecomp.sdc.be.model.User;
41 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
42 import org.openecomp.sdc.be.ui.model.UiComponentMetadata;
43 import org.openecomp.sdc.be.user.UserBusinessLogic;
44 import org.openecomp.sdc.common.api.Constants;
45 import org.openecomp.sdc.common.log.wrappers.Logger;
46 import org.openecomp.sdc.exception.ResponseFormat;
48 import javax.inject.Singleton;
49 import javax.servlet.ServletContext;
50 import javax.servlet.http.HttpServletRequest;
52 import javax.ws.rs.core.Context;
53 import javax.ws.rs.core.MediaType;
54 import javax.ws.rs.core.Response;
55 import org.springframework.beans.factory.annotation.Autowired;
57 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
59 @Api(value = "Lifecycle Actions Servlet", description = "Lifecycle Actions Servlet")
61 public class LifecycleServlet extends BeGenericServlet {
63 private static final Logger log = Logger.getLogger(LifecycleServlet.class);
64 private final LifecycleBusinessLogic lifecycleBusinessLogic;
67 public LifecycleServlet(UserBusinessLogic userBusinessLogic,
68 ComponentsUtils componentsUtils,
69 LifecycleBusinessLogic lifecycleBusinessLogic) {
70 super(userBusinessLogic, componentsUtils);
71 this.lifecycleBusinessLogic = lifecycleBusinessLogic;
76 @Path("/{componentCollection}/{componentId}/lifecycleState/{lifecycleOperation}")
77 @Consumes(MediaType.APPLICATION_JSON)
78 @Produces(MediaType.APPLICATION_JSON)
79 @ApiOperation(value = "Change Resource lifecycle State", httpMethod = "POST", response = Response.class)
80 @ApiResponses(value = { @ApiResponse(code = 200, message = "Resource state changed"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 409, message = "Resource already exist") })
81 public Response changeResourceState(@ApiParam(value = "LifecycleChangeInfo - relevant for checkin, failCertification, cancelCertification", required = false) String jsonChangeInfo,
82 @ApiParam(value = "validValues: resources / services / products", allowableValues = ComponentTypeEnum.RESOURCE_PARAM_NAME + "," + ComponentTypeEnum.SERVICE_PARAM_NAME + ","
83 + ComponentTypeEnum.PRODUCT_PARAM_NAME) @PathParam(value = "componentCollection") final String componentCollection,
84 @ApiParam(allowableValues = "checkout, undoCheckout, checkin, certificationRequest, startCertification, failCertification, cancelCertification, certify", required = true) @PathParam(value = "lifecycleOperation") final String lifecycleTransition,
85 @ApiParam(value = "id of component to be changed") @PathParam(value = "componentId") final String componentId, @Context final HttpServletRequest request,
86 @ApiParam(value = "id of user initiating the operation") @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
88 String url = request.getMethod() + " " + request.getRequestURI();
89 log.debug("Start handle request of {}", url);
91 Response response = null;
93 // get modifier from graph
94 log.debug("get modifier properties");
95 Either<User, ResponseFormat> eitherGetUser = getUser(request, userId);
96 if (eitherGetUser.isRight()) {
97 return buildErrorResponse(eitherGetUser.right().value());
99 User user = eitherGetUser.left().value();
101 String resourceIdLower = componentId.toLowerCase();
102 log.debug("perform {} operation to resource with id {} ", lifecycleTransition, resourceIdLower);
103 Either<LifeCycleTransitionEnum, Response> validateEnum = validateTransitionEnum(lifecycleTransition, user);
104 if (validateEnum.isRight()) {
105 return validateEnum.right().value();
108 LifecycleChangeInfoWithAction changeInfo = new LifecycleChangeInfoWithAction();
111 if (jsonChangeInfo != null && !jsonChangeInfo.isEmpty()) {
112 ObjectMapper mapper = new ObjectMapper();
113 changeInfo = new LifecycleChangeInfoWithAction(mapper.readValue(jsonChangeInfo, LifecycleChangeInfoBase.class).getUserRemarks());
117 catch (Exception e) {
118 BeEcompErrorManager.getInstance().logBeInvalidJsonInput("convertJsonToObject");
119 log.debug("failed to convert from json {}", jsonChangeInfo, e);
120 ResponseFormat responseFormat = getComponentsUtils().getInvalidContentErrorAndAudit(user, componentId, AuditingActionEnum.CHECKOUT_RESOURCE);
121 return buildErrorResponse(responseFormat);
125 LifeCycleTransitionEnum transitionEnum = validateEnum.left().value();
126 ComponentTypeEnum componentType = ComponentTypeEnum.findByParamName(componentCollection);
127 if (componentType != null) {
128 Either<? extends Component, ResponseFormat> actionResponse = lifecycleBusinessLogic
129 .changeComponentState(componentType, componentId, user, transitionEnum, changeInfo, false, true);
131 if (actionResponse.isRight()) {
132 log.info("failed to change resource state");
133 response = buildErrorResponse(actionResponse.right().value());
137 log.debug("change state successful !!!");
138 UiComponentMetadata componentMetatdata = UiComponentDataConverter.convertToUiComponentMetadata(actionResponse.left().value());
139 Object value = RepresentationUtils.toRepresentation(componentMetatdata);
140 response = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), value);
143 log.info("componentCollection \"{}\" is not valid. Supported componentCollection values are \"{}\", \"{}\" or \"{}\"", componentCollection, ComponentTypeEnum.RESOURCE_PARAM_NAME, ComponentTypeEnum.SERVICE_PARAM_NAME,
144 ComponentTypeEnum.PRODUCT_PARAM_NAME);
145 ResponseFormat error = getComponentsUtils().getInvalidContentErrorAndAudit(user, componentId, AuditingActionEnum.CHECKOUT_RESOURCE);
146 return buildErrorResponse(error);
148 } catch (Exception e) {
149 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Change Lifecycle State");
150 log.debug("change lifecycle state failed with exception", e);
151 response = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
157 private Either<LifeCycleTransitionEnum, Response> validateTransitionEnum(final String lifecycleTransition, User user) {
158 LifeCycleTransitionEnum transitionEnum = LifeCycleTransitionEnum.CHECKOUT;
160 transitionEnum = LifeCycleTransitionEnum.getFromDisplayName(lifecycleTransition);
161 } catch (IllegalArgumentException e) {
162 log.info("state operation is not valid. operations allowed are: {}", LifeCycleTransitionEnum.valuesAsString(), e);
163 ResponseFormat error = getComponentsUtils().getInvalidContentErrorAndAudit(user, "", AuditingActionEnum.CHECKOUT_RESOURCE);
164 return Either.right(buildErrorResponse(error));
166 return Either.left(transitionEnum);