Committed by
Gerrit Code Review
[ONOS-3857] BGP update message flow specification components parsing.
Change-Id: I9b58cdf2c47cb736a8119433cbc7911d871a83d1
Showing
14 changed files
with
243 additions
and
40 deletions
| ... | @@ -15,6 +15,7 @@ | ... | @@ -15,6 +15,7 @@ |
| 15 | */ | 15 | */ |
| 16 | package org.onosproject.bgpio.types; | 16 | package org.onosproject.bgpio.types; |
| 17 | 17 | ||
| 18 | +import java.util.LinkedList; | ||
| 18 | import java.util.Objects; | 19 | import java.util.Objects; |
| 19 | import java.util.List; | 20 | import java.util.List; |
| 20 | import org.jboss.netty.buffer.ChannelBuffer; | 21 | import org.jboss.netty.buffer.ChannelBuffer; |
| ... | @@ -78,12 +79,34 @@ public class BgpFsDestinationPortNum implements BgpValueType { | ... | @@ -78,12 +79,34 @@ public class BgpFsDestinationPortNum implements BgpValueType { |
| 78 | * Reads the channel buffer and returns object. | 79 | * Reads the channel buffer and returns object. |
| 79 | * | 80 | * |
| 80 | * @param cb channelBuffer | 81 | * @param cb channelBuffer |
| 81 | - * @param type address type | ||
| 82 | * @return object of flow spec destination port number | 82 | * @return object of flow spec destination port number |
| 83 | * @throws BgpParseException while parsing BgpFsDestinationPortNum | 83 | * @throws BgpParseException while parsing BgpFsDestinationPortNum |
| 84 | */ | 84 | */ |
| 85 | - public static BgpFsDestinationPortNum read(ChannelBuffer cb, short type) throws BgpParseException { | 85 | + public static BgpFsDestinationPortNum read(ChannelBuffer cb) throws BgpParseException { |
| 86 | - return null; | 86 | + List<BgpFsOperatorValue> operatorValue = new LinkedList<>(); |
| 87 | + byte option; | ||
| 88 | + short port; | ||
| 89 | + | ||
| 90 | + /* | ||
| 91 | + 0 1 2 3 4 5 6 7 | ||
| 92 | + +---+---+---+---+---+---+---+----+ | ||
| 93 | + | e | a | len | 0 |lt |gt |eq | | ||
| 94 | + +---+---+---+---+---+---+---+----+ | ||
| 95 | + */ | ||
| 96 | + | ||
| 97 | + do { | ||
| 98 | + option = (byte) cb.readByte(); | ||
| 99 | + int len = (option & Constants.BGP_FLOW_SPEC_LEN_MASK) >> 4; | ||
| 100 | + if ((1 << len) == 1) { | ||
| 101 | + port = cb.readByte(); | ||
| 102 | + operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) port})); | ||
| 103 | + } else { | ||
| 104 | + port = cb.readShort(); | ||
| 105 | + operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) (port >> 8), (byte) port})); | ||
| 106 | + } | ||
| 107 | + } while ((option & Constants.BGP_FLOW_SPEC_END_OF_LIST_MASK) == 0); | ||
| 108 | + | ||
| 109 | + return new BgpFsDestinationPortNum(operatorValue); | ||
| 87 | } | 110 | } |
| 88 | 111 | ||
| 89 | @Override | 112 | @Override | ... | ... |
| ... | @@ -22,6 +22,8 @@ import org.jboss.netty.buffer.ChannelBuffer; | ... | @@ -22,6 +22,8 @@ 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.util.Constants; | 24 | import org.onosproject.bgpio.util.Constants; |
| 25 | +import org.onosproject.bgpio.util.Validation; | ||
| 26 | + | ||
| 25 | import com.google.common.base.MoreObjects; | 27 | import com.google.common.base.MoreObjects; |
| 26 | import com.google.common.base.Preconditions; | 28 | import com.google.common.base.Preconditions; |
| 27 | 29 | ||
| ... | @@ -33,6 +35,7 @@ public class BgpFsDestinationPrefix implements BgpValueType { | ... | @@ -33,6 +35,7 @@ public class BgpFsDestinationPrefix implements BgpValueType { |
| 33 | public static final byte FLOW_SPEC_TYPE = Constants.BGP_FLOWSPEC_DST_PREFIX; | 35 | public static final byte FLOW_SPEC_TYPE = Constants.BGP_FLOWSPEC_DST_PREFIX; |
| 34 | private byte length; | 36 | private byte length; |
| 35 | private IpPrefix ipPrefix; | 37 | private IpPrefix ipPrefix; |
| 38 | + public static final int BYTE_IN_BITS = 8; | ||
| 36 | 39 | ||
| 37 | /** | 40 | /** |
| 38 | * Constructor to initialize parameters. | 41 | * Constructor to initialize parameters. |
| ... | @@ -89,12 +92,32 @@ public class BgpFsDestinationPrefix implements BgpValueType { | ... | @@ -89,12 +92,32 @@ public class BgpFsDestinationPrefix implements BgpValueType { |
| 89 | * Reads the channel buffer and returns object of IPv4AddressTlv. | 92 | * Reads the channel buffer and returns object of IPv4AddressTlv. |
| 90 | * | 93 | * |
| 91 | * @param cb channelBuffer | 94 | * @param cb channelBuffer |
| 92 | - * @param type address type | ||
| 93 | * @return object of flow spec destination prefix | 95 | * @return object of flow spec destination prefix |
| 94 | * @throws BgpParseException while parsing BgpFsDestinationPrefix | 96 | * @throws BgpParseException while parsing BgpFsDestinationPrefix |
| 95 | */ | 97 | */ |
| 96 | - public static BgpFsDestinationPrefix read(ChannelBuffer cb, short type) throws BgpParseException { | 98 | + public static BgpFsDestinationPrefix read(ChannelBuffer cb) throws BgpParseException { |
| 97 | - return null; | 99 | + IpPrefix ipPrefix; |
| 100 | + | ||
| 101 | + int length = cb.readByte(); | ||
| 102 | + if (length == 0) { | ||
| 103 | + byte[] prefix = new byte[] {0}; | ||
| 104 | + ipPrefix = Validation.bytesToPrefix(prefix, length); | ||
| 105 | + return new BgpFsDestinationPrefix((byte) ipPrefix.prefixLength(), ipPrefix); | ||
| 106 | + } | ||
| 107 | + int len = length / BYTE_IN_BITS; | ||
| 108 | + int reminder = length % BYTE_IN_BITS; | ||
| 109 | + if (reminder > 0) { | ||
| 110 | + len = len + 1; | ||
| 111 | + } | ||
| 112 | + if (cb.readableBytes() < len) { | ||
| 113 | + Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, | ||
| 114 | + BgpErrorType.MALFORMED_ATTRIBUTE_LIST, cb.readableBytes()); | ||
| 115 | + } | ||
| 116 | + byte[] prefix = new byte[len]; | ||
| 117 | + cb.readBytes(prefix, 0, len); | ||
| 118 | + ipPrefix = Validation.bytesToPrefix(prefix, length); | ||
| 119 | + | ||
| 120 | + return new BgpFsDestinationPrefix((byte) ipPrefix.prefixLength(), ipPrefix); | ||
| 98 | } | 121 | } |
| 99 | 122 | ||
| 100 | /** | 123 | /** | ... | ... |
| ... | @@ -15,6 +15,7 @@ | ... | @@ -15,6 +15,7 @@ |
| 15 | */ | 15 | */ |
| 16 | package org.onosproject.bgpio.types; | 16 | package org.onosproject.bgpio.types; |
| 17 | 17 | ||
| 18 | +import java.util.LinkedList; | ||
| 18 | import java.util.Objects; | 19 | import java.util.Objects; |
| 19 | import java.util.List; | 20 | import java.util.List; |
| 20 | import org.jboss.netty.buffer.ChannelBuffer; | 21 | import org.jboss.netty.buffer.ChannelBuffer; |
| ... | @@ -79,12 +80,20 @@ public class BgpFsDscpValue implements BgpValueType { | ... | @@ -79,12 +80,20 @@ public class BgpFsDscpValue implements BgpValueType { |
| 79 | * Reads the channel buffer and returns object. | 80 | * Reads the channel buffer and returns object. |
| 80 | * | 81 | * |
| 81 | * @param cb channelBuffer | 82 | * @param cb channelBuffer |
| 82 | - * @param type address type | ||
| 83 | * @return object of flow spec DSCP value | 83 | * @return object of flow spec DSCP value |
| 84 | * @throws BgpParseException while parsing BgpFsDscpValue | 84 | * @throws BgpParseException while parsing BgpFsDscpValue |
| 85 | */ | 85 | */ |
| 86 | - public static BgpFsDscpValue read(ChannelBuffer cb, short type) throws BgpParseException { | 86 | + public static BgpFsDscpValue read(ChannelBuffer cb) throws BgpParseException { |
| 87 | - return null; | 87 | + List<BgpFsOperatorValue> operatorValue = new LinkedList<>(); |
| 88 | + byte option; | ||
| 89 | + byte dscpValue; | ||
| 90 | + | ||
| 91 | + do { | ||
| 92 | + option = (byte) cb.readByte(); | ||
| 93 | + dscpValue = cb.readByte(); | ||
| 94 | + operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) dscpValue})); | ||
| 95 | + } while ((option & Constants.BGP_FLOW_SPEC_END_OF_LIST_MASK) == 0); | ||
| 96 | + return new BgpFsDscpValue(operatorValue); | ||
| 88 | } | 97 | } |
| 89 | 98 | ||
| 90 | @Override | 99 | @Override | ... | ... |
| ... | @@ -15,6 +15,7 @@ | ... | @@ -15,6 +15,7 @@ |
| 15 | */ | 15 | */ |
| 16 | package org.onosproject.bgpio.types; | 16 | package org.onosproject.bgpio.types; |
| 17 | 17 | ||
| 18 | +import java.util.LinkedList; | ||
| 18 | import java.util.Objects; | 19 | import java.util.Objects; |
| 19 | import java.util.List; | 20 | import java.util.List; |
| 20 | import org.jboss.netty.buffer.ChannelBuffer; | 21 | import org.jboss.netty.buffer.ChannelBuffer; |
| ... | @@ -79,12 +80,21 @@ public class BgpFsFragment implements BgpValueType { | ... | @@ -79,12 +80,21 @@ public class BgpFsFragment implements BgpValueType { |
| 79 | * Reads the channel buffer and returns object. | 80 | * Reads the channel buffer and returns object. |
| 80 | * | 81 | * |
| 81 | * @param cb channelBuffer | 82 | * @param cb channelBuffer |
| 82 | - * @param type address type | ||
| 83 | * @return object of flow spec fragment | 83 | * @return object of flow spec fragment |
| 84 | * @throws BgpParseException while parsing BgpFsFragment | 84 | * @throws BgpParseException while parsing BgpFsFragment |
| 85 | */ | 85 | */ |
| 86 | - public static BgpFsFragment read(ChannelBuffer cb, short type) throws BgpParseException { | 86 | + public static BgpFsFragment read(ChannelBuffer cb) throws BgpParseException { |
| 87 | - return null; | 87 | + List<BgpFsOperatorValue> operatorValue = new LinkedList<>(); |
| 88 | + byte option; | ||
| 89 | + byte fragment; | ||
| 90 | + | ||
| 91 | + do { | ||
| 92 | + option = (byte) cb.readByte(); | ||
| 93 | + fragment = cb.readByte(); | ||
| 94 | + operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) fragment})); | ||
| 95 | + } while ((option & Constants.BGP_FLOW_SPEC_END_OF_LIST_MASK) == 0); | ||
| 96 | + | ||
| 97 | + return new BgpFsFragment(operatorValue); | ||
| 88 | } | 98 | } |
| 89 | 99 | ||
| 90 | @Override | 100 | @Override | ... | ... |
| ... | @@ -15,6 +15,7 @@ | ... | @@ -15,6 +15,7 @@ |
| 15 | */ | 15 | */ |
| 16 | package org.onosproject.bgpio.types; | 16 | package org.onosproject.bgpio.types; |
| 17 | 17 | ||
| 18 | +import java.util.LinkedList; | ||
| 18 | import java.util.Objects; | 19 | import java.util.Objects; |
| 19 | 20 | ||
| 20 | import org.jboss.netty.buffer.ChannelBuffer; | 21 | import org.jboss.netty.buffer.ChannelBuffer; |
| ... | @@ -79,12 +80,21 @@ public class BgpFsIcmpCode implements BgpValueType { | ... | @@ -79,12 +80,21 @@ public class BgpFsIcmpCode implements BgpValueType { |
| 79 | * Reads the channel buffer and returns object. | 80 | * Reads the channel buffer and returns object. |
| 80 | * | 81 | * |
| 81 | * @param cb channelBuffer | 82 | * @param cb channelBuffer |
| 82 | - * @param type address type | ||
| 83 | * @return object of flow spec ICMP code | 83 | * @return object of flow spec ICMP code |
| 84 | * @throws BgpParseException while parsing BgpFsIcmpCode | 84 | * @throws BgpParseException while parsing BgpFsIcmpCode |
| 85 | */ | 85 | */ |
| 86 | - public static BgpFsIcmpCode read(ChannelBuffer cb, short type) throws BgpParseException { | 86 | + public static BgpFsIcmpCode read(ChannelBuffer cb) throws BgpParseException { |
| 87 | - return null; | 87 | + List<BgpFsOperatorValue> operatorValue = new LinkedList<>(); |
| 88 | + byte option; | ||
| 89 | + byte icmpCode; | ||
| 90 | + | ||
| 91 | + do { | ||
| 92 | + option = (byte) cb.readByte(); | ||
| 93 | + icmpCode = cb.readByte(); | ||
| 94 | + operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) icmpCode})); | ||
| 95 | + } while ((option & Constants.BGP_FLOW_SPEC_END_OF_LIST_MASK) == 0); | ||
| 96 | + | ||
| 97 | + return new BgpFsIcmpCode(operatorValue); | ||
| 88 | } | 98 | } |
| 89 | 99 | ||
| 90 | @Override | 100 | @Override | ... | ... |
| ... | @@ -15,6 +15,7 @@ | ... | @@ -15,6 +15,7 @@ |
| 15 | */ | 15 | */ |
| 16 | package org.onosproject.bgpio.types; | 16 | package org.onosproject.bgpio.types; |
| 17 | 17 | ||
| 18 | +import java.util.LinkedList; | ||
| 18 | import java.util.Objects; | 19 | import java.util.Objects; |
| 19 | 20 | ||
| 20 | import org.jboss.netty.buffer.ChannelBuffer; | 21 | import org.jboss.netty.buffer.ChannelBuffer; |
| ... | @@ -78,12 +79,21 @@ public class BgpFsIcmpType implements BgpValueType { | ... | @@ -78,12 +79,21 @@ public class BgpFsIcmpType implements BgpValueType { |
| 78 | * Reads the channel buffer and returns object. | 79 | * Reads the channel buffer and returns object. |
| 79 | * | 80 | * |
| 80 | * @param cb channelBuffer | 81 | * @param cb channelBuffer |
| 81 | - * @param type address type | ||
| 82 | * @return object of flow spec ICMP type | 82 | * @return object of flow spec ICMP type |
| 83 | * @throws BgpParseException while parsing BgpFsIcmpType | 83 | * @throws BgpParseException while parsing BgpFsIcmpType |
| 84 | */ | 84 | */ |
| 85 | - public static BgpFsIcmpType read(ChannelBuffer cb, short type) throws BgpParseException { | 85 | + public static BgpFsIcmpType read(ChannelBuffer cb) throws BgpParseException { |
| 86 | - return null; | 86 | + List<BgpFsOperatorValue> operatorValue = new LinkedList<>(); |
| 87 | + byte option; | ||
| 88 | + byte icmpType; | ||
| 89 | + | ||
| 90 | + do { | ||
| 91 | + option = (byte) cb.readByte(); | ||
| 92 | + icmpType = cb.readByte(); | ||
| 93 | + operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) icmpType})); | ||
| 94 | + } while ((option & Constants.BGP_FLOW_SPEC_END_OF_LIST_MASK) == 0); | ||
| 95 | + | ||
| 96 | + return new BgpFsIcmpType(operatorValue); | ||
| 87 | } | 97 | } |
| 88 | 98 | ||
| 89 | @Override | 99 | @Override | ... | ... |
| ... | @@ -15,6 +15,7 @@ | ... | @@ -15,6 +15,7 @@ |
| 15 | */ | 15 | */ |
| 16 | package org.onosproject.bgpio.types; | 16 | package org.onosproject.bgpio.types; |
| 17 | 17 | ||
| 18 | +import java.util.LinkedList; | ||
| 18 | import java.util.List; | 19 | import java.util.List; |
| 19 | import java.util.Objects; | 20 | import java.util.Objects; |
| 20 | 21 | ||
| ... | @@ -89,12 +90,21 @@ public class BgpFsIpProtocol implements BgpValueType { | ... | @@ -89,12 +90,21 @@ public class BgpFsIpProtocol implements BgpValueType { |
| 89 | * Reads the channel buffer and returns object. | 90 | * Reads the channel buffer and returns object. |
| 90 | * | 91 | * |
| 91 | * @param cb channelBuffer | 92 | * @param cb channelBuffer |
| 92 | - * @param type address type | ||
| 93 | * @return object of flow spec IP protocol | 93 | * @return object of flow spec IP protocol |
| 94 | * @throws BgpParseException while parsing BgpFsIpProtocol | 94 | * @throws BgpParseException while parsing BgpFsIpProtocol |
| 95 | */ | 95 | */ |
| 96 | - public static BgpFsIpProtocol read(ChannelBuffer cb, short type) throws BgpParseException { | 96 | + public static BgpFsIpProtocol read(ChannelBuffer cb) throws BgpParseException { |
| 97 | - return null; | 97 | + List<BgpFsOperatorValue> operatorValue = new LinkedList<>(); |
| 98 | + byte option; | ||
| 99 | + byte proto; | ||
| 100 | + | ||
| 101 | + do { | ||
| 102 | + option = (byte) cb.readByte(); | ||
| 103 | + proto = cb.readByte(); | ||
| 104 | + operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) proto})); | ||
| 105 | + } while ((option & Constants.BGP_FLOW_SPEC_END_OF_LIST_MASK) == 0); | ||
| 106 | + | ||
| 107 | + return new BgpFsIpProtocol(operatorValue); | ||
| 98 | } | 108 | } |
| 99 | 109 | ||
| 100 | @Override | 110 | @Override | ... | ... |
| ... | @@ -15,6 +15,7 @@ | ... | @@ -15,6 +15,7 @@ |
| 15 | */ | 15 | */ |
| 16 | package org.onosproject.bgpio.types; | 16 | package org.onosproject.bgpio.types; |
| 17 | 17 | ||
| 18 | +import java.util.LinkedList; | ||
| 18 | import java.util.Objects; | 19 | import java.util.Objects; |
| 19 | import java.util.List; | 20 | import java.util.List; |
| 20 | import org.jboss.netty.buffer.ChannelBuffer; | 21 | import org.jboss.netty.buffer.ChannelBuffer; |
| ... | @@ -79,12 +80,28 @@ public class BgpFsPacketLength implements BgpValueType { | ... | @@ -79,12 +80,28 @@ public class BgpFsPacketLength implements BgpValueType { |
| 79 | * Reads the channel buffer and returns object. | 80 | * Reads the channel buffer and returns object. |
| 80 | * | 81 | * |
| 81 | * @param cb channelBuffer | 82 | * @param cb channelBuffer |
| 82 | - * @param type address type | ||
| 83 | * @return object of flow spec packet length | 83 | * @return object of flow spec packet length |
| 84 | * @throws BgpParseException while parsing BgpFsPacketLength | 84 | * @throws BgpParseException while parsing BgpFsPacketLength |
| 85 | */ | 85 | */ |
| 86 | - public static BgpFsPacketLength read(ChannelBuffer cb, short type) throws BgpParseException { | 86 | + public static BgpFsPacketLength read(ChannelBuffer cb) throws BgpParseException { |
| 87 | - return null; | 87 | + List<BgpFsOperatorValue> operatorValue = new LinkedList<>(); |
| 88 | + byte option; | ||
| 89 | + short packetLen; | ||
| 90 | + | ||
| 91 | + do { | ||
| 92 | + option = (byte) cb.readByte(); | ||
| 93 | + int len = (option & Constants.BGP_FLOW_SPEC_LEN_MASK) >> 4; | ||
| 94 | + if ((1 << len) == 1) { | ||
| 95 | + packetLen = cb.readByte(); | ||
| 96 | + operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) packetLen})); | ||
| 97 | + } else { | ||
| 98 | + packetLen = cb.readShort(); | ||
| 99 | + operatorValue.add(new BgpFsOperatorValue(option, | ||
| 100 | + new byte[] {(byte) (packetLen >> 8), (byte) packetLen})); | ||
| 101 | + } | ||
| 102 | + } while ((option & Constants.BGP_FLOW_SPEC_END_OF_LIST_MASK) == 0); | ||
| 103 | + | ||
| 104 | + return new BgpFsPacketLength(operatorValue); | ||
| 88 | } | 105 | } |
| 89 | 106 | ||
| 90 | @Override | 107 | @Override | ... | ... |
| ... | @@ -15,6 +15,7 @@ | ... | @@ -15,6 +15,7 @@ |
| 15 | */ | 15 | */ |
| 16 | package org.onosproject.bgpio.types; | 16 | package org.onosproject.bgpio.types; |
| 17 | 17 | ||
| 18 | +import java.util.LinkedList; | ||
| 18 | import java.util.Objects; | 19 | import java.util.Objects; |
| 19 | import java.util.List; | 20 | import java.util.List; |
| 20 | import org.jboss.netty.buffer.ChannelBuffer; | 21 | import org.jboss.netty.buffer.ChannelBuffer; |
| ... | @@ -78,12 +79,29 @@ public class BgpFsPortNum implements BgpValueType { | ... | @@ -78,12 +79,29 @@ public class BgpFsPortNum implements BgpValueType { |
| 78 | * Reads the channel buffer and returns object. | 79 | * Reads the channel buffer and returns object. |
| 79 | * | 80 | * |
| 80 | * @param cb channelBuffer | 81 | * @param cb channelBuffer |
| 81 | - * @param type address type | ||
| 82 | * @return object of flow spec port number | 82 | * @return object of flow spec port number |
| 83 | * @throws BgpParseException while parsing BgpFsPortNum | 83 | * @throws BgpParseException while parsing BgpFsPortNum |
| 84 | */ | 84 | */ |
| 85 | - public static BgpFsPortNum read(ChannelBuffer cb, short type) throws BgpParseException { | 85 | + public static BgpFsPortNum read(ChannelBuffer cb) throws BgpParseException { |
| 86 | - return null; | 86 | + List<BgpFsOperatorValue> operatorValue = new LinkedList<>(); |
| 87 | + byte option; | ||
| 88 | + short port; | ||
| 89 | + | ||
| 90 | + do { | ||
| 91 | + option = (byte) cb.readByte(); | ||
| 92 | + int len = (option & Constants.BGP_FLOW_SPEC_LEN_MASK) >> 4; | ||
| 93 | + if ((1 << len) == 1) { | ||
| 94 | + port = cb.readByte(); | ||
| 95 | + operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) port})); | ||
| 96 | + } else { | ||
| 97 | + port = cb.readShort(); | ||
| 98 | + operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) (port >> 8), (byte) port})); | ||
| 99 | + } | ||
| 100 | + | ||
| 101 | + | ||
| 102 | + } while ((option & Constants.BGP_FLOW_SPEC_END_OF_LIST_MASK) == 0); | ||
| 103 | + | ||
| 104 | + return new BgpFsPortNum(operatorValue); | ||
| 87 | } | 105 | } |
| 88 | 106 | ||
| 89 | @Override | 107 | @Override | ... | ... |
| ... | @@ -15,6 +15,7 @@ | ... | @@ -15,6 +15,7 @@ |
| 15 | */ | 15 | */ |
| 16 | package org.onosproject.bgpio.types; | 16 | package org.onosproject.bgpio.types; |
| 17 | 17 | ||
| 18 | +import java.util.LinkedList; | ||
| 18 | import java.util.Objects; | 19 | import java.util.Objects; |
| 19 | import java.util.List; | 20 | import java.util.List; |
| 20 | import org.jboss.netty.buffer.ChannelBuffer; | 21 | import org.jboss.netty.buffer.ChannelBuffer; |
| ... | @@ -79,12 +80,27 @@ public class BgpFsSourcePortNum implements BgpValueType { | ... | @@ -79,12 +80,27 @@ public class BgpFsSourcePortNum implements BgpValueType { |
| 79 | * Reads the channel buffer and returns object. | 80 | * Reads the channel buffer and returns object. |
| 80 | * | 81 | * |
| 81 | * @param cb channelBuffer | 82 | * @param cb channelBuffer |
| 82 | - * @param type address type | ||
| 83 | * @return object of flow spec source port number | 83 | * @return object of flow spec source port number |
| 84 | * @throws BgpParseException while parsing BgpFsSourcePortNum | 84 | * @throws BgpParseException while parsing BgpFsSourcePortNum |
| 85 | */ | 85 | */ |
| 86 | - public static BgpFsSourcePortNum read(ChannelBuffer cb, short type) throws BgpParseException { | 86 | + public static BgpFsSourcePortNum read(ChannelBuffer cb) throws BgpParseException { |
| 87 | - return null; | 87 | + List<BgpFsOperatorValue> operatorValue = new LinkedList<>(); |
| 88 | + byte option; | ||
| 89 | + short port; | ||
| 90 | + | ||
| 91 | + do { | ||
| 92 | + option = (byte) cb.readByte(); | ||
| 93 | + int len = (option & Constants.BGP_FLOW_SPEC_LEN_MASK) >> 4; | ||
| 94 | + if ((1 << len) == 1) { | ||
| 95 | + port = cb.readByte(); | ||
| 96 | + operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) port})); | ||
| 97 | + } else { | ||
| 98 | + port = cb.readShort(); | ||
| 99 | + operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) (port >> 8), (byte) port})); | ||
| 100 | + } | ||
| 101 | + } while ((option & Constants.BGP_FLOW_SPEC_END_OF_LIST_MASK) == 0); | ||
| 102 | + | ||
| 103 | + return new BgpFsSourcePortNum(operatorValue); | ||
| 88 | } | 104 | } |
| 89 | 105 | ||
| 90 | @Override | 106 | @Override | ... | ... |
| ... | @@ -22,6 +22,7 @@ import org.jboss.netty.buffer.ChannelBuffer; | ... | @@ -22,6 +22,7 @@ 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.util.Constants; | 24 | import org.onosproject.bgpio.util.Constants; |
| 25 | +import org.onosproject.bgpio.util.Validation; | ||
| 25 | 26 | ||
| 26 | import com.google.common.base.MoreObjects; | 27 | import com.google.common.base.MoreObjects; |
| 27 | import com.google.common.base.Preconditions; | 28 | import com.google.common.base.Preconditions; |
| ... | @@ -34,7 +35,7 @@ public class BgpFsSourcePrefix implements BgpValueType { | ... | @@ -34,7 +35,7 @@ public class BgpFsSourcePrefix implements BgpValueType { |
| 34 | public static final byte FLOW_SPEC_TYPE = Constants.BGP_FLOWSPEC_SRC_PREFIX; | 35 | public static final byte FLOW_SPEC_TYPE = Constants.BGP_FLOWSPEC_SRC_PREFIX; |
| 35 | private byte length; | 36 | private byte length; |
| 36 | private IpPrefix ipPrefix; | 37 | private IpPrefix ipPrefix; |
| 37 | - | 38 | + public static final int BYTE_IN_BITS = 8; |
| 38 | /** | 39 | /** |
| 39 | * Constructor to initialize parameters. | 40 | * Constructor to initialize parameters. |
| 40 | * | 41 | * |
| ... | @@ -90,12 +91,33 @@ public class BgpFsSourcePrefix implements BgpValueType { | ... | @@ -90,12 +91,33 @@ public class BgpFsSourcePrefix implements BgpValueType { |
| 90 | * Reads the channel buffer and returns object of IPv4AddressTlv. | 91 | * Reads the channel buffer and returns object of IPv4AddressTlv. |
| 91 | * | 92 | * |
| 92 | * @param cb channelBuffer | 93 | * @param cb channelBuffer |
| 93 | - * @param type address type | ||
| 94 | * @return object of flow spec source prefix | 94 | * @return object of flow spec source prefix |
| 95 | * @throws BgpParseException while parsing BgpFsSourcePrefix | 95 | * @throws BgpParseException while parsing BgpFsSourcePrefix |
| 96 | */ | 96 | */ |
| 97 | - public static BgpFsSourcePrefix read(ChannelBuffer cb, short type) throws BgpParseException { | 97 | + public static BgpFsSourcePrefix read(ChannelBuffer cb) throws BgpParseException { |
| 98 | - return null; | 98 | + IpPrefix ipPrefix; |
| 99 | + | ||
| 100 | + int length = cb.readByte(); | ||
| 101 | + if (length == 0) { | ||
| 102 | + byte[] prefix = new byte[] {0}; | ||
| 103 | + ipPrefix = Validation.bytesToPrefix(prefix, length); | ||
| 104 | + return new BgpFsSourcePrefix((byte) ipPrefix.prefixLength(), ipPrefix); | ||
| 105 | + } | ||
| 106 | + | ||
| 107 | + int len = length / BYTE_IN_BITS; | ||
| 108 | + int reminder = length % BYTE_IN_BITS; | ||
| 109 | + if (reminder > 0) { | ||
| 110 | + len = len + 1; | ||
| 111 | + } | ||
| 112 | + if (cb.readableBytes() < len) { | ||
| 113 | + Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, | ||
| 114 | + BgpErrorType.MALFORMED_ATTRIBUTE_LIST, cb.readableBytes()); | ||
| 115 | + } | ||
| 116 | + byte[] prefix = new byte[len]; | ||
| 117 | + cb.readBytes(prefix, 0, len); | ||
| 118 | + ipPrefix = Validation.bytesToPrefix(prefix, length); | ||
| 119 | + | ||
| 120 | + return new BgpFsSourcePrefix((byte) ipPrefix.prefixLength(), ipPrefix); | ||
| 99 | } | 121 | } |
| 100 | 122 | ||
| 101 | /** | 123 | /** | ... | ... |
| ... | @@ -15,6 +15,7 @@ | ... | @@ -15,6 +15,7 @@ |
| 15 | */ | 15 | */ |
| 16 | package org.onosproject.bgpio.types; | 16 | package org.onosproject.bgpio.types; |
| 17 | 17 | ||
| 18 | +import java.util.LinkedList; | ||
| 18 | import java.util.Objects; | 19 | import java.util.Objects; |
| 19 | import java.util.List; | 20 | import java.util.List; |
| 20 | import org.jboss.netty.buffer.ChannelBuffer; | 21 | import org.jboss.netty.buffer.ChannelBuffer; |
| ... | @@ -78,12 +79,28 @@ public class BgpFsTcpFlags implements BgpValueType { | ... | @@ -78,12 +79,28 @@ public class BgpFsTcpFlags implements BgpValueType { |
| 78 | * Reads the channel buffer and returns object. | 79 | * Reads the channel buffer and returns object. |
| 79 | * | 80 | * |
| 80 | * @param cb channelBuffer | 81 | * @param cb channelBuffer |
| 81 | - * @param type address type | ||
| 82 | * @return object of flow spec TCP flags | 82 | * @return object of flow spec TCP flags |
| 83 | * @throws BgpParseException while parsing BgpFsTcpFlags | 83 | * @throws BgpParseException while parsing BgpFsTcpFlags |
| 84 | */ | 84 | */ |
| 85 | - public static BgpFsTcpFlags read(ChannelBuffer cb, short type) throws BgpParseException { | 85 | + public static BgpFsTcpFlags read(ChannelBuffer cb) throws BgpParseException { |
| 86 | - return null; | 86 | + List<BgpFsOperatorValue> operatorValue = new LinkedList<>(); |
| 87 | + byte option; | ||
| 88 | + short tcpFlag; | ||
| 89 | + | ||
| 90 | + do { | ||
| 91 | + option = (byte) cb.readByte(); | ||
| 92 | + int len = (option & Constants.BGP_FLOW_SPEC_LEN_MASK) >> 4; | ||
| 93 | + if ((1 << len) == 1) { | ||
| 94 | + tcpFlag = cb.readByte(); | ||
| 95 | + operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) tcpFlag})); | ||
| 96 | + } else { | ||
| 97 | + tcpFlag = cb.readShort(); | ||
| 98 | + operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) (tcpFlag >> 8), (byte) tcpFlag})); | ||
| 99 | + } | ||
| 100 | + | ||
| 101 | + } while ((option & Constants.BGP_FLOW_SPEC_END_OF_LIST_MASK) == 0); | ||
| 102 | + | ||
| 103 | + return new BgpFsTcpFlags(operatorValue); | ||
| 87 | } | 104 | } |
| 88 | 105 | ||
| 89 | @Override | 106 | @Override | ... | ... |
| ... | @@ -32,7 +32,7 @@ import com.google.common.base.Preconditions; | ... | @@ -32,7 +32,7 @@ import com.google.common.base.Preconditions; |
| 32 | */ | 32 | */ |
| 33 | public class NextHop implements BgpValueType { | 33 | public class NextHop implements BgpValueType { |
| 34 | public static final byte NEXTHOP_TYPE = 3; | 34 | public static final byte NEXTHOP_TYPE = 3; |
| 35 | - | 35 | + public static final byte FLAGS = (byte) 0x40; |
| 36 | private boolean isNextHop = false; | 36 | private boolean isNextHop = false; |
| 37 | private Ip4Address nextHop; | 37 | private Ip4Address nextHop; |
| 38 | 38 | ||
| ... | @@ -47,6 +47,14 @@ public class NextHop implements BgpValueType { | ... | @@ -47,6 +47,14 @@ public class NextHop implements BgpValueType { |
| 47 | } | 47 | } |
| 48 | 48 | ||
| 49 | /** | 49 | /** |
| 50 | + * Constructor to initialize default parameters. | ||
| 51 | + * | ||
| 52 | + */ | ||
| 53 | + public NextHop() { | ||
| 54 | + this.nextHop = null; | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + /** | ||
| 50 | * Returns whether next hop is present. | 58 | * Returns whether next hop is present. |
| 51 | * | 59 | * |
| 52 | * @return whether next hop is present | 60 | * @return whether next hop is present |
| ... | @@ -103,8 +111,16 @@ public class NextHop implements BgpValueType { | ... | @@ -103,8 +111,16 @@ public class NextHop implements BgpValueType { |
| 103 | 111 | ||
| 104 | @Override | 112 | @Override |
| 105 | public int write(ChannelBuffer cb) { | 113 | public int write(ChannelBuffer cb) { |
| 106 | - //Not required to be implemented now | 114 | + int iLenStartIndex = cb.writerIndex(); |
| 107 | - return 0; | 115 | + cb.writeByte(FLAGS); |
| 116 | + cb.writeByte(getType()); | ||
| 117 | + if (!isNextHopSet()) { | ||
| 118 | + cb.writeByte(0); | ||
| 119 | + } else { | ||
| 120 | + cb.writeInt(nextHop.toInt()); | ||
| 121 | + } | ||
| 122 | + | ||
| 123 | + return cb.writerIndex() - iLenStartIndex; | ||
| 108 | } | 124 | } |
| 109 | 125 | ||
| 110 | @Override | 126 | @Override | ... | ... |
| ... | @@ -68,4 +68,6 @@ public final class Constants { | ... | @@ -68,4 +68,6 @@ public final class Constants { |
| 68 | public static final short BGP_FLOWSPEC_ACTION_TRAFFIC_REDIRECT = (short) 0x8008; | 68 | public static final short BGP_FLOWSPEC_ACTION_TRAFFIC_REDIRECT = (short) 0x8008; |
| 69 | public static final short BGP_FLOWSPEC_ACTION_TRAFFIC_MARKING = (short) 0x8009; | 69 | public static final short BGP_FLOWSPEC_ACTION_TRAFFIC_MARKING = (short) 0x8009; |
| 70 | 70 | ||
| 71 | + public static final byte BGP_FLOW_SPEC_LEN_MASK = 0x30; | ||
| 72 | + public static final byte BGP_FLOW_SPEC_END_OF_LIST_MASK = (byte) 0x80; | ||
| 71 | } | 73 | } |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or login to post a comment