aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4
diff options
context:
space:
mode:
Diffstat (limited to 'framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4')
-rwxr-xr-xframework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpFactoryVer4.java58
-rw-r--r--framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpKeepaliveMsgVer4.java157
-rwxr-xr-xframework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpMessageVer4.java111
-rw-r--r--framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpNotificationMsgVer4.java265
-rw-r--r--framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpOpenMsgVer4.java518
-rw-r--r--framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpPathAttributes.java200
-rw-r--r--framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpUpdateMsgVer4.java285
-rwxr-xr-xframework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/package-info.java20
8 files changed, 1614 insertions, 0 deletions
diff --git a/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpFactoryVer4.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpFactoryVer4.java
new file mode 100755
index 00000000..c57832b6
--- /dev/null
+++ b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpFactoryVer4.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.bgpio.protocol.ver4;
+
+import org.onosproject.bgpio.protocol.BgpFactory;
+import org.onosproject.bgpio.protocol.BgpKeepaliveMsg;
+import org.onosproject.bgpio.protocol.BgpMessage;
+import org.onosproject.bgpio.protocol.BgpMessageReader;
+import org.onosproject.bgpio.protocol.BgpNotificationMsg;
+import org.onosproject.bgpio.protocol.BgpOpenMsg;
+import org.onosproject.bgpio.protocol.BgpVersion;
+
+/**
+ * Provides BGP Factory and returns builder classes for all objects and messages.
+ */
+public class BgpFactoryVer4 implements BgpFactory {
+
+ public static final BgpFactoryVer4 INSTANCE = new BgpFactoryVer4();
+
+ @Override
+ public BgpOpenMsg.Builder openMessageBuilder() {
+ return new BgpOpenMsgVer4.Builder();
+ }
+
+ @Override
+ public BgpKeepaliveMsg.Builder keepaliveMessageBuilder() {
+ return new BgpKeepaliveMsgVer4.Builder();
+ }
+
+ @Override
+ public BgpNotificationMsg.Builder notificationMessageBuilder() {
+ return new BgpNotificationMsgVer4.Builder();
+ }
+
+ @Override
+ public BgpMessageReader<BgpMessage> getReader() {
+ return BgpMessageVer4.READER;
+ }
+
+ @Override
+ public BgpVersion getVersion() {
+ return BgpVersion.BGP_4;
+ }
+} \ No newline at end of file
diff --git a/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpKeepaliveMsgVer4.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpKeepaliveMsgVer4.java
new file mode 100644
index 00000000..2c141586
--- /dev/null
+++ b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpKeepaliveMsgVer4.java
@@ -0,0 +1,157 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.bgpio.protocol.ver4;
+
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.onosproject.bgpio.exceptions.BgpParseException;
+import org.onosproject.bgpio.protocol.BgpKeepaliveMsg;
+import org.onosproject.bgpio.protocol.BgpMessageReader;
+import org.onosproject.bgpio.protocol.BgpMessageWriter;
+import org.onosproject.bgpio.types.BgpHeader;
+import org.onosproject.bgpio.protocol.BgpType;
+import org.onosproject.bgpio.protocol.BgpVersion;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.MoreObjects;
+
+/**
+ * Provides BGP keep alive message.
+ */
+public class BgpKeepaliveMsgVer4 implements BgpKeepaliveMsg {
+
+ /*
+ <Keepalive Message>::= <Common Header>
+ A KEEPALIVE message consists of only the message header and has a
+ length of 19 octets.
+
+ 0 1 2 3
+ 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ | |
+ + +
+ | |
+ + +
+ | Marker |
+ + +
+ | |
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ | Length | Type |
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+
+ REFERENCE : RFC 4271
+ */
+
+ protected static final Logger log = LoggerFactory
+ .getLogger(BgpKeepaliveMsgVer4.class);
+
+ private BgpHeader bgpMsgHeader;
+ public static final byte PACKET_VERSION = 4;
+ public static final int PACKET_MINIMUM_LENGTH = 19;
+ public static final int MARKER_LENGTH = 16;
+ public static final BgpType MSG_TYPE = BgpType.KEEP_ALIVE;
+ public static byte[] marker = new byte[] {(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff};
+
+ public static final BgpKeepaliveMsgVer4.Reader READER = new Reader();
+
+ /**
+ * Reader class for reading BGP keepalive message from channel buffer.
+ */
+ static class Reader implements BgpMessageReader<BgpKeepaliveMsg> {
+
+ @Override
+ public BgpKeepaliveMsg readFrom(ChannelBuffer cb, BgpHeader bgpHeader)
+ throws BgpParseException {
+
+ /* bgpHeader is not required in case of keepalive message and
+ Header is already read and no other fields except header in keepalive message.*/
+ return new BgpKeepaliveMsgVer4();
+ }
+ }
+
+ /**
+ * Default constructor.
+ */
+ public BgpKeepaliveMsgVer4() {
+ }
+
+ /**
+ * Builder class for BGP keepalive message.
+ */
+ static class Builder implements BgpKeepaliveMsg.Builder {
+ BgpHeader bgpMsgHeader;
+
+ @Override
+ public Builder setHeader(BgpHeader bgpMsgHeader) {
+ this.bgpMsgHeader = bgpMsgHeader;
+ return this;
+ }
+
+ @Override
+ public BgpKeepaliveMsg build() {
+ return new BgpKeepaliveMsgVer4();
+ }
+ }
+
+ @Override
+ public void writeTo(ChannelBuffer cb) {
+ WRITER.write(cb, this);
+ }
+
+ static final Writer WRITER = new Writer();
+
+ /**
+ * Writer class for writing the BGP keepalive message to channel buffer.
+ */
+ static class Writer implements BgpMessageWriter<BgpKeepaliveMsgVer4> {
+
+ @Override
+ public void write(ChannelBuffer cb, BgpKeepaliveMsgVer4 message) {
+
+ // write marker
+ cb.writeBytes(marker, 0, MARKER_LENGTH);
+
+ // write length of header
+ cb.writeShort(PACKET_MINIMUM_LENGTH);
+
+ // write the type of message
+ cb.writeByte(MSG_TYPE.getType());
+ }
+ }
+
+ @Override
+ public BgpVersion getVersion() {
+ return BgpVersion.BGP_4;
+ }
+
+ @Override
+ public BgpType getType() {
+ return MSG_TYPE;
+ }
+
+ @Override
+ public BgpHeader getHeader() {
+ return this.bgpMsgHeader;
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(getClass()).toString();
+ }
+}
diff --git a/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpMessageVer4.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpMessageVer4.java
new file mode 100755
index 00000000..1c05dae4
--- /dev/null
+++ b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpMessageVer4.java
@@ -0,0 +1,111 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.bgpio.protocol.ver4;
+
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.onosproject.bgpio.exceptions.BgpParseException;
+import org.onosproject.bgpio.protocol.BgpFactories;
+import org.onosproject.bgpio.protocol.BgpMessage;
+import org.onosproject.bgpio.protocol.BgpMessageReader;
+import org.onosproject.bgpio.types.BgpErrorType;
+import org.onosproject.bgpio.types.BgpHeader;
+import org.onosproject.bgpio.util.Validation;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Provides BGP messages.
+ */
+public abstract class BgpMessageVer4 {
+
+ protected static final Logger log = LoggerFactory.getLogger(BgpFactories.class);
+
+ static final byte OPEN_MSG_TYPE = 0x1;
+ static final byte KEEPALIVE_MSG_TYPE = 0x4;
+ static final byte UPDATE_MSG_TYPE = 0x2;
+ static final byte NOTIFICATION_MSG_TYPE = 0x3;
+ static final int MINIMUM_COMMON_HEADER_LENGTH = 19;
+ static final int HEADER_AND_MSG_LEN = 18;
+ static final int MAXIMUM_PACKET_LENGTH = 4096;
+
+ public static final BgpMessageVer4.Reader READER = new Reader();
+
+ /**
+ * Reader class for reading BGP messages from channel buffer.
+ *
+ */
+ static class Reader implements BgpMessageReader<BgpMessage> {
+ @Override
+ public BgpMessage readFrom(ChannelBuffer cb, BgpHeader bgpHeader)
+ throws BgpParseException {
+
+ if (cb.readableBytes() < MINIMUM_COMMON_HEADER_LENGTH) {
+ log.error("Packet should have minimum length.");
+ Validation.validateLen(BgpErrorType.MESSAGE_HEADER_ERROR, BgpErrorType.BAD_MESSAGE_LENGTH,
+ cb.readableBytes());
+ }
+ if (cb.readableBytes() > MAXIMUM_PACKET_LENGTH) {
+ log.error("Packet length should not exceed {}.", MAXIMUM_PACKET_LENGTH);
+ Validation.validateLen(BgpErrorType.MESSAGE_HEADER_ERROR, BgpErrorType.BAD_MESSAGE_LENGTH,
+ cb.readableBytes());
+ }
+ try {
+ // fixed value property version == 4
+ byte[] marker = new byte[BgpHeader.MARKER_LENGTH];
+ cb.readBytes(marker, 0, BgpHeader.MARKER_LENGTH);
+ bgpHeader.setMarker(marker);
+ for (int i = 0; i < BgpHeader.MARKER_LENGTH; i++) {
+ if (marker[i] != (byte) 0xff) {
+ throw new BgpParseException(BgpErrorType.MESSAGE_HEADER_ERROR,
+ BgpErrorType.CONNECTION_NOT_SYNCHRONIZED, null);
+ }
+ }
+ short length = cb.readShort();
+ if (length > cb.readableBytes() + HEADER_AND_MSG_LEN) {
+ Validation.validateLen(BgpErrorType.MESSAGE_HEADER_ERROR,
+ BgpErrorType.BAD_MESSAGE_LENGTH, length);
+ }
+ bgpHeader.setLength(length);
+ byte type = cb.readByte();
+ bgpHeader.setType(type);
+ log.debug("Reading update message of type " + type);
+
+ int len = length - MINIMUM_COMMON_HEADER_LENGTH;
+ switch (type) {
+ case OPEN_MSG_TYPE:
+ log.debug("OPEN MESSAGE is received");
+ return BgpOpenMsgVer4.READER.readFrom(cb.readBytes(len), bgpHeader);
+ case KEEPALIVE_MSG_TYPE:
+ log.debug("KEEPALIVE MESSAGE is received");
+ return BgpKeepaliveMsgVer4.READER.readFrom(cb.readBytes(len), bgpHeader);
+ case UPDATE_MSG_TYPE:
+ log.debug("UPDATE MESSAGE is received");
+ return BgpUpdateMsgVer4.READER.readFrom(cb.readBytes(len), bgpHeader);
+ case NOTIFICATION_MSG_TYPE:
+ log.debug("NOTIFICATION MESSAGE is received");
+ return BgpNotificationMsgVer4.READER.readFrom(cb.readBytes(len), bgpHeader);
+ default:
+ Validation.validateType(BgpErrorType.MESSAGE_HEADER_ERROR, BgpErrorType.BAD_MESSAGE_TYPE, type);
+ return null;
+ }
+ } catch (IndexOutOfBoundsException e) {
+ throw new BgpParseException(BgpErrorType.MESSAGE_HEADER_ERROR,
+ BgpErrorType.BAD_MESSAGE_LENGTH, null);
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpNotificationMsgVer4.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpNotificationMsgVer4.java
new file mode 100644
index 00000000..7243e21a
--- /dev/null
+++ b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpNotificationMsgVer4.java
@@ -0,0 +1,265 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.bgpio.protocol.ver4;
+
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.onosproject.bgpio.exceptions.BgpParseException;
+import org.onosproject.bgpio.protocol.BgpMessageReader;
+import org.onosproject.bgpio.protocol.BgpMessageWriter;
+import org.onosproject.bgpio.protocol.BgpNotificationMsg;
+import org.onosproject.bgpio.protocol.BgpType;
+import org.onosproject.bgpio.protocol.BgpVersion;
+import org.onosproject.bgpio.types.BgpErrorType;
+import org.onosproject.bgpio.types.BgpHeader;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.MoreObjects;
+
+/**
+ * A NOTIFICATION message is sent when an error condition is detected. The BGP connection is closed immediately after it
+ * is sent.
+ */
+class BgpNotificationMsgVer4 implements BgpNotificationMsg {
+
+ /*
+ 0 1 2 3
+ 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ | Error code | Error subcode | Data (variable) |
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ REFERENCE : RFC 4271
+ */
+
+ private static final Logger log = LoggerFactory.getLogger(BgpNotificationMsgVer4.class);
+
+ static final byte PACKET_VERSION = 4;
+ //BGPHeader(19) + Error code(1) + Error subcode(1)
+ static final int TOTAL_MESSAGE_MIN_LENGTH = 21;
+ static final int PACKET_MINIMUM_LENGTH = 2;
+ static final BgpType MSG_TYPE = BgpType.NOTIFICATION;
+ static final byte DEFAULT_ERRORSUBCODE = 0;
+ static final byte[] MARKER = {(byte) 0xff, (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff, (byte) 0xff };
+ static final byte MESSAGE_TYPE = 3;
+ static final BgpHeader DEFAULT_MESSAGE_HEADER = new BgpHeader(MARKER, BgpHeader.DEFAULT_HEADER_LENGTH,
+ MESSAGE_TYPE);
+
+ private byte errorCode;
+ private byte errorSubCode;
+ private byte[] data;
+ private BgpHeader bgpHeader;
+ public static final BgpNotificationMsgVer4.Reader READER = new Reader();
+
+ /**
+ * Initialize fields.
+ */
+ public BgpNotificationMsgVer4() {
+ this.bgpHeader = null;
+ this.data = null;
+ this.errorCode = 0;
+ this.errorSubCode = 0;
+ }
+
+ /**
+ * Constructor to initialize parameters.
+ *
+ * @param bgpHeader BGP Header in notification message
+ * @param errorCode error code
+ * @param errorSubCode error subcode
+ * @param data field
+ */
+ public BgpNotificationMsgVer4(BgpHeader bgpHeader, byte errorCode, byte errorSubCode, byte[] data) {
+ this.bgpHeader = bgpHeader;
+ this.data = data;
+ this.errorCode = errorCode;
+ this.errorSubCode = errorSubCode;
+ }
+
+ /**
+ * Reader reads BGP Notification Message from the channel buffer.
+ */
+ static class Reader implements BgpMessageReader<BgpNotificationMsg> {
+ @Override
+ public BgpNotificationMsg readFrom(ChannelBuffer cb, BgpHeader bgpHeader) throws BgpParseException {
+ byte errorCode;
+ byte errorSubCode;
+ if (cb.readableBytes() < PACKET_MINIMUM_LENGTH) {
+ throw new BgpParseException("Not enough readable bytes");
+ }
+ errorCode = cb.readByte();
+ errorSubCode = cb.readByte();
+ //Message Length = 21 + Data Length
+ int dataLength = bgpHeader.getLength() - TOTAL_MESSAGE_MIN_LENGTH;
+ byte[] data = new byte[dataLength];
+ cb.readBytes(data, 0, dataLength);
+ return new BgpNotificationMsgVer4(bgpHeader, errorCode, errorSubCode, data);
+ }
+ }
+
+ /**
+ * Builder class for BGP notification message.
+ */
+ static class Builder implements BgpNotificationMsg.Builder {
+ private byte errorCode;
+ private byte errorSubCode;
+ private byte[] data;
+ private BgpHeader bgpHeader;
+ private boolean isErrorCodeSet = false;
+ private boolean isErrorSubCodeSet = false;
+ private boolean isBGPHeaderSet = false;
+
+ @Override
+ public BgpNotificationMsg build() throws BgpParseException {
+ BgpHeader bgpHeader = this.isBGPHeaderSet ? this.bgpHeader : DEFAULT_MESSAGE_HEADER;
+ if (!this.isErrorCodeSet) {
+ throw new BgpParseException("Error code must be present");
+ }
+
+ byte errorSubCode = this.isErrorSubCodeSet ? this.errorSubCode : DEFAULT_ERRORSUBCODE;
+ return new BgpNotificationMsgVer4(bgpHeader, this.errorCode, errorSubCode, this.data);
+ }
+
+ @Override
+ public Builder setErrorCode(byte errorCode) {
+ this.errorCode = errorCode;
+ this.isErrorCodeSet = true;
+ return this;
+ }
+
+ @Override
+ public Builder setErrorSubCode(byte errorSubCode) {
+ this.errorSubCode = errorSubCode;
+ this.isErrorSubCodeSet = true;
+ return this;
+ }
+
+ @Override
+ public Builder setData(byte[] data) {
+ if (data != null) {
+ this.data = data;
+ }
+ return this;
+ }
+
+ @Override
+ public Builder setHeader(BgpHeader bgpMsgHeader) {
+ this.bgpHeader = bgpMsgHeader;
+ return this;
+ }
+ }
+
+ @Override
+ public BgpVersion getVersion() {
+ return BgpVersion.BGP_4;
+ }
+
+ @Override
+ public BgpType getType() {
+ return BgpType.NOTIFICATION;
+ }
+
+ @Override
+ public void writeTo(ChannelBuffer cb) throws BgpParseException {
+ WRITER.write(cb, this);
+ }
+
+ static final Writer WRITER = new Writer();
+
+ /**
+ * Writer writes BGP notification message to channel buffer.
+ */
+ static class Writer implements BgpMessageWriter<BgpNotificationMsgVer4> {
+ @Override
+ public void write(ChannelBuffer cb, BgpNotificationMsgVer4 message) throws BgpParseException {
+ int msgStartIndex = cb.writerIndex();
+ int headerLenIndex = message.bgpHeader.write(cb);
+ if (headerLenIndex <= 0) {
+ throw new BgpParseException(BgpErrorType.MESSAGE_HEADER_ERROR, (byte) 0, null);
+ }
+ cb.writeByte(message.errorCode);
+ cb.writeByte(message.errorSubCode);
+ if (message.data != null) {
+ cb.writeBytes(message.data);
+ }
+
+ //Update message length field in notification message
+ int length = cb.writerIndex() - msgStartIndex;
+ cb.setShort(headerLenIndex, (short) length);
+ message.bgpHeader.setLength((short) length);
+ }
+ }
+
+ @Override
+ public byte getErrorCode() {
+ return this.errorCode;
+ }
+
+ /**
+ * Sets errorcode with specified errorcode.
+ *
+ * @param errorCode field
+ */
+ public void setErrorCode(byte errorCode) {
+ this.errorCode = errorCode;
+ }
+
+ @Override
+ public byte getErrorSubCode() {
+ return this.errorSubCode;
+ }
+
+ /**
+ * Sets error subcode with specified errorSubCode.
+ *
+ * @param errorSubCode field
+ */
+ public void setErrorSubCode(byte errorSubCode) {
+ this.errorSubCode = errorSubCode;
+ }
+
+ @Override
+ public byte[] getData() {
+ return this.data;
+ }
+
+ /**
+ * Sets error data with specified data.
+ *
+ * @param data field
+ */
+ public void setData(byte[] data) {
+ this.data = data;
+ }
+
+ @Override
+ public BgpHeader getHeader() {
+ return this.bgpHeader;
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(getClass())
+ .omitNullValues()
+ .add("bgpHeader", bgpHeader)
+ .add("data", data)
+ .add("errorCode", errorCode)
+ .add("errorSubCode", errorSubCode)
+ .toString();
+ }
+} \ No newline at end of file
diff --git a/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpOpenMsgVer4.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpOpenMsgVer4.java
new file mode 100644
index 00000000..359eec25
--- /dev/null
+++ b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpOpenMsgVer4.java
@@ -0,0 +1,518 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.bgpio.protocol.ver4;
+
+import java.util.LinkedList;
+import java.util.ListIterator;
+
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.onosproject.bgpio.exceptions.BgpParseException;
+import org.onosproject.bgpio.protocol.BgpMessageReader;
+import org.onosproject.bgpio.protocol.BgpMessageWriter;
+import org.onosproject.bgpio.protocol.BgpOpenMsg;
+import org.onosproject.bgpio.protocol.BgpType;
+import org.onosproject.bgpio.protocol.BgpVersion;
+import org.onosproject.bgpio.types.BgpErrorType;
+import org.onosproject.bgpio.types.BgpHeader;
+import org.onosproject.bgpio.types.BgpValueType;
+import org.onosproject.bgpio.types.FourOctetAsNumCapabilityTlv;
+import org.onosproject.bgpio.types.MultiProtocolExtnCapabilityTlv;
+import org.onosproject.bgpio.util.Validation;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.MoreObjects;
+
+/**
+ * Provides BGP open message.
+ */
+public class BgpOpenMsgVer4 implements BgpOpenMsg {
+
+ /*
+ 0 1 2 3
+ 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+ +-+-+-+-+-+-+-+-+
+ | Version |
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ | My Autonomous System |
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ | Hold Time |
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ | BGP Identifier |
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ | Opt Parm Len |
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ | Optional Parameters (variable) |
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ OPEN Message Format
+ REFERENCE : RFC 4271
+ */
+
+ protected static final Logger log = LoggerFactory.getLogger(BgpOpenMsgVer4.class);
+
+ public static final byte PACKET_VERSION = 4;
+ public static final int OPEN_MSG_MINIMUM_LENGTH = 10;
+ public static final int MSG_HEADER_LENGTH = 19;
+ public static final int MARKER_LENGTH = 16;
+ public static final int DEFAULT_HOLD_TIME = 120;
+ public static final short AS_TRANS = 23456;
+ public static final int OPT_PARA_TYPE_CAPABILITY = 2;
+ public static final BgpType MSG_TYPE = BgpType.OPEN;
+ public static final short AFI = 16388;
+ public static final byte SAFI = 71;
+ public static final byte RES = 0;
+ public static final int FOUR_OCTET_AS_NUM_CAPA_TYPE = 65;
+ public static final byte[] MARKER = new byte[]{(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+ (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff};
+ public static final BgpHeader DEFAULT_OPEN_HEADER = new BgpHeader(MARKER,
+ (short) OPEN_MSG_MINIMUM_LENGTH, (byte) 0X01);
+ private BgpHeader bgpMsgHeader;
+ private byte version;
+ private short asNumber;
+ private short holdTime;
+ private int bgpId;
+ private boolean isLargeAsCapabilitySet;
+ private LinkedList<BgpValueType> capabilityTlv;
+
+ public static final BgpOpenMsgVer4.Reader READER = new Reader();
+
+ /**
+ * reset variables.
+ */
+ public BgpOpenMsgVer4() {
+ this.bgpMsgHeader = null;
+ this.version = 0;
+ this.holdTime = 0;
+ this.asNumber = 0;
+ this.bgpId = 0;
+ this.capabilityTlv = null;
+ }
+
+ /**
+ * Constructor to initialize all variables of BGP Open message.
+ *
+ * @param bgpMsgHeader BGP Header in open message
+ * @param version BGP version in open message
+ * @param holdTime hold time in open message
+ * @param asNumber AS number in open message
+ * @param bgpId BGP identifier in open message
+ * @param capabilityTlv capabilities in open message
+ */
+ public BgpOpenMsgVer4(BgpHeader bgpMsgHeader, byte version, short asNumber, short holdTime,
+ int bgpId, LinkedList<BgpValueType> capabilityTlv) {
+ this.bgpMsgHeader = bgpMsgHeader;
+ this.version = version;
+ this.asNumber = asNumber;
+ this.holdTime = holdTime;
+ this.bgpId = bgpId;
+ this.capabilityTlv = capabilityTlv;
+ }
+
+ @Override
+ public BgpHeader getHeader() {
+ return this.bgpMsgHeader;
+ }
+
+ @Override
+ public BgpVersion getVersion() {
+ return BgpVersion.BGP_4;
+ }
+
+ @Override
+ public BgpType getType() {
+ return MSG_TYPE;
+ }
+
+ @Override
+ public short getHoldTime() {
+ return this.holdTime;
+ }
+
+ @Override
+ public short getAsNumber() {
+ return this.asNumber;
+ }
+
+ @Override
+ public int getBgpId() {
+ return this.bgpId;
+ }
+
+ @Override
+ public LinkedList<BgpValueType> getCapabilityTlv() {
+ return this.capabilityTlv;
+ }
+
+ /**
+ * Reader class for reading BGP open message from channel buffer.
+ */
+ public static class Reader implements BgpMessageReader<BgpOpenMsg> {
+
+ @Override
+ public BgpOpenMsg readFrom(ChannelBuffer cb, BgpHeader bgpHeader) throws BgpParseException {
+
+ byte version;
+ short holdTime;
+ short asNumber;
+ int bgpId;
+ byte optParaLen = 0;
+ byte optParaType;
+ byte capParaLen = 0;
+ LinkedList<BgpValueType> capabilityTlv = new LinkedList<>();
+
+ if (cb.readableBytes() < OPEN_MSG_MINIMUM_LENGTH) {
+ log.error("[readFrom] Invalid length: Packet size is less than the minimum length ");
+ Validation.validateLen(BgpErrorType.OPEN_MESSAGE_ERROR, BgpErrorType.BAD_MESSAGE_LENGTH,
+ cb.readableBytes());
+ }
+
+ // Read version
+ version = cb.readByte();
+ if (version != PACKET_VERSION) {
+ log.error("[readFrom] Invalid version: " + version);
+ throw new BgpParseException(BgpErrorType.OPEN_MESSAGE_ERROR,
+ BgpErrorType.UNSUPPORTED_VERSION_NUMBER, null);
+ }
+
+ // Read AS number
+ asNumber = cb.readShort();
+
+ // Read Hold timer
+ holdTime = cb.readShort();
+
+ // Read BGP Identifier
+ bgpId = cb.readInt();
+
+ // Read optional parameter length
+ optParaLen = cb.readByte();
+
+ // Read Capabilities if optional parameter length is greater than 0
+ if (optParaLen != 0) {
+ // Read optional parameter type
+ optParaType = cb.readByte();
+
+ // Read optional parameter length
+ capParaLen = cb.readByte();
+
+ if (cb.readableBytes() < capParaLen) {
+ throw new BgpParseException(BgpErrorType.OPEN_MESSAGE_ERROR, (byte) 0, null);
+ }
+
+ ChannelBuffer capaCb = cb.readBytes(capParaLen);
+
+ // Parse capabilities only if optional parameter type is 2
+ if ((optParaType == OPT_PARA_TYPE_CAPABILITY) && (capParaLen != 0)) {
+ capabilityTlv = parseCapabilityTlv(capaCb);
+ } else {
+ throw new BgpParseException(BgpErrorType.OPEN_MESSAGE_ERROR,
+ BgpErrorType.UNSUPPORTED_OPTIONAL_PARAMETER, null);
+ }
+ }
+ return new BgpOpenMsgVer4(bgpHeader, version, asNumber, holdTime, bgpId, capabilityTlv);
+ }
+ }
+
+ /**
+ * Parsing capabilities.
+ *
+ * @param cb of type channel buffer
+ * @return capabilityTlv of open message
+ * @throws BgpParseException while parsing capabilities
+ */
+ protected static LinkedList<BgpValueType> parseCapabilityTlv(ChannelBuffer cb) throws BgpParseException {
+
+ LinkedList<BgpValueType> capabilityTlv = new LinkedList<>();
+
+ while (cb.readableBytes() > 0) {
+ BgpValueType tlv;
+ short type = cb.readByte();
+ short length = cb.readByte();
+
+ switch (type) {
+ case FourOctetAsNumCapabilityTlv.TYPE:
+ log.debug("FourOctetAsNumCapabilityTlv");
+ if (FourOctetAsNumCapabilityTlv.LENGTH != length) {
+ throw new BgpParseException("Invalid length received for FourOctetAsNumCapabilityTlv.");
+ }
+ if (length > cb.readableBytes()) {
+ throw new BgpParseException("Four octet as num tlv length"
+ + " is more than readableBytes.");
+ }
+ int as4Num = cb.readInt();
+ tlv = new FourOctetAsNumCapabilityTlv(as4Num);
+ break;
+ case MultiProtocolExtnCapabilityTlv.TYPE:
+ log.debug("MultiProtocolExtnCapabilityTlv");
+ if (MultiProtocolExtnCapabilityTlv.LENGTH != length) {
+ throw new BgpParseException("Invalid length received for MultiProtocolExtnCapabilityTlv.");
+ }
+ if (length > cb.readableBytes()) {
+ throw new BgpParseException("BGP LS tlv length is more than readableBytes.");
+ }
+ short afi = cb.readShort();
+ byte res = cb.readByte();
+ byte safi = cb.readByte();
+ tlv = new MultiProtocolExtnCapabilityTlv(afi, res, safi);
+ break;
+ default:
+ log.debug("Warning: Unsupported TLV: " + type);
+ cb.skipBytes(length);
+ continue;
+ }
+ capabilityTlv.add(tlv);
+ }
+ return capabilityTlv;
+ }
+
+ /**
+ * Builder class for BGP open message.
+ */
+ static class Builder implements BgpOpenMsg.Builder {
+
+ private boolean isHeaderSet = false;
+ private BgpHeader bgpMsgHeader;
+ private boolean isHoldTimeSet = false;
+ private short holdTime;
+ private boolean isAsNumSet = false;
+ private short asNumber;
+ private boolean isBgpIdSet = false;
+ private int bgpId;
+ private boolean isLargeAsCapabilityTlvSet = false;
+ private boolean isLsCapabilityTlvSet = false;
+
+ LinkedList<BgpValueType> capabilityTlv = new LinkedList<>();
+
+ @Override
+ public BgpOpenMsg build() throws BgpParseException {
+ BgpHeader bgpMsgHeader = this.isHeaderSet ? this.bgpMsgHeader : DEFAULT_OPEN_HEADER;
+ short holdTime = this.isHoldTimeSet ? this.holdTime : DEFAULT_HOLD_TIME;
+
+ if (!this.isAsNumSet) {
+ throw new BgpParseException("BGP AS number is not set (mandatory)");
+ }
+
+ if (!this.isBgpIdSet) {
+ throw new BgpParseException("BGPID is not set (mandatory)");
+ }
+
+ if (this.isLargeAsCapabilityTlvSet) {
+ BgpValueType tlv;
+ int value = this.asNumber;
+ tlv = new FourOctetAsNumCapabilityTlv(value);
+ this.capabilityTlv.add(tlv);
+ }
+
+ if (this.isLsCapabilityTlvSet) {
+ BgpValueType tlv;
+ tlv = new MultiProtocolExtnCapabilityTlv(AFI, RES, SAFI);
+ this.capabilityTlv.add(tlv);
+ }
+
+ return new BgpOpenMsgVer4(bgpMsgHeader, PACKET_VERSION, this.asNumber, holdTime, this.bgpId,
+ this.capabilityTlv);
+ }
+
+ @Override
+ public Builder setHeader(BgpHeader bgpMsgHeader) {
+ this.bgpMsgHeader = bgpMsgHeader;
+ return this;
+ }
+
+ @Override
+ public Builder setHoldTime(short holdTime) {
+ this.holdTime = holdTime;
+ this.isHoldTimeSet = true;
+ return this;
+ }
+
+ @Override
+ public Builder setAsNumber(short asNumber) {
+ this.asNumber = asNumber;
+ this.isAsNumSet = true;
+ return this;
+ }
+
+ @Override
+ public Builder setBgpId(int bgpId) {
+ this.bgpId = bgpId;
+ this.isBgpIdSet = true;
+ return this;
+ }
+
+ @Override
+ public Builder setCapabilityTlv(LinkedList<BgpValueType> capabilityTlv) {
+ this.capabilityTlv = capabilityTlv;
+ return this;
+ }
+
+ @Override
+ public Builder setLargeAsCapabilityTlv(boolean isLargeAsCapabilitySet) {
+ this.isLargeAsCapabilityTlvSet = isLargeAsCapabilitySet;
+ return this;
+ }
+
+ @Override
+ public Builder setLsCapabilityTlv(boolean isLsCapabilitySet) {
+ this.isLsCapabilityTlvSet = isLsCapabilitySet;
+ return this;
+ }
+ }
+
+ @Override
+ public void writeTo(ChannelBuffer cb) {
+ try {
+ WRITER.write(cb, this);
+ } catch (BgpParseException e) {
+ log.debug("[writeTo] Error: " + e.toString());
+ }
+ }
+
+ public static final Writer WRITER = new Writer();
+
+ /**
+ * Writer class for writing BGP open message to channel buffer.
+ */
+ public static class Writer implements BgpMessageWriter<BgpOpenMsgVer4> {
+
+ @Override
+ public void write(ChannelBuffer cb, BgpOpenMsgVer4 message) throws BgpParseException {
+
+ int optParaLen = 0;
+ int as4num = 0;
+
+ int startIndex = cb.writerIndex();
+
+ // write common header and get msg length index
+ int msgLenIndex = message.bgpMsgHeader.write(cb);
+
+ if (msgLenIndex <= 0) {
+ throw new BgpParseException("Unable to write message header.");
+ }
+
+ // write version in 1-octet
+ cb.writeByte(message.version);
+
+ // get as4num if LS Capability is set
+ if (message.isLargeAsCapabilitySet) {
+ LinkedList<BgpValueType> capabilityTlv = message
+ .getCapabilityTlv();
+ ListIterator<BgpValueType> listIterator = capabilityTlv
+ .listIterator();
+
+ while (listIterator.hasNext()) {
+ BgpValueType tlv = listIterator.next();
+ if (tlv.getType() == FOUR_OCTET_AS_NUM_CAPA_TYPE) {
+ as4num = ((FourOctetAsNumCapabilityTlv) tlv).getInt();
+ break;
+ }
+ }
+ }
+
+ if ((message.isLargeAsCapabilitySet) && (as4num > 65535)) {
+ // write As number as AS_TRANS
+ cb.writeShort(AS_TRANS);
+ } else {
+ // write AS number in next 2-octet
+ cb.writeShort(message.asNumber);
+ }
+
+ // write HoldTime in next 2-octet
+ cb.writeShort(message.holdTime);
+
+ // write BGP Identifier in next 4-octet
+ cb.writeInt(message.bgpId);
+
+ // store the index of Optional parameter length
+ int optParaLenIndex = cb.writerIndex();
+
+ // set optional parameter length as 0
+ cb.writeByte(0);
+
+ // Pack capability TLV
+ optParaLen = message.packCapabilityTlv(cb, message);
+
+ if (optParaLen != 0) {
+ // Update optional parameter length
+ cb.setByte(optParaLenIndex, (byte) (optParaLen + 2)); //+2 for optional parameter type.
+ }
+
+ // write OPEN Object Length
+ int length = cb.writerIndex() - startIndex;
+ cb.setShort(msgLenIndex, (short) length);
+ message.bgpMsgHeader.setLength((short) length);
+ }
+ }
+
+ /**
+ * returns length of capability tlvs.
+ *
+ * @param cb of type channel buffer
+ * @param message of type BGPOpenMsgVer4
+ * @return capParaLen of open message
+ */
+ protected int packCapabilityTlv(ChannelBuffer cb, BgpOpenMsgVer4 message) {
+ int startIndex = cb.writerIndex();
+ int capParaLen = 0;
+ int capParaLenIndex = 0;
+
+ LinkedList<BgpValueType> capabilityTlv = message.capabilityTlv;
+ ListIterator<BgpValueType> listIterator = capabilityTlv.listIterator();
+
+ if (listIterator.hasNext()) {
+ // Set optional parameter type as 2
+ cb.writeByte(OPT_PARA_TYPE_CAPABILITY);
+
+ // Store the index of capability parameter length and update length at the end
+ capParaLenIndex = cb.writerIndex();
+
+ // Set capability parameter length as 0
+ cb.writeByte(0);
+
+ // Update the startIndex to know the length of capability tlv
+ startIndex = cb.writerIndex();
+ }
+
+ while (listIterator.hasNext()) {
+ BgpValueType tlv = listIterator.next();
+ if (tlv == null) {
+ log.debug("Warning: tlv is null from CapabilityTlv list");
+ continue;
+ }
+ tlv.write(cb);
+ }
+
+ capParaLen = cb.writerIndex() - startIndex;
+
+ if (capParaLen != 0) {
+ // Update capability parameter length
+ cb.setByte(capParaLenIndex, (byte) capParaLen);
+ }
+ return capParaLen;
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(getClass())
+ .add("bgpMsgHeader", bgpMsgHeader)
+ .add("version", version)
+ .add("holdTime", holdTime)
+ .add("asNumber", asNumber)
+ .add("bgpId", bgpId)
+ .add("capabilityTlv", capabilityTlv)
+ .toString();
+ }
+}
diff --git a/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpPathAttributes.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpPathAttributes.java
new file mode 100644
index 00000000..13e52ca2
--- /dev/null
+++ b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpPathAttributes.java
@@ -0,0 +1,200 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.bgpio.protocol.ver4;
+
+import java.util.LinkedList;
+import java.util.List;
+
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.onosproject.bgpio.exceptions.BgpParseException;
+import org.onosproject.bgpio.types.As4Path;
+import org.onosproject.bgpio.types.AsPath;
+import org.onosproject.bgpio.types.BgpErrorType;
+import org.onosproject.bgpio.types.BgpValueType;
+import org.onosproject.bgpio.types.LocalPref;
+import org.onosproject.bgpio.types.Med;
+import org.onosproject.bgpio.types.NextHop;
+import org.onosproject.bgpio.types.Origin;
+import org.onosproject.bgpio.types.MpReachNlri;
+import org.onosproject.bgpio.types.MpUnReachNlri;
+import org.onosproject.bgpio.util.UnSupportedAttribute;
+import org.onosproject.bgpio.util.Validation;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.MoreObjects;
+
+/**
+ * Provides Implementation of BGP Path Attribute.
+ */
+public class BgpPathAttributes {
+
+ /* Path attribute:
+ <attribute type, attribute length, attribute value>
+
+ 0 1
+ 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ | Attr. Flags |Attr. Type Code|
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ REFERENCE : RFC 4271
+ */
+ protected static final Logger log = LoggerFactory.getLogger(BgpPathAttributes.class);
+
+ public static final int LINK_STATE_ATTRIBUTE_TYPE = 50;
+ public static final int MPREACHNLRI_TYPE = 14;
+ public static final int MPUNREACHNLRI_TYPE = 15;
+
+ private final List<BgpValueType> pathAttribute;
+
+ /**
+ * Initialize parameter.
+ */
+ public BgpPathAttributes() {
+ this.pathAttribute = null;
+ }
+
+ /**
+ * Constructor to initialize parameters for BGP path attributes.
+ *
+ * @param pathAttribute list of path attributes
+ */
+ public BgpPathAttributes(List<BgpValueType> pathAttribute) {
+ this.pathAttribute = pathAttribute;
+ }
+
+ /**
+ * Returns list of path attributes.
+ *
+ * @return list of path attributes
+ */
+ public List<BgpValueType> pathAttributes() {
+ return this.pathAttribute;
+ }
+
+ /**
+ * Reads from channelBuffer and parses BGP path attributes.
+ *
+ * @param cb channelBuffer
+ * @return object of BgpPathAttributes
+ * @throws BgpParseException while parsing BGP path attributes
+ */
+ public static BgpPathAttributes read(ChannelBuffer cb)
+ throws BgpParseException {
+
+ BgpValueType pathAttribute = null;
+ List<BgpValueType> pathAttributeList = new LinkedList<>();
+ boolean isOrigin = false;
+ boolean isAsPath = false;
+ boolean isNextHop = false;
+ boolean isMpReach = false;
+ boolean isMpUnReach = false;
+ while (cb.readableBytes() > 0) {
+ cb.markReaderIndex();
+ byte flags = cb.readByte();
+ byte typeCode = cb.readByte();
+ cb.resetReaderIndex();
+ switch (typeCode) {
+ case Origin.ORIGIN_TYPE:
+ pathAttribute = Origin.read(cb);
+ isOrigin = ((Origin) pathAttribute).isOriginSet();
+ break;
+ case AsPath.ASPATH_TYPE:
+ pathAttribute = AsPath.read(cb);
+ isAsPath = ((AsPath) pathAttribute).isaspathSet();
+ break;
+ case As4Path.AS4PATH_TYPE:
+ pathAttribute = As4Path.read(cb);
+ break;
+ case NextHop.NEXTHOP_TYPE:
+ pathAttribute = NextHop.read(cb);
+ isNextHop = ((NextHop) pathAttribute).isNextHopSet();
+ break;
+ case Med.MED_TYPE:
+ pathAttribute = Med.read(cb);
+ break;
+ case LocalPref.LOCAL_PREF_TYPE:
+ pathAttribute = LocalPref.read(cb);
+ break;
+ case MpReachNlri.MPREACHNLRI_TYPE:
+ pathAttribute = MpReachNlri.read(cb);
+ isMpReach = ((MpReachNlri) pathAttribute).isMpReachNlriSet();
+ break;
+ case MpUnReachNlri.MPUNREACHNLRI_TYPE:
+ pathAttribute = MpUnReachNlri.read(cb);
+ isMpUnReach = ((MpUnReachNlri) pathAttribute)
+ .isMpUnReachNlriSet();
+ break;
+ case LINK_STATE_ATTRIBUTE_TYPE:
+ //TODO: To be merged later
+ break;
+ default:
+ //skip bytes for unsupported attribute types
+ UnSupportedAttribute.read(cb);
+ }
+ pathAttributeList.add(pathAttribute);
+ }
+
+ checkMandatoryAttr(isOrigin, isAsPath, isNextHop, isMpReach, isMpUnReach);
+ //TODO:if mp_reach or mp_unreach not present ignore the packet
+ return new BgpPathAttributes(pathAttributeList);
+ }
+
+ /**
+ * Checks mandatory attributes are presents, if not present throws exception.
+ *
+ * @param isOrigin say whether origin attribute is present
+ * @param isAsPath say whether aspath attribute is present
+ * @param isNextHop say whether nexthop attribute is present
+ * @param isMpReach say whether mpreach attribute is present
+ * @param isMpUnReach say whether mpunreach attribute is present
+ * @throws BgpParseException if mandatory path attribute is not present
+ */
+ public static void checkMandatoryAttr(boolean isOrigin, boolean isAsPath,
+ boolean isNextHop, boolean isMpReach, boolean isMpUnReach)
+ throws BgpParseException {
+ // Mandatory attributes validation not required for MP_UNREACH
+ if (isMpUnReach) {
+ return;
+ }
+
+ if (!isOrigin) {
+ log.debug("Mandatory Attributes not Present");
+ Validation.validateType(BgpErrorType.UPDATE_MESSAGE_ERROR,
+ BgpErrorType.MISSING_WELLKNOWN_ATTRIBUTE,
+ Origin.ORIGIN_TYPE);
+ }
+ if (!isAsPath) {
+ log.debug("Mandatory Attributes not Present");
+ Validation.validateType(BgpErrorType.UPDATE_MESSAGE_ERROR,
+ BgpErrorType.MISSING_WELLKNOWN_ATTRIBUTE,
+ AsPath.ASPATH_TYPE);
+ }
+ if (!isMpUnReach && !isMpReach && !isNextHop) {
+ log.debug("Mandatory Attributes not Present");
+ Validation.validateType(BgpErrorType.UPDATE_MESSAGE_ERROR,
+ BgpErrorType.MISSING_WELLKNOWN_ATTRIBUTE,
+ NextHop.NEXTHOP_TYPE);
+ }
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(getClass())
+ .add("pathAttribute", pathAttribute)
+ .toString();
+ }
+}
diff --git a/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpUpdateMsgVer4.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpUpdateMsgVer4.java
new file mode 100644
index 00000000..4d6af594
--- /dev/null
+++ b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpUpdateMsgVer4.java
@@ -0,0 +1,285 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.bgpio.protocol.ver4;
+
+import java.util.LinkedList;
+import java.util.List;
+
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.onlab.packet.IpPrefix;
+import org.onosproject.bgpio.exceptions.BgpParseException;
+import org.onosproject.bgpio.protocol.BgpMessageReader;
+import org.onosproject.bgpio.protocol.BgpType;
+import org.onosproject.bgpio.protocol.BgpUpdateMsg;
+import org.onosproject.bgpio.types.BgpErrorType;
+import org.onosproject.bgpio.types.BgpHeader;
+import org.onosproject.bgpio.util.Validation;
+import org.onosproject.bgpio.protocol.BgpVersion;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.MoreObjects;
+
+/**
+ * BGP Update Message: UPDATE messages are used to transfer routing information
+ * between BGP peers. The information in the UPDATE message is used by core to
+ * construct a graph
+ */
+public class BgpUpdateMsgVer4 implements BgpUpdateMsg {
+
+ /* 0 1 2 3
+ 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ | |
+ + +
+ | |
+ + +
+ | Marker |
+ + +
+ | |
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ | Length | Type |
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ | Withdrawn Routes Length (2 octets) |
+ +-----------------------------------------------------+
+ | Withdrawn Routes (variable) |
+ +-----------------------------------------------------+
+ | Total Path Attribute Length (2 octets) |
+ +-----------------------------------------------------+
+ | Path Attributes (variable) |
+ +-----------------------------------------------------+
+ | Network Layer Reachability Information (variable) |
+ +-----------------------------------------------------+
+ REFERENCE : RFC 4271
+ */
+
+ protected static final Logger log = LoggerFactory
+ .getLogger(BgpUpdateMsgVer4.class);
+
+ public static final byte PACKET_VERSION = 4;
+ //Withdrawn Routes Length(2) + Total Path Attribute Length(2)
+ public static final int PACKET_MINIMUM_LENGTH = 4;
+ public static final int BYTE_IN_BITS = 8;
+ public static final int MIN_LEN_AFTER_WITHDRW_ROUTES = 2;
+ public static final int MINIMUM_COMMON_HEADER_LENGTH = 19;
+ public static final BgpType MSG_TYPE = BgpType.UPDATE;
+ public static final BgpUpdateMsgVer4.Reader READER = new Reader();
+
+ private List<IpPrefix> withdrawnRoutes;
+ private BgpPathAttributes bgpPathAttributes;
+ private BgpHeader bgpHeader;
+ private List<IpPrefix> nlri;
+
+ /**
+ * Constructor to initialize parameters for BGP Update message.
+ *
+ * @param bgpHeader in Update message
+ * @param withdrawnRoutes withdrawn routes
+ * @param bgpPathAttributes BGP Path attributes
+ * @param nlri Network Layer Reachability Information
+ */
+ public BgpUpdateMsgVer4(BgpHeader bgpHeader, List<IpPrefix> withdrawnRoutes,
+ BgpPathAttributes bgpPathAttributes, List<IpPrefix> nlri) {
+ this.bgpHeader = bgpHeader;
+ this.withdrawnRoutes = withdrawnRoutes;
+ this.bgpPathAttributes = bgpPathAttributes;
+ this.nlri = nlri;
+ }
+
+ /**
+ * Reader reads BGP Update Message from the channel buffer.
+ */
+ static class Reader implements BgpMessageReader<BgpUpdateMsg> {
+
+ @Override
+ public BgpUpdateMsg readFrom(ChannelBuffer cb, BgpHeader bgpHeader)
+ throws BgpParseException {
+
+ if (cb.readableBytes() != (bgpHeader.getLength() - MINIMUM_COMMON_HEADER_LENGTH)) {
+ Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
+ BgpErrorType.BAD_MESSAGE_LENGTH, bgpHeader.getLength());
+ }
+
+ LinkedList<IpPrefix> withDrwRoutes = new LinkedList<>();
+ LinkedList<IpPrefix> nlri = new LinkedList<>();
+ BgpPathAttributes bgpPathAttributes = new BgpPathAttributes();
+ // Reading Withdrawn Routes Length
+ Short withDrwLen = cb.readShort();
+
+ if (cb.readableBytes() < withDrwLen) {
+ Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
+ BgpErrorType.MALFORMED_ATTRIBUTE_LIST,
+ cb.readableBytes());
+ }
+ ChannelBuffer tempCb = cb.readBytes(withDrwLen);
+ if (withDrwLen != 0) {
+ // Parsing WithdrawnRoutes
+ withDrwRoutes = parseWithdrawnRoutes(tempCb);
+ }
+ if (cb.readableBytes() < MIN_LEN_AFTER_WITHDRW_ROUTES) {
+ log.debug("Bgp Path Attribute len field not present");
+ throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR,
+ BgpErrorType.MALFORMED_ATTRIBUTE_LIST, null);
+ }
+
+ // Reading Total Path Attribute Length
+ short totPathAttrLen = cb.readShort();
+ int len = withDrwLen + totPathAttrLen + PACKET_MINIMUM_LENGTH;
+ if (len > bgpHeader.getLength()) {
+ throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR,
+ BgpErrorType.MALFORMED_ATTRIBUTE_LIST, null);
+ }
+ if (totPathAttrLen != 0) {
+ // Parsing BGPPathAttributes
+ if (cb.readableBytes() < totPathAttrLen) {
+ Validation
+ .validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
+ BgpErrorType.MALFORMED_ATTRIBUTE_LIST,
+ cb.readableBytes());
+ }
+ tempCb = cb.readBytes(totPathAttrLen);
+ bgpPathAttributes = BgpPathAttributes.read(tempCb);
+ }
+ if (cb.readableBytes() > 0) {
+ // Parsing NLRI
+ nlri = parseNlri(cb);
+ }
+ return new BgpUpdateMsgVer4(bgpHeader, withDrwRoutes,
+ bgpPathAttributes, nlri);
+ }
+ }
+
+ /**
+ * Parses NLRI from channel buffer.
+ *
+ * @param cb channelBuffer
+ * @return list of IP Prefix
+ * @throws BgpParseException while parsing NLRI
+ */
+ public static LinkedList<IpPrefix> parseNlri(ChannelBuffer cb)
+ throws BgpParseException {
+ LinkedList<IpPrefix> nlri = new LinkedList<>();
+ while (cb.readableBytes() > 0) {
+ int length = cb.readByte();
+ IpPrefix ipPrefix;
+ if (length == 0) {
+ byte[] prefix = new byte[] {0};
+ ipPrefix = Validation.bytesToPrefix(prefix, length);
+ nlri.add(ipPrefix);
+ } else {
+ int len = length / BYTE_IN_BITS;
+ int reminder = length % BYTE_IN_BITS;
+ if (reminder > 0) {
+ len = len + 1;
+ }
+ if (cb.readableBytes() < len) {
+ Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
+ BgpErrorType.MALFORMED_ATTRIBUTE_LIST,
+ cb.readableBytes());
+ }
+ byte[] prefix = new byte[len];
+ cb.readBytes(prefix, 0, len);
+ ipPrefix = Validation.bytesToPrefix(prefix, length);
+ nlri.add(ipPrefix);
+ }
+ }
+ return nlri;
+ }
+
+ /**
+ * Parsing withdrawn routes from channel buffer.
+ *
+ * @param cb channelBuffer
+ * @return list of IP prefix
+ * @throws BgpParseException while parsing withdrawn routes
+ */
+ public static LinkedList<IpPrefix> parseWithdrawnRoutes(ChannelBuffer cb)
+ throws BgpParseException {
+ LinkedList<IpPrefix> withDrwRoutes = new LinkedList<>();
+ while (cb.readableBytes() > 0) {
+ int length = cb.readByte();
+ IpPrefix ipPrefix;
+ if (length == 0) {
+ byte[] prefix = new byte[] {0};
+ ipPrefix = Validation.bytesToPrefix(prefix, length);
+ withDrwRoutes.add(ipPrefix);
+ } else {
+ int len = length / BYTE_IN_BITS;
+ int reminder = length % BYTE_IN_BITS;
+ if (reminder > 0) {
+ len = len + 1;
+ }
+ if (cb.readableBytes() < len) {
+ Validation
+ .validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
+ BgpErrorType.MALFORMED_ATTRIBUTE_LIST,
+ cb.readableBytes());
+ }
+ byte[] prefix = new byte[len];
+ cb.readBytes(prefix, 0, len);
+ ipPrefix = Validation.bytesToPrefix(prefix, length);
+ withDrwRoutes.add(ipPrefix);
+ }
+ }
+ return withDrwRoutes;
+ }
+
+ @Override
+ public BgpVersion getVersion() {
+ return BgpVersion.BGP_4;
+ }
+
+ @Override
+ public BgpType getType() {
+ return BgpType.UPDATE;
+ }
+
+ @Override
+ public void writeTo(ChannelBuffer channelBuffer) throws BgpParseException {
+ //Not to be implemented as of now
+ }
+
+ @Override
+ public BgpPathAttributes bgpPathAttributes() {
+ return this.bgpPathAttributes;
+ }
+
+ @Override
+ public List<IpPrefix> withdrawnRoutes() {
+ return withdrawnRoutes;
+ }
+
+ @Override
+ public List<IpPrefix> nlri() {
+ return nlri;
+ }
+
+ @Override
+ public BgpHeader getHeader() {
+ return this.bgpHeader;
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(getClass())
+ .omitNullValues()
+ .add("bgpHeader", bgpHeader)
+ .add("withDrawnRoutes", withdrawnRoutes)
+ .add("nlri", nlri)
+ .add("bgpPathAttributes", bgpPathAttributes)
+ .toString();
+ }
+}
diff --git a/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/package-info.java b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/package-info.java
new file mode 100755
index 00000000..fb8c67c0
--- /dev/null
+++ b/framework/src/onos/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * BGP Protocol specific details of version 4.
+ */
+package org.onosproject.bgpio.protocol.ver4; \ No newline at end of file