Merge "Make MatchReactor a proper constant"
[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.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
18
19 /**
20  * Created by Martin Bobak <mbobak@cisco.com> on 8.4.2015.
21  */
22 public class FlowRegistryKeyFactory {
23
24
25     public FlowRegistryKeyFactory() {
26     }
27
28     public static FlowRegistryKey create(Flow flow) {
29         return new FlowRegistryKeyDto(flow);
30     }
31
32     private static final class FlowRegistryKeyDto implements FlowRegistryKey {
33
34         private final short tableId;
35         private final int priority;
36         private final BigInteger cookie;
37         private final Match match;
38
39         public FlowRegistryKeyDto(final Flow flow) {
40             tableId = Preconditions.checkNotNull(flow.getTableId(), "flow tableId must not be null");
41             priority = Preconditions.checkNotNull(flow.getPriority(), "flow priority must not be null");
42             match = Preconditions.checkNotNull(flow.getMatch(), "Match value must not be null");
43             cookie = MoreObjects.firstNonNull(flow.getCookie(), OFConstants.DEFAULT_FLOW_COOKIE).getValue();
44         }
45
46         @Override
47         public boolean equals(final Object o) {
48             if (this == o) return true;
49             if (o == null || getClass() != o.getClass()) return false;
50
51             final FlowRegistryKeyDto that = (FlowRegistryKeyDto) o;
52
53             if (priority != that.priority) return false;
54             if (tableId != that.tableId) return false;
55             if (!match.equals(that.match)) return false;
56
57             return true;
58         }
59
60         @Override
61         public int hashCode() {
62             int result = (int) tableId;
63             result = 31 * result + priority;
64             result = 31 * result + match.hashCode();
65             return result;
66         }
67
68         @Override
69         public short getTableId() {
70             return tableId;
71         }
72
73         @Override
74         public int getPriority() {
75             return priority;
76         }
77
78         @Override
79         public BigInteger getCookie() {
80             return cookie;
81         }
82     }
83 }