Priyanka B
Committed by Gerrit Code Review

[ONOS-2613] Unit test the BGP Update message(LinkStateAttribute)

Change-Id: Id9545296612bed858459c8386368c66cb2159173
......@@ -34,9 +34,9 @@ public final class BgpId {
private final IpAddress ipAddress;
/**
* Private constructor.
* Constructor to initialize ipAddress.
*/
private BgpId(IpAddress ipAddress) {
public BgpId(IpAddress ipAddress) {
this.ipAddress = ipAddress;
}
......
......@@ -24,6 +24,7 @@ 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.LinkStateAttributes;
import org.onosproject.bgpio.types.LocalPref;
import org.onosproject.bgpio.types.Med;
import org.onosproject.bgpio.types.NextHop;
......@@ -54,7 +55,7 @@ public class BgpPathAttributes {
*/
protected static final Logger log = LoggerFactory.getLogger(BgpPathAttributes.class);
public static final int LINK_STATE_ATTRIBUTE_TYPE = 50;
public static final int LINK_STATE_ATTRIBUTE_TYPE = 29;
public static final int MPREACHNLRI_TYPE = 14;
public static final int MPUNREACHNLRI_TYPE = 15;
......@@ -139,7 +140,7 @@ public class BgpPathAttributes {
.isMpUnReachNlriSet();
break;
case LINK_STATE_ATTRIBUTE_TYPE:
//TODO: To be merged later
pathAttribute = LinkStateAttributes.read(cb);
break;
default:
//skip bytes for unsupported attribute types
......
......@@ -89,7 +89,7 @@ public class LinkStateAttributes implements BgpValueType {
public static final short ATTR_PREFIX_OSPF_FWD_ADDR = 1156;
public static final short ATTR_PREFIX_OPAQUE_ATTR = 1157;
public static final byte LINKSTATE_ATTRIB_TYPE = 50;
public static final byte LINKSTATE_ATTRIB_TYPE = 29;
public static final byte TYPE_AND_LEN = 4;
private boolean isLinkStateAttribute = false;
private List<BgpValueType> linkStateAttribList;
......@@ -139,14 +139,14 @@ public class LinkStateAttributes implements BgpValueType {
public static LinkStateAttributes read(ChannelBuffer cb)
throws BgpParseException {
ChannelBuffer tempBuf = cb;
ChannelBuffer tempBuf = cb.copy();
Validation parseFlags = Validation.parseAttributeHeader(cb);
int len = parseFlags.isShort() ? parseFlags.getLength() + TYPE_AND_LEN
: parseFlags.getLength() + 3;
ChannelBuffer data = tempBuf.readBytes(len);
if (!parseFlags.getFirstBit() || parseFlags.getSecondBit()
|| parseFlags.getThirdBit()) {
if (!parseFlags.getFirstBit() && parseFlags.getSecondBit()
&& parseFlags.getThirdBit()) {
throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR,
BgpErrorType.ATTRIBUTE_FLAGS_ERROR,
data);
......
......@@ -56,7 +56,7 @@ public final class BgpAttrNodeFlagBitTlv implements BgpValueType {
* @param bExternalBit External bit
* @param bAbrBit ABR Bit
*/
private BgpAttrNodeFlagBitTlv(boolean bOverloadBit, boolean bAttachedBit,
public BgpAttrNodeFlagBitTlv(boolean bOverloadBit, boolean bAttachedBit,
boolean bExternalBit, boolean bAbrBit) {
this.bOverloadBit = bOverloadBit;
this.bAttachedBit = bAttachedBit;
......
......@@ -21,9 +21,8 @@ import org.jboss.netty.buffer.ChannelBuffer;
import org.onosproject.bgpio.exceptions.BgpParseException;
import org.onosproject.bgpio.types.BgpErrorType;
import org.onosproject.bgpio.types.BgpValueType;
import org.onosproject.bgpio.util.Constants;
import org.onosproject.bgpio.util.Validation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.MoreObjects;
......@@ -31,70 +30,55 @@ import com.google.common.base.MoreObjects;
* Implements BGP link protection type attribute.
*/
public final class BgpLinkAttrProtectionType implements BgpValueType {
protected static final Logger log = LoggerFactory
.getLogger(BgpLinkAttrProtectionType.class);
public static final int ATTRLINK_PROTECTIONTYPE = 1093;
public static final int LINK_PROTECTION_LEN = 2;
public static final int EXTRA_TRAFFIC = 0x01;
public static final int UNPROTECTED = 0x02;
public static final int SHARED = 0x04;
public static final int DEDICATED_ONE_ISTO_ONE = 0x08;
public static final int DEDICATED_ONE_PLUS_ONE = 0x10;
public static final int ENHANCED = 0x20;
/* Link Protection type flags */
private final boolean bExtraTraffic;
private final boolean bUnprotected;
private final boolean bShared;
private final boolean bDedOneIstoOne;
private final boolean bDedOnePlusOne;
private final boolean bEnhanced;
private byte linkProtectionType;
/**
* Enum to provide Link protection types.
*/
public enum ProtectionType {
EXTRA_TRAFFIC(1), UNPROTECTED(2), SHARED(4), DEDICATED_ONE_ISTO_ONE(8),
DEDICATED_ONE_PLUS_ONE(0x10), ENHANCED(0x20), RESERVED(0x40);
int value;
/**
* Assign val with the value as the link protection type.
*
* @param val link protection
*/
ProtectionType(int val) {
value = val;
}
/**
* Returns value of link protection type.
*
* @return link protection type
*/
public byte type() {
return (byte) value;
}
}
/**
* Constructor to initialize the value.
*
* @param bExtraTraffic Extra Traffic
* @param bUnprotected Unprotected
* @param bShared Shared
* @param bDedOneIstoOne Dedicated 1:1
* @param bDedOnePlusOne Dedicated 1+1
* @param bEnhanced Enhanced
* @param linkProtectionType link protection type
*/
private BgpLinkAttrProtectionType(boolean bExtraTraffic,
boolean bUnprotected,
boolean bShared, boolean bDedOneIstoOne,
boolean bDedOnePlusOne, boolean bEnhanced) {
this.bExtraTraffic = bExtraTraffic;
this.bUnprotected = bUnprotected;
this.bShared = bShared;
this.bDedOneIstoOne = bDedOneIstoOne;
this.bDedOnePlusOne = bDedOnePlusOne;
this.bEnhanced = bEnhanced;
public BgpLinkAttrProtectionType(byte linkProtectionType) {
this.linkProtectionType = linkProtectionType;
}
/**
* Returns object of this class with specified values.
*
* @param bExtraTraffic Extra Traffic
* @param bUnprotected Unprotected
* @param bShared Shared
* @param bDedOneIstoOne Dedicated 1:1
* @param bDedOnePlusOne Dedicated 1+1
* @param bEnhanced Enhanced
* @param linkProtectionType link protection type
* @return object of BgpLinkAttrProtectionType
*/
public static BgpLinkAttrProtectionType of(boolean bExtraTraffic,
boolean bUnprotected,
boolean bShared,
boolean bDedOneIstoOne,
boolean bDedOnePlusOne,
boolean bEnhanced) {
return new BgpLinkAttrProtectionType(bExtraTraffic, bUnprotected,
bShared, bDedOneIstoOne,
bDedOnePlusOne, bEnhanced);
public static BgpLinkAttrProtectionType of(byte linkProtectionType) {
return new BgpLinkAttrProtectionType(linkProtectionType);
}
/**
......@@ -106,91 +90,43 @@ public final class BgpLinkAttrProtectionType implements BgpValueType {
*/
public static BgpLinkAttrProtectionType read(ChannelBuffer cb)
throws BgpParseException {
short linkProtectionType;
byte higherByte;
short lsAttrLength = cb.readShort();
boolean bExtraTraffic;
boolean bUnprotected;
boolean bShared;
boolean bDedOneIstoOne;
boolean bDedOnePlusOne;
boolean bEnhanced;
if ((lsAttrLength != LINK_PROTECTION_LEN)
|| (cb.readableBytes() < lsAttrLength)) {
Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
lsAttrLength);
if ((lsAttrLength != LINK_PROTECTION_LEN) || (cb.readableBytes() < lsAttrLength)) {
Validation
.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR, lsAttrLength);
}
linkProtectionType = cb.readShort();
higherByte = (byte) (linkProtectionType >> 8);
bExtraTraffic = ((higherByte & (byte) EXTRA_TRAFFIC) == EXTRA_TRAFFIC);
bUnprotected = ((higherByte & (byte) UNPROTECTED) == UNPROTECTED);
bShared = ((higherByte & (byte) SHARED) == SHARED);
bDedOneIstoOne = ((higherByte & (byte) DEDICATED_ONE_ISTO_ONE) == DEDICATED_ONE_ISTO_ONE);
bDedOnePlusOne = ((higherByte & (byte) DEDICATED_ONE_PLUS_ONE) == DEDICATED_ONE_PLUS_ONE);
bEnhanced = ((higherByte & (byte) ENHANCED) == ENHANCED);
return BgpLinkAttrProtectionType.of(bExtraTraffic, bUnprotected,
bShared, bDedOneIstoOne,
bDedOnePlusOne, bEnhanced);
}
/**
* Returns ExtraTraffic Bit.
*
* @return ExtraTraffic Bit
*/
public boolean extraTraffic() {
return bExtraTraffic;
}
/**
* Returns Unprotected Bit.
*
* @return Unprotected Bit
*/
public boolean unprotected() {
return bUnprotected;
}
/**
* Returns Shared Bit.
*
* @return Shared Bit
*/
public boolean shared() {
return bShared;
}
byte linkProtectionType = cb.readByte();
byte reserved = cb.readByte();
/**
* Returns DedOneIstoOne Bit.
*
* @return DedOneIstoOne Bit
*/
public boolean dedOneIstoOne() {
return bDedOneIstoOne;
return BgpLinkAttrProtectionType.of(linkProtectionType);
}
/**
* Returns DedOnePlusOne Bit.
* Returns Link Protection Type.
*
* @return DedOnePlusOne Bit
* @return Link Protection Type
*/
public boolean dedOnePlusOne() {
return bDedOnePlusOne;
}
/**
* Returns Enhanced Bit.
*
* @return Enhanced Bit
*/
public boolean enhanced() {
return bEnhanced;
public ProtectionType protectionType() throws BgpParseException {
switch (linkProtectionType) {
case Constants.EXTRA_TRAFFIC:
return ProtectionType.EXTRA_TRAFFIC;
case Constants.UNPROTECTED:
return ProtectionType.UNPROTECTED;
case Constants.SHARED:
return ProtectionType.SHARED;
case Constants.DEDICATED_ONE_ISTO_ONE:
return ProtectionType.DEDICATED_ONE_ISTO_ONE;
case Constants.DEDICATED_ONE_PLUS_ONE:
return ProtectionType.DEDICATED_ONE_PLUS_ONE;
case Constants.ENHANCED:
return ProtectionType.ENHANCED;
case Constants.RESERVED:
return ProtectionType.RESERVED;
default:
throw new BgpParseException("Got another type " + linkProtectionType);
}
}
@Override
......@@ -200,8 +136,7 @@ public final class BgpLinkAttrProtectionType implements BgpValueType {
@Override
public int hashCode() {
return Objects.hash(bExtraTraffic, bUnprotected, bShared,
bDedOneIstoOne, bDedOnePlusOne, bEnhanced);
return Objects.hash(linkProtectionType);
}
@Override
......@@ -212,12 +147,7 @@ public final class BgpLinkAttrProtectionType implements BgpValueType {
if (obj instanceof BgpLinkAttrProtectionType) {
BgpLinkAttrProtectionType other = (BgpLinkAttrProtectionType) obj;
return Objects.equals(bExtraTraffic, other.bExtraTraffic)
&& Objects.equals(bUnprotected, other.bUnprotected)
&& Objects.equals(bShared, other.bShared)
&& Objects.equals(bDedOneIstoOne, other.bDedOneIstoOne)
&& Objects.equals(bDedOnePlusOne, other.bDedOnePlusOne)
&& Objects.equals(bEnhanced, other.bEnhanced);
return Objects.equals(linkProtectionType, other.linkProtectionType);
}
return false;
}
......@@ -231,11 +161,8 @@ public final class BgpLinkAttrProtectionType implements BgpValueType {
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.add("bExtraTraffic", bExtraTraffic)
.add("bUnprotected", bUnprotected).add("bShared", bShared)
.add("bDedOneIstoOne", bDedOneIstoOne)
.add("bDedOnePlusOne", bDedOnePlusOne)
.add("bEnhanced", bEnhanced).toString();
.add("linkProtectionType", linkProtectionType)
.toString();
}
@Override
......
......@@ -33,6 +33,7 @@ import com.google.common.base.MoreObjects;
public class BgpLinkAttrSrlg implements BgpValueType {
public static final short ATTRNODE_SRLG = 1097;
public static final short SIZE = 4;
/* Shared Risk Link Group */
private List<Integer> sRlg = new ArrayList<Integer>();
......@@ -69,7 +70,7 @@ public class BgpLinkAttrSrlg implements BgpValueType {
ArrayList<Integer> sRlg = new ArrayList<Integer>();
short lsAttrLength = cb.readShort();
int len = lsAttrLength / Integer.SIZE; // each element is of 4 octets
int len = lsAttrLength / SIZE; // each element is of 4 octets
if (cb.readableBytes() < lsAttrLength) {
Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
......
......@@ -57,7 +57,7 @@ public final class BgpPrefixAttrIgpFlags implements BgpValueType {
* @param bOspfLclAddrBit OSPF local address Bit
* @param bOspfNSSABit OSPF propagate NSSA Bit
*/
BgpPrefixAttrIgpFlags(boolean bisisUpDownBit,
public BgpPrefixAttrIgpFlags(boolean bisisUpDownBit,
boolean bOspfNoUnicastBit,
boolean bOspfLclAddrBit, boolean bOspfNSSABit) {
this.bisisUpDownBit = bisisUpDownBit;
......
......@@ -38,6 +38,7 @@ public class BgpPrefixAttrRouteTag implements BgpValueType {
.getLogger(BgpPrefixAttrRouteTag.class);
public static final short ATTR_PREFIX_ROUTETAG = 1153;
public static final short SIZE = 4;
/* Prefix Route Tag */
private List<Integer> pfxRouteTag = new ArrayList<Integer>();
......@@ -74,7 +75,7 @@ public class BgpPrefixAttrRouteTag implements BgpValueType {
ArrayList<Integer> pfxRouteTag = new ArrayList<Integer>();
short lsAttrLength = cb.readShort();
int len = lsAttrLength / Integer.SIZE;
int len = lsAttrLength / SIZE;
if (cb.readableBytes() < lsAttrLength) {
Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
......
......@@ -34,4 +34,11 @@ public final class Constants {
public static final short AFI_VALUE = 16388;
public static final byte VPN_SAFI_VALUE = (byte) 0x80;
public static final byte SAFI_VALUE = 71;
public static final int EXTRA_TRAFFIC = 0x01;
public static final int UNPROTECTED = 0x02;
public static final int SHARED = 0x04;
public static final int DEDICATED_ONE_ISTO_ONE = 0x08;
public static final int DEDICATED_ONE_PLUS_ONE = 0x10;
public static final int ENHANCED = 0x20;
public static final int RESERVED = 0x40;
}
\ No newline at end of file
......
This diff could not be displayed because it is too large.
......@@ -20,37 +20,20 @@ import org.junit.Test;
import com.google.common.testing.EqualsTester;
/**
* Test for MPLS protocol mask attribute.
* Test for Protection Type attribute.
*/
public class BgpLinkAttrProtectionTypeTest {
boolean bExtraTraffic = true;
boolean bUnprotected = true;
boolean bShared = true;
boolean bDedOneIstoOne = true;
boolean bDedOnePlusOne = true;
boolean bEnhanced = true;
private final byte linkProtectionType1 = 0x04;
private final byte linkProtectionType2 = 0x40;
boolean bExtraTraffic1 = false;
boolean bUnprotected1 = false;
boolean bShared1 = false;
boolean bDedOneIstoOne1 = false;
boolean bDedOnePlusOne1 = false;
boolean bEnhanced1 = false;
private final BgpLinkAttrProtectionType data = BgpLinkAttrProtectionType
.of(bExtraTraffic, bUnprotected, bShared, bDedOneIstoOne,
bDedOnePlusOne, bEnhanced);
private final BgpLinkAttrProtectionType sameAsData = BgpLinkAttrProtectionType
.of(bExtraTraffic, bUnprotected, bShared, bDedOneIstoOne,
bDedOnePlusOne, bEnhanced);
private final BgpLinkAttrProtectionType diffData = BgpLinkAttrProtectionType
.of(bExtraTraffic1, bUnprotected1, bShared1, bDedOneIstoOne1,
bDedOnePlusOne1, bEnhanced1);
private final BgpLinkAttrProtectionType attr1 = BgpLinkAttrProtectionType.of(linkProtectionType1);
private final BgpLinkAttrProtectionType sameAsAttr1 = BgpLinkAttrProtectionType.of(linkProtectionType1);
private final BgpLinkAttrProtectionType attr2 = BgpLinkAttrProtectionType.of(linkProtectionType2);
@Test
public void basics() {
new EqualsTester().addEqualityGroup(data, sameAsData)
.addEqualityGroup(diffData).testEquals();
public void testEquality() {
new EqualsTester().addEqualityGroup(attr1, sameAsAttr1)
.addEqualityGroup(attr2)
.testEquals();
}
}
......