2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.components.merge;
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;
35 import java.util.List;
38 @org.springframework.stereotype.Component
39 public class TopologyComparator {
41 public static final Logger log = Logger.getLogger(TopologyComparator.class);
43 @javax.annotation.Resource
44 private RelationsComparator relationsComparator;
46 @javax.annotation.Resource
47 private ToscaOperationFacade toscaOperationFacade;
49 @javax.annotation.Resource
50 private ComponentsUtils componentsUtils;
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
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);
65 if (oldInstances == null && newInstances == null) {
66 return Either.left(false);
68 Map<String, ComponentInstance> oldInstancesByName = MapUtil.toMap(oldInstances, ComponentInstance::getName);
69 Map<String, ComponentInstance> newInstancesByName = MapUtil.toMap(newInstances, ComponentInstance::getName);
70 return isTopologyInstancesChanged(oldResource, newResource, oldInstancesByName, newInstancesByName);
73 private Either<Boolean, ActionStatus> isTopologyInstancesChanged(Resource oldResource, Resource newResource, Map<String, ComponentInstance> oldInstancesByName, Map<String, ComponentInstance> newInstancesByName) {
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());
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())) {
95 private boolean isInstanceNamesChanged(Map<String, ComponentInstance> oldInstanceByName, Map<String, ComponentInstance> newInstancesByName) {
96 return !oldInstanceByName.keySet().equals(newInstancesByName.keySet());
99 private boolean isSameToscaTypeOrOriginComponent(ComponentInstance oldInstance, ComponentInstance newInstance) {
100 return isSameToscaType(oldInstance, newInstance) ||
101 isSameOriginComponent(oldInstance, newInstance);
104 private boolean isSameToscaType(ComponentInstance oldInstance, ComponentInstance newInstance) {
105 return oldInstance.getToscaComponentName().equals(newInstance.getToscaComponentName());
108 private boolean isSameOriginComponent(ComponentInstance oldInstance, ComponentInstance newInstance) {
109 if (oldInstance.getComponentUid().equals(newInstance.getComponentUid())) {
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());
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));