f75962ff5613ab683df352c1cf1febac2a878e54
[groupbasedpolicy.git] / renderers / ofoverlay / src / main / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / equivalence / MatchEquivalence.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.equivalence;
10
11 import com.google.common.base.Equivalence;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlow;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionList;
15
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Objects;
19 import java.util.Set;
20
21 /**
22  * Custom Equivalence for Match
23  *
24  * @see FlowEquivalence
25  */
26 public class MatchEquivalence extends Equivalence<Match> {
27
28     MatchEquivalence() {
29     }
30
31     @Override
32     protected boolean doEquivalent(Match a, Match b) {
33
34         if (!Objects.equals(a.getEthernetMatch(), b.getEthernetMatch())) {
35             return false;
36         }
37         if (!Objects.equals(a.getIcmpv4Match(), b.getIcmpv4Match())) {
38             return false;
39         }
40         if (!Objects.equals(a.getIcmpv6Match(), b.getIcmpv6Match())) {
41             return false;
42         }
43         if (!Objects.equals(a.getInPhyPort(), b.getInPhyPort())) {
44             return false;
45         }
46         if (!Objects.equals(a.getInPort(), b.getInPort())) {
47             return false;
48         }
49         if (!Objects.equals(a.getIpMatch(), b.getIpMatch())) {
50             return false;
51         }
52         if (!Objects.equals(a.getLayer3Match(), b.getLayer3Match())) {
53             return false;
54         }
55         if (!Objects.equals(a.getLayer4Match(), b.getLayer4Match())) {
56             return false;
57         }
58         if (!Objects.equals(a.getMetadata(), b.getMetadata())) {
59             return false;
60         }
61         if (!Objects.equals(a.getProtocolMatchFields(), b.getProtocolMatchFields())) {
62             return false;
63         }
64         if (!Objects.equals(a.getTcpFlagMatch(), b.getTcpFlagMatch())) {
65             return false;
66         }
67         if (!Objects.equals(a.getTunnel(), b.getTunnel())) {
68             return false;
69         }
70         if (!Objects.equals(a.getVlanMatch(), b.getVlanMatch())) {
71             return false;
72         }
73         GeneralAugMatchNodesNodeTableFlow generalAugMatchA =
74                 a.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class);
75         GeneralAugMatchNodesNodeTableFlow generalAugMatchB =
76                 b.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class);
77
78         if (generalAugMatchA != null && generalAugMatchB != null) {
79             // if both have GeneralAugMatchNodesNodeTableFlow augmentation
80             // create sets of ExtentionList type (not a List/Collection at all, as of yet)
81             Set<ExtensionList> setA = new HashSet<>();
82             Set<ExtensionList> setB = new HashSet<>();
83             if (generalAugMatchA.getExtensionList() != null) {
84                 setA = new HashSet<>(generalAugMatchA.getExtensionList());
85             }
86             if (generalAugMatchB.getExtensionList() != null) {
87                 setB = new HashSet<>(generalAugMatchB.getExtensionList());
88             }
89             if (!setA.equals(setB)) {
90                 return false;
91             }
92
93         } else if ((generalAugMatchA == null && generalAugMatchB != null)
94                 || generalAugMatchA != null) {
95             // if only one has GeneralAugMatchNodesNodeTableFlow augmentation, they are not equal
96             return false;
97         } // if no-one has GeneralAugMatchNodesNodeTableFlow augmentation, continue matching
98
99         return true;
100     }
101
102     @Override
103     protected int doHash(Match m) {
104         final int prime = 31;
105         int result = 1;
106         result = prime * result + ((m.getEthernetMatch() == null) ? 0 : m.getEthernetMatch().hashCode());
107         result = prime * result + ((m.getIcmpv4Match() == null) ? 0 : m.getIcmpv4Match().hashCode());
108         result = prime * result + ((m.getIcmpv6Match() == null) ? 0 : m.getIcmpv6Match().hashCode());
109         result = prime * result + ((m.getInPhyPort() == null) ? 0 : m.getInPhyPort().hashCode());
110         result = prime * result + ((m.getInPort() == null) ? 0 : m.getInPort().hashCode());
111         result = prime * result + ((m.getIpMatch() == null) ? 0 : m.getIpMatch().hashCode());
112         result = prime * result + ((m.getLayer3Match() == null) ? 0 : m.getLayer3Match().hashCode());
113         result = prime * result + ((m.getLayer4Match() == null) ? 0 : m.getLayer4Match().hashCode());
114         result = prime * result + ((m.getMetadata() == null) ? 0 : m.getMetadata().hashCode());
115         result = prime * result + ((m.getProtocolMatchFields() == null) ? 0 : m.getProtocolMatchFields().hashCode());
116         result = prime * result + ((m.getTcpFlagMatch() == null) ? 0 : m.getTcpFlagMatch().hashCode());
117         result = prime * result + ((m.getTunnel() == null) ? 0 : m.getTunnel().hashCode());
118         result = prime * result + ((m.getVlanMatch() == null) ? 0 : m.getVlanMatch().hashCode());
119
120         GeneralAugMatchNodesNodeTableFlow generalAugMatch =
121                 m.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class);
122         if (generalAugMatch != null) {
123             List<ExtensionList> augMatchExtensionList = generalAugMatch.getExtensionList();
124             Set<ExtensionList> extensionListSet = new HashSet<>();
125             if (augMatchExtensionList != null) {
126                 extensionListSet =
127                         new HashSet<>(augMatchExtensionList);
128             }
129             result = prime * result + extensionListSet.hashCode();
130         }
131         return result;
132     }
133 }