Bug 3302: fix for GroupTable
[groupbasedpolicy.git] / renderers / ofoverlay / src / main / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / equivalence / FlowEquivalence.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.inventory.rev130819.tables.table.Flow;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
14
15 import java.util.ArrayList;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Objects;
19 import java.util.Set;
20
21 /**
22  * Custom {@link Equivalence} for {@link Flow}
23  *
24  */
25 public class FlowEquivalence extends Equivalence<Flow> {
26
27     FlowEquivalence() {
28     }
29
30     @Override
31     protected boolean doEquivalent(Flow a, Flow b) {
32
33         if (!Objects.equals(a.getBufferId(), b.getBufferId())) {
34             return false;
35         }
36         if (!Objects.equals(a.getContainerName(), b.getContainerName())) {
37             return false;
38         }
39         if (!Objects.equals(a.getCookie(), b.getCookie())) {
40             return false;
41         }
42         if (!Objects.equals(a.getCookieMask(), b.getCookieMask())) {
43             return false;
44         }
45         if (!Objects.equals(a.getFlags(), b.getFlags())) {
46             return false;
47         }
48         if (!Objects.equals(a.getFlowName(), b.getFlowName())) {
49             return false;
50         }
51
52         List<Instruction> listA = new ArrayList<>();
53         if (a.getInstructions() != null) {
54             listA = a.getInstructions().getInstruction();
55         }
56         Set<Instruction> setA = new HashSet<>();
57         if (listA != null) {
58             setA = new HashSet<>(listA);
59         }
60         List<Instruction> listB = new ArrayList<>();
61         if (b.getInstructions() != null) {
62             listB = b.getInstructions().getInstruction();
63         }
64         Set<Instruction> setB = new HashSet<>();
65         if (listB != null) {
66             setB = new HashSet<>(listB);
67         }
68         if (!setA.equals(setB)) {
69             return false;
70         }
71
72         if (!EquivalenceFabric.MATCH_EQUIVALENCE
73                 .equivalent(a.getMatch(), b.getMatch())) {
74             return false;
75         }
76         if (!Objects.equals(a.getOutGroup(), b.getOutGroup())) {
77             return false;
78         }
79         if (!Objects.equals(a.getOutPort(), b.getOutPort())) {
80             return false;
81         }
82         if (!Objects.equals(a.getPriority(), b.getPriority())) {
83             return false;
84         }
85         if (!Objects.equals(a.getTableId(), b.getTableId())) {
86             return false;
87         }
88         if (!Objects.equals(a.isBarrier(), b.isBarrier())) {
89             return false;
90         }
91         if (!Objects.equals(a.isInstallHw(), b.isInstallHw())) {
92             return false;
93         }
94         if (!Objects.equals(a.isStrict(), b.isStrict())) {
95             return false;
96         }
97
98         return true;
99     }
100
101     @Override
102     protected int doHash(Flow flow) {
103         final int prime = 31;
104         int result = 1;
105
106         result = prime * result + ((flow.getBufferId() == null) ? 0 : flow.getBufferId().hashCode());
107         result = prime * result + ((flow.getContainerName() == null) ? 0 : flow.getContainerName().hashCode());
108         result = prime * result + ((flow.getCookie() == null) ? 0 : flow.getCookie().hashCode());
109         result = prime * result + ((flow.getCookieMask() == null) ? 0 : flow.getCookieMask().hashCode());
110         result = prime * result + ((flow.getFlags() == null) ? 0 : flow.getFlags().hashCode());
111         result = prime * result + ((flow.getFlowName() == null) ? 0 : flow.getFlowName().hashCode());
112
113         if (flow.getInstructions() != null
114                 && flow.getInstructions().getInstruction() != null
115                 && !flow.getInstructions().getInstruction().isEmpty()) {
116             Set<Instruction> instructions = new HashSet<>(flow.getInstructions().getInstruction());
117             result = prime * result + instructions.hashCode();
118         }
119
120         result = prime * result + ((flow.getMatch() == null) ? 0
121                 : EquivalenceFabric.MATCH_EQUIVALENCE.wrap(flow.getMatch()).hashCode());
122         result = prime * result + ((flow.getOutGroup() == null) ? 0 : flow.getOutGroup().hashCode());
123         result = prime * result + ((flow.getOutPort() == null) ? 0 : flow.getOutPort().hashCode());
124         result = prime * result + ((flow.getPriority() == null) ? 0 : flow.getPriority().hashCode());
125         result = prime * result + ((flow.getTableId() == null) ? 0 : flow.getTableId().hashCode());
126         result = prime * result + ((flow.isBarrier() == null) ? 0 : flow.isBarrier().hashCode());
127         result = prime * result + ((flow.isInstallHw() == null) ? 0 : flow.isInstallHw().hashCode());
128         result = prime * result + ((flow.isStrict() == null) ? 0 : flow.isStrict().hashCode());
129
130         return result;
131     }
132 }