Committed by
Gerrit Code Review
[Emu] [ONOS-2598] Implementation of BGP Capabilities parsing, decoding and encoding
Change-Id: I5893c490f97ad23a0180b847e8b8af9b00560806
Showing
4 changed files
with
397 additions
and
25 deletions
... | @@ -13,7 +13,6 @@ | ... | @@ -13,7 +13,6 @@ |
13 | * See the License for the specific language governing permissions and | 13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. | 14 | * limitations under the License. |
15 | */ | 15 | */ |
16 | - | ||
17 | package org.onosproject.bgpio.protocol; | 16 | package org.onosproject.bgpio.protocol; |
18 | 17 | ||
19 | import java.util.LinkedList; | 18 | import java.util.LinkedList; |
... | @@ -91,8 +90,7 @@ public interface BGPOpenMsg extends BGPMessage { | ... | @@ -91,8 +90,7 @@ public interface BGPOpenMsg extends BGPMessage { |
91 | /** | 90 | /** |
92 | * Sets hold time in Open Message and return its builder. | 91 | * Sets hold time in Open Message and return its builder. |
93 | * | 92 | * |
94 | - * @param holdtime | 93 | + * @param holdtime hold timer value in open message |
95 | - * hold timer value in open message | ||
96 | * @return builder by setting hold time | 94 | * @return builder by setting hold time |
97 | */ | 95 | */ |
98 | Builder setHoldTime(short holdtime); | 96 | Builder setHoldTime(short holdtime); |
... | @@ -107,8 +105,7 @@ public interface BGPOpenMsg extends BGPMessage { | ... | @@ -107,8 +105,7 @@ public interface BGPOpenMsg extends BGPMessage { |
107 | /** | 105 | /** |
108 | * Sets AS number in Open Message and return its builder. | 106 | * Sets AS number in Open Message and return its builder. |
109 | * | 107 | * |
110 | - * @param asNumber | 108 | + * @param asNumber as number in open message |
111 | - * as number in open message | ||
112 | * @return builder by setting asNumber | 109 | * @return builder by setting asNumber |
113 | */ | 110 | */ |
114 | Builder setAsNumber(short asNumber); | 111 | Builder setAsNumber(short asNumber); |
... | @@ -123,8 +120,7 @@ public interface BGPOpenMsg extends BGPMessage { | ... | @@ -123,8 +120,7 @@ public interface BGPOpenMsg extends BGPMessage { |
123 | /** | 120 | /** |
124 | * Sets BGP Identifier in Open Message and return its builder. | 121 | * Sets BGP Identifier in Open Message and return its builder. |
125 | * | 122 | * |
126 | - * @param bgpId | 123 | + * @param bgpId BGP Identifier in open message |
127 | - * BGP Identifier in open message | ||
128 | * @return builder by setting BGP Identifier | 124 | * @return builder by setting BGP Identifier |
129 | */ | 125 | */ |
130 | Builder setBgpId(int bgpId); | 126 | Builder setBgpId(int bgpId); |
... | @@ -139,12 +135,29 @@ public interface BGPOpenMsg extends BGPMessage { | ... | @@ -139,12 +135,29 @@ public interface BGPOpenMsg extends BGPMessage { |
139 | /** | 135 | /** |
140 | * Sets capabilities in Open Message and return its builder. | 136 | * Sets capabilities in Open Message and return its builder. |
141 | * | 137 | * |
142 | - * @param capabilityTlv | 138 | + * @param capabilityTlv capabilities in open message |
143 | - * capabilities in open message | ||
144 | * @return builder by setting capabilities | 139 | * @return builder by setting capabilities |
145 | */ | 140 | */ |
146 | Builder setCapabilityTlv(LinkedList<BGPValueType> capabilityTlv); | 141 | Builder setCapabilityTlv(LinkedList<BGPValueType> capabilityTlv); |
147 | 142 | ||
143 | + /** | ||
144 | + * Sets isLargeAsCapabilityTlvSet and return its builder. | ||
145 | + * | ||
146 | + * @param isLargeAsCapabilitySet | ||
147 | + * boolean value to know whether large AS capability is set or not | ||
148 | + * @return builder by setting capabilities | ||
149 | + */ | ||
150 | + Builder setLargeAsCapabilityTlv(boolean isLargeAsCapabilitySet); | ||
151 | + | ||
152 | + /** | ||
153 | + * Sets isLsCapabilityTlvSet and return its builder. | ||
154 | + * | ||
155 | + * @param isLsCapabilitySet | ||
156 | + * boolean value to know whether LS capability is set or not | ||
157 | + * @return builder by setting capabilities | ||
158 | + */ | ||
159 | + Builder setLsCapabilityTlv(boolean isLsCapabilitySet); | ||
160 | + | ||
148 | @Override | 161 | @Override |
149 | Builder setHeader(BGPHeader bgpMsgHeader); | 162 | Builder setHeader(BGPHeader bgpMsgHeader); |
150 | } | 163 | } | ... | ... |
... | @@ -13,7 +13,6 @@ | ... | @@ -13,7 +13,6 @@ |
13 | * See the License for the specific language governing permissions and | 13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. | 14 | * limitations under the License. |
15 | */ | 15 | */ |
16 | - | ||
17 | package org.onosproject.bgpio.protocol.ver4; | 16 | package org.onosproject.bgpio.protocol.ver4; |
18 | 17 | ||
19 | import java.util.LinkedList; | 18 | import java.util.LinkedList; |
... | @@ -29,6 +28,8 @@ import org.onosproject.bgpio.protocol.BGPVersion; | ... | @@ -29,6 +28,8 @@ import org.onosproject.bgpio.protocol.BGPVersion; |
29 | import org.onosproject.bgpio.types.BGPErrorType; | 28 | import org.onosproject.bgpio.types.BGPErrorType; |
30 | import org.onosproject.bgpio.types.BGPHeader; | 29 | import org.onosproject.bgpio.types.BGPHeader; |
31 | import org.onosproject.bgpio.types.BGPValueType; | 30 | import org.onosproject.bgpio.types.BGPValueType; |
31 | +import org.onosproject.bgpio.types.FourOctetAsNumCapabilityTlv; | ||
32 | +import org.onosproject.bgpio.types.MultiProtocolExtnCapabilityTlv; | ||
32 | import org.onosproject.bgpio.util.Validation; | 33 | import org.onosproject.bgpio.util.Validation; |
33 | import org.slf4j.Logger; | 34 | import org.slf4j.Logger; |
34 | import org.slf4j.LoggerFactory; | 35 | import org.slf4j.LoggerFactory; |
... | @@ -67,8 +68,13 @@ public class BGPOpenMsgVer4 implements BGPOpenMsg { | ... | @@ -67,8 +68,13 @@ public class BGPOpenMsgVer4 implements BGPOpenMsg { |
67 | public static final int MSG_HEADER_LENGTH = 19; | 68 | public static final int MSG_HEADER_LENGTH = 19; |
68 | public static final int MARKER_LENGTH = 16; | 69 | public static final int MARKER_LENGTH = 16; |
69 | public static final int DEFAULT_HOLD_TIME = 120; | 70 | public static final int DEFAULT_HOLD_TIME = 120; |
71 | + public static final short AS_TRANS = 23456; | ||
70 | public static final int OPT_PARA_TYPE_CAPABILITY = 2; | 72 | public static final int OPT_PARA_TYPE_CAPABILITY = 2; |
71 | public static final BGPType MSG_TYPE = BGPType.OPEN; | 73 | public static final BGPType MSG_TYPE = BGPType.OPEN; |
74 | + public static final short AFI = 16388; | ||
75 | + public static final byte SAFI = 71; | ||
76 | + public static final byte RES = 0; | ||
77 | + public static final int FOUR_OCTET_AS_NUM_CAPA_TYPE = 65; | ||
72 | public static final byte[] MARKER = new byte[]{(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, | 78 | public static final byte[] MARKER = new byte[]{(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, |
73 | (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, | 79 | (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, |
74 | (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff}; | 80 | (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff}; |
... | @@ -79,6 +85,7 @@ public class BGPOpenMsgVer4 implements BGPOpenMsg { | ... | @@ -79,6 +85,7 @@ public class BGPOpenMsgVer4 implements BGPOpenMsg { |
79 | private short asNumber; | 85 | private short asNumber; |
80 | private short holdTime; | 86 | private short holdTime; |
81 | private int bgpId; | 87 | private int bgpId; |
88 | + private boolean isLargeAsCapabilitySet; | ||
82 | private LinkedList<BGPValueType> capabilityTlv; | 89 | private LinkedList<BGPValueType> capabilityTlv; |
83 | 90 | ||
84 | public static final BGPOpenMsgVer4.Reader READER = new Reader(); | 91 | public static final BGPOpenMsgVer4.Reader READER = new Reader(); |
... | @@ -98,18 +105,12 @@ public class BGPOpenMsgVer4 implements BGPOpenMsg { | ... | @@ -98,18 +105,12 @@ public class BGPOpenMsgVer4 implements BGPOpenMsg { |
98 | /** | 105 | /** |
99 | * Constructor to initialize all variables of BGP Open message. | 106 | * Constructor to initialize all variables of BGP Open message. |
100 | * | 107 | * |
101 | - * @param bgpMsgHeader | 108 | + * @param bgpMsgHeader BGP Header in open message |
102 | - * BGP Header in open message | 109 | + * @param version BGP version in open message |
103 | - * @param version | 110 | + * @param holdTime hold time in open message |
104 | - * BGP version in open message | 111 | + * @param asNumber AS number in open message |
105 | - * @param holdTime | 112 | + * @param bgpId BGP identifier in open message |
106 | - * hold time in open message | 113 | + * @param capabilityTlv capabilities in open message |
107 | - * @param asNumber | ||
108 | - * AS number in open message | ||
109 | - * @param bgpId | ||
110 | - * BGP identifier in open message | ||
111 | - * @param capabilityTlv | ||
112 | - * capabilities in open message | ||
113 | */ | 114 | */ |
114 | public BGPOpenMsgVer4(BGPHeader bgpMsgHeader, byte version, short asNumber, short holdTime, | 115 | public BGPOpenMsgVer4(BGPHeader bgpMsgHeader, byte version, short asNumber, short holdTime, |
115 | int bgpId, LinkedList<BGPValueType> capabilityTlv) { | 116 | int bgpId, LinkedList<BGPValueType> capabilityTlv) { |
... | @@ -236,7 +237,44 @@ public class BGPOpenMsgVer4 implements BGPOpenMsg { | ... | @@ -236,7 +237,44 @@ public class BGPOpenMsgVer4 implements BGPOpenMsg { |
236 | 237 | ||
237 | LinkedList<BGPValueType> capabilityTlv = new LinkedList<>(); | 238 | LinkedList<BGPValueType> capabilityTlv = new LinkedList<>(); |
238 | 239 | ||
239 | - // TODO: Capability parsing | 240 | + while (cb.readableBytes() > 0) { |
241 | + BGPValueType tlv; | ||
242 | + short type = cb.readByte(); | ||
243 | + short length = cb.readByte(); | ||
244 | + | ||
245 | + switch (type) { | ||
246 | + case FourOctetAsNumCapabilityTlv.TYPE: | ||
247 | + log.debug("FourOctetAsNumCapabilityTlv"); | ||
248 | + if (FourOctetAsNumCapabilityTlv.LENGTH != length) { | ||
249 | + throw new BGPParseException("Invalid length received for FourOctetAsNumCapabilityTlv."); | ||
250 | + } | ||
251 | + if (length > cb.readableBytes()) { | ||
252 | + throw new BGPParseException("Four octet as num tlv length" | ||
253 | + + " is more than readableBytes."); | ||
254 | + } | ||
255 | + int as4Num = cb.readInt(); | ||
256 | + tlv = new FourOctetAsNumCapabilityTlv(as4Num); | ||
257 | + break; | ||
258 | + case MultiProtocolExtnCapabilityTlv.TYPE: | ||
259 | + log.debug("MultiProtocolExtnCapabilityTlv"); | ||
260 | + if (MultiProtocolExtnCapabilityTlv.LENGTH != length) { | ||
261 | + throw new BGPParseException("Invalid length received for MultiProtocolExtnCapabilityTlv."); | ||
262 | + } | ||
263 | + if (length > cb.readableBytes()) { | ||
264 | + throw new BGPParseException("BGP LS tlv length is more than readableBytes."); | ||
265 | + } | ||
266 | + short afi = cb.readShort(); | ||
267 | + byte res = cb.readByte(); | ||
268 | + byte safi = cb.readByte(); | ||
269 | + tlv = new MultiProtocolExtnCapabilityTlv(afi, res, safi); | ||
270 | + break; | ||
271 | + default: | ||
272 | + log.debug("Warning: Unsupported TLV: " + type); | ||
273 | + cb.skipBytes(length); | ||
274 | + continue; | ||
275 | + } | ||
276 | + capabilityTlv.add(tlv); | ||
277 | + } | ||
240 | return capabilityTlv; | 278 | return capabilityTlv; |
241 | } | 279 | } |
242 | 280 | ||
... | @@ -253,6 +291,8 @@ public class BGPOpenMsgVer4 implements BGPOpenMsg { | ... | @@ -253,6 +291,8 @@ public class BGPOpenMsgVer4 implements BGPOpenMsg { |
253 | private short asNumber; | 291 | private short asNumber; |
254 | private boolean isBgpIdSet = false; | 292 | private boolean isBgpIdSet = false; |
255 | private int bgpId; | 293 | private int bgpId; |
294 | + private boolean isLargeAsCapabilityTlvSet = false; | ||
295 | + private boolean isLsCapabilityTlvSet = false; | ||
256 | 296 | ||
257 | LinkedList<BGPValueType> capabilityTlv = new LinkedList<>(); | 297 | LinkedList<BGPValueType> capabilityTlv = new LinkedList<>(); |
258 | 298 | ||
... | @@ -269,7 +309,18 @@ public class BGPOpenMsgVer4 implements BGPOpenMsg { | ... | @@ -269,7 +309,18 @@ public class BGPOpenMsgVer4 implements BGPOpenMsg { |
269 | throw new BGPParseException("BGPID is not set (mandatory)"); | 309 | throw new BGPParseException("BGPID is not set (mandatory)"); |
270 | } | 310 | } |
271 | 311 | ||
272 | - // TODO: capabilities build | 312 | + if (this.isLargeAsCapabilityTlvSet) { |
313 | + BGPValueType tlv; | ||
314 | + int iValue = this.getAsNumber(); | ||
315 | + tlv = new FourOctetAsNumCapabilityTlv(iValue); | ||
316 | + this.capabilityTlv.add(tlv); | ||
317 | + } | ||
318 | + | ||
319 | + if (this.isLsCapabilityTlvSet) { | ||
320 | + BGPValueType tlv; | ||
321 | + tlv = new MultiProtocolExtnCapabilityTlv(AFI, RES, SAFI); | ||
322 | + this.capabilityTlv.add(tlv); | ||
323 | + } | ||
273 | 324 | ||
274 | return new BGPOpenMsgVer4(bgpMsgHeader, PACKET_VERSION, this.asNumber, holdTime, this.bgpId, | 325 | return new BGPOpenMsgVer4(bgpMsgHeader, PACKET_VERSION, this.asNumber, holdTime, this.bgpId, |
275 | this.capabilityTlv); | 326 | this.capabilityTlv); |
... | @@ -342,6 +393,18 @@ public class BGPOpenMsgVer4 implements BGPOpenMsg { | ... | @@ -342,6 +393,18 @@ public class BGPOpenMsgVer4 implements BGPOpenMsg { |
342 | this.capabilityTlv = capabilityTlv; | 393 | this.capabilityTlv = capabilityTlv; |
343 | return this; | 394 | return this; |
344 | } | 395 | } |
396 | + | ||
397 | + @Override | ||
398 | + public Builder setLargeAsCapabilityTlv(boolean isLargeAsCapabilitySet) { | ||
399 | + this.isLargeAsCapabilityTlvSet = isLargeAsCapabilitySet; | ||
400 | + return this; | ||
401 | + } | ||
402 | + | ||
403 | + @Override | ||
404 | + public Builder setLsCapabilityTlv(boolean isLsCapabilitySet) { | ||
405 | + this.isLsCapabilityTlvSet = isLsCapabilitySet; | ||
406 | + return this; | ||
407 | + } | ||
345 | } | 408 | } |
346 | 409 | ||
347 | @Override | 410 | @Override |
... | @@ -364,6 +427,7 @@ public class BGPOpenMsgVer4 implements BGPOpenMsg { | ... | @@ -364,6 +427,7 @@ public class BGPOpenMsgVer4 implements BGPOpenMsg { |
364 | public void write(ChannelBuffer cb, BGPOpenMsgVer4 message) throws BGPParseException { | 427 | public void write(ChannelBuffer cb, BGPOpenMsgVer4 message) throws BGPParseException { |
365 | 428 | ||
366 | int optParaLen = 0; | 429 | int optParaLen = 0; |
430 | + int as4num = 0; | ||
367 | 431 | ||
368 | int startIndex = cb.writerIndex(); | 432 | int startIndex = cb.writerIndex(); |
369 | 433 | ||
... | @@ -377,8 +441,29 @@ public class BGPOpenMsgVer4 implements BGPOpenMsg { | ... | @@ -377,8 +441,29 @@ public class BGPOpenMsgVer4 implements BGPOpenMsg { |
377 | // write version in 1-octet | 441 | // write version in 1-octet |
378 | cb.writeByte(message.version); | 442 | cb.writeByte(message.version); |
379 | 443 | ||
380 | - // TODO : Write AS number based on capabilities | 444 | + // get as4num if LS Capability is set |
445 | + if (message.isLargeAsCapabilitySet) { | ||
446 | + LinkedList<BGPValueType> capabilityTlv = message | ||
447 | + .getCapabilityTlv(); | ||
448 | + ListIterator<BGPValueType> listIterator = capabilityTlv | ||
449 | + .listIterator(); | ||
450 | + | ||
451 | + while (listIterator.hasNext()) { | ||
452 | + BGPValueType tlv = listIterator.next(); | ||
453 | + if (tlv.getType() == FOUR_OCTET_AS_NUM_CAPA_TYPE) { | ||
454 | + as4num = ((FourOctetAsNumCapabilityTlv) tlv).getInt(); | ||
455 | + break; | ||
456 | + } | ||
457 | + } | ||
458 | + } | ||
459 | + | ||
460 | + if ((message.isLargeAsCapabilitySet) && (as4num > 65535)) { | ||
461 | + // write As number as AS_TRANS | ||
462 | + cb.writeShort(AS_TRANS); | ||
463 | + } else { | ||
464 | + // write AS number in next 2-octet | ||
381 | cb.writeShort(message.asNumber); | 465 | cb.writeShort(message.asNumber); |
466 | + } | ||
382 | 467 | ||
383 | // write HoldTime in next 2-octet | 468 | // write HoldTime in next 2-octet |
384 | cb.writeShort(message.holdTime); | 469 | cb.writeShort(message.holdTime); | ... | ... |
1 | +/* | ||
2 | + * Copyright 2015 Open Networking Laboratory | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | +package org.onosproject.bgpio.types; | ||
17 | + | ||
18 | +import java.util.Objects; | ||
19 | + | ||
20 | +import org.jboss.netty.buffer.ChannelBuffer; | ||
21 | +import org.slf4j.Logger; | ||
22 | +import org.slf4j.LoggerFactory; | ||
23 | + | ||
24 | +import com.google.common.base.MoreObjects; | ||
25 | + | ||
26 | +/** | ||
27 | + * Provides FourOctetAsNumCapabilityTlv Capability Tlv. | ||
28 | + */ | ||
29 | +public class FourOctetAsNumCapabilityTlv implements BGPValueType { | ||
30 | + | ||
31 | + /** | ||
32 | + * support to indicate its support for four-octet AS numbers -CAPABILITY TLV format. | ||
33 | + */ | ||
34 | + protected static final Logger log = LoggerFactory | ||
35 | + .getLogger(FourOctetAsNumCapabilityTlv.class); | ||
36 | + | ||
37 | + public static final byte TYPE = 65; | ||
38 | + public static final byte LENGTH = 4; | ||
39 | + | ||
40 | + private final int rawValue; | ||
41 | + | ||
42 | + /** | ||
43 | + * constructor to initialize rawValue. | ||
44 | + * @param rawValue FourOctetAsNumCapabilityTlv | ||
45 | + */ | ||
46 | + public FourOctetAsNumCapabilityTlv(int rawValue) { | ||
47 | + this.rawValue = rawValue; | ||
48 | + } | ||
49 | + | ||
50 | + /** | ||
51 | + * constructor to initialize raw. | ||
52 | + * @param raw AS number | ||
53 | + * @return object of FourOctetAsNumCapabilityTlv | ||
54 | + */ | ||
55 | + public static FourOctetAsNumCapabilityTlv of(final int raw) { | ||
56 | + return new FourOctetAsNumCapabilityTlv(raw); | ||
57 | + } | ||
58 | + | ||
59 | + /** | ||
60 | + * Returns value of TLV. | ||
61 | + * @return int value of rawValue | ||
62 | + */ | ||
63 | + public int getInt() { | ||
64 | + return rawValue; | ||
65 | + } | ||
66 | + | ||
67 | + @Override | ||
68 | + public short getType() { | ||
69 | + return TYPE; | ||
70 | + } | ||
71 | + | ||
72 | + @Override | ||
73 | + public int hashCode() { | ||
74 | + return Objects.hash(rawValue); | ||
75 | + } | ||
76 | + | ||
77 | + @Override | ||
78 | + public boolean equals(Object obj) { | ||
79 | + if (this == obj) { | ||
80 | + return true; | ||
81 | + } | ||
82 | + if (obj instanceof FourOctetAsNumCapabilityTlv) { | ||
83 | + FourOctetAsNumCapabilityTlv other = (FourOctetAsNumCapabilityTlv) obj; | ||
84 | + return Objects.equals(rawValue, other.rawValue); | ||
85 | + } | ||
86 | + return false; | ||
87 | + } | ||
88 | + | ||
89 | + @Override | ||
90 | + public int write(ChannelBuffer cb) { | ||
91 | + int iLenStartIndex = cb.writerIndex(); | ||
92 | + cb.writeByte(TYPE); | ||
93 | + cb.writeByte(LENGTH); | ||
94 | + cb.writeInt(rawValue); | ||
95 | + return cb.writerIndex() - iLenStartIndex; | ||
96 | + } | ||
97 | + | ||
98 | + /** | ||
99 | + * Reads the channel buffer and returns object of FourOctetAsNumCapabilityTlv. | ||
100 | + * @param cb type of channel buffer | ||
101 | + * @return object of FourOctetAsNumCapabilityTlv | ||
102 | + */ | ||
103 | + public static FourOctetAsNumCapabilityTlv read(ChannelBuffer cb) { | ||
104 | + return FourOctetAsNumCapabilityTlv.of(cb.readInt()); | ||
105 | + } | ||
106 | + | ||
107 | + @Override | ||
108 | + public String toString() { | ||
109 | + return MoreObjects.toStringHelper(getClass()) | ||
110 | + .add("Type", TYPE) | ||
111 | + .add("Length", LENGTH) | ||
112 | + .add("Value", rawValue).toString(); | ||
113 | + } | ||
114 | +} |
1 | +/* | ||
2 | + * Copyright 2015 Open Networking Laboratory | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | +package org.onosproject.bgpio.types; | ||
17 | + | ||
18 | +import java.util.Objects; | ||
19 | +import org.jboss.netty.buffer.ChannelBuffer; | ||
20 | +import org.slf4j.Logger; | ||
21 | +import org.slf4j.LoggerFactory; | ||
22 | + | ||
23 | +import com.google.common.base.MoreObjects; | ||
24 | + | ||
25 | +/** | ||
26 | + * Provides MultiProtocolExtnCapabilityTlv. | ||
27 | + */ | ||
28 | +public class MultiProtocolExtnCapabilityTlv implements BGPValueType { | ||
29 | + | ||
30 | + /* | ||
31 | + 0 7 15 23 31 | ||
32 | + +-------+-------+-------+-------+ | ||
33 | + | AFI | Res | SAFI | | ||
34 | + +-------+-------+-------+-------+ | ||
35 | + | ||
36 | + Multiprotocol Extensions CAPABILITY TLV format | ||
37 | + REFERENCE : RFC 4760 | ||
38 | + */ | ||
39 | + protected static final Logger log = LoggerFactory | ||
40 | + .getLogger(MultiProtocolExtnCapabilityTlv.class); | ||
41 | + | ||
42 | + public static final byte TYPE = 1; | ||
43 | + public static final byte LENGTH = 4; | ||
44 | + | ||
45 | + private final short afi; | ||
46 | + private final byte res; | ||
47 | + private final byte safi; | ||
48 | + | ||
49 | + /** | ||
50 | + * Constructor to initialize variables. | ||
51 | + * @param afi Address Family Identifiers | ||
52 | + * @param res reserved field | ||
53 | + * @param safi Subsequent Address Family Identifier | ||
54 | + */ | ||
55 | + public MultiProtocolExtnCapabilityTlv(short afi, byte res, byte safi) { | ||
56 | + this.afi = afi; | ||
57 | + this.res = res; | ||
58 | + this.safi = safi; | ||
59 | + } | ||
60 | + | ||
61 | + /** | ||
62 | + * Returns object of MultiProtocolExtnCapabilityTlv. | ||
63 | + * @param afi Address Family Identifiers | ||
64 | + * @param res reserved field | ||
65 | + * @param safi Subsequent Address Family Identifier | ||
66 | + * @return object of MultiProtocolExtnCapabilityTlv | ||
67 | + */ | ||
68 | + public static MultiProtocolExtnCapabilityTlv of(short afi, byte res, | ||
69 | + byte safi) { | ||
70 | + return new MultiProtocolExtnCapabilityTlv(afi, res, safi); | ||
71 | + } | ||
72 | + | ||
73 | + /** | ||
74 | + * Returns afi Address Family Identifiers value. | ||
75 | + * @return afi Address Family Identifiers value | ||
76 | + */ | ||
77 | + public short getAFI() { | ||
78 | + return afi; | ||
79 | + } | ||
80 | + | ||
81 | + /** | ||
82 | + * Returns res reserved field value. | ||
83 | + * @return res reserved field value | ||
84 | + */ | ||
85 | + public byte getRes() { | ||
86 | + return res; | ||
87 | + } | ||
88 | + | ||
89 | + /** | ||
90 | + * Returns safi Subsequent Address Family Identifier value. | ||
91 | + * @return safi Subsequent Address Family Identifier value | ||
92 | + */ | ||
93 | + public byte getSAFI() { | ||
94 | + return safi; | ||
95 | + } | ||
96 | + | ||
97 | + @Override | ||
98 | + public short getType() { | ||
99 | + return TYPE; | ||
100 | + } | ||
101 | + | ||
102 | + @Override | ||
103 | + public int hashCode() { | ||
104 | + return Objects.hash(afi, res, safi); | ||
105 | + } | ||
106 | + | ||
107 | + @Override | ||
108 | + public boolean equals(Object obj) { | ||
109 | + if (this == obj) { | ||
110 | + return true; | ||
111 | + } | ||
112 | + if (obj instanceof MultiProtocolExtnCapabilityTlv) { | ||
113 | + MultiProtocolExtnCapabilityTlv other = (MultiProtocolExtnCapabilityTlv) obj; | ||
114 | + return Objects.equals(this.afi, other.afi) | ||
115 | + && Objects.equals(this.res, other.res) | ||
116 | + && Objects.equals(this.safi, other.safi); | ||
117 | + } | ||
118 | + return false; | ||
119 | + } | ||
120 | + | ||
121 | + @Override | ||
122 | + public int write(ChannelBuffer cb) { | ||
123 | + int iLenStartIndex = cb.writerIndex(); | ||
124 | + cb.writeByte(TYPE); | ||
125 | + cb.writeByte(LENGTH); | ||
126 | + | ||
127 | + // write afi | ||
128 | + cb.writeShort(afi); | ||
129 | + | ||
130 | + // write res | ||
131 | + cb.writeByte(res); | ||
132 | + | ||
133 | + // write safi | ||
134 | + cb.writeByte(safi); | ||
135 | + | ||
136 | + return cb.writerIndex() - iLenStartIndex; | ||
137 | + } | ||
138 | + | ||
139 | + /** | ||
140 | + * Reads from channel buffer and returns object of MultiprotocolCapabilityTlv. | ||
141 | + * @param cb of type channel buffer | ||
142 | + * @return object of MultiProtocolExtnCapabilityTlv | ||
143 | + */ | ||
144 | + public static BGPValueType read(ChannelBuffer cb) { | ||
145 | + short afi = cb.readShort(); | ||
146 | + byte res = cb.readByte(); | ||
147 | + byte safi = cb.readByte(); | ||
148 | + return new MultiProtocolExtnCapabilityTlv(afi, res, safi); | ||
149 | + } | ||
150 | + | ||
151 | + @Override | ||
152 | + public String toString() { | ||
153 | + return MoreObjects.toStringHelper(getClass()) | ||
154 | + .add("Type", TYPE) | ||
155 | + .add("Length", LENGTH) | ||
156 | + .add("AFI", afi) | ||
157 | + .add("Reserved", res) | ||
158 | + .add("SAFI", safi).toString(); | ||
159 | + } | ||
160 | +} |
-
Please register or login to post a comment