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