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.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;
23 * Created by Martin Bobak <mbobak@cisco.com> on 8.4.2015.
25 public class FlowHashFactory {
28 public FlowHashFactory() {
31 public static FlowHash create(Flow flow, DeviceContext deviceContext) {
32 long hash = calculateHash(flow, deviceContext);
33 return new FlowHashDto(hash, flow);
36 private static long calculateHash(Flow flow, DeviceContext deviceContext) {
37 return HashUtil.calculateMatchHash(flow.getMatch(), deviceContext);
41 private static final class FlowHashDto implements FlowHash {
43 private final long flowHash;
44 private final int intHashCode;
46 private final short tableId;
47 private final int priority;
48 private final BigInteger cookie;
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();
60 public int hashCode() {
65 public boolean equals(final Object obj) {
69 if (!(obj instanceof FlowHash)) {
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())) {
83 public long getFlowHash() {
88 public short getTableId() {
93 public int getPriority() {
98 public BigInteger getCookie() {