445b3a97506502bcdecaf009f31936c30fa1f865
[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 import java.util.List;
25
26 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
27 import org.openecomp.sdc.be.components.impl.ComponentBusinessLogic;
28 import org.openecomp.sdc.be.config.BeEcompErrorManager;
29 import org.openecomp.sdc.be.dao.api.ActionStatus;
30 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
31 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
32 import org.openecomp.sdc.be.impl.ComponentsUtils;
33 import org.openecomp.sdc.be.model.ArtifactDefinition;
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.Resource;
38 import org.openecomp.sdc.be.model.User;
39 import org.openecomp.sdc.be.model.operations.api.ILifecycleOperation;
40 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
41 import org.openecomp.sdc.be.model.operations.impl.ResourceOperation;
42 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
43 import org.openecomp.sdc.be.user.Role;
44 import org.openecomp.sdc.common.config.EcompErrorName;
45 import org.openecomp.sdc.exception.ResponseFormat;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 import fj.data.Either;
50
51 public class CertificationChangeTransition extends LifeCycleTransition {
52
53         private static Logger log = LoggerFactory.getLogger(CertificationChangeTransition.class.getName());
54
55         private LifecycleStateEnum nextState;
56         private LifeCycleTransitionEnum name;
57         private AuditingActionEnum auditingAction;
58         private ArtifactsBusinessLogic artifactsManager;
59
60         public CertificationChangeTransition(LifeCycleTransitionEnum name, ComponentsUtils componentUtils, ILifecycleOperation lifecycleOperation) {
61                 super(componentUtils, lifecycleOperation);
62
63                 this.name = name;
64
65                 // authorized roles
66                 Role[] certificationChangeRoles = { Role.ADMIN, Role.TESTER };
67                 addAuthorizedRoles(ComponentTypeEnum.RESOURCE, Arrays.asList(certificationChangeRoles));
68                 addAuthorizedRoles(ComponentTypeEnum.SERVICE, Arrays.asList(certificationChangeRoles));
69                 // TODO to be later defined for product
70
71                 switch (this.name) {
72                 case CERTIFY:
73                         this.auditingAction = AuditingActionEnum.CERTIFICATION_SUCCESS_RESOURCE;
74                         this.nextState = LifecycleStateEnum.CERTIFIED;
75                         break;
76                 case FAIL_CERTIFICATION:
77                         this.auditingAction = AuditingActionEnum.FAIL_CERTIFICATION_RESOURCE;
78                         nextState = LifecycleStateEnum.NOT_CERTIFIED_CHECKIN;
79                         break;
80                 case CANCEL_CERTIFICATION:
81                         this.auditingAction = AuditingActionEnum.CANCEL_CERTIFICATION_RESOURCE;
82                         nextState = LifecycleStateEnum.READY_FOR_CERTIFICATION;
83                         break;
84                 default:
85                         break;
86                 }
87
88         }
89
90         @Override
91         public LifeCycleTransitionEnum getName() {
92                 return name;
93         }
94
95         @Override
96         public AuditingActionEnum getAuditingAction() {
97                 return auditingAction;
98         }
99
100         public ArtifactsBusinessLogic getArtifactsManager() {
101                 return artifactsManager;
102         }
103
104         public void setArtifactsManager(ArtifactsBusinessLogic artifactsManager) {
105                 this.artifactsManager = artifactsManager;
106         }
107
108         private ResponseFormat formatCertificationError(Component component, StorageOperationStatus response, ComponentTypeEnum componentType) {
109                 BeEcompErrorManager.getInstance().processEcompError(EcompErrorName.BeDaoSystemError, "Change LifecycleState - Certify failed on graph");
110                 BeEcompErrorManager.getInstance().logBeDaoSystemError("Change LifecycleState - Certify failed on graph");
111                 log.debug("certification change failed on graph");
112
113                 ActionStatus actionStatus = componentUtils.convertFromStorageResponse(response);
114                 ResponseFormat responseFormat = componentUtils.getResponseFormatByComponent(actionStatus, component, componentType);
115                 return responseFormat;
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 certification change. resource name={}, oldState={}, owner userId={}", componentName, oldState, owner.getUserId());
122
123                 // validate user
124                 Either<Boolean, ResponseFormat> userValidationResponse = userRoleValidation(modifier, componentType, lifecycleChangeInfo);
125                 if (userValidationResponse.isRight()) {
126                         return userValidationResponse;
127                 }
128
129                 if (!oldState.equals(LifecycleStateEnum.CERTIFICATION_IN_PROGRESS)) {
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.CERTIFICATION_IN_PROGRESS) && !modifier.equals(owner) && !modifier.getRole().equals(Role.ADMIN.name())) {
135                         ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_IN_CERT_IN_PROGRESS_STATE, componentName, componentType.name().toLowerCase(), owner.getFirstName(), owner.getLastName(), owner.getUserId());
136                         return Either.right(error);
137                 }
138
139                 return Either.left(true);
140         }
141
142         @Override
143         public Either<? extends Component, ResponseFormat> changeState(ComponentTypeEnum componentType, Component component, ComponentBusinessLogic componentBl, User modifier, User owner, boolean shouldLock, boolean inTransaction) {
144
145                 log.info("start performing certification change for resource {}", component.getUniqueId());
146                 Either<? extends Component, ResponseFormat> result = null;
147                 NodeTypeEnum nodeType = componentType.getNodeType();
148
149                 try {
150                         Either<? extends Component, StorageOperationStatus> certificationChangeResult = Either.right(StorageOperationStatus.GENERAL_ERROR);
151                         if (nextState.equals(LifecycleStateEnum.CERTIFIED)) {
152                                 certificationChangeResult = lifeCycleOperation.certifyComponent(nodeType, component, modifier, owner, true);
153                         } else {
154                                 certificationChangeResult = lifeCycleOperation.cancelOrFailCertification(nodeType, component, modifier, owner, nextState, true);
155                         }
156
157                         if (certificationChangeResult.isRight()) {
158                                 ResponseFormat responseFormat = formatCertificationError(component, certificationChangeResult.right().value(), componentType);
159                                 result = Either.right(responseFormat);
160                                 return result;
161                         }
162
163                         if (nextState.equals(LifecycleStateEnum.CERTIFIED)) {
164                                 Either<Boolean, StorageOperationStatus> deleteOldComponentVersions = lifeCycleOperation.deleteOldComponentVersions(nodeType, component.getComponentMetadataDefinition().getMetadataDataDefinition().getName(),
165                                                 component.getComponentMetadataDefinition().getMetadataDataDefinition().getUUID(), true);
166                                 if (deleteOldComponentVersions.isRight()) {
167                                         ResponseFormat responseFormat = formatCertificationError(component, deleteOldComponentVersions.right().value(), componentType);
168                                         result = Either.right(responseFormat);
169                                         return result;
170                                 }
171                         }
172
173                         result = Either.left(certificationChangeResult.left().value());
174                         return result;
175                 } finally {
176                         if (result == null || result.isRight()) {
177                                 BeEcompErrorManager.getInstance().processEcompError(EcompErrorName.BeDaoSystemError, "Change LifecycleState");
178                                 BeEcompErrorManager.getInstance().logBeDaoSystemError("Change LifecycleState");
179                                 if (inTransaction == false) {
180                                         log.debug("operation failed. do rollback");
181                                         lifeCycleOperation.getResourceOperation().getTitanGenericDao().rollback();
182                                 }
183                         } else {
184                                 if (inTransaction == false) {
185                                         log.debug("operation success. do commit");
186                                         lifeCycleOperation.getResourceOperation().getTitanGenericDao().commit();
187                                 }
188                         }
189                 }
190
191         }
192
193         public StorageOperationStatus deleteOldVersion(List<ArtifactDefinition> artifactsToDelete, Resource resourceToDelete) {
194                 ResourceOperation resourceOperation = lifeCycleOperation.getResourceOperation();
195
196                 Either<List<ArtifactDefinition>, StorageOperationStatus> artifactsRes = resourceOperation.getComponentArtifactsForDelete(resourceToDelete.getUniqueId(), NodeTypeEnum.Resource, true);
197                 if (artifactsRes.isRight()) {
198                         return artifactsRes.right().value();
199                 }
200                 Either<Resource, StorageOperationStatus> deleteResourceRes = resourceOperation.deleteResource(resourceToDelete.getUniqueId(), true);
201                 if (deleteResourceRes.isRight()) {
202                         return deleteResourceRes.right().value();
203                 }
204                 artifactsToDelete.addAll(artifactsRes.left().value());
205
206                 return StorageOperationStatus.OK;
207         }
208
209 }