2 * Copyright (c) 2014 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
8 package org.opendaylight.openflowjava.nx.codec.match;
10 import com.google.common.primitives.Ints;
16 public class NxmHeader {
18 private final long headerAsLong;
19 private final int oxmClass;
20 private final int nxmField;
21 private final boolean hasMask;
22 private final int length;
24 public NxmHeader(long header) {
25 this.headerAsLong = header;
26 this.oxmClass = Ints.checkedCast(extractSub(header, 16, 16));
27 this.nxmField = Ints.checkedCast(extractSub(header, 7, 9));
28 this.hasMask = extractSub(header, 1, 8) == 1 ? true : false;
29 this.length = Ints.checkedCast(extractSub(header, 8, 0));
32 public NxmHeader(int oxmClass, int nxmField, boolean hasMask, int length) {
33 this.oxmClass = oxmClass;
34 this.nxmField = nxmField;
35 this.hasMask = hasMask;
37 this.headerAsLong = ((((long) oxmClass) << 16) | (nxmField << 9) | ((hasMask ? 1 : 0) << 8) | (length));
40 private static long extractSub(final long l, final int nrBits, final int offset) {
41 final long rightShifted = l >>> offset;
42 final long mask = (1L << nrBits) - 1L;
43 return rightShifted & mask;
46 public long toLong() {
50 public int getOxmClass() {
54 public int getNxmField() {
58 public boolean isHasMask() {
62 public int getLength() {
67 public int hashCode() {
70 result = prime * result + (int) (headerAsLong ^ (headerAsLong >>> 32));
75 public boolean equals(Object obj) {
82 if (getClass() != obj.getClass()) {
85 NxmHeader other = (NxmHeader) obj;
86 if (headerAsLong != other.headerAsLong) {
93 public String toString() {
94 return "NxmHeader [headerAsLong=" + headerAsLong + ", oxmClass=" + oxmClass + ", nxmField=" + nxmField
95 + ", hasMask=" + hasMask + ", length=" + length + "]";