409fad22b4f422cbb05c13173b395aef35b89871
[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.ArtifactsBusinessLogic;
25 import org.openecomp.sdc.be.components.impl.ComponentBusinessLogic;
26 import org.openecomp.sdc.be.config.BeEcompErrorManager;
27 import org.openecomp.sdc.be.dao.api.ActionStatus;
28 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
29 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
30 import org.openecomp.sdc.be.impl.ComponentsUtils;
31 import org.openecomp.sdc.be.model.*;
32 import org.openecomp.sdc.be.model.category.CategoryDefinition;
33 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
34 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.NodeTemplateOperation;
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.tosca.ToscaUtils;
41 import org.openecomp.sdc.be.user.Role;
42 import org.openecomp.sdc.common.log.wrappers.Logger;
43 import org.openecomp.sdc.exception.ResponseFormat;
44
45 import java.util.Arrays;
46 import java.util.List;
47 import java.util.Map;
48 import java.util.Optional;
49
50 public class CertificationChangeTransition extends LifeCycleTransition {
51     private static final  String ALLOTTED_CATEGORY = "Allotted Resource";
52     private static final  String DEPENDING_SRV_NAME = "depending_service_name";
53     private static final  String PROVIDING_SRV_NAME = "providing_service_name";
54     private static final  String PROVIDING_SRV_UUID = "providing_service_uuid";
55     private static final  String DEPENDING_SRV_UUID = "depending_service_uuid";
56
57     private static final Logger log = Logger.getLogger(CertificationChangeTransition.class);
58
59     private LifecycleStateEnum nextState;
60     private LifeCycleTransitionEnum name;
61     private AuditingActionEnum auditingAction;
62     private NodeTemplateOperation nodeTemplateOperation;
63
64     public CertificationChangeTransition(LifeCycleTransitionEnum name, ComponentsUtils componentUtils, ToscaElementLifecycleOperation lifecycleOperation, ToscaOperationFacade toscaOperationFacade, JanusGraphDao janusGraphDao) {
65         super(componentUtils, lifecycleOperation, toscaOperationFacade, janusGraphDao);
66
67         this.name = name;
68
69         // authorized roles
70         Role[] certificationChangeRoles = { Role.ADMIN, Role.TESTER };
71         Role[] resourceRoles = { Role.ADMIN, Role.TESTER, Role.DESIGNER};
72         addAuthorizedRoles(ComponentTypeEnum.RESOURCE, Arrays.asList(resourceRoles));
73         addAuthorizedRoles(ComponentTypeEnum.SERVICE, Arrays.asList(certificationChangeRoles));
74
75         //additional authorized roles for resource type
76         switch (this.name) {
77         case CERTIFY:
78             this.auditingAction = AuditingActionEnum.CERTIFICATION_SUCCESS_RESOURCE;
79             this.nextState = LifecycleStateEnum.CERTIFIED;
80             break;
81         case FAIL_CERTIFICATION:
82             this.auditingAction = AuditingActionEnum.FAIL_CERTIFICATION_RESOURCE;
83             nextState = LifecycleStateEnum.NOT_CERTIFIED_CHECKIN;
84             break;
85         case CANCEL_CERTIFICATION:
86             this.auditingAction = AuditingActionEnum.CANCEL_CERTIFICATION_RESOURCE;
87             nextState = LifecycleStateEnum.READY_FOR_CERTIFICATION;
88             break;
89         default:
90             break;
91         }
92
93     }
94
95     @Override
96     public LifeCycleTransitionEnum getName() {
97         return name;
98     }
99
100     @Override
101     public AuditingActionEnum getAuditingAction() {
102         return auditingAction;
103     }
104
105     public NodeTemplateOperation getNodeTemplateOperation() {
106         return nodeTemplateOperation;
107     }
108
109     public void setNodeTemplateOperation(NodeTemplateOperation nodeTemplateOperation) {
110         this.nodeTemplateOperation = nodeTemplateOperation;
111     }
112
113     private ResponseFormat formatCertificationError(Component component, StorageOperationStatus response, ComponentTypeEnum componentType) {
114         BeEcompErrorManager.getInstance().logBeDaoSystemError("Change LifecycleState - Certify failed on graph");
115         log.debug("certification change failed on graph");
116
117         return componentUtils.getResponseFormatByComponent(componentUtils.convertFromStorageResponse(response), component, componentType);
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.debug("userRoleValidation failed");
129             return userValidationResponse;
130         }
131
132         if ( componentType != ComponentTypeEnum.RESOURCE ){
133             if (!oldState.equals(LifecycleStateEnum.CERTIFICATION_IN_PROGRESS)  ) {
134                 log.debug("oldState={} should be={}",oldState,ActionStatus.COMPONENT_NOT_READY_FOR_CERTIFICATION);
135                 ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_NOT_READY_FOR_CERTIFICATION, componentName, componentType.name().toLowerCase());
136                 return Either.right(error);
137             }
138     
139             if (oldState.equals(LifecycleStateEnum.CERTIFICATION_IN_PROGRESS) && !modifier.getUserId().equals(owner.getUserId()) && !modifier.getRole().equals(Role.ADMIN.name())) {
140                 log.debug("oldState={} should not be={}",oldState,ActionStatus.COMPONENT_IN_CERT_IN_PROGRESS_STATE);
141                 log.debug("&& modifier({})!={}  && modifier.role({})!={}", modifier, owner, modifier.getRole(), owner.getRole());
142                 ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_IN_CERT_IN_PROGRESS_STATE, componentName, componentType.name().toLowerCase(), owner.getFirstName(), owner.getLastName(), owner.getUserId());
143                 return Either.right(error);
144             }
145         }
146         return Either.left(true);
147     }
148
149     @Override
150     public Either<? extends Component, ResponseFormat> changeState(ComponentTypeEnum componentType, Component component, ComponentBusinessLogic componentBl, User modifier, User owner, boolean shouldLock, boolean inTransaction) {
151
152         log.info("start performing certification change for resource {}", component.getUniqueId());
153         Either<? extends Component, ResponseFormat> result = null;
154
155         try {
156             Either<ToscaElement, StorageOperationStatus> certificationChangeResult = Either.right(StorageOperationStatus.GENERAL_ERROR);
157             if (nextState.equals(LifecycleStateEnum.CERTIFIED)) {
158                 certificationChangeResult = lifeCycleOperation.certifyToscaElement(component.getUniqueId(), modifier.getUserId(), owner.getUserId());
159             } else {
160                 certificationChangeResult = lifeCycleOperation.cancelOrFailCertification(component.getUniqueId(), modifier.getUserId(), owner.getUserId(), nextState);
161             }
162
163             if (certificationChangeResult.isRight()) {
164                 ResponseFormat responseFormat = formatCertificationError(component, certificationChangeResult.right().value(), componentType);
165                 result = Either.right(responseFormat);
166                 return result;
167             }
168
169             if (nextState.equals(LifecycleStateEnum.CERTIFIED)) {
170                 Either<Boolean, StorageOperationStatus> deleteOldComponentVersions = lifeCycleOperation.deleteOldToscaElementVersions(ModelConverter.getVertexType(component), componentType, component.getComponentMetadataDefinition().getMetadataDataDefinition().getName(),
171                         component.getComponentMetadataDefinition().getMetadataDataDefinition().getUUID());
172                 if (deleteOldComponentVersions.isRight()) {
173                     ResponseFormat responseFormat = formatCertificationError(component, deleteOldComponentVersions.right().value(), componentType);
174                     result = Either.right(responseFormat);
175                 }
176             }
177             ToscaElement certificationResult = certificationChangeResult.left().value();
178             Component componentAfterCertification = ModelConverter.convertFromToscaElement(certificationResult);
179             if ( result == null || result.isLeft() ){
180                 //update edges for allotted resource 
181                 StorageOperationStatus status = handleConnectionsForAllotted(componentAfterCertification);
182                 if ( status != StorageOperationStatus.OK){
183                     ResponseFormat responseFormat = formatCertificationError(componentAfterCertification, status, componentType);
184                     result = Either.right(responseFormat);
185                 }
186             }
187             updateCalculatedCapabilitiesRequirements(componentAfterCertification);
188             updateCapReqPropertiesOwnerId(componentAfterCertification);
189             result = Either.left(componentAfterCertification);
190             return result;
191         } finally {
192             if (result == null || result.isRight()) {
193                 BeEcompErrorManager.getInstance().logBeDaoSystemError("Change LifecycleState");
194                 if ( !inTransaction ) {
195                     log.debug("operation failed. do rollback");
196                     janusGraphDao.rollback();
197                 }
198             } else {
199                 if ( !inTransaction ) {
200                     log.debug("operation success. do commit");
201                     janusGraphDao.commit();
202                 }
203             }
204         }
205
206     }
207
208     private void updateCapReqPropertiesOwnerId(Component component) {
209         if(component.isTopologyTemplate() && ToscaUtils.isNotComplexVfc(component)) {
210             toscaOperationFacade.updateCapReqPropertiesOwnerId(component.getUniqueId());
211         }
212     }
213
214     private void updateCalculatedCapabilitiesRequirements(Component certifiedComponent) {
215         if(certifiedComponent.getComponentType() == ComponentTypeEnum.SERVICE){
216             toscaOperationFacade.updateNamesOfCalculatedCapabilitiesRequirements(certifiedComponent.getUniqueId());
217         }
218     }
219
220     private StorageOperationStatus handleConnectionsForAllotted(Component component){
221         StorageOperationStatus status = StorageOperationStatus.OK;
222         if (component.getComponentType() == ComponentTypeEnum.RESOURCE && component.isTopologyTemplate()  ){
223             List<CategoryDefinition> categories = component.getCategories();
224             Optional<CategoryDefinition> findFirst = categories.stream().filter(c->c.getName().equals(ALLOTTED_CATEGORY)).findFirst();
225             if ( findFirst.isPresent() ){
226                 findInstanceByAllottedProperties(component);
227             }else{
228                 log.debug("Component isn't from allotted category.");
229             }
230         }
231         return status;
232     }
233
234     private void findInstanceByAllottedProperties(Component component) {
235         log.debug("Component is from alloted category. Remove all previous ALLOTTED_OF connections for all instances");
236         nodeTemplateOperation.removeAllAllotedEdges(component.getUniqueId());
237         Map<String, List<ComponentInstanceProperty>> componentInstancesProperties = component.getComponentInstancesProperties();
238         if ( componentInstancesProperties != null ){
239             componentInstancesProperties.entrySet().forEach(e->{
240                 List<ComponentInstanceProperty> props = e.getValue();
241                 Optional<ComponentInstanceProperty> findProp = props.stream().filter(p -> p.getName().equals(DEPENDING_SRV_NAME) ||  p.getName().equals(PROVIDING_SRV_NAME)).findFirst();
242                 if ( findProp.isPresent() ){
243                     log.debug("Find specific properties [{} or {}]on instance {} ", DEPENDING_SRV_NAME,PROVIDING_SRV_NAME, e.getKey()  );
244                     handleAllotedInstance(component.getUniqueId(), e.getKey(), e.getValue() );
245                 }else{
246                     log.debug("Not defined specific properties [{} or {}]on instance {} ", DEPENDING_SRV_NAME,PROVIDING_SRV_NAME, e.getKey()  );
247                 }
248             });
249         }
250     }
251
252     private StorageOperationStatus handleAllotedInstance(String componentId, String instanceId, List<ComponentInstanceProperty> props) {
253         ComponentInstanceProperty serviceUUIDProp = props.stream().filter(p -> p.getName().equals(PROVIDING_SRV_UUID) ||  p.getName().equals(DEPENDING_SRV_UUID)).findFirst().get();
254         if ( serviceUUIDProp.getValue() != null && !serviceUUIDProp.getValue().contains("get_input")){
255             log.debug("Handle Allotted edge on instance {} for service UUID {} ", instanceId,  serviceUUIDProp.getValue() );
256             return  nodeTemplateOperation.createAllottedOfEdge(componentId, instanceId, serviceUUIDProp.getValue());
257         }else{
258             log.debug("An incorrectly defined service UUID for Allotted instance {} . Skip instance", instanceId,  serviceUUIDProp.getValue() );
259             return StorageOperationStatus.OK;
260         }
261     }
262 }