2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.components.lifecycle;
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.TitanDao;
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.jsontitan.datamodel.ToscaElement;
34 import org.openecomp.sdc.be.model.jsontitan.operations.NodeTemplateOperation;
35 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaElementLifecycleOperation;
36 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
37 import org.openecomp.sdc.be.model.jsontitan.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;
45 import java.util.Arrays;
46 import java.util.List;
48 import java.util.Optional;
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";
57 private static final Logger log = Logger.getLogger(CertificationChangeTransition.class);
59 private LifecycleStateEnum nextState;
60 private LifeCycleTransitionEnum name;
61 private AuditingActionEnum auditingAction;
62 private ArtifactsBusinessLogic artifactsManager;
63 private NodeTemplateOperation nodeTemplateOperation;
65 public CertificationChangeTransition(LifeCycleTransitionEnum name, ComponentsUtils componentUtils, ToscaElementLifecycleOperation lifecycleOperation, ToscaOperationFacade toscaOperationFacade, TitanDao titanDao) {
66 super(componentUtils, lifecycleOperation, toscaOperationFacade, titanDao);
71 Role[] certificationChangeRoles = { Role.ADMIN, Role.TESTER };
72 Role[] resourceRoles = { Role.ADMIN, Role.TESTER, Role.DESIGNER};
73 addAuthorizedRoles(ComponentTypeEnum.RESOURCE, Arrays.asList(resourceRoles));
74 addAuthorizedRoles(ComponentTypeEnum.SERVICE, Arrays.asList(certificationChangeRoles));
76 //additional authorized roles for resource type
79 this.auditingAction = AuditingActionEnum.CERTIFICATION_SUCCESS_RESOURCE;
80 this.nextState = LifecycleStateEnum.CERTIFIED;
82 case FAIL_CERTIFICATION:
83 this.auditingAction = AuditingActionEnum.FAIL_CERTIFICATION_RESOURCE;
84 nextState = LifecycleStateEnum.NOT_CERTIFIED_CHECKIN;
86 case CANCEL_CERTIFICATION:
87 this.auditingAction = AuditingActionEnum.CANCEL_CERTIFICATION_RESOURCE;
88 nextState = LifecycleStateEnum.READY_FOR_CERTIFICATION;
97 public LifeCycleTransitionEnum getName() {
102 public AuditingActionEnum getAuditingAction() {
103 return auditingAction;
106 public ArtifactsBusinessLogic getArtifactsManager() {
107 return artifactsManager;
110 public void setArtifactsManager(ArtifactsBusinessLogic artifactsManager) {
111 this.artifactsManager = artifactsManager;
114 public NodeTemplateOperation getNodeTemplateOperation() {
115 return nodeTemplateOperation;
118 public void setNodeTemplateOperation(NodeTemplateOperation nodeTemplateOperation) {
119 this.nodeTemplateOperation = nodeTemplateOperation;
122 private ResponseFormat formatCertificationError(Component component, StorageOperationStatus response, ComponentTypeEnum componentType) {
123 BeEcompErrorManager.getInstance().logBeDaoSystemError("Change LifecycleState - Certify failed on graph");
124 log.debug("certification change failed on graph");
126 return componentUtils.getResponseFormatByComponent(componentUtils.convertFromStorageResponse(response), component, componentType);
130 public Either<Boolean, ResponseFormat> validateBeforeTransition(Component component, ComponentTypeEnum componentType, User modifier, User owner, LifecycleStateEnum oldState, LifecycleChangeInfoWithAction lifecycleChangeInfo) {
131 String componentName = component.getComponentMetadataDefinition().getMetadataDataDefinition().getName();
132 log.info("validate before certification change. resource name={}, oldState={}, owner userId={}", componentName, oldState, owner.getUserId());
135 Either<Boolean, ResponseFormat> userValidationResponse = userRoleValidation(modifier,component, componentType, lifecycleChangeInfo);
136 if (userValidationResponse.isRight()) {
137 log.debug("userRoleValidation failed");
138 return userValidationResponse;
141 if ( componentType != ComponentTypeEnum.RESOURCE ){
142 if (!oldState.equals(LifecycleStateEnum.CERTIFICATION_IN_PROGRESS) ) {
143 log.debug("oldState={} should be={}",oldState,ActionStatus.COMPONENT_NOT_READY_FOR_CERTIFICATION);
144 ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_NOT_READY_FOR_CERTIFICATION, componentName, componentType.name().toLowerCase());
145 return Either.right(error);
148 if (oldState.equals(LifecycleStateEnum.CERTIFICATION_IN_PROGRESS) && !modifier.getUserId().equals(owner.getUserId()) && !modifier.getRole().equals(Role.ADMIN.name())) {
149 log.debug("oldState={} should not be={}",oldState,ActionStatus.COMPONENT_IN_CERT_IN_PROGRESS_STATE);
150 log.debug("&& modifier({})!={} && modifier.role({})!={}", modifier, owner, modifier.getRole(), owner.getRole());
151 ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_IN_CERT_IN_PROGRESS_STATE, componentName, componentType.name().toLowerCase(), owner.getFirstName(), owner.getLastName(), owner.getUserId());
152 return Either.right(error);
155 return Either.left(true);
159 public Either<? extends Component, ResponseFormat> changeState(ComponentTypeEnum componentType, Component component, ComponentBusinessLogic componentBl, User modifier, User owner, boolean shouldLock, boolean inTransaction) {
161 log.info("start performing certification change for resource {}", component.getUniqueId());
162 Either<? extends Component, ResponseFormat> result = null;
165 Either<ToscaElement, StorageOperationStatus> certificationChangeResult = Either.right(StorageOperationStatus.GENERAL_ERROR);
166 if (nextState.equals(LifecycleStateEnum.CERTIFIED)) {
167 certificationChangeResult = lifeCycleOperation.certifyToscaElement(component.getUniqueId(), modifier.getUserId(), owner.getUserId());
169 certificationChangeResult = lifeCycleOperation.cancelOrFailCertification(component.getUniqueId(), modifier.getUserId(), owner.getUserId(), nextState);
172 if (certificationChangeResult.isRight()) {
173 ResponseFormat responseFormat = formatCertificationError(component, certificationChangeResult.right().value(), componentType);
174 result = Either.right(responseFormat);
178 if (nextState.equals(LifecycleStateEnum.CERTIFIED)) {
179 Either<Boolean, StorageOperationStatus> deleteOldComponentVersions = lifeCycleOperation.deleteOldToscaElementVersions(ModelConverter.getVertexType(component), componentType, component.getComponentMetadataDefinition().getMetadataDataDefinition().getName(),
180 component.getComponentMetadataDefinition().getMetadataDataDefinition().getUUID());
181 if (deleteOldComponentVersions.isRight()) {
182 ResponseFormat responseFormat = formatCertificationError(component, deleteOldComponentVersions.right().value(), componentType);
183 result = Either.right(responseFormat);
186 ToscaElement certificationResult = certificationChangeResult.left().value();
187 Component componentAfterCertification = ModelConverter.convertFromToscaElement(certificationResult);
188 if ( result == null || result.isLeft() ){
189 //update edges for allotted resource
190 StorageOperationStatus status = handleConnectionsForAllotted(componentAfterCertification);
191 if ( status != StorageOperationStatus.OK){
192 ResponseFormat responseFormat = formatCertificationError(componentAfterCertification, status, componentType);
193 result = Either.right(responseFormat);
196 updateCalculatedCapabilitiesRequirements(componentAfterCertification);
197 result = Either.left(componentAfterCertification);
200 if (result == null || result.isRight()) {
201 BeEcompErrorManager.getInstance().logBeDaoSystemError("Change LifecycleState");
202 if ( !inTransaction ) {
203 log.debug("operation failed. do rollback");
207 if ( !inTransaction ) {
208 log.debug("operation success. do commit");
216 private void updateCalculatedCapabilitiesRequirements(Component certifiedComponent) {
217 if(certifiedComponent.getComponentType() == ComponentTypeEnum.SERVICE){
218 toscaOperationFacade.updateNamesOfCalculatedCapabilitiesRequirements(certifiedComponent.getUniqueId());
222 private StorageOperationStatus handleConnectionsForAllotted(Component component){
223 StorageOperationStatus status = StorageOperationStatus.OK;
224 if (component.getComponentType() == ComponentTypeEnum.RESOURCE && component.isTopologyTemplate() ){
225 List<CategoryDefinition> categories = component.getCategories();
226 Optional<CategoryDefinition> findFirst = categories.stream().filter(c->c.getName().equals(ALLOTTED_CATEGORY)).findFirst();
227 if ( findFirst.isPresent() ){
228 findInstanceByAllottedProperties(component);
230 log.debug("Component isn't from allotted category.");
236 private void findInstanceByAllottedProperties(Component component) {
237 log.debug("Component is from alloted category. Remove all previous ALLOTTED_OF connections for all instances");
238 nodeTemplateOperation.removeAllAllotedEdges(component.getUniqueId());
239 Map<String, List<ComponentInstanceProperty>> componentInstancesProperties = component.getComponentInstancesProperties();
240 if ( componentInstancesProperties != null ){
241 componentInstancesProperties.entrySet().forEach(e->{
242 List<ComponentInstanceProperty> props = e.getValue();
243 Optional<ComponentInstanceProperty> findProp = props.stream().filter(p -> p.getName().equals(DEPENDING_SRV_NAME) || p.getName().equals(PROVIDING_SRV_NAME)).findFirst();
244 if ( findProp.isPresent() ){
245 log.debug("Find specific properties [{} or {}]on instance {} ", DEPENDING_SRV_NAME,PROVIDING_SRV_NAME, e.getKey() );
246 handleAllotedInstance(component.getUniqueId(), e.getKey(), e.getValue() );
248 log.debug("Not defined specific properties [{} or {}]on instance {} ", DEPENDING_SRV_NAME,PROVIDING_SRV_NAME, e.getKey() );
254 private StorageOperationStatus handleAllotedInstance(String componentId, String instanceId, List<ComponentInstanceProperty> props) {
255 ComponentInstanceProperty serviceUUIDProp = props.stream().filter(p -> p.getName().equals(PROVIDING_SRV_UUID) || p.getName().equals(DEPENDING_SRV_UUID)).findFirst().get();
256 if ( serviceUUIDProp.getValue() != null && !serviceUUIDProp.getValue().contains("get_input")){
257 log.debug("Handle Allotted edge on instance {} for service UUID {} ", instanceId, serviceUUIDProp.getValue() );
258 return nodeTemplateOperation.createAllottedOfEdge(componentId, instanceId, serviceUUIDProp.getValue());
260 log.debug("An incorrectly defined service UUID for Allotted instance {} . Skip instance", instanceId, serviceUUIDProp.getValue() );
261 return StorageOperationStatus.OK;