re base code
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / utils / TypeCompareUtils.java
1 package org.openecomp.sdc.be.model.utils;
2
3 import com.google.common.base.Strings;
4 import fj.data.Either;
5 import org.apache.commons.collections.SetUtils;
6 import org.openecomp.sdc.be.dao.utils.MapUtil;
7 import org.openecomp.sdc.be.model.*;
8 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
9 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
10
11 import java.util.List;
12 import java.util.Map;
13 import java.util.Objects;
14 import java.util.stream.Collectors;
15
16 import static org.springframework.util.CollectionUtils.isEmpty;
17
18
19 /**
20  * Types comparison utils
21  * The class is required since origin class "equals" methods
22  * take in account fields that should be ignored during update of that types.
23  * @author dr2032
24  *
25  */
26 public class TypeCompareUtils {
27
28     private TypeCompareUtils() {
29     }
30     
31     public static <R> Either<R, StorageOperationStatus> typeAlreadyExists() {
32         return Either.right(StorageOperationStatus.OK);
33     }
34
35     public static boolean isGroupTypesEquals(GroupTypeDefinition gt1, GroupTypeDefinition gt2) {
36         if (gt1 == gt2) {
37             return true;
38         }
39         if (gt1 == null || gt2 == null) {
40             return false;
41         }
42         
43         /*
44          * We compare here attributes, capabilities and not inherited properties of group types.
45          * So even if properties of group type parent were changed it will not effect on comparison of these group types.
46          */
47         return Objects.equals(gt1.getType(), gt2.getType()) &&
48                 Objects.equals(gt1.getName(), gt2.getName()) &&
49                 Objects.equals(gt1.getIcon(), gt2.getIcon()) &&
50                 Objects.equals(gt1.getVersion(), gt2.getVersion()) &&
51                 Objects.equals(gt1.getDerivedFrom(), gt2.getDerivedFrom()) &&
52                 Objects.equals(gt1.getMembers(), gt2.getMembers()) &&
53                 Objects.equals(gt1.getMetadata(), gt2.getMetadata()) &&
54                 capabilitiesEqual(gt1.getCapabilities(), gt2.getCapabilities()) && 
55                 propertiesEquals(collectNotInheritedProperties(gt1.getProperties(), gt1.getUniqueId()), 
56                                 collectNotInheritedProperties(gt2.getProperties(), gt2.getUniqueId()));
57     }
58     
59     public static boolean isCapabilityTypesEquals(CapabilityTypeDefinition ct1, CapabilityTypeDefinition ct2) {
60         if (ct1 == ct2) {
61             return true;
62         }
63         
64         if (ct1 == null || ct2 == null) {
65             return false;
66         }
67         
68         return Objects.equals(ct1.getType(), ct2.getType()) &&
69                Objects.equals(ct1.getDerivedFrom(), ct2.getDerivedFrom()) &&
70                Objects.equals(ct1.getDescription(), ct2.getDescription()) &&
71                SetUtils.isEqualSet(ct1.getValidSourceTypes(), ct2.getValidSourceTypes()) &&
72                propertiesEquals(ct1.getProperties(), ct2.getProperties());
73     }
74     
75     private static boolean propertiesEquals(Map<String, PropertyDefinition> props1, Map<String, PropertyDefinition> props2) {
76         if (props1 == props2) {
77             return true;
78         }
79         
80         if (isEmpty(props1) && isEmpty(props2)) {
81             return true;
82         }
83         else if(props1 == null || props2 == null) {
84             return false;
85         }
86         else if(props1.size() != props2.size())
87         {
88             return false;
89         }
90
91         return props2.entrySet().stream()
92                                 .allMatch(entry -> propertyEquals(props1.get(entry.getKey()), entry.getValue()));
93         
94     }
95     
96     public static boolean propertiesEquals(List<PropertyDefinition> props1, List<PropertyDefinition> props2) {
97         if (props1 == props2) {
98             return true;
99         }
100         
101         if (isEmpty(props1) && isEmpty(props2)) {
102             return true;
103         }
104         else if(props1 == null || props2 == null) {
105             return false;
106         }
107         else if(props1.size() != props2.size())
108         {
109             return false;
110         }
111         
112         Map<String, PropertyDefinition> pt1PropsByName = MapUtil.toMap(props1, PropertyDefinition::getName);
113         return props2.stream()
114                        .allMatch(pt2Prop -> propertyEquals(pt1PropsByName.get(pt2Prop.getName()), pt2Prop));
115     }
116
117     private static boolean propertyEquals(PropertyDefinition prop1, PropertyDefinition prop2) {
118         if (prop1 == prop2) {
119             return true;
120         }
121         if (prop1 == null || prop2 == null) {
122             return false;
123         }
124         return Objects.equals(prop1.getDefaultValue(), prop2.getDefaultValue()) &&
125                 prop1.isDefinition() == prop2.isDefinition() &&
126                 Objects.equals(prop1.getDescription(), prop2.getDescription()) &&
127                 prop1.isPassword() == prop2.isPassword() &&
128                 prop1.isRequired() == prop2.isRequired() &&
129                 Objects.equals(prop1.getSchemaType(), prop2.getSchemaType()) &&
130                 Objects.equals(prop1.getType(), prop2.getType());
131     }
132
133     private static boolean capabilitiesEqual(Map<String, CapabilityDefinition> caps1, Map<String, CapabilityDefinition>  caps2) { 
134         if (caps1 == caps2) {
135             return true;
136         }
137         
138         if (caps1 == null || caps2 == null) {
139             return false;
140         }
141         
142         if(caps1.size() != caps2.size()) {
143             return false;
144         }
145             
146         return caps2.entrySet().stream()
147                 .allMatch(capEntry2 -> capabilityEquals(caps1.get(capEntry2.getKey()), capEntry2.getValue()));
148     }
149
150     public static boolean capabilityEquals(CapabilityDefinition capDef1, CapabilityDefinition capDef2) {
151         return Objects.equals(capDef1.getName(), capDef2.getName()) &&
152                 Objects.equals(capDef1.getType(), capDef2.getType()) &&
153                 Objects.equals(capDef1.getDescription(), capDef2.getDescription()) &&
154                 propValuesEqual(capDef1.getProperties(), capDef2.getProperties());
155     }
156
157     private static boolean propValuesEqual(final List<ComponentInstanceProperty> props1, final List<ComponentInstanceProperty> props2) {
158         Map<String, String> propValues1 = toValueMap(props1);
159         Map<String, String> propValues2 = toValueMap(props2);
160         
161         return propValues1.equals(propValues2);
162     }
163
164     /**
165      * @param props
166      * @return
167      */
168     private static Map<String, String> toValueMap(final List<ComponentInstanceProperty> props) {
169         return props.stream()
170                     .filter(TypeCompareUtils::isCapabilityPropValue)
171                     .collect(Collectors.toMap(ComponentInstanceProperty::getName, p -> p.getValue() != null? p.getValue(): ""));
172     }
173     
174     /**
175      * Returns true if the property object was created from property value or false otherwise
176      * 
177      * We try to segregate original properties values from dummy ones created from relevant properties.
178      * Such dummy property value doesn't have their valueUniqueId but it has uniqueId taken from property.
179      *    
180      * @param property
181      * @return
182      */
183     private static boolean isCapabilityPropValue(ComponentInstanceProperty property) {
184         return property.getValueUniqueUid() != null || property.getUniqueId() == null;
185     }
186     
187     /**
188      * Collect properties of resource that belongs to it without taking in account properties inherited from resource parents.
189      */
190     private static List<PropertyDefinition> collectNotInheritedProperties(List<PropertyDefinition> properties, 
191                                                                           String resourceId) {
192         if (Strings.isNullOrEmpty(resourceId)) {
193             return properties;
194         }
195         
196         return properties.stream()
197                          .filter(prop-> !isInherited(prop, resourceId))
198                          .collect(Collectors.toList());
199     }
200     
201     
202     private static boolean isInherited(PropertyDefinition prop, String resourceId) {
203         return prop.getUniqueId() != null && 
204                 !prop.getUniqueId().equals(UniqueIdBuilder.buildPropertyUniqueId(resourceId, prop.getName()));
205     }
206     
207 }