Sonar issues
[openflowplugin.git] / extension / openflowjava-extension-nicira / src / main / java / org / opendaylight / openflowjava / nx / codec / match / NxmHeader.java
1 /**
2  * Copyright (c) 2014 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 package org.opendaylight.openflowjava.nx.codec.match;
9
10 import com.google.common.primitives.Ints;
11
12 /**
13  * @author msunal
14  *
15  */
16 public class NxmHeader {
17
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;
23
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));
30     }
31
32     public NxmHeader(int oxmClass, int nxmField, boolean hasMask, int length) {
33         this.oxmClass = oxmClass;
34         this.nxmField = nxmField;
35         this.hasMask = hasMask;
36         this.length = length;
37         this.headerAsLong = ((((long) oxmClass) << 16) | (nxmField << 9) | ((hasMask ? 1 : 0) << 8) | (length));
38     }
39
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;
44     }
45
46     public long toLong() {
47         return headerAsLong;
48     }
49
50     public int getOxmClass() {
51         return oxmClass;
52     }
53
54     public int getNxmField() {
55         return nxmField;
56     }
57
58     public boolean isHasMask() {
59         return hasMask;
60     }
61
62     public int getLength() {
63         return length;
64     }
65
66     @Override
67     public int hashCode() {
68         final int prime = 31;
69         int result = 1;
70         result = prime * result + (int) (headerAsLong ^ (headerAsLong >>> 32));
71         return result;
72     }
73
74     @Override
75     public boolean equals(Object obj) {
76         if (this == obj) {
77             return true;
78         }
79         if (obj == null) {
80             return false;
81         }
82         if (getClass() != obj.getClass()) {
83             return false;
84         }
85         NxmHeader other = (NxmHeader) obj;
86         if (headerAsLong != other.headerAsLong) {
87             return false;
88         }
89         return true;
90     }
91
92     @Override
93     public String toString() {
94         return "NxmHeader [headerAsLong=" + headerAsLong + ", oxmClass=" + oxmClass + ", nxmField=" + nxmField
95                 + ", hasMask=" + hasMask + ", length=" + length + "]";
96     }
97
98 }