1 package org.openecomp.sdc.be.components.merge;
4 import org.openecomp.sdc.be.dao.api.ActionStatus;
5 import org.openecomp.sdc.be.dao.utils.MapUtil;
6 import org.openecomp.sdc.be.exception.SdcActionException;
7 import org.openecomp.sdc.be.impl.ComponentsUtils;
8 import org.openecomp.sdc.be.model.Component;
9 import org.openecomp.sdc.be.model.ComponentInstance;
10 import org.openecomp.sdc.be.model.Resource;
11 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
12 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
16 import java.util.List;
19 @org.springframework.stereotype.Component
20 public class TopologyComparator {
22 public static final Logger LOGGER = LoggerFactory.getLogger(TopologyComparator.class);
24 @javax.annotation.Resource
25 private RelationsComparator relationsComparator;
27 @javax.annotation.Resource
28 private ToscaOperationFacade toscaOperationFacade;
30 @javax.annotation.Resource
31 private ComponentsUtils componentsUtils;
35 * @param oldResource the old version of the resource of which to check for topology change
36 * @param newResource the new version of the resource of which to check for topology change
37 * @return true if there was a topology change between the old resource and new resource or false otherwise
38 * in case the action to find topology change failed, an appropriate {@link ActionStatus} will be returned
40 public Either<Boolean, ActionStatus> isTopologyChanged(Resource oldResource, Resource newResource) {
41 List<ComponentInstance> oldInstances = oldResource.getComponentInstances();
42 List<ComponentInstance> newInstances = newResource.getComponentInstances();
43 if (oldInstances != null && newInstances == null || oldInstances == null && newInstances != null) {
44 return Either.left(true);
46 if (oldInstances == null && newInstances == null) {
47 return Either.left(false);
49 Map<String, ComponentInstance> oldInstancesByName = MapUtil.toMap(oldInstances, ComponentInstance::getName);
50 Map<String, ComponentInstance> newInstancesByName = MapUtil.toMap(newInstances, ComponentInstance::getName);
51 return isTopologyInstancesChanged(oldResource, newResource, oldInstancesByName, newInstancesByName);
54 private Either<Boolean, ActionStatus> isTopologyInstancesChanged(Resource oldResource, Resource newResource, Map<String, ComponentInstance> oldInstancesByName, Map<String, ComponentInstance> newInstancesByName) {
56 boolean isTopologyChanged = isInstanceNamesChanged(oldInstancesByName, newInstancesByName) ||
57 isInstanceTypesChanged(oldInstancesByName, newInstancesByName) ||
58 relationsComparator.isRelationsChanged(oldResource, newResource);
59 return Either.left(isTopologyChanged);
60 } catch (SdcActionException e) {
61 LOGGER.error("failed to merge entities of previous resource %s to current resource %s. reason: %s", oldResource.getUniqueId(), newResource.getUniqueId(), e.getActionStatus(), e);
62 return Either.right(e.getActionStatus());
66 private boolean isInstanceTypesChanged(Map<String, ComponentInstance> oldInstancesByName, Map<String, ComponentInstance> newInstancesByName) {
67 for (Map.Entry<String, ComponentInstance> instanceByName : newInstancesByName.entrySet()) {
68 ComponentInstance oldInstance = oldInstancesByName.get(instanceByName.getKey());
69 if (!isSameToscaTypeOrOriginComponent(oldInstance, instanceByName.getValue())) {
76 private boolean isInstanceNamesChanged(Map<String, ComponentInstance> oldInstanceByName, Map<String, ComponentInstance> newInstancesByName) {
77 return !oldInstanceByName.keySet().equals(newInstancesByName.keySet());
80 private boolean isSameToscaTypeOrOriginComponent(ComponentInstance oldInstance, ComponentInstance newInstance) {
81 return isSameToscaType(oldInstance, newInstance) ||
82 isSameOriginComponent(oldInstance, newInstance);
85 private boolean isSameToscaType(ComponentInstance oldInstance, ComponentInstance newInstance) {
86 return oldInstance.getToscaComponentName().equals(newInstance.getToscaComponentName());
89 private boolean isSameOriginComponent(ComponentInstance oldInstance, ComponentInstance newInstance) {
90 if (oldInstance.getComponentUid().equals(newInstance.getComponentUid())) {
93 Component oldOriginCmpt = toscaOperationFacade.getToscaElement(oldInstance.getComponentUid()).left().on(storageStatus -> throwSdcActionException(storageStatus, oldInstance));
94 Component newOriginCmpt = toscaOperationFacade.getToscaElement(newInstance.getComponentUid()).left().on(storageStatus -> throwSdcActionException(storageStatus, newInstance));
95 return oldOriginCmpt.getInvariantUUID().equals(newOriginCmpt.getInvariantUUID());
98 private Component throwSdcActionException(StorageOperationStatus storageOperationStatus, ComponentInstance cmptInstance) {
99 LOGGER.error("failed to fetch origin node type %s for instance %s", cmptInstance.getUniqueId(), cmptInstance.getComponentUid());
100 throw new SdcActionException(componentsUtils.convertFromStorageResponse(storageOperationStatus));