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