2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.components.impl.utils;
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;
27 import java.util.List;
29 import java.util.Objects;
31 import static org.apache.commons.collections.CollectionUtils.isNotEmpty;
32 import static org.springframework.util.CollectionUtils.isEmpty;
34 public class PolicyTypeImportUtils {
36 private PolicyTypeImportUtils() {
39 public static boolean isPolicyTypesEquals(PolicyTypeDefinition pt1, PolicyTypeDefinition pt2) {
43 if (pt1 == null || pt2 == null) {
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());
57 private static boolean isPolicyPropertiesEquals(List<PropertyDefinition> pt1Props, List<PropertyDefinition> pt2Props) {
58 if (pt1Props == pt2Props) {
61 if (pt1Props == null && isEmpty(pt2Props) || pt2Props == null && isEmpty(pt1Props)) {
64 if (isPropertiesListSizesNotEquals(pt1Props, pt2Props)) {
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();
72 private static boolean policyPropertyEquals(PropertyDefinition pt1Prop, PropertyDefinition pt2Prop) {
73 if (pt1Prop == pt2Prop) {
76 if (pt1Prop == null || pt2Prop == null) {
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());
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();