Shashikanth VH
Committed by Gerrit Code Review

[ONOS-3857] BGP update message flow specification components parsing.

Change-Id: I9b58cdf2c47cb736a8119433cbc7911d871a83d1
......@@ -15,6 +15,7 @@
*/
package org.onosproject.bgpio.types;
import java.util.LinkedList;
import java.util.Objects;
import java.util.List;
import org.jboss.netty.buffer.ChannelBuffer;
......@@ -78,12 +79,34 @@ public class BgpFsDestinationPortNum implements BgpValueType {
* Reads the channel buffer and returns object.
*
* @param cb channelBuffer
* @param type address type
* @return object of flow spec destination port number
* @throws BgpParseException while parsing BgpFsDestinationPortNum
*/
public static BgpFsDestinationPortNum read(ChannelBuffer cb, short type) throws BgpParseException {
return null;
public static BgpFsDestinationPortNum read(ChannelBuffer cb) throws BgpParseException {
List<BgpFsOperatorValue> operatorValue = new LinkedList<>();
byte option;
short port;
/*
0 1 2 3 4 5 6 7
+---+---+---+---+---+---+---+----+
| e | a | len | 0 |lt |gt |eq |
+---+---+---+---+---+---+---+----+
*/
do {
option = (byte) cb.readByte();
int len = (option & Constants.BGP_FLOW_SPEC_LEN_MASK) >> 4;
if ((1 << len) == 1) {
port = cb.readByte();
operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) port}));
} else {
port = cb.readShort();
operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) (port >> 8), (byte) port}));
}
} while ((option & Constants.BGP_FLOW_SPEC_END_OF_LIST_MASK) == 0);
return new BgpFsDestinationPortNum(operatorValue);
}
@Override
......
......@@ -22,6 +22,8 @@ import org.jboss.netty.buffer.ChannelBuffer;
import org.onlab.packet.IpPrefix;
import org.onosproject.bgpio.exceptions.BgpParseException;
import org.onosproject.bgpio.util.Constants;
import org.onosproject.bgpio.util.Validation;
import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
......@@ -33,6 +35,7 @@ public class BgpFsDestinationPrefix implements BgpValueType {
public static final byte FLOW_SPEC_TYPE = Constants.BGP_FLOWSPEC_DST_PREFIX;
private byte length;
private IpPrefix ipPrefix;
public static final int BYTE_IN_BITS = 8;
/**
* Constructor to initialize parameters.
......@@ -89,12 +92,32 @@ public class BgpFsDestinationPrefix implements BgpValueType {
* Reads the channel buffer and returns object of IPv4AddressTlv.
*
* @param cb channelBuffer
* @param type address type
* @return object of flow spec destination prefix
* @throws BgpParseException while parsing BgpFsDestinationPrefix
*/
public static BgpFsDestinationPrefix read(ChannelBuffer cb, short type) throws BgpParseException {
return null;
public static BgpFsDestinationPrefix read(ChannelBuffer cb) throws BgpParseException {
IpPrefix ipPrefix;
int length = cb.readByte();
if (length == 0) {
byte[] prefix = new byte[] {0};
ipPrefix = Validation.bytesToPrefix(prefix, length);
return new BgpFsDestinationPrefix((byte) ipPrefix.prefixLength(), ipPrefix);
}
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);
return new BgpFsDestinationPrefix((byte) ipPrefix.prefixLength(), ipPrefix);
}
/**
......
......@@ -15,6 +15,7 @@
*/
package org.onosproject.bgpio.types;
import java.util.LinkedList;
import java.util.Objects;
import java.util.List;
import org.jboss.netty.buffer.ChannelBuffer;
......@@ -79,12 +80,20 @@ public class BgpFsDscpValue implements BgpValueType {
* Reads the channel buffer and returns object.
*
* @param cb channelBuffer
* @param type address type
* @return object of flow spec DSCP value
* @throws BgpParseException while parsing BgpFsDscpValue
*/
public static BgpFsDscpValue read(ChannelBuffer cb, short type) throws BgpParseException {
return null;
public static BgpFsDscpValue read(ChannelBuffer cb) throws BgpParseException {
List<BgpFsOperatorValue> operatorValue = new LinkedList<>();
byte option;
byte dscpValue;
do {
option = (byte) cb.readByte();
dscpValue = cb.readByte();
operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) dscpValue}));
} while ((option & Constants.BGP_FLOW_SPEC_END_OF_LIST_MASK) == 0);
return new BgpFsDscpValue(operatorValue);
}
@Override
......
......@@ -15,6 +15,7 @@
*/
package org.onosproject.bgpio.types;
import java.util.LinkedList;
import java.util.Objects;
import java.util.List;
import org.jboss.netty.buffer.ChannelBuffer;
......@@ -79,12 +80,21 @@ public class BgpFsFragment implements BgpValueType {
* Reads the channel buffer and returns object.
*
* @param cb channelBuffer
* @param type address type
* @return object of flow spec fragment
* @throws BgpParseException while parsing BgpFsFragment
*/
public static BgpFsFragment read(ChannelBuffer cb, short type) throws BgpParseException {
return null;
public static BgpFsFragment read(ChannelBuffer cb) throws BgpParseException {
List<BgpFsOperatorValue> operatorValue = new LinkedList<>();
byte option;
byte fragment;
do {
option = (byte) cb.readByte();
fragment = cb.readByte();
operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) fragment}));
} while ((option & Constants.BGP_FLOW_SPEC_END_OF_LIST_MASK) == 0);
return new BgpFsFragment(operatorValue);
}
@Override
......
......@@ -15,6 +15,7 @@
*/
package org.onosproject.bgpio.types;
import java.util.LinkedList;
import java.util.Objects;
import org.jboss.netty.buffer.ChannelBuffer;
......@@ -79,12 +80,21 @@ public class BgpFsIcmpCode implements BgpValueType {
* Reads the channel buffer and returns object.
*
* @param cb channelBuffer
* @param type address type
* @return object of flow spec ICMP code
* @throws BgpParseException while parsing BgpFsIcmpCode
*/
public static BgpFsIcmpCode read(ChannelBuffer cb, short type) throws BgpParseException {
return null;
public static BgpFsIcmpCode read(ChannelBuffer cb) throws BgpParseException {
List<BgpFsOperatorValue> operatorValue = new LinkedList<>();
byte option;
byte icmpCode;
do {
option = (byte) cb.readByte();
icmpCode = cb.readByte();
operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) icmpCode}));
} while ((option & Constants.BGP_FLOW_SPEC_END_OF_LIST_MASK) == 0);
return new BgpFsIcmpCode(operatorValue);
}
@Override
......
......@@ -15,6 +15,7 @@
*/
package org.onosproject.bgpio.types;
import java.util.LinkedList;
import java.util.Objects;
import org.jboss.netty.buffer.ChannelBuffer;
......@@ -78,12 +79,21 @@ public class BgpFsIcmpType implements BgpValueType {
* Reads the channel buffer and returns object.
*
* @param cb channelBuffer
* @param type address type
* @return object of flow spec ICMP type
* @throws BgpParseException while parsing BgpFsIcmpType
*/
public static BgpFsIcmpType read(ChannelBuffer cb, short type) throws BgpParseException {
return null;
public static BgpFsIcmpType read(ChannelBuffer cb) throws BgpParseException {
List<BgpFsOperatorValue> operatorValue = new LinkedList<>();
byte option;
byte icmpType;
do {
option = (byte) cb.readByte();
icmpType = cb.readByte();
operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) icmpType}));
} while ((option & Constants.BGP_FLOW_SPEC_END_OF_LIST_MASK) == 0);
return new BgpFsIcmpType(operatorValue);
}
@Override
......
......@@ -15,6 +15,7 @@
*/
package org.onosproject.bgpio.types;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
......@@ -89,12 +90,21 @@ public class BgpFsIpProtocol implements BgpValueType {
* Reads the channel buffer and returns object.
*
* @param cb channelBuffer
* @param type address type
* @return object of flow spec IP protocol
* @throws BgpParseException while parsing BgpFsIpProtocol
*/
public static BgpFsIpProtocol read(ChannelBuffer cb, short type) throws BgpParseException {
return null;
public static BgpFsIpProtocol read(ChannelBuffer cb) throws BgpParseException {
List<BgpFsOperatorValue> operatorValue = new LinkedList<>();
byte option;
byte proto;
do {
option = (byte) cb.readByte();
proto = cb.readByte();
operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) proto}));
} while ((option & Constants.BGP_FLOW_SPEC_END_OF_LIST_MASK) == 0);
return new BgpFsIpProtocol(operatorValue);
}
@Override
......
......@@ -15,6 +15,7 @@
*/
package org.onosproject.bgpio.types;
import java.util.LinkedList;
import java.util.Objects;
import java.util.List;
import org.jboss.netty.buffer.ChannelBuffer;
......@@ -79,12 +80,28 @@ public class BgpFsPacketLength implements BgpValueType {
* Reads the channel buffer and returns object.
*
* @param cb channelBuffer
* @param type address type
* @return object of flow spec packet length
* @throws BgpParseException while parsing BgpFsPacketLength
*/
public static BgpFsPacketLength read(ChannelBuffer cb, short type) throws BgpParseException {
return null;
public static BgpFsPacketLength read(ChannelBuffer cb) throws BgpParseException {
List<BgpFsOperatorValue> operatorValue = new LinkedList<>();
byte option;
short packetLen;
do {
option = (byte) cb.readByte();
int len = (option & Constants.BGP_FLOW_SPEC_LEN_MASK) >> 4;
if ((1 << len) == 1) {
packetLen = cb.readByte();
operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) packetLen}));
} else {
packetLen = cb.readShort();
operatorValue.add(new BgpFsOperatorValue(option,
new byte[] {(byte) (packetLen >> 8), (byte) packetLen}));
}
} while ((option & Constants.BGP_FLOW_SPEC_END_OF_LIST_MASK) == 0);
return new BgpFsPacketLength(operatorValue);
}
@Override
......
......@@ -15,6 +15,7 @@
*/
package org.onosproject.bgpio.types;
import java.util.LinkedList;
import java.util.Objects;
import java.util.List;
import org.jboss.netty.buffer.ChannelBuffer;
......@@ -78,12 +79,29 @@ public class BgpFsPortNum implements BgpValueType {
* Reads the channel buffer and returns object.
*
* @param cb channelBuffer
* @param type address type
* @return object of flow spec port number
* @throws BgpParseException while parsing BgpFsPortNum
*/
public static BgpFsPortNum read(ChannelBuffer cb, short type) throws BgpParseException {
return null;
public static BgpFsPortNum read(ChannelBuffer cb) throws BgpParseException {
List<BgpFsOperatorValue> operatorValue = new LinkedList<>();
byte option;
short port;
do {
option = (byte) cb.readByte();
int len = (option & Constants.BGP_FLOW_SPEC_LEN_MASK) >> 4;
if ((1 << len) == 1) {
port = cb.readByte();
operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) port}));
} else {
port = cb.readShort();
operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) (port >> 8), (byte) port}));
}
} while ((option & Constants.BGP_FLOW_SPEC_END_OF_LIST_MASK) == 0);
return new BgpFsPortNum(operatorValue);
}
@Override
......
......@@ -15,6 +15,7 @@
*/
package org.onosproject.bgpio.types;
import java.util.LinkedList;
import java.util.Objects;
import java.util.List;
import org.jboss.netty.buffer.ChannelBuffer;
......@@ -79,12 +80,27 @@ public class BgpFsSourcePortNum implements BgpValueType {
* Reads the channel buffer and returns object.
*
* @param cb channelBuffer
* @param type address type
* @return object of flow spec source port number
* @throws BgpParseException while parsing BgpFsSourcePortNum
*/
public static BgpFsSourcePortNum read(ChannelBuffer cb, short type) throws BgpParseException {
return null;
public static BgpFsSourcePortNum read(ChannelBuffer cb) throws BgpParseException {
List<BgpFsOperatorValue> operatorValue = new LinkedList<>();
byte option;
short port;
do {
option = (byte) cb.readByte();
int len = (option & Constants.BGP_FLOW_SPEC_LEN_MASK) >> 4;
if ((1 << len) == 1) {
port = cb.readByte();
operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) port}));
} else {
port = cb.readShort();
operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) (port >> 8), (byte) port}));
}
} while ((option & Constants.BGP_FLOW_SPEC_END_OF_LIST_MASK) == 0);
return new BgpFsSourcePortNum(operatorValue);
}
@Override
......
......@@ -22,6 +22,7 @@ import org.jboss.netty.buffer.ChannelBuffer;
import org.onlab.packet.IpPrefix;
import org.onosproject.bgpio.exceptions.BgpParseException;
import org.onosproject.bgpio.util.Constants;
import org.onosproject.bgpio.util.Validation;
import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
......@@ -34,7 +35,7 @@ public class BgpFsSourcePrefix implements BgpValueType {
public static final byte FLOW_SPEC_TYPE = Constants.BGP_FLOWSPEC_SRC_PREFIX;
private byte length;
private IpPrefix ipPrefix;
public static final int BYTE_IN_BITS = 8;
/**
* Constructor to initialize parameters.
*
......@@ -90,12 +91,33 @@ public class BgpFsSourcePrefix implements BgpValueType {
* Reads the channel buffer and returns object of IPv4AddressTlv.
*
* @param cb channelBuffer
* @param type address type
* @return object of flow spec source prefix
* @throws BgpParseException while parsing BgpFsSourcePrefix
*/
public static BgpFsSourcePrefix read(ChannelBuffer cb, short type) throws BgpParseException {
return null;
public static BgpFsSourcePrefix read(ChannelBuffer cb) throws BgpParseException {
IpPrefix ipPrefix;
int length = cb.readByte();
if (length == 0) {
byte[] prefix = new byte[] {0};
ipPrefix = Validation.bytesToPrefix(prefix, length);
return new BgpFsSourcePrefix((byte) ipPrefix.prefixLength(), ipPrefix);
}
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);
return new BgpFsSourcePrefix((byte) ipPrefix.prefixLength(), ipPrefix);
}
/**
......
......@@ -15,6 +15,7 @@
*/
package org.onosproject.bgpio.types;
import java.util.LinkedList;
import java.util.Objects;
import java.util.List;
import org.jboss.netty.buffer.ChannelBuffer;
......@@ -78,12 +79,28 @@ public class BgpFsTcpFlags implements BgpValueType {
* Reads the channel buffer and returns object.
*
* @param cb channelBuffer
* @param type address type
* @return object of flow spec TCP flags
* @throws BgpParseException while parsing BgpFsTcpFlags
*/
public static BgpFsTcpFlags read(ChannelBuffer cb, short type) throws BgpParseException {
return null;
public static BgpFsTcpFlags read(ChannelBuffer cb) throws BgpParseException {
List<BgpFsOperatorValue> operatorValue = new LinkedList<>();
byte option;
short tcpFlag;
do {
option = (byte) cb.readByte();
int len = (option & Constants.BGP_FLOW_SPEC_LEN_MASK) >> 4;
if ((1 << len) == 1) {
tcpFlag = cb.readByte();
operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) tcpFlag}));
} else {
tcpFlag = cb.readShort();
operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) (tcpFlag >> 8), (byte) tcpFlag}));
}
} while ((option & Constants.BGP_FLOW_SPEC_END_OF_LIST_MASK) == 0);
return new BgpFsTcpFlags(operatorValue);
}
@Override
......
......@@ -32,7 +32,7 @@ import com.google.common.base.Preconditions;
*/
public class NextHop implements BgpValueType {
public static final byte NEXTHOP_TYPE = 3;
public static final byte FLAGS = (byte) 0x40;
private boolean isNextHop = false;
private Ip4Address nextHop;
......@@ -47,6 +47,14 @@ public class NextHop implements BgpValueType {
}
/**
* Constructor to initialize default parameters.
*
*/
public NextHop() {
this.nextHop = null;
}
/**
* Returns whether next hop is present.
*
* @return whether next hop is present
......@@ -103,8 +111,16 @@ public class NextHop implements BgpValueType {
@Override
public int write(ChannelBuffer cb) {
//Not required to be implemented now
return 0;
int iLenStartIndex = cb.writerIndex();
cb.writeByte(FLAGS);
cb.writeByte(getType());
if (!isNextHopSet()) {
cb.writeByte(0);
} else {
cb.writeInt(nextHop.toInt());
}
return cb.writerIndex() - iLenStartIndex;
}
@Override
......
......@@ -68,4 +68,6 @@ public final class Constants {
public static final short BGP_FLOWSPEC_ACTION_TRAFFIC_REDIRECT = (short) 0x8008;
public static final short BGP_FLOWSPEC_ACTION_TRAFFIC_MARKING = (short) 0x8009;
public static final byte BGP_FLOW_SPEC_LEN_MASK = 0x30;
public static final byte BGP_FLOW_SPEC_END_OF_LIST_MASK = (byte) 0x80;
}
\ No newline at end of file
......