Fixup Augmentable and Identifiable methods changing
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / registry / flow / FlowRegistryKeyFactory.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.openflowplugin.impl.registry.flow;
10
11 import com.google.common.base.MoreObjects;
12 import com.google.common.base.Preconditions;
13 import java.math.BigInteger;
14 import java.util.Objects;
15 import javax.annotation.Nonnull;
16 import org.opendaylight.openflowplugin.api.OFConstants;
17 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
18 import org.opendaylight.openflowplugin.impl.util.MatchNormalizationUtil;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlow;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionList;
23
24 public final class FlowRegistryKeyFactory {
25
26     private FlowRegistryKeyFactory() {
27         // Hide implicit constructor
28     }
29
30     @Nonnull
31     public static FlowRegistryKey create(final short version, @Nonnull final Flow flow) {
32         //TODO: mandatory flow input values (or default values) should be specified via yang model
33         final short tableId = Preconditions.checkNotNull(flow.getTableId(), "flow tableId must not be null");
34         final int priority = MoreObjects.firstNonNull(flow.getPriority(), OFConstants.DEFAULT_FLOW_PRIORITY);
35         final BigInteger cookie =
36                 MoreObjects.firstNonNull(flow.getCookie(), OFConstants.DEFAULT_FLOW_COOKIE).getValue();
37         Match match = MatchNormalizationUtil
38                 .normalizeMatch(MoreObjects.firstNonNull(flow.getMatch(), OFConstants.EMPTY_MATCH), version);
39         return new FlowRegistryKeyDto(tableId, priority, cookie, match);
40     }
41
42     private static final class FlowRegistryKeyDto implements FlowRegistryKey {
43         private final short tableId;
44         private final int priority;
45         private final BigInteger cookie;
46         private final Match match;
47
48         private FlowRegistryKeyDto(final short tableId,
49                                    final int priority,
50                                    @Nonnull final BigInteger cookie,
51                                    @Nonnull final Match match) {
52             this.tableId = tableId;
53             this.priority = priority;
54             this.cookie = cookie;
55             this.match = match;
56         }
57
58         @Override
59         public boolean equals(final Object object) {
60             if (this == object) {
61                 return true;
62             }
63
64             if (object == null || !(object instanceof FlowRegistryKey)) {
65                 return false;
66             }
67
68             final FlowRegistryKey that = (FlowRegistryKey) object;
69
70             return getPriority() == that.getPriority()
71                     && getTableId() == that.getTableId()
72                     && getCookie().equals(that.getCookie())
73                     && equalMatch(that.getMatch());
74         }
75
76         private boolean equalMatch(final Match input) {
77             GeneralAugMatchNodesNodeTableFlow thisAug = match.augmentation(GeneralAugMatchNodesNodeTableFlow.class);
78             GeneralAugMatchNodesNodeTableFlow inputAug = input.augmentation(GeneralAugMatchNodesNodeTableFlow.class);
79             if (thisAug != inputAug) {
80                 if (thisAug != null) {
81                     if (inputAug == null) {
82                         return false;
83                     }
84                     if (!Objects.equals(match.getEthernetMatch(), input.getEthernetMatch())) {
85                         return false;
86                     }
87                     if (!Objects.equals(match.getIcmpv4Match(), input.getIcmpv4Match())) {
88                         return false;
89                     }
90                     if (!Objects.equals(match.getIcmpv6Match(), input.getIcmpv6Match())) {
91                         return false;
92                     }
93                     if (!Objects.equals(match.getInPhyPort(), input.getInPhyPort())) {
94                         return false;
95                     }
96                     if (!Objects.equals(match.getInPort(), input.getInPort())) {
97                         return false;
98                     }
99                     if (!Objects.equals(match.getIpMatch(), input.getIpMatch())) {
100                         return false;
101                     }
102                     if (!Objects.equals(match.getLayer3Match(), input.getLayer3Match())) {
103                         return false;
104                     }
105                     if (!Objects.equals(match.getLayer4Match(), input.getLayer4Match())) {
106                         return false;
107                     }
108                     if (!Objects.equals(match.getMetadata(), input.getMetadata())) {
109                         return false;
110                     }
111                     if (!Objects.equals(match.getProtocolMatchFields(), input.getProtocolMatchFields())) {
112                         return false;
113                     }
114                     if (!Objects.equals(match.getTcpFlagsMatch(), input.getTcpFlagsMatch())) {
115                         return false;
116                     }
117                     if (!Objects.equals(match.getTunnel(), input.getTunnel())) {
118                         return false;
119                     }
120                     if (!Objects.equals(match.getVlanMatch(), input.getVlanMatch())) {
121                         return false;
122                     }
123                     for (ExtensionList inputExtensionList : inputAug.getExtensionList()) {
124                         if (!thisAug.getExtensionList().contains(inputExtensionList)) {
125                             return false;
126                         }
127                     }
128                 }
129             } else {
130                 return getMatch().equals(input);
131             }
132             return true;
133         }
134
135         @Override
136         public int hashCode() {
137             int result = tableId;
138             result = 31 * result + priority;
139             result = 31 * result + cookie.hashCode();
140             result = 31 * result + match.hashCode();
141             return result;
142         }
143
144         @Override
145         public String toString() {
146             return "FlowRegistryKeyDto{"
147                     + "tableId=" + tableId
148                     + ", priority=" + priority
149                     + ", cookie=" + cookie
150                     + ", match=" + match
151                     + '}';
152         }
153
154         @Override
155         public short getTableId() {
156             return tableId;
157         }
158
159         @Override
160         public int getPriority() {
161             return priority;
162         }
163
164         @Override
165         public BigInteger getCookie() {
166             return cookie;
167         }
168
169         @Override
170         public Match getMatch() {
171             return match;
172         }
173     }
174 }