Shashikanth VH
Committed by Gerrit Code Review

[ONOS-3857] BGP Update message builder.

Change-Id: I02d750f662602fc51b090e6beb89d73d5eb36436
...@@ -45,6 +45,13 @@ public interface BgpFactory { ...@@ -45,6 +45,13 @@ public interface BgpFactory {
45 BgpNotificationMsg.Builder notificationMessageBuilder(); 45 BgpNotificationMsg.Builder notificationMessageBuilder();
46 46
47 /** 47 /**
48 + * Gets the builder object for a update message.
49 + *
50 + * @return builder object for update message
51 + */
52 + BgpUpdateMsg.Builder updateMessageBuilder();
53 +
54 + /**
48 * Gets the BGP message reader. 55 * Gets the BGP message reader.
49 * 56 *
50 * @return BGP message reader 57 * @return BGP message reader
......
...@@ -121,6 +121,24 @@ public interface BgpOpenMsg extends BgpMessage { ...@@ -121,6 +121,24 @@ public interface BgpOpenMsg extends BgpMessage {
121 */ 121 */
122 Builder setLsCapabilityTlv(boolean isLsCapabilitySet); 122 Builder setLsCapabilityTlv(boolean isLsCapabilitySet);
123 123
124 + /**
125 + * Sets flow specification capability and return its builder.
126 + *
127 + * @param isFlowSpecCapabilitySet boolean value to know whether flow specification capability is set or not
128 + *
129 + * @return builder by setting capabilities
130 + */
131 + Builder setFlowSpecCapabilityTlv(boolean isFlowSpecCapabilitySet);
132 +
133 + /**
134 + * Sets VPN flow specification capability and return its builder.
135 + *
136 + * @param isVpnFlowSpecCapabilitySet boolean value to know whether flow spec capability is set or not
137 + *
138 + * @return builder by setting capabilities
139 + */
140 + Builder setVpnFlowSpecCapabilityTlv(boolean isVpnFlowSpecCapabilitySet);
141 +
124 @Override 142 @Override
125 Builder setHeader(BgpHeader bgpMsgHeader); 143 Builder setHeader(BgpHeader bgpMsgHeader);
126 } 144 }
......
...@@ -18,13 +18,47 @@ package org.onosproject.bgpio.protocol; ...@@ -18,13 +18,47 @@ package org.onosproject.bgpio.protocol;
18 18
19 import java.util.List; 19 import java.util.List;
20 20
21 +import org.jboss.netty.buffer.ChannelBuffer;
21 import org.onlab.packet.IpPrefix; 22 import org.onlab.packet.IpPrefix;
22 import org.onosproject.bgpio.protocol.ver4.BgpPathAttributes; 23 import org.onosproject.bgpio.protocol.ver4.BgpPathAttributes;
24 +import org.onosproject.bgpio.protocol.flowspec.BgpFlowSpecDetails;
25 +import org.onosproject.bgpio.types.BgpValueType;
26 +import org.onosproject.bgpio.types.BgpHeader;
23 27
24 /** 28 /**
25 * Abstraction of an entity providing BGP Update Message. 29 * Abstraction of an entity providing BGP Update Message.
26 */ 30 */
27 public interface BgpUpdateMsg extends BgpMessage { 31 public interface BgpUpdateMsg extends BgpMessage {
32 +
33 + @Override
34 + BgpVersion getVersion();
35 +
36 + @Override
37 + BgpType getType();
38 +
39 + @Override
40 + void writeTo(ChannelBuffer channelBuffer);
41 +
42 + @Override
43 + BgpHeader getHeader();
44 +
45 + /**
46 + * Builder interface with set functions to build update message.
47 + */
48 + interface Builder extends BgpMessage.Builder {
49 + @Override
50 + BgpUpdateMsg build();
51 +
52 + Builder setBgpPathAttributes(List<BgpValueType> attributes);
53 +
54 + Builder setNlriIdentifier(short afi, byte safi);
55 +
56 + Builder setBgpFlowSpecComponents(BgpFlowSpecDetails flowSpecComponents);
57 +
58 + @Override
59 + Builder setHeader(BgpHeader bgpMsgHeader);
60 + }
61 +
28 /** 62 /**
29 * Returns path attributes in BGP Update Message. 63 * Returns path attributes in BGP Update Message.
30 * 64 *
......
...@@ -22,6 +22,7 @@ import org.onosproject.bgpio.protocol.BgpMessage; ...@@ -22,6 +22,7 @@ import org.onosproject.bgpio.protocol.BgpMessage;
22 import org.onosproject.bgpio.protocol.BgpMessageReader; 22 import org.onosproject.bgpio.protocol.BgpMessageReader;
23 import org.onosproject.bgpio.protocol.BgpNotificationMsg; 23 import org.onosproject.bgpio.protocol.BgpNotificationMsg;
24 import org.onosproject.bgpio.protocol.BgpOpenMsg; 24 import org.onosproject.bgpio.protocol.BgpOpenMsg;
25 +import org.onosproject.bgpio.protocol.BgpUpdateMsg;
25 import org.onosproject.bgpio.protocol.BgpVersion; 26 import org.onosproject.bgpio.protocol.BgpVersion;
26 27
27 /** 28 /**
...@@ -47,6 +48,11 @@ public class BgpFactoryVer4 implements BgpFactory { ...@@ -47,6 +48,11 @@ public class BgpFactoryVer4 implements BgpFactory {
47 } 48 }
48 49
49 @Override 50 @Override
51 + public BgpUpdateMsg.Builder updateMessageBuilder() {
52 + return new BgpUpdateMsgVer4.Builder();
53 + }
54 +
55 + @Override
50 public BgpMessageReader<BgpMessage> getReader() { 56 public BgpMessageReader<BgpMessage> getReader() {
51 return BgpMessageVer4.READER; 57 return BgpMessageVer4.READER;
52 } 58 }
......
...@@ -31,6 +31,7 @@ import org.onosproject.bgpio.types.BgpValueType; ...@@ -31,6 +31,7 @@ import org.onosproject.bgpio.types.BgpValueType;
31 import org.onosproject.bgpio.types.FourOctetAsNumCapabilityTlv; 31 import org.onosproject.bgpio.types.FourOctetAsNumCapabilityTlv;
32 import org.onosproject.bgpio.types.MultiProtocolExtnCapabilityTlv; 32 import org.onosproject.bgpio.types.MultiProtocolExtnCapabilityTlv;
33 import org.onosproject.bgpio.util.Validation; 33 import org.onosproject.bgpio.util.Validation;
34 +import org.onosproject.bgpio.util.Constants;
34 import org.slf4j.Logger; 35 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory; 36 import org.slf4j.LoggerFactory;
36 37
...@@ -293,6 +294,8 @@ public class BgpOpenMsgVer4 implements BgpOpenMsg { ...@@ -293,6 +294,8 @@ public class BgpOpenMsgVer4 implements BgpOpenMsg {
293 private int bgpId; 294 private int bgpId;
294 private boolean isLargeAsCapabilityTlvSet = false; 295 private boolean isLargeAsCapabilityTlvSet = false;
295 private boolean isLsCapabilityTlvSet = false; 296 private boolean isLsCapabilityTlvSet = false;
297 + private boolean isFlowSpecCapabilityTlvSet = false;
298 + private boolean isVpnFlowSpecCapabilityTlvSet = false;
296 299
297 LinkedList<BgpValueType> capabilityTlv = new LinkedList<>(); 300 LinkedList<BgpValueType> capabilityTlv = new LinkedList<>();
298 301
...@@ -322,6 +325,20 @@ public class BgpOpenMsgVer4 implements BgpOpenMsg { ...@@ -322,6 +325,20 @@ public class BgpOpenMsgVer4 implements BgpOpenMsg {
322 this.capabilityTlv.add(tlv); 325 this.capabilityTlv.add(tlv);
323 } 326 }
324 327
328 + if (this.isFlowSpecCapabilityTlvSet) {
329 + BgpValueType tlv;
330 + tlv = new MultiProtocolExtnCapabilityTlv(Constants.AFI_FLOWSPEC_VALUE,
331 + RES, Constants.SAFI_FLOWSPEC_VALUE);
332 + this.capabilityTlv.add(tlv);
333 + }
334 +
335 + if (this.isVpnFlowSpecCapabilityTlvSet) {
336 + BgpValueType tlv;
337 + tlv = new MultiProtocolExtnCapabilityTlv(Constants.AFI_FLOWSPEC_VALUE,
338 + RES, Constants.VPN_SAFI_FLOWSPEC_VALUE);
339 + this.capabilityTlv.add(tlv);
340 + }
341 +
325 return new BgpOpenMsgVer4(bgpMsgHeader, PACKET_VERSION, this.asNumber, holdTime, this.bgpId, 342 return new BgpOpenMsgVer4(bgpMsgHeader, PACKET_VERSION, this.asNumber, holdTime, this.bgpId,
326 this.capabilityTlv); 343 this.capabilityTlv);
327 } 344 }
...@@ -370,6 +387,18 @@ public class BgpOpenMsgVer4 implements BgpOpenMsg { ...@@ -370,6 +387,18 @@ public class BgpOpenMsgVer4 implements BgpOpenMsg {
370 this.isLsCapabilityTlvSet = isLsCapabilitySet; 387 this.isLsCapabilityTlvSet = isLsCapabilitySet;
371 return this; 388 return this;
372 } 389 }
390 +
391 + @Override
392 + public Builder setFlowSpecCapabilityTlv(boolean isFlowSpecCapabilitySet) {
393 + this.isFlowSpecCapabilityTlvSet = isFlowSpecCapabilitySet;
394 + return this;
395 + }
396 +
397 + @Override
398 + public Builder setVpnFlowSpecCapabilityTlv(boolean isVpnFlowSpecCapabilitySet) {
399 + this.isVpnFlowSpecCapabilityTlvSet = isVpnFlowSpecCapabilitySet;
400 + return this;
401 + }
373 } 402 }
374 403
375 @Override 404 @Override
......
...@@ -22,12 +22,17 @@ import org.jboss.netty.buffer.ChannelBuffer; ...@@ -22,12 +22,17 @@ import org.jboss.netty.buffer.ChannelBuffer;
22 import org.onlab.packet.IpPrefix; 22 import org.onlab.packet.IpPrefix;
23 import org.onosproject.bgpio.exceptions.BgpParseException; 23 import org.onosproject.bgpio.exceptions.BgpParseException;
24 import org.onosproject.bgpio.protocol.BgpMessageReader; 24 import org.onosproject.bgpio.protocol.BgpMessageReader;
25 +import org.onosproject.bgpio.protocol.BgpMessageWriter;
25 import org.onosproject.bgpio.protocol.BgpType; 26 import org.onosproject.bgpio.protocol.BgpType;
26 import org.onosproject.bgpio.protocol.BgpUpdateMsg; 27 import org.onosproject.bgpio.protocol.BgpUpdateMsg;
28 +import org.onosproject.bgpio.protocol.BgpVersion;
29 +import org.onosproject.bgpio.protocol.flowspec.BgpFlowSpecDetails;
27 import org.onosproject.bgpio.types.BgpErrorType; 30 import org.onosproject.bgpio.types.BgpErrorType;
28 import org.onosproject.bgpio.types.BgpHeader; 31 import org.onosproject.bgpio.types.BgpHeader;
32 +import org.onosproject.bgpio.util.Constants;
29 import org.onosproject.bgpio.util.Validation; 33 import org.onosproject.bgpio.util.Validation;
30 -import org.onosproject.bgpio.protocol.BgpVersion; 34 +
35 +import org.onosproject.bgpio.types.BgpValueType;
31 import org.slf4j.Logger; 36 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory; 37 import org.slf4j.LoggerFactory;
33 38
...@@ -72,16 +77,26 @@ public class BgpUpdateMsgVer4 implements BgpUpdateMsg { ...@@ -72,16 +77,26 @@ public class BgpUpdateMsgVer4 implements BgpUpdateMsg {
72 public static final byte PACKET_VERSION = 4; 77 public static final byte PACKET_VERSION = 4;
73 //Withdrawn Routes Length(2) + Total Path Attribute Length(2) 78 //Withdrawn Routes Length(2) + Total Path Attribute Length(2)
74 public static final int PACKET_MINIMUM_LENGTH = 4; 79 public static final int PACKET_MINIMUM_LENGTH = 4;
80 + public static final int MARKER_LENGTH = 16;
75 public static final int BYTE_IN_BITS = 8; 81 public static final int BYTE_IN_BITS = 8;
76 public static final int MIN_LEN_AFTER_WITHDRW_ROUTES = 2; 82 public static final int MIN_LEN_AFTER_WITHDRW_ROUTES = 2;
77 public static final int MINIMUM_COMMON_HEADER_LENGTH = 19; 83 public static final int MINIMUM_COMMON_HEADER_LENGTH = 19;
78 public static final BgpType MSG_TYPE = BgpType.UPDATE; 84 public static final BgpType MSG_TYPE = BgpType.UPDATE;
85 + public static byte[] marker = new byte[] {(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
86 + (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
87 + (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
88 + (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff};
89 + public static final BgpHeader DEFAULT_UPDATE_HEADER = new BgpHeader(marker,
90 + (short) PACKET_MINIMUM_LENGTH, (byte) 0X02);
79 public static final BgpUpdateMsgVer4.Reader READER = new Reader(); 91 public static final BgpUpdateMsgVer4.Reader READER = new Reader();
80 92
81 private List<IpPrefix> withdrawnRoutes; 93 private List<IpPrefix> withdrawnRoutes;
82 private BgpPathAttributes bgpPathAttributes; 94 private BgpPathAttributes bgpPathAttributes;
83 private BgpHeader bgpHeader; 95 private BgpHeader bgpHeader;
84 private List<IpPrefix> nlri; 96 private List<IpPrefix> nlri;
97 + private BgpFlowSpecDetails bgpFlowSpecComponents;
98 + private short afi;
99 + private byte safi;
85 100
86 /** 101 /**
87 * Constructor to initialize parameters for BGP Update message. 102 * Constructor to initialize parameters for BGP Update message.
...@@ -99,6 +114,15 @@ public class BgpUpdateMsgVer4 implements BgpUpdateMsg { ...@@ -99,6 +114,15 @@ public class BgpUpdateMsgVer4 implements BgpUpdateMsg {
99 this.nlri = nlri; 114 this.nlri = nlri;
100 } 115 }
101 116
117 + public BgpUpdateMsgVer4(BgpHeader bgpHeader, short afi, byte safi, BgpPathAttributes bgpPathAttributes,
118 + BgpFlowSpecDetails bgpFlowSpecComponents) {
119 + this.bgpHeader = bgpHeader;
120 + this.bgpFlowSpecComponents = bgpFlowSpecComponents;
121 + this.bgpPathAttributes = bgpPathAttributes;
122 + this.afi = afi;
123 + this.safi = safi;
124 + }
125 +
102 /** 126 /**
103 * Reader reads BGP Update Message from the channel buffer. 127 * Reader reads BGP Update Message from the channel buffer.
104 */ 128 */
...@@ -163,6 +187,82 @@ public class BgpUpdateMsgVer4 implements BgpUpdateMsg { ...@@ -163,6 +187,82 @@ public class BgpUpdateMsgVer4 implements BgpUpdateMsg {
163 } 187 }
164 188
165 /** 189 /**
190 + * Builder class for BGP update message.
191 + */
192 + static class Builder implements BgpUpdateMsg.Builder {
193 + BgpHeader bgpMsgHeader = null;
194 + BgpFlowSpecDetails bgpFlowSpecComponents;
195 + BgpPathAttributes bgpPathAttributes;
196 + private short afi;
197 + private byte safi;
198 +
199 + @Override
200 + public BgpUpdateMsg build() /* throws BgpParseException */ {
201 + BgpHeader bgpMsgHeader = DEFAULT_UPDATE_HEADER;
202 +
203 + return new BgpUpdateMsgVer4(bgpMsgHeader, afi, safi, bgpPathAttributes, bgpFlowSpecComponents);
204 + }
205 +
206 + @Override
207 + public Builder setHeader(BgpHeader bgpMsgHeader) {
208 + this.bgpMsgHeader = bgpMsgHeader;
209 + return this;
210 + }
211 +
212 + @Override
213 + public Builder setBgpFlowSpecComponents(BgpFlowSpecDetails bgpFlowSpecComponents) {
214 + this.bgpFlowSpecComponents = bgpFlowSpecComponents;
215 + return this;
216 + }
217 +
218 + @Override
219 + public Builder setBgpPathAttributes(List<BgpValueType> attributes) {
220 + this.bgpPathAttributes = new BgpPathAttributes(attributes);
221 + return this;
222 + }
223 +
224 + @Override
225 + public Builder setNlriIdentifier(short afi, byte safi) {
226 + this.afi = afi;
227 + this.safi = safi;
228 + return this;
229 + }
230 + }
231 +
232 + public static final Writer WRITER = new Writer();
233 +
234 + /**
235 + * Writer class for writing BGP update message to channel buffer.
236 + */
237 + public static class Writer implements BgpMessageWriter<BgpUpdateMsgVer4> {
238 +
239 + @Override
240 + public void write(ChannelBuffer cb, BgpUpdateMsgVer4 message) throws BgpParseException {
241 +
242 + int startIndex = cb.writerIndex();
243 +
244 + // write common header and get msg length index
245 + int msgLenIndex = message.bgpHeader.write(cb);
246 +
247 + if (msgLenIndex <= 0) {
248 + throw new BgpParseException("Unable to write message header.");
249 + }
250 +
251 + if ((message.afi == Constants.AFI_FLOWSPEC_VALUE) && (message.safi == Constants.SAFI_FLOWSPEC_VALUE)) {
252 + //unfeasible route length
253 + cb.writeShort(0);
254 + }
255 +
256 + // TODO: write path attributes
257 +
258 + // write UPDATE Object Length
259 + int length = cb.writerIndex() - startIndex;
260 + cb.setShort(msgLenIndex, (short) length);
261 + message.bgpHeader.setLength((short) length);
262 + }
263 + }
264 +
265 + /**
166 * Parses NLRI from channel buffer. 266 * Parses NLRI from channel buffer.
167 * 267 *
168 * @param cb channelBuffer 268 * @param cb channelBuffer
...@@ -248,8 +348,12 @@ public class BgpUpdateMsgVer4 implements BgpUpdateMsg { ...@@ -248,8 +348,12 @@ public class BgpUpdateMsgVer4 implements BgpUpdateMsg {
248 } 348 }
249 349
250 @Override 350 @Override
251 - public void writeTo(ChannelBuffer channelBuffer) throws BgpParseException { 351 + public void writeTo(ChannelBuffer channelBuffer) {
252 - //Not to be implemented as of now 352 + try {
353 + WRITER.write(channelBuffer, this);
354 + } catch (BgpParseException e) {
355 + log.debug("[writeTo] Error: " + e.toString());
356 + }
253 } 357 }
254 358
255 @Override 359 @Override
......