Fix comparison between port numbers in match
[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 org.opendaylight.openflowplugin.api.OFConstants;
15 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
16 import org.opendaylight.openflowplugin.impl.util.MatchComparatorFactory;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
20
21 /**
22  * Created by Martin Bobak <mbobak@cisco.com> on 8.4.2015.
23  */
24 public class FlowRegistryKeyFactory {
25
26     private FlowRegistryKeyFactory() {
27         // Hide implicit constructor
28     }
29
30     public static FlowRegistryKey create(final short version, final Flow flow) {
31         return new FlowRegistryKeyDto(version, flow);
32     }
33
34     private static final class FlowRegistryKeyDto implements FlowRegistryKey {
35         private final short tableId;
36         private final int priority;
37         private final BigInteger cookie;
38         private final Match match;
39         private final short version;
40
41         private FlowRegistryKeyDto(final short version, final Flow flow) {
42             //TODO: mandatory flow input values (or default values) should be specified via yang model
43             tableId = Preconditions.checkNotNull(flow.getTableId(), "flow tableId must not be null");
44             priority = MoreObjects.firstNonNull(flow.getPriority(), OFConstants.DEFAULT_FLOW_PRIORITY);
45             match = MoreObjects.firstNonNull(flow.getMatch(), OFConstants.EMPTY_MATCH);
46             cookie = MoreObjects.firstNonNull(flow.getCookie(), OFConstants.DEFAULT_FLOW_COOKIE).getValue();
47             this.version = version;
48         }
49
50         @Override
51         public boolean equals(final Object o) {
52             if (this == o) {
53                 return true;
54             }
55
56             if (o == null || !(o instanceof FlowRegistryKey)) {
57                 return false;
58             }
59
60             final FlowRegistryKey that = (FlowRegistryKey) o;
61
62             return getPriority() == that.getPriority() &&
63                     getTableId() == that.getTableId() &&
64                     getCookie().equals(that.getCookie()) &&
65                     MatchComparatorFactory.createMatch().areObjectsEqual(version, getMatch(), that.getMatch());
66         }
67
68         @Override
69         public int hashCode() {
70             int result = tableId;
71             result = 31 * result + priority;
72             result = 31 * result + cookie.hashCode();
73             result = 31 * result + match.hashCode();
74             return result;
75         }
76
77         @Override
78         public short getTableId() {
79             return tableId;
80         }
81
82         @Override
83         public int getPriority() {
84             return priority;
85         }
86
87         @Override
88         public BigInteger getCookie() {
89             return cookie;
90         }
91
92         @Override
93         public Match getMatch() {
94             return match;
95         }
96     }
97 }