CSIT Fix for SDC-2585
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / lifecycle / StartCertificationTransition.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.config.BeEcompErrorManager;
26 import org.openecomp.sdc.be.dao.api.ActionStatus;
27 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
28 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
29 import org.openecomp.sdc.be.impl.ComponentsUtils;
30 import org.openecomp.sdc.be.model.Component;
31 import org.openecomp.sdc.be.model.LifeCycleTransitionEnum;
32 import org.openecomp.sdc.be.model.LifecycleStateEnum;
33 import org.openecomp.sdc.be.model.User;
34 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
35 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaElementLifecycleOperation;
36 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
37 import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter;
38 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
39 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
40 import org.openecomp.sdc.be.user.Role;
41 import org.openecomp.sdc.common.log.wrappers.Logger;
42 import org.openecomp.sdc.exception.ResponseFormat;
43
44 import java.util.Arrays;
45
46 public class StartCertificationTransition extends LifeCycleTransition {
47
48     private static final Logger log = Logger.getLogger(StartCertificationTransition.class);
49
50     public StartCertificationTransition(ComponentsUtils componentUtils, ToscaElementLifecycleOperation lifecycleOperation, ToscaOperationFacade toscaOperationFacade, JanusGraphDao janusGraphDao) {
51         super(componentUtils, lifecycleOperation, toscaOperationFacade, janusGraphDao);
52
53         // authorized roles
54         Role[] rsrcServiceStartCertificationRoles = { Role.ADMIN, Role.TESTER };
55         Role[] resourceRoles = { Role.ADMIN, Role.TESTER, Role.DESIGNER};
56         addAuthorizedRoles(ComponentTypeEnum.RESOURCE, Arrays.asList(resourceRoles));
57         addAuthorizedRoles(ComponentTypeEnum.SERVICE, Arrays.asList(rsrcServiceStartCertificationRoles));
58         // TODO to be later defined for product
59
60         //additional authorized roles for resource type
61 //        addResouceAuthorizedRoles(ResourceTypeEnum.VFCMT, Arrays.asList(resourceRoles));
62     }
63
64     @Override
65     public LifeCycleTransitionEnum getName() {
66         return LifeCycleTransitionEnum.START_CERTIFICATION;
67     }
68
69     @Override
70     public AuditingActionEnum getAuditingAction() {
71         return AuditingActionEnum.START_CERTIFICATION_RESOURCE;
72     }
73
74     @Override
75     public Either<? extends Component, ResponseFormat> changeState(ComponentTypeEnum componentType, Component component, ComponentBusinessLogic componentBl, User modifier, User owner, boolean shouldLock, boolean inTransaction) {
76
77         log.debug("start performing certification test for resource {}", component.getUniqueId());
78         Either<? extends Component, ResponseFormat> result = null;
79         try{
80             Either<ToscaElement, StorageOperationStatus> stateChangeResult = lifeCycleOperation.startCertificationToscaElement(component.getUniqueId(), modifier.getUserId(), owner.getUserId());
81             if (stateChangeResult.isRight()) {
82                 log.debug("start certification failed on graph");
83                 StorageOperationStatus response = stateChangeResult.right().value();
84                 ActionStatus actionStatus = componentUtils.convertFromStorageResponse(response);
85
86                 if (response.equals(StorageOperationStatus.ENTITY_ALREADY_EXISTS)) {
87                     actionStatus = ActionStatus.COMPONENT_VERSION_ALREADY_EXIST;
88                 }
89                 ResponseFormat responseFormat = componentUtils.getResponseFormatByComponent(actionStatus, component, componentType);
90                 result =  Either.right(responseFormat);
91             }
92             else {
93                 result =  Either.left(ModelConverter.convertFromToscaElement(stateChangeResult.left().value()));
94             }
95         } finally {
96             if (result == null || result.isRight()) {
97                 BeEcompErrorManager.getInstance().logBeDaoSystemError("Change LifecycleState");
98                 if (!inTransaction) {
99                     log.debug("operation failed. do rollback");
100                     janusGraphDao.rollback();
101                 }
102             } else {
103                 if (!inTransaction) {
104                     log.debug("operation success. do commit");
105                     janusGraphDao.commit();
106                 }
107             }
108         }
109         return result;
110     }
111
112     @Override
113     public Either<Boolean, ResponseFormat> validateBeforeTransition(Component component, ComponentTypeEnum componentType, User modifier, User owner, LifecycleStateEnum oldState, LifecycleChangeInfoWithAction lifecycleChangeInfo) {
114         String componentName = component.getComponentMetadataDefinition().getMetadataDataDefinition().getName();
115         log.debug("validate before start certification test. resource name={}, oldState={}, owner userId={}", componentName, oldState, owner.getUserId());
116
117         // validate user
118         Either<Boolean, ResponseFormat> userValidationResponse = userRoleValidation(modifier,component, componentType, lifecycleChangeInfo);
119         if (userValidationResponse.isRight()) {
120             return userValidationResponse;
121         }
122
123         if (oldState.equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN) || oldState.equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT)) {
124             ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_NOT_READY_FOR_CERTIFICATION, componentName, componentType.name().toLowerCase());
125             return Either.right(error);
126         }
127
128         if (oldState.equals(LifecycleStateEnum.CERTIFIED)) {
129             ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_ALREADY_CERTIFIED, componentName, componentType.name().toLowerCase(), owner.getFirstName(), owner.getLastName(), owner.getUserId());
130             return Either.right(error);
131         }
132
133         if (oldState.equals(LifecycleStateEnum.CERTIFICATION_IN_PROGRESS)) {
134             ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_IN_CERT_IN_PROGRESS_STATE, componentName, componentType.name().toLowerCase(), owner.getFirstName(), owner.getLastName(), owner.getUserId());
135             return Either.right(error);
136         }
137
138         return Either.left(true);
139     }
140
141 }