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