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.jsonjanusgraph.operations.ToscaOperationFacade;
12 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
13 import org.openecomp.sdc.common.log.wrappers.Logger;
15 import java.util.List;
18 @org.springframework.stereotype.Component
19 public class TopologyComparator {
21 public static final Logger log = Logger.getLogger(TopologyComparator.class);
23 @javax.annotation.Resource
24 private RelationsComparator relationsComparator;
26 @javax.annotation.Resource
27 private ToscaOperationFacade toscaOperationFacade;
29 @javax.annotation.Resource
30 private ComponentsUtils componentsUtils;
34 * @param oldResource the old version of the resource of which to check for topology change
35 * @param newResource the new version of the resource of which to check for topology change
36 * @return true if there was a topology change between the old resource and new resource or false otherwise
37 * in case the action to find topology change failed, an appropriate {@link ActionStatus} will be returned
39 public Either<Boolean, ActionStatus> isTopologyChanged(Resource oldResource, Resource newResource) {
40 List<ComponentInstance> oldInstances = oldResource.getComponentInstances();
41 List<ComponentInstance> newInstances = newResource.getComponentInstances();
42 if (oldInstances != null && newInstances == null || oldInstances == null && newInstances != null) {
43 return Either.left(true);
45 if (oldInstances == null && newInstances == null) {
46 return Either.left(false);
48 Map<String, ComponentInstance> oldInstancesByName = MapUtil.toMap(oldInstances, ComponentInstance::getName);
49 Map<String, ComponentInstance> newInstancesByName = MapUtil.toMap(newInstances, ComponentInstance::getName);
50 return isTopologyInstancesChanged(oldResource, newResource, oldInstancesByName, newInstancesByName);
53 private Either<Boolean, ActionStatus> isTopologyInstancesChanged(Resource oldResource, Resource newResource, Map<String, ComponentInstance> oldInstancesByName, Map<String, ComponentInstance> newInstancesByName) {
55 boolean isTopologyChanged = isInstanceNamesChanged(oldInstancesByName, newInstancesByName) ||
56 isInstanceTypesChanged(oldInstancesByName, newInstancesByName) ||
57 relationsComparator.isRelationsChanged(oldResource, newResource);
58 return Either.left(isTopologyChanged);
59 } catch (SdcActionException e) {
60 log.error("failed to merge entities of previous resource %s to current resource %s. reason: %s", oldResource.getUniqueId(), newResource.getUniqueId(), e.getActionStatus(), e);
61 return Either.right(e.getActionStatus());
65 private boolean isInstanceTypesChanged(Map<String, ComponentInstance> oldInstancesByName, Map<String, ComponentInstance> newInstancesByName) {
66 for (Map.Entry<String, ComponentInstance> instanceByName : newInstancesByName.entrySet()) {
67 ComponentInstance oldInstance = oldInstancesByName.get(instanceByName.getKey());
68 if (!isSameToscaTypeOrOriginComponent(oldInstance, instanceByName.getValue())) {
75 private boolean isInstanceNamesChanged(Map<String, ComponentInstance> oldInstanceByName, Map<String, ComponentInstance> newInstancesByName) {
76 return !oldInstanceByName.keySet().equals(newInstancesByName.keySet());
79 private boolean isSameToscaTypeOrOriginComponent(ComponentInstance oldInstance, ComponentInstance newInstance) {
80 return isSameToscaType(oldInstance, newInstance) ||
81 isSameOriginComponent(oldInstance, newInstance);
84 private boolean isSameToscaType(ComponentInstance oldInstance, ComponentInstance newInstance) {
85 return oldInstance.getToscaComponentName().equals(newInstance.getToscaComponentName());
88 private boolean isSameOriginComponent(ComponentInstance oldInstance, ComponentInstance newInstance) {
89 if (oldInstance.getComponentUid().equals(newInstance.getComponentUid())) {
92 Component oldOriginCmpt = toscaOperationFacade.getToscaElement(oldInstance.getComponentUid()).left().on(storageStatus -> throwSdcActionException(storageStatus, oldInstance));
93 Component newOriginCmpt = toscaOperationFacade.getToscaElement(newInstance.getComponentUid()).left().on(storageStatus -> throwSdcActionException(storageStatus, newInstance));
94 return oldOriginCmpt.getInvariantUUID().equals(newOriginCmpt.getInvariantUUID());
97 private Component throwSdcActionException(StorageOperationStatus storageOperationStatus, ComponentInstance cmptInstance) {
98 log.error("failed to fetch origin node type %s for instance %s", cmptInstance.getUniqueId(), cmptInstance.getComponentUid());
99 throw new SdcActionException(componentsUtils.convertFromStorageResponse(storageOperationStatus));