7082483ecbbdf4a912f62d534c900f8766efc14e
[onosfw.git] /
1 /*\r
2  * Copyright 2015 Open Networking Laboratory\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 package org.onosproject.bgpio.types;\r
17 \r
18 import java.util.Objects;\r
19 import org.jboss.netty.buffer.ChannelBuffer;\r
20 import org.slf4j.Logger;\r
21 import org.slf4j.LoggerFactory;\r
22 \r
23 import com.google.common.base.MoreObjects;\r
24 \r
25 /**\r
26  * Provides MultiProtocolExtnCapabilityTlv.\r
27  */\r
28 public class MultiProtocolExtnCapabilityTlv implements BgpValueType {\r
29 \r
30     /*\r
31         0       7       15      23      31\r
32         +-------+-------+-------+-------+\r
33         |  AFI          | Res   |  SAFI |\r
34         +-------+-------+-------+-------+\r
35 \r
36         Multiprotocol Extensions CAPABILITY TLV format\r
37         REFERENCE : RFC 4760\r
38      */\r
39     protected static final Logger log = LoggerFactory\r
40             .getLogger(MultiProtocolExtnCapabilityTlv.class);\r
41 \r
42     public static final byte TYPE = 1;\r
43     public static final byte LENGTH = 4;\r
44 \r
45     private final short afi;\r
46     private final byte res;\r
47     private final byte safi;\r
48 \r
49     /**\r
50      * Constructor to initialize variables.\r
51      * @param afi Address Family Identifiers\r
52      * @param res reserved field\r
53      * @param safi Subsequent Address Family Identifier\r
54      */\r
55     public MultiProtocolExtnCapabilityTlv(short afi, byte res, byte safi) {\r
56         this.afi = afi;\r
57         this.res = res;\r
58         this.safi = safi;\r
59     }\r
60 \r
61     /**\r
62      * Returns object of MultiProtocolExtnCapabilityTlv.\r
63      * @param afi Address Family Identifiers\r
64      * @param res reserved field\r
65      * @param safi Subsequent Address Family Identifier\r
66      * @return object of MultiProtocolExtnCapabilityTlv\r
67      */\r
68     public static MultiProtocolExtnCapabilityTlv of(short afi, byte res,\r
69                                                     byte safi) {\r
70         return new MultiProtocolExtnCapabilityTlv(afi, res, safi);\r
71     }\r
72 \r
73     /**\r
74      * Returns afi Address Family Identifiers value.\r
75      * @return afi Address Family Identifiers value\r
76      */\r
77     public short getAFI() {\r
78         return afi;\r
79     }\r
80 \r
81     /**\r
82      * Returns res reserved field value.\r
83      * @return res reserved field value\r
84      */\r
85     public byte getRes() {\r
86         return res;\r
87     }\r
88 \r
89     /**\r
90      * Returns safi Subsequent Address Family Identifier value.\r
91      * @return safi Subsequent Address Family Identifier value\r
92      */\r
93     public byte getSAFI() {\r
94         return safi;\r
95     }\r
96 \r
97     @Override\r
98     public short getType() {\r
99         return TYPE;\r
100     }\r
101 \r
102     @Override\r
103     public int hashCode() {\r
104         return Objects.hash(afi, res, safi);\r
105     }\r
106 \r
107     @Override\r
108     public boolean equals(Object obj) {\r
109         if (this == obj) {\r
110             return true;\r
111         }\r
112         if (obj instanceof MultiProtocolExtnCapabilityTlv) {\r
113             MultiProtocolExtnCapabilityTlv other = (MultiProtocolExtnCapabilityTlv) obj;\r
114             return Objects.equals(this.afi, other.afi)\r
115                     && Objects.equals(this.res, other.res)\r
116                     && Objects.equals(this.safi, other.safi);\r
117         }\r
118         return false;\r
119     }\r
120 \r
121     @Override\r
122     public int write(ChannelBuffer cb) {\r
123         int iLenStartIndex = cb.writerIndex();\r
124         cb.writeByte(TYPE);\r
125         cb.writeByte(LENGTH);\r
126 \r
127         // write afi\r
128         cb.writeShort(afi);\r
129 \r
130         // write res\r
131         cb.writeByte(res);\r
132 \r
133         // write safi\r
134         cb.writeByte(safi);\r
135 \r
136         return cb.writerIndex() - iLenStartIndex;\r
137     }\r
138 \r
139     /**\r
140      * Reads from channel buffer and returns object of MultiprotocolCapabilityTlv.\r
141      * @param cb of type channel buffer\r
142      * @return object of MultiProtocolExtnCapabilityTlv\r
143      */\r
144     public static BgpValueType read(ChannelBuffer cb) {\r
145         short afi = cb.readShort();\r
146         byte res = cb.readByte();\r
147         byte safi = cb.readByte();\r
148         return new MultiProtocolExtnCapabilityTlv(afi, res, safi);\r
149     }\r
150 \r
151     @Override\r
152     public String toString() {\r
153         return MoreObjects.toStringHelper(getClass())\r
154                 .add("Type", TYPE)\r
155                 .add("Length", LENGTH)\r
156                 .add("AFI", afi)\r
157                 .add("Reserved", res)\r
158                 .add("SAFI", safi).toString();\r
159     }\r
160 \r
161     @Override\r
162     public int compareTo(Object o) {\r
163         // TODO Auto-generated method stub\r
164         return 0;\r
165     }\r
166 }\r