Catalog alignment
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / merge / RelationsComparator.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 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;
28
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Optional;
32
33 @Component
34 public class RelationsComparator {
35
36     /**
37      *
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
42      */
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)) {
50                 return true;
51             }
52         }
53         return false;
54
55     }
56
57     private boolean isInstanceRelationsChanged(Resource oldResource, List<RequirementCapabilityRelDef> oldRelations, Resource newResource, List<RequirementCapabilityRelDef> newRelations) {
58         if (oldRelations == null || oldRelations.size() != newRelations.size()){
59             return true;
60         }
61         return newRelations.stream().anyMatch(newRelation -> !findRelation(oldResource, oldRelations, newResource, newRelation));
62     }
63
64
65
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)) {
71                 return true;
72             }
73         }
74         return false;
75     }
76
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());
83     }
84
85     private boolean isRelationEqual(RelationshipInfo oldRelationship, RelationshipInfo newRelationship) {
86         return isRelationshipTypeEquals(oldRelationship, newRelationship) &&
87                isRelationshipCapabilityEquals(oldRelationship, newRelationship) &&
88                isRelationshipReqNameEquals(oldRelationship, newRelationship);
89     }
90
91     private boolean isRelationshipCapabilityEquals(RelationshipInfo oldRelationship, RelationshipInfo newRelationship) {
92         if(oldRelationship.getCapabilityUid() !=null && newRelationship.getCapabilityUid() != null){
93             return oldRelationship.getCapabilityUid().equals(newRelationship.getCapabilityUid());
94         }
95         else if(oldRelationship.getCapabilityUid() == null && newRelationship.getCapabilityUid() == null){
96             return true;
97         }
98         return false;
99     }
100
101     private boolean isRelationshipTypeEquals(RelationshipInfo oldRelationship, RelationshipInfo newRelationship) {
102         return oldRelationship.getRelationship().getType().equals(newRelationship.getRelationship().getType());
103     }
104
105     private boolean isRelationshipReqNameEquals(RelationshipInfo oldRelationship, RelationshipInfo newRelationship) {
106         if(oldRelationship.getRequirement() != null && newRelationship.getRequirement() != null){
107             return oldRelationship.getRequirement().equals(newRelationship.getRequirement());
108         }
109         else if(oldRelationship.getRequirement() == null && newRelationship.getRequirement() == null){
110             return true;
111         }
112         return false;
113     }
114
115 }