Added oparent to sdc main
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / utils / PolicyTypeImportUtils.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.impl.utils;
22
23 import org.openecomp.sdc.be.dao.utils.MapUtil;
24 import org.openecomp.sdc.be.model.PolicyTypeDefinition;
25 import org.openecomp.sdc.be.model.PropertyDefinition;
26
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Objects;
30
31 import static org.apache.commons.collections.CollectionUtils.isNotEmpty;
32 import static org.springframework.util.CollectionUtils.isEmpty;
33
34 public class PolicyTypeImportUtils {
35
36     private PolicyTypeImportUtils() {
37     }
38
39     public static boolean isPolicyTypesEquals(PolicyTypeDefinition pt1, PolicyTypeDefinition pt2) {
40         if (pt1 == pt2) {
41             return true;
42         }
43         if (pt1 == null || pt2 == null) {
44             return false;
45         }
46         return Objects.equals(pt1.getType(), pt2.getType()) &&
47                 Objects.equals(pt1.getName(), pt2.getName()) &&
48                 Objects.equals(pt1.getIcon(), pt2.getIcon()) &&
49                 Objects.equals(pt1.getVersion(), pt2.getVersion()) &&
50                 Objects.equals(pt1.getDerivedFrom(), pt2.getDerivedFrom()) &&
51                 Objects.equals(pt1.getTargets(), pt2.getTargets()) &&
52                 Objects.equals(pt1.getMetadata(), pt2.getMetadata()) &&
53                 Objects.equals(pt1.getDescription(), pt2.getDescription()) &&
54                 PolicyTypeImportUtils.isPolicyPropertiesEquals(pt1.getProperties(), pt2.getProperties());
55     }
56
57     private static boolean isPolicyPropertiesEquals(List<PropertyDefinition> pt1Props, List<PropertyDefinition> pt2Props) {
58         if (pt1Props == pt2Props) {
59             return true;
60         }
61         if (pt1Props == null && isEmpty(pt2Props) || pt2Props == null && isEmpty(pt1Props)) {
62             return true;
63         }
64         if (isPropertiesListSizesNotEquals(pt1Props, pt2Props)) {
65             return false;
66         }
67         Map<String, PropertyDefinition> pt1PropsByName = MapUtil.toMap(pt1Props, PropertyDefinition::getName);
68         long numberOfEqualsProperties = pt2Props.stream().filter(pt2Prop -> policyPropertyEquals(pt1PropsByName.get(pt2Prop.getName()), pt2Prop)).count();
69         return numberOfEqualsProperties == pt1Props.size();
70     }
71
72     private static boolean policyPropertyEquals(PropertyDefinition pt1Prop, PropertyDefinition pt2Prop) {
73         if (pt1Prop == pt2Prop) {
74             return true;
75         }
76         if (pt1Prop == null || pt2Prop == null) {
77             return false;
78         }
79         return Objects.equals(pt1Prop.getDefaultValue(), pt2Prop.getDefaultValue()) &&
80                Objects.equals(pt1Prop.isDefinition(), pt2Prop.isDefinition()) &&
81                Objects.equals(pt1Prop.getDescription(), pt2Prop.getDescription()) &&
82                Objects.equals(pt1Prop.isPassword(), pt2Prop.isPassword()) &&
83                Objects.equals(pt1Prop.isRequired(), pt2Prop.isRequired()) &&
84                Objects.equals(pt1Prop.getSchemaType(), pt2Prop.getSchemaType()) &&
85                Objects.equals(pt1Prop.getType(), pt2Prop.getType());
86     }
87
88     private static boolean isPropertiesListSizesNotEquals(List<PropertyDefinition> pt1Props, List<PropertyDefinition> pt2Props) {
89         return isEmpty(pt1Props) && isNotEmpty(pt2Props) ||
90                isEmpty(pt2Props) && isNotEmpty(pt1Props) ||
91                pt1Props.size() != pt2Props.size();
92     }
93
94 }