Sync Integ to Master
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / merge / TopologyComparator.java
1 package org.openecomp.sdc.be.components.merge;
2
3 import fj.data.Either;
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;
15
16 import java.util.List;
17 import java.util.Map;
18
19 @org.springframework.stereotype.Component
20 public class TopologyComparator {
21
22     public static final Logger LOGGER = LoggerFactory.getLogger(TopologyComparator.class);
23
24     @javax.annotation.Resource
25     private RelationsComparator relationsComparator;
26
27     @javax.annotation.Resource
28     private ToscaOperationFacade toscaOperationFacade;
29
30     @javax.annotation.Resource
31     private ComponentsUtils componentsUtils;
32
33     /**
34      *
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
39      */
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);
45         }
46         if (oldInstances == null && newInstances == null) {
47             return Either.left(false);
48         }
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);
52     }
53
54     private Either<Boolean, ActionStatus> isTopologyInstancesChanged(Resource oldResource, Resource newResource, Map<String, ComponentInstance> oldInstancesByName, Map<String, ComponentInstance> newInstancesByName) {
55         try {
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());
63         }
64     }
65
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())) {
70                 return true;
71             }
72         }
73         return false;
74     }
75
76     private boolean isInstanceNamesChanged(Map<String, ComponentInstance> oldInstanceByName, Map<String, ComponentInstance> newInstancesByName) {
77         return !oldInstanceByName.keySet().equals(newInstancesByName.keySet());
78     }
79
80     private boolean isSameToscaTypeOrOriginComponent(ComponentInstance oldInstance, ComponentInstance newInstance) {
81         return isSameToscaType(oldInstance, newInstance) ||
82                isSameOriginComponent(oldInstance, newInstance);
83     }
84
85     private boolean isSameToscaType(ComponentInstance oldInstance, ComponentInstance newInstance) {
86         return oldInstance.getToscaComponentName().equals(newInstance.getToscaComponentName());
87     }
88
89     private boolean isSameOriginComponent(ComponentInstance oldInstance, ComponentInstance newInstance) {
90         if (oldInstance.getComponentUid().equals(newInstance.getComponentUid())) {
91             return true;
92         }
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());
96     }
97
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));
101     }
102
103
104 }