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 org.openecomp.sdc.be.model.ComponentInstance;
24 import org.openecomp.sdc.be.model.RelationshipInfo;
25 import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
26 import org.openecomp.sdc.be.model.Resource;
27 import org.springframework.stereotype.Component;
29 import java.util.List;
31 import java.util.Optional;
34 public class RelationsComparator {
38 * @param oldResource the old resource
39 * @param newResource the new resource
40 * @return true if there was a change in one of the relations between the old and new resource
41 * a change in relation is determine by comparing the relations type, node, capability and requirement name
43 public boolean isRelationsChanged(Resource oldResource, Resource newResource) {
44 Map<String, List<RequirementCapabilityRelDef>> oldRelationsByInstance = oldResource.groupRelationsFromCsarByInstanceName(oldResource);
45 Map<String, List<RequirementCapabilityRelDef>> newRelationsByInstance = newResource.groupRelationsFromCsarByInstanceName(newResource);
46 for (Map.Entry<String, List<RequirementCapabilityRelDef>> relationByInst : newRelationsByInstance.entrySet()) {
47 List<RequirementCapabilityRelDef> oldRelations = oldRelationsByInstance.get(relationByInst.getKey());
48 List<RequirementCapabilityRelDef> newRelations = relationByInst.getValue();
49 if (isInstanceRelationsChanged(oldResource, oldRelations, newResource, newRelations)) {
57 private boolean isInstanceRelationsChanged(Resource oldResource, List<RequirementCapabilityRelDef> oldRelations, Resource newResource, List<RequirementCapabilityRelDef> newRelations) {
58 if (oldRelations == null || oldRelations.size() != newRelations.size()){
61 return newRelations.stream().anyMatch(newRelation -> !findRelation(oldResource, oldRelations, newResource, newRelation));
66 private boolean findRelation(Resource oldResource, List<RequirementCapabilityRelDef> oldRelations, Resource newResource, RequirementCapabilityRelDef newRelation) {
67 for (RequirementCapabilityRelDef oldRelation : oldRelations) {
68 RelationshipInfo oldRelationship = oldRelation.resolveSingleRelationship().getRelation();
69 RelationshipInfo newRelationship = newRelation.resolveSingleRelationship().getRelation();
70 if (oldRelationship != null && newRelationship != null && isRelationEqual(oldRelationship, newRelationship) && isRelationToNodeEquals(oldResource, oldRelation, newResource, newRelation)) {
77 private boolean isRelationToNodeEquals(Resource oldResource, RequirementCapabilityRelDef oldRelation, Resource newResource, RequirementCapabilityRelDef newRelation) {
78 String oldToNodeId = oldRelation.getToNode();
79 String newToNodeId = newRelation.getToNode();
80 Optional<ComponentInstance> oldRelationToNode = oldResource.getComponentInstanceById(oldToNodeId);
81 Optional<ComponentInstance> newRelationToNode = newResource.getComponentInstanceById(newToNodeId);
82 return oldRelationToNode.isPresent() && newRelationToNode.isPresent() && oldRelationToNode.get().getInvariantName().equals(newRelationToNode.get().getInvariantName());
85 private boolean isRelationEqual(RelationshipInfo oldRelationship, RelationshipInfo newRelationship) {
86 return isRelationshipTypeEquals(oldRelationship, newRelationship) &&
87 isRelationshipCapabilityEquals(oldRelationship, newRelationship) &&
88 isRelationshipReqNameEquals(oldRelationship, newRelationship);
91 private boolean isRelationshipCapabilityEquals(RelationshipInfo oldRelationship, RelationshipInfo newRelationship) {
92 if(oldRelationship.getCapabilityUid() !=null && newRelationship.getCapabilityUid() != null){
93 return oldRelationship.getCapabilityUid().equals(newRelationship.getCapabilityUid());
95 else if(oldRelationship.getCapabilityUid() == null && newRelationship.getCapabilityUid() == null){
101 private boolean isRelationshipTypeEquals(RelationshipInfo oldRelationship, RelationshipInfo newRelationship) {
102 return oldRelationship.getRelationship().getType().equals(newRelationship.getRelationship().getType());
105 private boolean isRelationshipReqNameEquals(RelationshipInfo oldRelationship, RelationshipInfo newRelationship) {
106 if(oldRelationship.getRequirement() != null && newRelationship.getRequirement() != null){
107 return oldRelationship.getRequirement().equals(newRelationship.getRequirement());
109 else if(oldRelationship.getRequirement() == null && newRelationship.getRequirement() == null){