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=========================================================
20 package org.openecomp.sdc.be.components.merge;
22 import fj.data.Either;
23 import java.util.List;
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;
36 @org.springframework.stereotype.Component
37 public class TopologyComparator {
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;
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
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);
59 if (oldInstances == null && newInstances == null) {
60 return Either.left(false);
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);
67 private Either<Boolean, ActionStatus> isTopologyInstancesChanged(Resource oldResource, Resource newResource,
68 Map<String, ComponentInstance> oldInstancesByName,
69 Map<String, ComponentInstance> newInstancesByName) {
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());
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())) {
92 private boolean isInstanceNamesChanged(Map<String, ComponentInstance> oldInstanceByName, Map<String, ComponentInstance> newInstancesByName) {
93 return !oldInstanceByName.keySet().equals(newInstancesByName.keySet());
96 private boolean isSameToscaTypeOrOriginComponent(ComponentInstance oldInstance, ComponentInstance newInstance) {
97 return isSameToscaType(oldInstance, newInstance) || isSameOriginComponent(oldInstance, newInstance);
100 private boolean isSameToscaType(ComponentInstance oldInstance, ComponentInstance newInstance) {
101 return oldInstance.getToscaComponentName().equals(newInstance.getToscaComponentName());
104 private boolean isSameOriginComponent(ComponentInstance oldInstance, ComponentInstance newInstance) {
105 if (oldInstance.getComponentUid().equals(newInstance.getComponentUid())) {
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());
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));