Shashikanth VH
Committed by Gerrit Code Review

[ONOS-3856] BGP flow specification extended community path attribute parsing.

Change-Id: I9ea9db5565e91598328fdb703186c3d6577e2dc7
/*
* Copyright 2016 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.types;
import com.google.common.base.MoreObjects;
import org.jboss.netty.buffer.ChannelBuffer;
import org.onosproject.bgpio.exceptions.BgpParseException;
import org.onosproject.bgpio.util.Constants;
import org.onosproject.bgpio.util.Validation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Objects;
/**
* Provides implementation of extended community BGP Path Attribute.
*/
public class BgpExtendedCommunity implements BgpValueType {
private static final Logger log = LoggerFactory.getLogger(BgpExtendedCommunity.class);
public static final short TYPE = Constants.BGP_EXTENDED_COMMUNITY;
public static final byte FLAGS = (byte) 0xC0;
private BgpValueType fsActionTlv;
/**
* Constructor to initialize the value.
*
* @param fsActionTlv flow specification action type
*/
public BgpExtendedCommunity(BgpValueType fsActionTlv) {
this.fsActionTlv = fsActionTlv;
}
/**
* Returns extended community type.
*
* @return extended community
*/
public BgpValueType fsActionTlv() {
return this.fsActionTlv;
}
/**
* Reads from the channel buffer and parses extended community.
*
* @param cb ChannelBuffer
* @return object of BgpExtendedCommunity
* @throws BgpParseException while parsing extended community
*/
public static BgpExtendedCommunity read(ChannelBuffer cb) throws BgpParseException {
ChannelBuffer tempCb = cb.copy();
Validation validation = Validation.parseAttributeHeader(cb);
BgpValueType fsActionTlv = null;
if (cb.readableBytes() < validation.getLength()) {
Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
validation.getLength());
}
//if fourth bit is set, length is read as short otherwise as byte , len includes type, length and value
int len = validation.isShort() ? validation.getLength() + Constants.TYPE_AND_LEN_AS_SHORT : validation
.getLength() + Constants.TYPE_AND_LEN_AS_BYTE;
ChannelBuffer data = tempCb.readBytes(len);
if (validation.getFirstBit() && !validation.getSecondBit() && validation.getThirdBit()) {
throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_FLAGS_ERROR, data);
}
ChannelBuffer tempBuf = cb.readBytes(validation.getLength());
if (tempBuf.readableBytes() > 0) {
ChannelBuffer actionBuf = tempBuf.readBytes(validation.getLength());
short actionType = actionBuf.readShort();
short length = (short) validation.getLength();
switch (actionType) {
case Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_ACTION:
fsActionTlv = BgpFsActionTrafficAction.read(actionBuf);
break;
case Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_MARKING:
fsActionTlv = BgpFsActionTrafficMarking.read(actionBuf);
break;
case Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_RATE:
fsActionTlv = BgpFsActionTrafficRate.read(actionBuf);
break;
case Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_REDIRECT:
fsActionTlv = BgpFsActionReDirect.read(actionBuf);
break;
default: log.debug("Other type Not Supported:" + actionType);
break;
}
}
return new BgpExtendedCommunity(fsActionTlv);
}
@Override
public short getType() {
return TYPE;
}
@Override
public int hashCode() {
return Objects.hash(fsActionTlv);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof BgpExtendedCommunity) {
BgpExtendedCommunity other = (BgpExtendedCommunity) obj;
return Objects.equals(fsActionTlv, other.fsActionTlv);
}
return false;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.omitNullValues()
.add("fsActionTlv", fsActionTlv)
.toString();
}
@Override
public int write(ChannelBuffer cb) {
int iLenStartIndex = cb.writerIndex();
cb.writeByte(FLAGS);
cb.writeByte(getType());
int iActionLenIndex = cb.writerIndex();
cb.writeByte(0);
if (fsActionTlv.getType() == Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_ACTION) {
BgpFsActionTrafficAction trafficAction = (BgpFsActionTrafficAction) fsActionTlv;
trafficAction.write(cb);
} else if (fsActionTlv.getType() == Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_MARKING) {
BgpFsActionTrafficMarking trafficMarking = (BgpFsActionTrafficMarking) fsActionTlv;
trafficMarking.write(cb);
} else if (fsActionTlv.getType() == Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_RATE) {
BgpFsActionTrafficRate trafficRate = (BgpFsActionTrafficRate) fsActionTlv;
trafficRate.write(cb);
} else if (fsActionTlv.getType() == Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_REDIRECT) {
BgpFsActionReDirect trafficRedirect = (BgpFsActionReDirect) fsActionTlv;
trafficRedirect.write(cb);
}
int fsActionLen = cb.writerIndex() - iActionLenIndex;
cb.setByte(iActionLenIndex, (byte) (fsActionLen - 1));
return cb.writerIndex() - iLenStartIndex;
}
@Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
return 0;
}
}
......@@ -77,12 +77,15 @@ public class BgpFsActionReDirect implements BgpValueType {
* Reads the channel buffer and returns object.
*
* @param cb channelBuffer
* @param type address type
* @return object of flow spec action redirect
* @throws BgpParseException while parsing BgpFsActionReDirect
*/
public static BgpFsActionReDirect read(ChannelBuffer cb, short type) throws BgpParseException {
return null;
public static BgpFsActionReDirect read(ChannelBuffer cb) throws BgpParseException {
byte[] routeTarget;
ChannelBuffer tempCb = cb.copy();
routeTarget = tempCb.readBytes(tempCb.readableBytes()).array();
return new BgpFsActionReDirect(routeTarget);
}
@Override
......
......@@ -77,12 +77,15 @@ public class BgpFsActionTrafficAction implements BgpValueType {
* Reads the channel buffer and returns object.
*
* @param cb channelBuffer
* @param type address type
* @return object of flow spec action traffic rate
* @throws BgpParseException while parsing BgpFsActionTrafficAction
*/
public static BgpFsActionTrafficAction read(ChannelBuffer cb, short type) throws BgpParseException {
return null;
public static BgpFsActionTrafficAction read(ChannelBuffer cb) throws BgpParseException {
byte[] bitMask;
ChannelBuffer tempCb = cb.copy();
bitMask = tempCb.readBytes(tempCb.readableBytes()).array();
return new BgpFsActionTrafficAction(bitMask);
}
@Override
......
......@@ -77,12 +77,15 @@ public class BgpFsActionTrafficMarking implements BgpValueType {
* Reads the channel buffer and returns object.
*
* @param cb channelBuffer
* @param type address type
* @return object of flow spec action traffic marking
* @throws BgpParseException while parsing BgpFsActionTrafficMarking
*/
public static BgpFsActionTrafficMarking read(ChannelBuffer cb, short type) throws BgpParseException {
return null;
public static BgpFsActionTrafficMarking read(ChannelBuffer cb) throws BgpParseException {
byte[] dscpValue;
ChannelBuffer tempCb = cb.copy();
dscpValue = tempCb.readBytes(tempCb.readableBytes()).array();
return new BgpFsActionTrafficMarking(dscpValue);
}
@Override
......
......@@ -80,12 +80,17 @@ public class BgpFsActionTrafficRate implements BgpValueType {
* Reads the channel buffer and returns object.
*
* @param cb channelBuffer
* @param type address type
* @return object of flow spec action traffic rate
* @throws BgpParseException while parsing BgpFsActionTrafficRate
*/
public static BgpFsActionTrafficRate read(ChannelBuffer cb, short type) throws BgpParseException {
return null;
public static BgpFsActionTrafficRate read(ChannelBuffer cb) throws BgpParseException {
short asn;
float rate;
ChannelBuffer tempCb = cb.copy();
asn = tempCb.readShort();
rate = tempCb.readFloat();
return new BgpFsActionTrafficRate(asn, rate);
}
@Override
......
......@@ -48,6 +48,8 @@ public final class Constants {
public static final int ENHANCED = 0x20;
public static final int RESERVED = 0x40;
public static final byte BGP_EXTENDED_COMMUNITY = 0x10;
public static final byte BGP_FLOWSPEC_DST_PREFIX = 0x01;
public static final byte BGP_FLOWSPEC_SRC_PREFIX = 0x02;
public static final byte BGP_FLOWSPEC_IP_PROTO = 0x03;
......