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