c5b531590217fddb54f9d7306173611266ccbd31
[controller.git] / opendaylight / adsal / sal / api / src / main / java / org / opendaylight / controller / sal / match / extensible / NwProtocol.java
1 package org.opendaylight.controller.sal.match.extensible;
2
3 import javax.xml.bind.annotation.XmlAccessType;
4 import javax.xml.bind.annotation.XmlAccessorType;
5 import javax.xml.bind.annotation.XmlElement;
6 import javax.xml.bind.annotation.XmlRootElement;
7
8 import org.opendaylight.controller.sal.utils.NetUtils;
9
10 @XmlRootElement
11 @XmlAccessorType(XmlAccessType.NONE)
12 public class NwProtocol extends MatchField<Byte> {
13     private static final long serialVersionUID = 1L;
14     public static final String TYPE = "NW_PROTO";
15     private static final short MAX = 255;
16     private byte protocol;
17
18     /**
19      * Creates a Match field for the network protocol
20      *
21      * @param protocol
22      *            the protocol number
23      */
24     public NwProtocol(byte protocol) {
25         super(TYPE);
26         this.protocol = protocol;
27     }
28
29     public NwProtocol(int protocol) {
30         super(TYPE);
31         this.protocol = (byte) protocol;
32     }
33
34     public NwProtocol(short protocol) {
35         super(TYPE);
36         this.protocol = (byte) protocol;
37     }
38
39     // To satisfy JAXB
40     private NwProtocol() {
41         super(TYPE);
42     }
43
44     @Override
45     public Byte getValue() {
46         return protocol;
47     }
48
49     @Override
50     @XmlElement(name = "value")
51     protected String getValueString() {
52         return String.format("0X%s", Integer.toHexString(NetUtils.getUnsignedByte(protocol)));
53     }
54
55     @Override
56     public Byte getMask() {
57         return null;
58     }
59
60     @Override
61     protected String getMaskString() {
62         return null;
63     }
64
65     @Override
66     public boolean isValid() {
67         int intProtocol = NetUtils.getUnsignedByte(protocol);
68         return intProtocol >= 0 && intProtocol <= MAX;
69     }
70
71     @Override
72     public boolean hasReverse() {
73         return false;
74     }
75
76     @Override
77     public NwProtocol getReverse() {
78         return this.clone();
79     }
80
81     @Override
82     public NwProtocol clone() {
83         return new NwProtocol(protocol);
84     }
85
86     @Override
87     public boolean isV6() {
88         return true;
89     }
90
91     @Override
92     public int hashCode() {
93         final int prime = 31;
94         int result = 1;
95         result = prime * result + protocol;
96         return result;
97     }
98
99     @Override
100     public boolean equals(Object obj) {
101         if (this == obj) {
102             return true;
103         }
104         if (obj == null) {
105             return false;
106         }
107         if (!(obj instanceof NwProtocol)) {
108             return false;
109         }
110         NwProtocol other = (NwProtocol) obj;
111         if (protocol != other.protocol) {
112             return false;
113         }
114         return true;
115     }
116 }