Release version 1.13.7
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / merge / TopologyComparator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.openecomp.sdc.be.components.merge;
21
22 import fj.data.Either;
23 import java.util.List;
24 import java.util.Map;
25 import org.openecomp.sdc.be.dao.api.ActionStatus;
26 import org.openecomp.sdc.be.dao.utils.MapUtil;
27 import org.openecomp.sdc.be.exception.SdcActionException;
28 import org.openecomp.sdc.be.impl.ComponentsUtils;
29 import org.openecomp.sdc.be.model.Component;
30 import org.openecomp.sdc.be.model.ComponentInstance;
31 import org.openecomp.sdc.be.model.Resource;
32 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
33 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
34 import org.openecomp.sdc.common.log.wrappers.Logger;
35
36 @org.springframework.stereotype.Component
37 public class TopologyComparator {
38
39     public static final Logger log = Logger.getLogger(TopologyComparator.class);
40     @javax.annotation.Resource
41     private RelationsComparator relationsComparator;
42     @javax.annotation.Resource
43     private ToscaOperationFacade toscaOperationFacade;
44     @javax.annotation.Resource
45     private ComponentsUtils componentsUtils;
46
47     /**
48      * @param oldResource the old version of the resource of which to check for topology change
49      * @param newResource the new version of the resource of which to check for topology change
50      * @return true if there was a topology change between the old resource and new resource or false otherwise in case the action to find topology
51      * change failed, an appropriate {@link ActionStatus} will be returned
52      */
53     public Either<Boolean, ActionStatus> isTopologyChanged(Resource oldResource, Resource newResource) {
54         List<ComponentInstance> oldInstances = oldResource.getComponentInstances();
55         List<ComponentInstance> newInstances = newResource.getComponentInstances();
56         if (oldInstances != null && newInstances == null || oldInstances == null && newInstances != null) {
57             return Either.left(true);
58         }
59         if (oldInstances == null && newInstances == null) {
60             return Either.left(false);
61         }
62         Map<String, ComponentInstance> oldInstancesByName = MapUtil.toMap(oldInstances, ComponentInstance::getInvariantName);
63         Map<String, ComponentInstance> newInstancesByName = MapUtil.toMap(newInstances, ComponentInstance::getInvariantName);
64         return isTopologyInstancesChanged(oldResource, newResource, oldInstancesByName, newInstancesByName);
65     }
66
67     private Either<Boolean, ActionStatus> isTopologyInstancesChanged(Resource oldResource, Resource newResource,
68                                                                      Map<String, ComponentInstance> oldInstancesByName,
69                                                                      Map<String, ComponentInstance> newInstancesByName) {
70         try {
71             boolean isTopologyChanged =
72                 isInstanceNamesChanged(oldInstancesByName, newInstancesByName) || isInstanceTypesChanged(oldInstancesByName, newInstancesByName)
73                     || relationsComparator.isRelationsChanged(oldResource, newResource);
74             return Either.left(isTopologyChanged);
75         } catch (SdcActionException e) {
76             log.error("failed to merge entities of previous resource %s to current resource %s. reason: %s", oldResource.getUniqueId(),
77                 newResource.getUniqueId(), e.getActionStatus(), e);
78             return Either.right(e.getActionStatus());
79         }
80     }
81
82     private boolean isInstanceTypesChanged(Map<String, ComponentInstance> oldInstancesByName, Map<String, ComponentInstance> newInstancesByName) {
83         for (Map.Entry<String, ComponentInstance> instanceByName : newInstancesByName.entrySet()) {
84             ComponentInstance oldInstance = oldInstancesByName.get(instanceByName.getKey());
85             if (!isSameToscaTypeOrOriginComponent(oldInstance, instanceByName.getValue())) {
86                 return true;
87             }
88         }
89         return false;
90     }
91
92     private boolean isInstanceNamesChanged(Map<String, ComponentInstance> oldInstanceByName, Map<String, ComponentInstance> newInstancesByName) {
93         return !oldInstanceByName.keySet().equals(newInstancesByName.keySet());
94     }
95
96     private boolean isSameToscaTypeOrOriginComponent(ComponentInstance oldInstance, ComponentInstance newInstance) {
97         return isSameToscaType(oldInstance, newInstance) || isSameOriginComponent(oldInstance, newInstance);
98     }
99
100     private boolean isSameToscaType(ComponentInstance oldInstance, ComponentInstance newInstance) {
101         return oldInstance.getToscaComponentName().equals(newInstance.getToscaComponentName());
102     }
103
104     private boolean isSameOriginComponent(ComponentInstance oldInstance, ComponentInstance newInstance) {
105         if (oldInstance.getComponentUid().equals(newInstance.getComponentUid())) {
106             return true;
107         }
108         Component oldOriginCmpt = toscaOperationFacade.getToscaElement(oldInstance.getComponentUid()).left()
109             .on(storageStatus -> throwSdcActionException(storageStatus, oldInstance));
110         Component newOriginCmpt = toscaOperationFacade.getToscaElement(newInstance.getComponentUid()).left()
111             .on(storageStatus -> throwSdcActionException(storageStatus, newInstance));
112         return oldOriginCmpt.getInvariantUUID().equals(newOriginCmpt.getInvariantUUID());
113     }
114
115     private Component throwSdcActionException(StorageOperationStatus storageOperationStatus, ComponentInstance cmptInstance) {
116         log.error("failed to fetch origin node type %s for instance %s", cmptInstance.getUniqueId(), cmptInstance.getComponentUid());
117         throw new SdcActionException(componentsUtils.convertFromStorageResponse(storageOperationStatus));
118     }
119 }