re base code
[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.openecomp.sdc.common.log.wrappers.Logger;
14
15 import java.util.List;
16 import java.util.Map;
17
18 @org.springframework.stereotype.Component
19 public class TopologyComparator {
20
21     public static final Logger log = Logger.getLogger(TopologyComparator.class);
22
23     @javax.annotation.Resource
24     private RelationsComparator relationsComparator;
25
26     @javax.annotation.Resource
27     private ToscaOperationFacade toscaOperationFacade;
28
29     @javax.annotation.Resource
30     private ComponentsUtils componentsUtils;
31
32     /**
33      *
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
38      */
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);
44         }
45         if (oldInstances == null && newInstances == null) {
46             return Either.left(false);
47         }
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);
51     }
52
53     private Either<Boolean, ActionStatus> isTopologyInstancesChanged(Resource oldResource, Resource newResource, Map<String, ComponentInstance> oldInstancesByName, Map<String, ComponentInstance> newInstancesByName) {
54         try {
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());
62         }
63     }
64
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())) {
69                 return true;
70             }
71         }
72         return false;
73     }
74
75     private boolean isInstanceNamesChanged(Map<String, ComponentInstance> oldInstanceByName, Map<String, ComponentInstance> newInstancesByName) {
76         return !oldInstanceByName.keySet().equals(newInstancesByName.keySet());
77     }
78
79     private boolean isSameToscaTypeOrOriginComponent(ComponentInstance oldInstance, ComponentInstance newInstance) {
80         return isSameToscaType(oldInstance, newInstance) ||
81                isSameOriginComponent(oldInstance, newInstance);
82     }
83
84     private boolean isSameToscaType(ComponentInstance oldInstance, ComponentInstance newInstance) {
85         return oldInstance.getToscaComponentName().equals(newInstance.getToscaComponentName());
86     }
87
88     private boolean isSameOriginComponent(ComponentInstance oldInstance, ComponentInstance newInstance) {
89         if (oldInstance.getComponentUid().equals(newInstance.getComponentUid())) {
90             return true;
91         }
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());
95     }
96
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));
100     }
101
102
103 }