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 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;
22 * Created by Martin Bobak <mbobak@cisco.com> on 8.4.2015.
24 public class FlowHashFactory {
27 public FlowHashFactory() {
30 public static FlowHash create(Flow flow, short version) {
31 long hash = calculateHash(flow, version);
32 return new FlowHashDto(hash, flow);
35 private static long calculateHash(Flow flow, short version) {
36 return HashUtil.calculateMatchHash(flow.getMatch(), version);
40 private static final class FlowHashDto implements FlowHash {
42 private final long flowHash;
43 private final int intHashCode;
45 private final short tableId;
46 private final int priority;
47 private final BigInteger cookie;
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();
59 public int hashCode() {
64 public boolean equals(final Object obj) {
68 if (!(obj instanceof FlowHash)) {
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())) {
82 public long getFlowHash() {
87 public short getTableId() {
92 public int getPriority() {
97 public BigInteger getCookie() {