Sync Integ to Master
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / lifecycle / CertificationChangeTransition.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
25 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
26 import org.openecomp.sdc.be.components.impl.ComponentBusinessLogic;
27 import org.openecomp.sdc.be.config.BeEcompErrorManager;
28 import org.openecomp.sdc.be.dao.api.ActionStatus;
29 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
30 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
31 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
32 import org.openecomp.sdc.be.impl.ComponentsUtils;
33 import org.openecomp.sdc.be.model.Component;
34 import org.openecomp.sdc.be.model.LifeCycleTransitionEnum;
35 import org.openecomp.sdc.be.model.LifecycleStateEnum;
36 import org.openecomp.sdc.be.model.User;
37 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElement;
38 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaElementLifecycleOperation;
39 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
40 import org.openecomp.sdc.be.model.jsontitan.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.exception.ResponseFormat;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 import java.util.Arrays;
49
50 public class CertificationChangeTransition extends LifeCycleTransition {
51
52     private static final Logger log = LoggerFactory.getLogger(CertificationChangeTransition.class);
53
54     private LifecycleStateEnum nextState;
55     private LifeCycleTransitionEnum name;
56     private AuditingActionEnum auditingAction;
57     private ArtifactsBusinessLogic artifactsManager;
58
59     public CertificationChangeTransition(LifeCycleTransitionEnum name, ComponentsUtils componentUtils, ToscaElementLifecycleOperation lifecycleOperation, ToscaOperationFacade toscaOperationFacade, TitanDao titanDao) {
60         super(componentUtils, lifecycleOperation, toscaOperationFacade, titanDao);
61
62         this.name = name;
63
64         // authorized roles
65         Role[] certificationChangeRoles = { Role.ADMIN, Role.TESTER };
66         addAuthorizedRoles(ComponentTypeEnum.RESOURCE, Arrays.asList(certificationChangeRoles));
67         addAuthorizedRoles(ComponentTypeEnum.SERVICE, Arrays.asList(certificationChangeRoles));
68         // TODO to be later defined for product
69
70         //additional authorized roles for resource type
71         Role[] resourceRoles = { Role.DESIGNER};
72         addResouceAuthorizedRoles(ResourceTypeEnum.VFCMT, Arrays.asList(resourceRoles));
73
74         switch (this.name) {
75         case CERTIFY:
76             this.auditingAction = AuditingActionEnum.CERTIFICATION_SUCCESS_RESOURCE;
77             this.nextState = LifecycleStateEnum.CERTIFIED;
78             break;
79         case FAIL_CERTIFICATION:
80             this.auditingAction = AuditingActionEnum.FAIL_CERTIFICATION_RESOURCE;
81             nextState = LifecycleStateEnum.NOT_CERTIFIED_CHECKIN;
82             break;
83         case CANCEL_CERTIFICATION:
84             this.auditingAction = AuditingActionEnum.CANCEL_CERTIFICATION_RESOURCE;
85             nextState = LifecycleStateEnum.READY_FOR_CERTIFICATION;
86             break;
87         default:
88             break;
89         }
90
91     }
92
93     @Override
94     public LifeCycleTransitionEnum getName() {
95         return name;
96     }
97
98     @Override
99     public AuditingActionEnum getAuditingAction() {
100         return auditingAction;
101     }
102
103     public ArtifactsBusinessLogic getArtifactsManager() {
104         return artifactsManager;
105     }
106
107     public void setArtifactsManager(ArtifactsBusinessLogic artifactsManager) {
108         this.artifactsManager = artifactsManager;
109     }
110
111     private ResponseFormat formatCertificationError(Component component, StorageOperationStatus response, ComponentTypeEnum componentType) {
112         BeEcompErrorManager.getInstance().logBeDaoSystemError("Change LifecycleState - Certify failed on graph");
113         log.debug("certification change failed on graph");
114
115         ActionStatus actionStatus = componentUtils.convertFromStorageResponse(response);
116         ResponseFormat responseFormat = componentUtils.getResponseFormatByComponent(actionStatus, component, componentType);
117         return responseFormat;
118     }
119
120     @Override
121     public Either<Boolean, ResponseFormat> validateBeforeTransition(Component component, ComponentTypeEnum componentType, User modifier, User owner, LifecycleStateEnum oldState, LifecycleChangeInfoWithAction lifecycleChangeInfo) {
122         String componentName = component.getComponentMetadataDefinition().getMetadataDataDefinition().getName();
123         log.info("validate before certification change. resource name={}, oldState={}, owner userId={}", componentName, oldState, owner.getUserId());
124
125         // validate user
126         Either<Boolean, ResponseFormat> userValidationResponse = userRoleValidation(modifier,component, componentType, lifecycleChangeInfo);
127         if (userValidationResponse.isRight()) {
128             log.error("userRoleValidation failed");
129             return userValidationResponse;
130         }
131
132         if (!oldState.equals(LifecycleStateEnum.CERTIFICATION_IN_PROGRESS)) {
133             log.error("oldState={} should be={}",oldState,ActionStatus.COMPONENT_NOT_READY_FOR_CERTIFICATION);
134             ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_NOT_READY_FOR_CERTIFICATION, componentName, componentType.name().toLowerCase());
135             return Either.right(error);
136         }
137
138         if (oldState.equals(LifecycleStateEnum.CERTIFICATION_IN_PROGRESS) && !modifier.equals(owner) && !modifier.getRole().equals(Role.ADMIN.name())) {
139             log.error("oldState={} should not be={}",oldState,ActionStatus.COMPONENT_IN_CERT_IN_PROGRESS_STATE);
140             log.error("&& modifier({})!={}  && modifier.role({})!={}",modifier,owner);
141             ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_IN_CERT_IN_PROGRESS_STATE, componentName, componentType.name().toLowerCase(), owner.getFirstName(), owner.getLastName(), owner.getUserId());
142             return Either.right(error);
143         }
144
145         return Either.left(true);
146     }
147
148     @Override
149     public Either<? extends Component, ResponseFormat> changeState(ComponentTypeEnum componentType, Component component, ComponentBusinessLogic componentBl, User modifier, User owner, boolean shouldLock, boolean inTransaction) {
150
151         log.info("start performing certification change for resource {}", component.getUniqueId());
152         Either<? extends Component, ResponseFormat> result = null;
153
154         try {
155             Either<ToscaElement, StorageOperationStatus> certificationChangeResult = Either.right(StorageOperationStatus.GENERAL_ERROR);
156             if (nextState.equals(LifecycleStateEnum.CERTIFIED)) {
157                 certificationChangeResult = lifeCycleOperation.certifyToscaElement(component.getUniqueId(), modifier.getUserId(), owner.getUserId());
158             } else {
159                 certificationChangeResult = lifeCycleOperation.cancelOrFailCertification(component.getUniqueId(), modifier.getUserId(), owner.getUserId(), nextState);
160             }
161
162             if (certificationChangeResult.isRight()) {
163                 ResponseFormat responseFormat = formatCertificationError(component, certificationChangeResult.right().value(), componentType);
164                 result = Either.right(responseFormat);
165                 return result;
166             }
167
168             if (nextState.equals(LifecycleStateEnum.CERTIFIED)) {
169                 Either<Boolean, StorageOperationStatus> deleteOldComponentVersions = lifeCycleOperation.deleteOldToscaElementVersions(ModelConverter.getVertexType(component), componentType, component.getComponentMetadataDefinition().getMetadataDataDefinition().getName(),
170                         component.getComponentMetadataDefinition().getMetadataDataDefinition().getUUID());
171                 if (deleteOldComponentVersions.isRight()) {
172                     ResponseFormat responseFormat = formatCertificationError(component, deleteOldComponentVersions.right().value(), componentType);
173                     result = Either.right(responseFormat);
174                 }
175             }
176
177             result = Either.left(ModelConverter.convertFromToscaElement(certificationChangeResult.left().value()));
178             return result;
179         } finally {
180             if (result == null || result.isRight()) {
181                 BeEcompErrorManager.getInstance().logBeDaoSystemError("Change LifecycleState");
182                 if (inTransaction == false) {
183                     log.debug("operation failed. do rollback");
184                     titanDao.rollback();
185                 }
186             } else {
187                 if (inTransaction == false) {
188                     log.debug("operation success. do commit");
189                     titanDao.commit();
190                 }
191             }
192         }
193
194     }
195 }