2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.groupbasedpolicy.renderer;
11 import java.util.List;
13 import java.util.Objects;
15 import javax.annotation.Nullable;
17 import com.google.common.annotations.VisibleForTesting;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.CommonEndpointFields;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.NatAddress;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoint.locations.AddressEndpointLocation;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoint.locations.ContainmentEndpointLocation;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.address.endpoints.AddressEndpoint;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.address.endpoints.AddressEndpointKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.containment.endpoints.ContainmentEndpoint;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.containment.endpoints.ContainmentEndpointKey;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.relative.location.RelativeLocations;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.rev160427.Forwarding;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.rev160427.forwarding.ForwardingByTenant;
31 import com.google.common.base.Equivalence;
32 import com.google.common.base.Function;
33 import com.google.common.collect.ImmutableMap;
34 import com.google.common.collect.MapDifference;
35 import com.google.common.collect.Maps;
37 class DtoEquivalenceUtils {
39 private static final Function<ForwardingByTenant, TenantId> FORWARDING_BY_TENANT_TO_TENANT_ID =
40 new Function<ForwardingByTenant, TenantId>() {
43 public TenantId apply(ForwardingByTenant input) {
44 return input.getTenantId();
49 static final Equivalence<AddressEndpoint> ADDR_EP_EQ = new Equivalence<AddressEndpoint>() {
52 protected boolean doEquivalent(AddressEndpoint a, AddressEndpoint b) {
53 if (!Objects.equals(a.getKey(), b.getKey())) {
56 if (!Objects.equals(a.getParentEndpointChoice(), b.getParentEndpointChoice())) {
59 if (!equalsDtoLists(a.getChildEndpoint(), b.getChildEndpoint())) {
62 if (!equalsCommonEndpointFields(a, b)) {
65 if (!equalsAugmentations(a, b)) {
72 protected int doHash(AddressEndpoint t) {
77 static final Equivalence<ContainmentEndpoint> CONT_EP_EQ = new Equivalence<ContainmentEndpoint>() {
80 protected boolean doEquivalent(ContainmentEndpoint a, ContainmentEndpoint b) {
81 if (!Objects.equals(a.getKey(), b.getKey())) {
84 if (!equalsDtoLists(a.getChildEndpoint(), b.getChildEndpoint())) {
87 if (!equalsCommonEndpointFields(a, b)) {
94 protected int doHash(ContainmentEndpoint t) {
99 static final Equivalence<AddressEndpointLocation> ADDR_EP_LOC_EQ =
100 new Equivalence<AddressEndpointLocation>() {
103 protected boolean doEquivalent(AddressEndpointLocation a, AddressEndpointLocation b) {
104 if (!Objects.equals(a.getKey(), b.getKey())) {
107 if (!Objects.equals(a.getAbsoluteLocation(), b.getAbsoluteLocation())) {
110 if (!equalsRelativeLocations(a.getRelativeLocations(), b.getRelativeLocations())) {
117 protected int doHash(AddressEndpointLocation t) {
122 static final Equivalence<ContainmentEndpointLocation> CONT_EP_LOC_EQ =
123 new Equivalence<ContainmentEndpointLocation>() {
126 protected boolean doEquivalent(ContainmentEndpointLocation a, ContainmentEndpointLocation b) {
127 if (!Objects.equals(a.getKey(), b.getKey())) {
130 if (!equalsRelativeLocations(a.getRelativeLocations(), b.getRelativeLocations())) {
137 protected int doHash(ContainmentEndpointLocation t) {
142 static final Equivalence<ForwardingByTenant> FWD_BY_TENANT_EQ = new Equivalence<ForwardingByTenant>() {
145 protected boolean doEquivalent(ForwardingByTenant x, ForwardingByTenant y) {
146 if (!Objects.equals(x.getKey(), y.getKey())) {
149 if (!equalsDtoLists(x.getForwardingContext(), y.getForwardingContext())) {
152 if (!equalsDtoLists(x.getNetworkDomain(), y.getNetworkDomain())) {
159 protected int doHash(ForwardingByTenant t) {
164 private DtoEquivalenceUtils() {}
166 private static boolean equalsAugmentations(AddressEndpoint a, AddressEndpoint b) {
167 if (a.getAugmentation(NatAddress.class) != null && b.getAugmentation(NatAddress.class) != null) {
168 if (!a.getAugmentation(NatAddress.class)
170 .equals(b.getAugmentation(NatAddress.class).getNatAddress())) {
173 } else if (a.getAugmentation(NatAddress.class) != null || b.getAugmentation(NatAddress.class) != null) {
179 static boolean equalsAddressEpByKey(@Nullable Map<AddressEndpointKey, AddressEndpoint> o1,
180 @Nullable Map<AddressEndpointKey, AddressEndpoint> o2) {
181 return equalsDtoMapsByEquivalence(o1, o2, ADDR_EP_EQ);
184 static boolean equalsContainmentEpByKey(@Nullable Map<ContainmentEndpointKey, ContainmentEndpoint> o1,
185 @Nullable Map<ContainmentEndpointKey, ContainmentEndpoint> o2) {
186 return equalsDtoMapsByEquivalence(o1, o2, CONT_EP_EQ);
189 static boolean equalsAddrEpLocByAddrEpKey(Map<AddressEndpointKey, AddressEndpointLocation> o1,
190 Map<AddressEndpointKey, AddressEndpointLocation> o2) {
191 return equalsDtoMapsByEquivalence(o1, o2, ADDR_EP_LOC_EQ);
194 static boolean equalsContEpLocByContEpKey(Map<ContainmentEndpointKey, ContainmentEndpointLocation> o1,
195 Map<ContainmentEndpointKey, ContainmentEndpointLocation> o2) {
196 return equalsDtoMapsByEquivalence(o1, o2, CONT_EP_LOC_EQ);
199 static boolean equalsForwarding(@Nullable Forwarding a, @Nullable Forwarding b) {
200 if ((a == null && b != null) || (a != null && b == null)) {
203 if (a == null && b == null) {
206 return equalsForwardingByTenantLists(a.getForwardingByTenant(), b.getForwardingByTenant());
209 private static boolean equalsCommonEndpointFields(CommonEndpointFields a, CommonEndpointFields b) {
210 if (!Objects.equals(a.getNetworkContainment(), b.getNetworkContainment())) {
213 if (!Objects.equals(a.getTenant(), b.getTenant())) {
216 if (!Objects.equals(a.getTimestamp(), b.getTimestamp())) {
219 if (!equalsDtoLists(a.getCondition(), b.getCondition())) {
222 if (!equalsDtoLists(a.getEndpointGroup(), b.getEndpointGroup())) {
228 private static boolean equalsRelativeLocations(RelativeLocations a, RelativeLocations b) {
229 if ((a == null && b != null) || (a != null && b == null)) {
232 if (a == null && b == null) {
235 if (!equalsDtoLists(a.getExternalLocation(), b.getExternalLocation())) {
238 if (!equalsDtoLists(a.getInternalLocation(), b.getInternalLocation())) {
244 private static boolean equalsForwardingByTenantLists(List<ForwardingByTenant> a, List<ForwardingByTenant> b) {
245 if ((a == null && b != null) || (a != null && b == null)) {
248 if (a == null && b == null) {
251 if (a.size() != b.size()) {
254 ImmutableMap<TenantId, ForwardingByTenant> aMap = Maps.uniqueIndex(a, FORWARDING_BY_TENANT_TO_TENANT_ID);
255 ImmutableMap<TenantId, ForwardingByTenant> bMap = Maps.uniqueIndex(b, FORWARDING_BY_TENANT_TO_TENANT_ID);
256 return equalsDtoMapsByEquivalence(aMap, bMap, FWD_BY_TENANT_EQ);
259 private static <K, V> boolean equalsDtoMapsByEquivalence(@Nullable Map<K, V> o1, @Nullable Map<K, V> o2,
261 if ((o1 == null && o2 != null) || (o1 != null && o2 == null)) {
264 if (o1 == null && o2 == null) {
267 if (o1.size() != o2.size()) {
270 MapDifference<K, V> diff = Maps.difference(o1, o2, eq);
271 if (!diff.entriesOnlyOnLeft().isEmpty() || !diff.entriesOnlyOnRight().isEmpty()) {
274 if (!diff.entriesDiffering().isEmpty()) {
280 private static <T> boolean equalsDtoLists(@Nullable List<T> a, @Nullable List<T> b) {
281 if ((a == null && b != null) || (a != null && b == null)) {
284 if (a == null && b == null) {
287 return a.size() == b.size() && a.containsAll(b);