Catalog alignment
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / lifecycle / CheckinTransition.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.components.lifecycle;
22
23 import fj.data.Either;
24 import org.openecomp.sdc.be.components.impl.ComponentBusinessLogic;
25 import org.openecomp.sdc.be.components.impl.version.VesionUpdateHandler;
26 import org.openecomp.sdc.be.config.BeEcompErrorManager;
27 import org.openecomp.sdc.be.dao.api.ActionStatus;
28 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
29 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
30 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
31 import org.openecomp.sdc.be.impl.ComponentsUtils;
32 import org.openecomp.sdc.be.model.Component;
33 import org.openecomp.sdc.be.model.LifeCycleTransitionEnum;
34 import org.openecomp.sdc.be.model.LifecycleStateEnum;
35 import org.openecomp.sdc.be.model.User;
36 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
37 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElementTypeEnum;
38 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaElementLifecycleOperation;
39 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
40 import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter;
41 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
42 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
43 import org.openecomp.sdc.be.user.Role;
44 import org.openecomp.sdc.common.log.wrappers.Logger;
45 import org.openecomp.sdc.exception.ResponseFormat;
46
47 import java.util.Arrays;
48
49 public class CheckinTransition extends LifeCycleTransition {
50
51     private static final Logger log = Logger.getLogger(CheckinTransition.class);
52
53     private VesionUpdateHandler vesionUpdateHandler;
54
55     public CheckinTransition(ComponentsUtils componentUtils, ToscaElementLifecycleOperation lifecycleOperation, ToscaOperationFacade toscaOperationFacade, JanusGraphDao janusGraphDao,  VesionUpdateHandler groupUpdateHandler) {
56         super(componentUtils, lifecycleOperation, toscaOperationFacade, janusGraphDao);
57         this.vesionUpdateHandler = groupUpdateHandler;
58         // authorized roles
59         Role[] resourceServiceCheckoutRoles = { Role.ADMIN, Role.DESIGNER };
60         Role[] productCheckoutRoles = { Role.ADMIN, Role.PRODUCT_MANAGER };
61         addAuthorizedRoles(ComponentTypeEnum.RESOURCE, Arrays.asList(resourceServiceCheckoutRoles));
62         addAuthorizedRoles(ComponentTypeEnum.SERVICE, Arrays.asList(resourceServiceCheckoutRoles));
63         addAuthorizedRoles(ComponentTypeEnum.PRODUCT, Arrays.asList(productCheckoutRoles));
64
65     }
66
67     @Override
68     public LifeCycleTransitionEnum getName() {
69         return LifeCycleTransitionEnum.CHECKIN;
70     }
71
72     @Override
73     public AuditingActionEnum getAuditingAction() {
74         return AuditingActionEnum.CHECKIN_RESOURCE;
75     }
76
77     @Override
78     public Either<? extends Component, ResponseFormat> changeState(ComponentTypeEnum componentType, Component component, ComponentBusinessLogic componentBl, User modifier, User owner, boolean shouldLock, boolean inTransaction) {
79         log.debug("start performing checkin for {} {}", componentType, component.getUniqueId());
80
81         Either<? extends Component, ResponseFormat> result = null;
82         try{
83             Either<ToscaElement, StorageOperationStatus> checkinResourceResult = lifeCycleOperation.
84                     checkinToscaELement(component.getLifecycleState(), component.getUniqueId(), modifier.getUserId(), owner.getUserId());
85
86             if (checkinResourceResult.isRight()) {
87                 log.debug("checkout failed on graph");
88                 StorageOperationStatus response = checkinResourceResult.right().value();
89                 ActionStatus actionStatus = componentUtils.convertFromStorageResponse(response);
90
91                 if (response.equals(StorageOperationStatus.ENTITY_ALREADY_EXISTS)) {
92                     actionStatus = ActionStatus.COMPONENT_VERSION_ALREADY_EXIST;
93                 }
94                 ResponseFormat responseFormat = componentUtils.getResponseFormatByComponent(actionStatus, component, componentType);
95                 result =  Either.right(responseFormat);
96             }
97             else {
98                 updateCalculatedCapabilitiesRequirements(checkinResourceResult.left().value());
99                 Component r = ModelConverter.convertFromToscaElement(checkinResourceResult.left().value());
100                 updateGroupsAndPolicesVersion(r);
101                 result =  Either.left(r);
102
103
104             }
105         } finally {
106             if (result == null || result.isRight()) {
107                 BeEcompErrorManager.getInstance().logBeDaoSystemError("Change LifecycleState");
108                 if (!inTransaction) {
109                     log.debug("operation failed. do rollback");
110                     janusGraphDao.rollback();
111                 }
112             } else {
113                 if (!inTransaction) {
114                     log.debug("operation success. do commit");
115                     janusGraphDao.commit();
116                 }
117             }
118         }
119         return result;
120     }
121
122     private void updateGroupsAndPolicesVersion(Component container) {
123         vesionUpdateHandler.doPostChangeVersionCommand(container);
124     }
125
126     @Override
127     public Either<Boolean, ResponseFormat> validateBeforeTransition(Component component, ComponentTypeEnum componentType, User modifier, User owner, LifecycleStateEnum oldState, LifecycleChangeInfoWithAction lifecycleChangeInfo) {
128         String componentName = component.getComponentMetadataDefinition().getMetadataDataDefinition().getName();
129         log.debug("validate before checkin. component name={}, oldState={}, owner userId={}", componentName, oldState, owner.getUserId());
130
131         // validate user
132         Either<Boolean, ResponseFormat> userValidationResponse = userRoleValidation(modifier,component, componentType, lifecycleChangeInfo);
133         if (userValidationResponse.isRight()) {
134             return userValidationResponse;
135         }
136
137         if (!oldState.equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT)) {
138             ActionStatus action = ActionStatus.COMPONENT_ALREADY_CHECKED_IN;
139             if (oldState.equals(LifecycleStateEnum.CERTIFIED)){
140                 action = ActionStatus.COMPONENT_ALREADY_CERTIFIED;
141             }
142             ResponseFormat error = componentUtils.getResponseFormat(action, componentName, componentType.name().toLowerCase(), owner.getFirstName(), owner.getLastName(), owner.getUserId());
143             return Either.right(error);
144         }
145
146         if (oldState.equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT) && !modifier.getUserId().equals(owner.getUserId()) && !modifier.getRole().equals(Role.ADMIN.name())) {
147             ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_CHECKOUT_BY_ANOTHER_USER, componentName, componentType.name().toLowerCase(), owner.getFirstName(), owner.getLastName(), owner.getUserId());
148             return Either.right(error);
149         }
150
151         return Either.left(true);
152     }
153
154     private void updateCalculatedCapabilitiesRequirements(ToscaElement toscaElement) {
155         if(toscaElement.getToscaType() == ToscaElementTypeEnum.TOPOLOGY_TEMPLATE && toscaElement.getResourceType() != ResourceTypeEnum.CVFC){
156             toscaOperationFacade.updateNamesOfCalculatedCapabilitiesRequirements(toscaElement.getUniqueId());
157         }
158     }
159 }