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