Remove unneded check
[openflowplugin.git] / extension / openflowjava-extension-eric / src / main / java / org / opendaylight / openflowjava / eric / codec / match / EricHeader.java
1 /*
2  * Copyright (c) 2018 Ericsson India Global Services Pvt Ltd. 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.eric.codec.match;
9
10 import com.google.common.primitives.Ints;
11
12 /**
13  * Eric header.
14  */
15 public class EricHeader {
16
17     private final long headerAsLong;
18     private final int oxmClass;
19     private final int ericField;
20     private final boolean hasMask;
21     private final int length;
22
23     public EricHeader(final long header) {
24         this.headerAsLong = header;
25         this.oxmClass = Ints.checkedCast(extractSub(header, 16, 16));
26         this.ericField = Ints.checkedCast(extractSub(header, 7, 9));
27         this.hasMask = extractSub(header, 1, 8) == 1;
28         this.length = Ints.checkedCast(extractSub(header, 8, 0));
29     }
30
31     public EricHeader(final int oxmClass, final int ericField, final boolean hasMask, final int length) {
32         this.oxmClass = oxmClass;
33         this.ericField = ericField;
34         this.hasMask = hasMask;
35         this.length = length;
36         this.headerAsLong = (long) oxmClass << 16 | ericField << 9 | (hasMask ? 1 : 0) << 8 | length;
37     }
38
39     private static long extractSub(final long value, final int nrBits, final int offset) {
40         final long rightShifted = value >>> offset;
41         final long mask = (1L << nrBits) - 1L;
42         return rightShifted & mask;
43     }
44
45     public long toLong() {
46         return headerAsLong;
47     }
48
49     public int getOxmClass() {
50         return oxmClass;
51     }
52
53     public int getEricField() {
54         return ericField;
55     }
56
57     public boolean isHasMask() {
58         return hasMask;
59     }
60
61     public int getLength() {
62         return length;
63     }
64
65     @Override
66     public int hashCode() {
67         final int prime = 31;
68         int result = 1;
69         result = prime * result + (int) (headerAsLong ^ headerAsLong >>> 32);
70         return result;
71     }
72
73     @Override
74     public boolean equals(final Object obj) {
75         if (this == obj) {
76             return true;
77         }
78         if (obj == null) {
79             return false;
80         }
81         if (getClass() != obj.getClass()) {
82             return false;
83         }
84         EricHeader other = (EricHeader) obj;
85         if (headerAsLong != other.headerAsLong) {
86             return false;
87         }
88         return true;
89     }
90
91     @Override
92     public String toString() {
93         return "EricHeader [headerAsLong=" + headerAsLong + ", oxmClass=" + oxmClass + ", ericField=" + ericField
94                  + ", hasMask=" + hasMask + ", length=" + length + "]";
95     }
96
97 }