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.util.Objects;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.openflowplugin.api.OFConstants;
16 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
17 import org.opendaylight.openflowplugin.impl.util.MatchNormalizationUtil;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlow;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionList;
22 import org.opendaylight.yangtools.yang.common.Uint16;
23 import org.opendaylight.yangtools.yang.common.Uint64;
24 import org.opendaylight.yangtools.yang.common.Uint8;
26 public final class FlowRegistryKeyFactory {
28 private FlowRegistryKeyFactory() {
29 // Hide implicit constructor
33 public static FlowRegistryKey create(final short version, @NonNull final Flow flow) {
34 //TODO: mandatory flow input values (or default values) should be specified via yang model
35 final Uint8 tableId = Preconditions.checkNotNull(flow.getTableId(), "flow tableId must not be null");
36 final Uint16 priority = MoreObjects.firstNonNull(flow.getPriority(), OFConstants.DEFAULT_FLOW_PRIORITY);
38 MoreObjects.firstNonNull(flow.getCookie(), OFConstants.DEFAULT_FLOW_COOKIE).getValue();
39 Match match = MatchNormalizationUtil
40 .normalizeMatch(MoreObjects.firstNonNull(flow.getMatch(), OFConstants.EMPTY_MATCH), version);
41 return new FlowRegistryKeyDto(tableId.toJava(), priority.toJava(), cookie, match);
44 private static final class FlowRegistryKeyDto implements FlowRegistryKey {
45 private final short tableId;
46 private final int priority;
47 private final Uint64 cookie;
48 private final Match match;
50 private FlowRegistryKeyDto(final short tableId,
52 @NonNull final Uint64 cookie,
53 @NonNull final Match match) {
54 this.tableId = tableId;
55 this.priority = priority;
61 public boolean equals(final Object object) {
66 if (object == null || !(object instanceof FlowRegistryKey)) {
70 final FlowRegistryKey that = (FlowRegistryKey) object;
72 return getPriority() == that.getPriority()
73 && getTableId() == that.getTableId()
74 && getCookie().equals(that.getCookie())
75 && equalMatch(that.getMatch());
78 private boolean equalMatch(final Match input) {
79 GeneralAugMatchNodesNodeTableFlow thisAug = match.augmentation(GeneralAugMatchNodesNodeTableFlow.class);
80 GeneralAugMatchNodesNodeTableFlow inputAug = input.augmentation(GeneralAugMatchNodesNodeTableFlow.class);
81 if (thisAug != inputAug) {
82 if (thisAug != null) {
83 if (inputAug == null) {
86 if (!Objects.equals(match.getEthernetMatch(), input.getEthernetMatch())) {
89 if (!Objects.equals(match.getIcmpv4Match(), input.getIcmpv4Match())) {
92 if (!Objects.equals(match.getIcmpv6Match(), input.getIcmpv6Match())) {
95 if (!Objects.equals(match.getInPhyPort(), input.getInPhyPort())) {
98 if (!Objects.equals(match.getInPort(), input.getInPort())) {
101 if (!Objects.equals(match.getIpMatch(), input.getIpMatch())) {
104 if (!Objects.equals(match.getLayer3Match(), input.getLayer3Match())) {
107 if (!Objects.equals(match.getLayer4Match(), input.getLayer4Match())) {
110 if (!Objects.equals(match.getMetadata(), input.getMetadata())) {
113 if (!Objects.equals(match.getProtocolMatchFields(), input.getProtocolMatchFields())) {
116 if (!Objects.equals(match.getTcpFlagsMatch(), input.getTcpFlagsMatch())) {
119 if (!Objects.equals(match.getTunnel(), input.getTunnel())) {
122 if (!Objects.equals(match.getVlanMatch(), input.getVlanMatch())) {
125 for (ExtensionList inputExtensionList : inputAug.getExtensionList()) {
126 if (!thisAug.getExtensionList().contains(inputExtensionList)) {
132 return getMatch().equals(input);
138 public int hashCode() {
139 int result = tableId;
140 result = 31 * result + priority;
141 result = 31 * result + cookie.hashCode();
142 result = 31 * result + match.hashCode();
147 public String toString() {
148 return "FlowRegistryKeyDto{"
149 + "tableId=" + tableId
150 + ", priority=" + priority
151 + ", cookie=" + cookie
157 public short getTableId() {
162 public int getPriority() {
167 public Uint64 getCookie() {
172 public Match getMatch() {