summaryrefslogtreecommitdiffstats
path: root/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/utils/PolicyTypeImportUtils.java
blob: 30a93fa0bc14fbe42a260687e7dc74f981e290f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package org.openecomp.sdc.be.components.impl.utils;

import org.apache.commons.collections.CollectionUtils;
import org.openecomp.sdc.be.dao.utils.MapUtil;
import org.openecomp.sdc.be.model.PolicyTypeDefinition;
import org.openecomp.sdc.be.model.PropertyDefinition;

import java.util.List;
import java.util.Map;
import java.util.Objects;

public class PolicyTypeImportUtils {

    private PolicyTypeImportUtils() {
    }

    public static boolean isPolicyTypesEquals(PolicyTypeDefinition pt1, PolicyTypeDefinition pt2) {
        if (pt1 == pt2) {
            return true;
        }
        if (pt1 == null || pt2 == null) {
            return false;
        }
        return Objects.equals(pt1.getType(), pt2.getType()) &&
                Objects.equals(pt1.getVersion(), pt2.getVersion()) &&
                Objects.equals(pt1.getDerivedFrom(), pt2.getDerivedFrom()) &&
                Objects.equals(pt1.getTargets(), pt2.getTargets()) &&
                Objects.equals(pt1.getMetadata(), pt2.getMetadata()) &&
                Objects.equals(pt1.getDescription(), pt2.getDescription()) &&
                PolicyTypeImportUtils.isPolicyPropertiesEquals(pt1.getProperties(), pt2.getProperties());
    }

    private static boolean isPolicyPropertiesEquals(List<PropertyDefinition> pt1Props, List<PropertyDefinition> pt2Props) {
        if (pt1Props == pt2Props) {
            return true;
        }
        if (pt1Props == null || pt2Props == null) {
            return false;
        }
        if (isPropertiesListSizesNotEquals(pt1Props, pt2Props)) {
            return false;
        }
        Map<String, PropertyDefinition> pt1PropsByName = MapUtil.toMap(pt1Props, PropertyDefinition::getName);
        long numberOfEqualsProperties = pt2Props.stream().filter(pt2Prop -> policyPropertyEquals(pt1PropsByName.get(pt2Prop.getName()), pt2Prop)).count();
        return numberOfEqualsProperties == pt1Props.size();
    }

    private static boolean policyPropertyEquals(PropertyDefinition pt1Prop, PropertyDefinition pt2Prop) {
        if (pt1Prop == pt2Prop) {
            return true;
        }
        if (pt1Prop == null || pt2Prop == null) {
            return false;
        }
        return Objects.equals(pt1Prop.getDefaultValue(), pt2Prop.getDefaultValue()) &&
               Objects.equals(pt1Prop.isDefinition(), pt2Prop.isDefinition()) &&
               Objects.equals(pt1Prop.getDescription(), pt2Prop.getDescription()) &&
               Objects.equals(pt1Prop.isPassword(), pt2Prop.isPassword()) &&
               Objects.equals(pt1Prop.isRequired(), pt2Prop.isRequired()) &&
               Objects.equals(pt1Prop.getSchemaType(), pt2Prop.getSchemaType()) &&
               Objects.equals(pt1Prop.getType(), pt2Prop.getType());
    }

    private static boolean isPropertiesListSizesNotEquals(List<PropertyDefinition> pt1Props, List<PropertyDefinition> pt2Props) {
        return CollectionUtils.isEmpty(pt1Props) && CollectionUtils.isNotEmpty(pt2Props) ||
                CollectionUtils.isEmpty(pt2Props) && CollectionUtils.isNotEmpty(pt1Props) ||
                pt1Props.size() != pt2Props.size();
    }

}