9124a90cb2e430bc76081e4562422243700507f8
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / registry / flow / FlowHashFactory.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 com.google.common.primitives.Longs;
14 import java.math.BigInteger;
15 import java.util.Objects;
16 import org.opendaylight.openflowplugin.api.OFConstants;
17 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowHash;
18 import org.opendaylight.openflowplugin.impl.util.HashUtil;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow;
20
21 /**
22  * Created by Martin Bobak <mbobak@cisco.com> on 8.4.2015.
23  */
24 public class FlowHashFactory {
25
26
27     public FlowHashFactory() {
28     }
29
30     public static FlowHash create(Flow flow, short version) {
31         long hash = calculateHash(flow, version);
32         return new FlowHashDto(hash, flow);
33     }
34
35     private static long calculateHash(Flow flow, short version) {
36         return HashUtil.calculateMatchHash(flow.getMatch(), version);
37     }
38
39
40     private static final class FlowHashDto implements FlowHash {
41
42         private final long flowHash;
43         private final int intHashCode;
44
45         private final short tableId;
46         private final int priority;
47         private final BigInteger cookie;
48
49         public FlowHashDto(final long flowHash, final Flow flow) {
50             this.flowHash = flowHash;
51             this.intHashCode = Longs.hashCode(flowHash);
52             tableId = Preconditions.checkNotNull(flow.getTableId(), "flow tableId must not be null");
53             priority = Preconditions.checkNotNull(flow.getPriority(), "flow priority must not be null");
54             cookie = MoreObjects.firstNonNull(flow.getCookie(), OFConstants.DEFAULT_FLOW_COOKIE).getValue();
55         }
56
57
58         @Override
59         public int hashCode() {
60             return intHashCode;
61         }
62
63         @Override
64         public boolean equals(final Object obj) {
65             if (null == obj) {
66                 return false;
67             }
68             if (!(obj instanceof FlowHash)) {
69                 return false;
70             }
71             FlowHash that = (FlowHash) obj;
72             if (this.flowHash == that.getFlowHash()
73                     && this.tableId == that.getTableId()
74                     && this.priority == that.getPriority()
75                     && Objects.equals(this.cookie, that.getCookie())) {
76                 return true;
77             }
78             return false;
79         }
80
81         @Override
82         public long getFlowHash() {
83             return flowHash;
84         }
85
86         @Override
87         public short getTableId() {
88             return tableId;
89         }
90
91         @Override
92         public int getPriority() {
93             return priority;
94         }
95
96         @Override
97         public BigInteger getCookie() {
98             return cookie;
99         }
100     }
101 }