2 * Copyright (c) 2015 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.openflowplugin.impl.registry.flow;
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;
24 public final class FlowRegistryKeyFactory {
26 private FlowRegistryKeyFactory() {
27 // Hide implicit constructor
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);
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;
48 private FlowRegistryKeyDto(final short tableId,
50 @Nonnull final BigInteger cookie,
51 @Nonnull final Match match) {
52 this.tableId = tableId;
53 this.priority = priority;
59 public boolean equals(final Object object) {
64 if (object == null || !(object instanceof FlowRegistryKey)) {
68 final FlowRegistryKey that = (FlowRegistryKey) object;
70 return getPriority() == that.getPriority()
71 && getTableId() == that.getTableId()
72 && getCookie().equals(that.getCookie())
73 && equalMatch(that.getMatch());
76 private boolean equalMatch(final Match input) {
77 GeneralAugMatchNodesNodeTableFlow thisAug = match.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class);
78 GeneralAugMatchNodesNodeTableFlow inputAug = input.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class);
79 if (thisAug != inputAug) {
80 if (thisAug != null) {
81 if (inputAug == null) {
84 if (!Objects.equals(match.getEthernetMatch(), input.getEthernetMatch())) {
87 if (!Objects.equals(match.getIcmpv4Match(), input.getIcmpv4Match())) {
90 if (!Objects.equals(match.getIcmpv6Match(), input.getIcmpv6Match())) {
93 if (!Objects.equals(match.getInPhyPort(), input.getInPhyPort())) {
96 if (!Objects.equals(match.getInPort(), input.getInPort())) {
99 if (!Objects.equals(match.getIpMatch(), input.getIpMatch())) {
102 if (!Objects.equals(match.getLayer3Match(), input.getLayer3Match())) {
105 if (!Objects.equals(match.getLayer4Match(), input.getLayer4Match())) {
108 if (!Objects.equals(match.getMetadata(), input.getMetadata())) {
111 if (!Objects.equals(match.getProtocolMatchFields(), input.getProtocolMatchFields())) {
114 if (!Objects.equals(match.getTcpFlagsMatch(), input.getTcpFlagsMatch())) {
117 if (!Objects.equals(match.getTunnel(), input.getTunnel())) {
120 if (!Objects.equals(match.getVlanMatch(), input.getVlanMatch())) {
123 for (ExtensionList inputExtensionList : inputAug.getExtensionList()) {
124 if (!thisAug.getExtensionList().contains(inputExtensionList)) {
130 return getMatch().equals(input);
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();
145 public String toString() {
146 return "FlowRegistryKeyDto{"
147 + "tableId=" + tableId
148 + ", priority=" + priority
149 + ", cookie=" + cookie
155 public short getTableId() {
160 public int getPriority() {
165 public BigInteger getCookie() {
170 public Match getMatch() {