diff options
Diffstat (limited to 'framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types')
5 files changed, 837 insertions, 0 deletions
diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/MpReachNlri.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/MpReachNlri.java new file mode 100644 index 00000000..90a1cc6b --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/MpReachNlri.java @@ -0,0 +1,221 @@ +/* + * 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.types; + +import java.net.InetAddress; +import java.util.LinkedList; +import java.util.List; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.onlab.packet.Ip4Address; +import org.onosproject.bgpio.exceptions.BGPParseException; +import org.onosproject.bgpio.protocol.BGPLSNlri; +import org.onosproject.bgpio.protocol.linkstate.BGPPrefixIPv4LSNlriVer4; +import org.onosproject.bgpio.protocol.linkstate.BGPNodeLSNlriVer4; +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; + +/* + * Provides Implementation of MpReach Nlri BGP Path Attribute. + */ +public class MpReachNlri implements BGPValueType { + + private static final Logger log = LoggerFactory.getLogger(MpReachNlri.class); + + public static final byte MPREACHNLRI_TYPE = 14; + public static final byte LINK_NLRITYPE = 2; + + private boolean isMpReachNlri = false; + private final List<BGPLSNlri> mpReachNlri; + private final int length; + private final short afi; + private final byte safi; + private final Ip4Address ipNextHop; + + /** + * Constructor to initialize parameters. + * + * @param mpReachNlri MpReach Nlri attribute + * @param afi address family identifier + * @param safi subsequent address family identifier + * @param ipNextHop nexthop IpAddress + * @param length of MpReachNlri + */ + public MpReachNlri(List<BGPLSNlri> mpReachNlri, short afi, byte safi, Ip4Address ipNextHop, int length) { + this.mpReachNlri = mpReachNlri; + this.isMpReachNlri = true; + this.ipNextHop = ipNextHop; + this.afi = afi; + this.safi = safi; + this.length = length; + } + + /** + * Returns whether MpReachNlri is present. + * + * @return whether MpReachNlri is present + */ + public boolean isMpReachNlriSet() { + return this.isMpReachNlri; + } + + /** + * Returns list of MpReach Nlri. + * + * @return list of MpReach Nlri + */ + public List<BGPLSNlri> mpReachNlri() { + return this.mpReachNlri; + } + + /** + * Returns length of MpReachNlri. + * + * @return length of MpReachNlri + */ + public int mpReachNlriLen() { + return this.length; + } + + /** + * Reads from ChannelBuffer and parses MpReachNlri. + * + * @param cb channelBuffer + * @return object of MpReachNlri + * @throws BGPParseException while parsing MpReachNlri + */ + public static MpReachNlri read(ChannelBuffer cb) throws BGPParseException { + ChannelBuffer tempBuf = cb.copy(); + Validation parseFlags = Validation.parseAttributeHeader(cb); + int len = parseFlags.isShort() ? parseFlags.getLength() + Constants.TYPE_AND_LEN_AS_SHORT : + parseFlags.getLength() + Constants.TYPE_AND_LEN_AS_BYTE; + ChannelBuffer data = tempBuf.readBytes(len); + + if (cb.readableBytes() < parseFlags.getLength()) { + Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR, BGPErrorType.ATTRIBUTE_LENGTH_ERROR, + parseFlags.getLength()); + } + if (!parseFlags.getFirstBit() && parseFlags.getSecondBit() && parseFlags.getThirdBit()) { + throw new BGPParseException(BGPErrorType.UPDATE_MESSAGE_ERROR, BGPErrorType.ATTRIBUTE_FLAGS_ERROR, data); + } + + BGPLSNlri bgpLSNlri = null; + List<BGPLSNlri> mpReachNlri = new LinkedList<>(); + ChannelBuffer tempCb = cb.readBytes(parseFlags.getLength()); + short afi = 0; + byte safi = 0; + Ip4Address ipNextHop = null; + while (tempCb.readableBytes() > 0) { + afi = tempCb.readShort(); + safi = tempCb.readByte(); + + //Supporting for AFI 16388 / SAFI 71 and VPN AFI 16388 / SAFI 128 + if ((afi == Constants.AFI_VALUE) && (safi == Constants.SAFI_VALUE) || (afi == Constants.AFI_VALUE) + && (safi == Constants.VPN_SAFI_VALUE)) { + byte nextHopLen = tempCb.readByte(); + //TODO: use Validation.toInetAddress once Validation is merged + InetAddress ipAddress = (InetAddress) cb.readBytes(nextHopLen); + if (ipAddress.isMulticastAddress()) { + throw new BGPParseException("Multicast not supported"); + } + ipNextHop = Ip4Address.valueOf(ipAddress); + byte reserved = tempCb.readByte(); + + while (tempCb.readableBytes() > 0) { + short nlriType = tempCb.readShort(); + short totNlriLen = tempCb.readShort(); + if (tempCb.readableBytes() < totNlriLen) { + Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR, + BGPErrorType.ATTRIBUTE_LENGTH_ERROR, totNlriLen); + } + tempBuf = tempCb.readBytes(totNlriLen); + switch (nlriType) { + case BGPNodeLSNlriVer4.NODE_NLRITYPE: + bgpLSNlri = BGPNodeLSNlriVer4.read(tempBuf, afi, safi); + break; + case LINK_NLRITYPE: + //TODO: To be merged later + break; + case BGPPrefixIPv4LSNlriVer4.PREFIX_IPV4_NLRITYPE: + bgpLSNlri = BGPPrefixIPv4LSNlriVer4.read(tempBuf, afi, safi); + break; + default: + log.debug("nlriType not supported" + nlriType); + } + mpReachNlri.add(bgpLSNlri); + } + } else { + //TODO: check with the values got from capability + throw new BGPParseException("Not Supporting afi " + afi + "safi " + safi); + } + } + return new MpReachNlri(mpReachNlri, afi, safi, ipNextHop, parseFlags.getLength()); + } + + @Override + public short getType() { + return MPREACHNLRI_TYPE; + } + + /** + * Returns AFI. + * + * @return AFI + */ + public short afi() { + return this.afi; + } + + /** + * Returns Nexthop IpAddress. + * + * @return Nexthop IpAddress + */ + public Ip4Address nexthop4() { + return this.ipNextHop; + } + + /** + * Returns SAFI. + * + * @return SAFI + */ + public byte safi() { + return this.safi; + } + + @Override + public int write(ChannelBuffer cb) { + //Not to be Implemented as of now + return 0; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(getClass()) + .add("mpReachNlri", mpReachNlri) + .add("afi", afi) + .add("safi", safi) + .add("ipNextHop", ipNextHop) + .add("length", length) + .toString(); + } +}
\ No newline at end of file diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/MpUnReachNlri.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/MpUnReachNlri.java new file mode 100644 index 00000000..3efed95c --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/MpUnReachNlri.java @@ -0,0 +1,203 @@ +/* + * 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.types; + +import java.util.LinkedList; +import java.util.List; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.onosproject.bgpio.exceptions.BGPParseException; +import org.onosproject.bgpio.protocol.BGPLSNlri; +import org.onosproject.bgpio.protocol.linkstate.BGPNodeLSNlriVer4; +import org.onosproject.bgpio.protocol.linkstate.BGPPrefixIPv4LSNlriVer4; +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; + +/** + * Provides Implementation of MpUnReach Nlri BGP Path Attribute. + */ +public class MpUnReachNlri implements BGPValueType { + + protected static final Logger log = LoggerFactory.getLogger(MpUnReachNlri.class); + + public static final byte MPUNREACHNLRI_TYPE = 15; + public static final byte LINK_NLRITYPE = 2; + private boolean isMpUnReachNlri = false; + private final short afi; + private final byte safi; + private final List<BGPLSNlri> mpUnReachNlri; + private final int length; + + /** + * Constructor to initialize parameters. + * + * @param mpUnReachNlri MpUnReach Nlri attribute + * @param afi address family identifier + * @param safi subsequent address family identifier + * @param length of MpUnReachNlri + */ + public MpUnReachNlri(List<BGPLSNlri> mpUnReachNlri, short afi, byte safi, + int length) { + this.mpUnReachNlri = mpUnReachNlri; + this.isMpUnReachNlri = true; + this.afi = afi; + this.safi = safi; + this.length = length; + } + + /** + * Reads from ChannelBuffer and parses MpUnReachNlri. + * + * @param cb ChannelBuffer + * @return object of MpUnReachNlri + * @throws BGPParseException while parsing MpUnReachNlri + */ + public static MpUnReachNlri read(ChannelBuffer cb) throws BGPParseException { + ChannelBuffer tempBuf = cb.copy(); + Validation parseFlags = Validation.parseAttributeHeader(cb); + int len = parseFlags.isShort() ? parseFlags.getLength() + Constants.TYPE_AND_LEN_AS_SHORT + : parseFlags.getLength() + Constants.TYPE_AND_LEN_AS_BYTE; + ChannelBuffer data = tempBuf.readBytes(len); + + if (!parseFlags.getFirstBit() && parseFlags.getSecondBit() + && parseFlags.getThirdBit()) { + throw new BGPParseException(BGPErrorType.UPDATE_MESSAGE_ERROR, + BGPErrorType.ATTRIBUTE_FLAGS_ERROR, data); + } + + if (cb.readableBytes() < parseFlags.getLength()) { + Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR, + BGPErrorType.ATTRIBUTE_LENGTH_ERROR, parseFlags.getLength()); + } + + LinkedList<BGPLSNlri> mpUnReachNlri = new LinkedList<>(); + BGPLSNlri bgpLSNlri = null; + short afi = 0; + byte safi = 0; + ChannelBuffer tempCb = cb.readBytes(parseFlags.getLength()); + while (tempCb.readableBytes() > 0) { + afi = tempCb.readShort(); + safi = tempCb.readByte(); + + //Supporting only for AFI 16388 / SAFI 71 + if ((afi == Constants.AFI_VALUE) && (safi == Constants.SAFI_VALUE) + || (afi == Constants.AFI_VALUE) && (safi == Constants.VPN_SAFI_VALUE)) { + while (tempCb.readableBytes() > 0) { + short nlriType = tempCb.readShort(); + short totNlriLen = tempCb.readShort(); + if (tempCb.readableBytes() < totNlriLen) { + Validation.validateLen( + BGPErrorType.UPDATE_MESSAGE_ERROR, + BGPErrorType.ATTRIBUTE_LENGTH_ERROR, totNlriLen); + } + tempBuf = tempCb.readBytes(totNlriLen); + switch (nlriType) { + case BGPNodeLSNlriVer4.NODE_NLRITYPE: + bgpLSNlri = BGPNodeLSNlriVer4.read(tempBuf, afi, safi); + break; + case LINK_NLRITYPE: + //TODO: to be merged later + break; + case BGPPrefixIPv4LSNlriVer4.PREFIX_IPV4_NLRITYPE: + bgpLSNlri = BGPPrefixIPv4LSNlriVer4.read(tempBuf, afi, + safi); + break; + default: + log.debug("nlriType not supported" + nlriType); + } + mpUnReachNlri.add(bgpLSNlri); + } + } else { + //TODO: check with the values got from capability + throw new BGPParseException("Not Supporting afi " + afi + + "safi " + safi); + } + } + return new MpUnReachNlri(mpUnReachNlri, afi, safi, + parseFlags.getLength()); + } + + @Override + public short getType() { + return MPUNREACHNLRI_TYPE; + } + + /** + * Returns SAFI. + * + * @return SAFI + */ + public byte safi() { + return this.safi; + } + + /** + * Returns AFI. + * + * @return AFI + */ + public short afi() { + return this.afi; + } + + /** + * Returns list of MpUnReach Nlri. + * + * @return list of MpUnReach Nlri + */ + public List<BGPLSNlri> mpUnReachNlri() { + return this.mpUnReachNlri; + } + + /** + * Returns whether MpReachNlri is present. + * + * @return whether MpReachNlri is present + */ + public boolean isMpUnReachNlriSet() { + return this.isMpUnReachNlri; + } + + /** + * Returns length of MpUnReach. + * + * @return length of MpUnReach + */ + public int mpUnReachNlriLen() { + return this.length; + } + + @Override + public int write(ChannelBuffer cb) { + //Not to be Implemented as of now + return 0; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(getClass()) + .add("mpReachNlri", mpUnReachNlri) + .add("afi", afi) + .add("safi", safi) + .add("length", length) + .toString(); + } +}
\ No newline at end of file diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/attr/BgpLinkAttrIsIsAdminstGrp.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/attr/BgpLinkAttrIsIsAdminstGrp.java new file mode 100644 index 00000000..086e8b06 --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/attr/BgpLinkAttrIsIsAdminstGrp.java @@ -0,0 +1,130 @@ +/* + * 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.types.attr; + +import java.util.Objects; + +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.Validation; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.MoreObjects; + +/** + * Implements BGP attribute Is Is Administrative area. + */ +public final class BgpLinkAttrIsIsAdminstGrp implements BGPValueType { + + protected static final Logger log = LoggerFactory + .getLogger(BgpLinkAttrIsIsAdminstGrp.class); + + public static final int ATTRLINK_PROTECTIONTYPE = 1088; + public static final int ISIS_ADMIN_DATA_LEN = 4; + + /* ISIS administrative group */ + private final long isisAdminGrp; + + /** + * Constructor to initialize the values. + * + * @param isisAdminGrp ISIS protocol admin group + */ + public BgpLinkAttrIsIsAdminstGrp(long isisAdminGrp) { + this.isisAdminGrp = isisAdminGrp; + } + + /** + * Returns object of this class with specified values. + * + * @param isisAdminGrp ISIS admin group + * @return object of BgpLinkAttrIsIsAdminstGrp + */ + public static BgpLinkAttrIsIsAdminstGrp of(final long isisAdminGrp) { + return new BgpLinkAttrIsIsAdminstGrp(isisAdminGrp); + } + + /** + * Reads the BGP link attributes of ISIS administrative group area. + * + * @param cb Channel buffer + * @return object of type BgpLinkAttrIsIsAdminstGrp + * @throws BGPParseException while parsing BgpLinkAttrIsIsAdminstGrp + */ + public static BgpLinkAttrIsIsAdminstGrp read(ChannelBuffer cb) + throws BGPParseException { + long isisAdminGrp; + short lsAttrLength = cb.readShort(); + + if ((lsAttrLength != ISIS_ADMIN_DATA_LEN) + || (cb.readableBytes() < lsAttrLength)) { + Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR, + BGPErrorType.ATTRIBUTE_LENGTH_ERROR, + lsAttrLength); + } + + isisAdminGrp = cb.readUnsignedInt(); + + return BgpLinkAttrIsIsAdminstGrp.of(isisAdminGrp); + } + + /** + * Link attributes of ISIS administrative group area. + * + * @return long value of the administrative group area + */ + public long linkAttrIsIsAdminGrp() { + return isisAdminGrp; + } + + @Override + public short getType() { + return ATTRLINK_PROTECTIONTYPE; + } + + @Override + public int hashCode() { + return Objects.hash(isisAdminGrp); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + + if (obj instanceof BgpLinkAttrIsIsAdminstGrp) { + BgpLinkAttrIsIsAdminstGrp other = (BgpLinkAttrIsIsAdminstGrp) obj; + return Objects.equals(isisAdminGrp, other.isisAdminGrp); + } + return false; + } + + @Override + public int write(ChannelBuffer cb) { + // TODO This will be implemented in the next version + return 0; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(getClass()) + .add("isisAdminGrp", isisAdminGrp).toString(); + } +} diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/attr/BgpLinkAttrMaxLinkBandwidth.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/attr/BgpLinkAttrMaxLinkBandwidth.java new file mode 100644 index 00000000..a1f0198b --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/attr/BgpLinkAttrMaxLinkBandwidth.java @@ -0,0 +1,150 @@ +/* + * 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.types.attr; + +import java.util.Objects; + +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.Validation; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.MoreObjects; + +/** + * Implements BGP attribute Max Link bandwidth. + */ +public final class BgpLinkAttrMaxLinkBandwidth implements BGPValueType { + + protected static final Logger log = LoggerFactory + .getLogger(BgpLinkAttrMaxLinkBandwidth.class); + + public static final int MAX_BANDWIDTH_LEN = 4; + public static final int NO_OF_BITS = 8; + + public short type; + + /* ISIS administrative group */ + private final float maxBandwidth; + + /** + * Constructor to initialize the values. + * + * @param maxBandwidth Maximum link bandwidth. + * @param type TLV type + */ + private BgpLinkAttrMaxLinkBandwidth(float maxBandwidth, short type) { + this.maxBandwidth = maxBandwidth; + this.type = type; + } + + /** + * Returns object of this class with specified values. + * + * @param maxBandwidth Maximum link bandwidth. + * @param type TLV type + * @return object of BgpLinkAttrMaxLinkBandwidth + */ + public static BgpLinkAttrMaxLinkBandwidth of(final float maxBandwidth, + final short type) { + return new BgpLinkAttrMaxLinkBandwidth(maxBandwidth, type); + } + + /** + * Reads the BGP link attributes of Maximum link bandwidth. + * + * @param cb Channel buffer + * @param type type of this tlv + * @return object of type BgpLinkAttrMaxLinkBandwidth + * @throws BGPParseException while parsing BgpLinkAttrMaxLinkBandwidth + */ + public static BgpLinkAttrMaxLinkBandwidth read(ChannelBuffer cb, short type) + throws BGPParseException { + float maxBandwidth; + short lsAttrLength = cb.readShort(); + + if ((lsAttrLength != MAX_BANDWIDTH_LEN) + || (cb.readableBytes() < lsAttrLength)) { + Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR, + BGPErrorType.ATTRIBUTE_LENGTH_ERROR, + lsAttrLength); + } + + maxBandwidth = ieeeToFloatRead(cb.readInt()) * NO_OF_BITS; + + return BgpLinkAttrMaxLinkBandwidth.of(maxBandwidth, type); + } + + /** + * Returns Maximum link bandwidth. + * + * @return Maximum link bandwidth + */ + float linkAttrMaxLinkBandwidth() { + return maxBandwidth; + } + + /** + * Parse the IEEE floating point notation and returns it in normal float. + * + * @param iVal IEEE floating point number + * @return normal float + */ + static float ieeeToFloatRead(int iVal) { + iVal = (((iVal & 0xFF) << 24) | ((iVal & 0xFF00) << 8) + | ((iVal & 0xFF0000) >> 8) | ((iVal >> 24) & 0xFF)); + + return Float.intBitsToFloat(iVal); + } + + @Override + public short getType() { + return this.type; + } + + @Override + public int hashCode() { + return Objects.hash(maxBandwidth); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + + if (obj instanceof BgpLinkAttrMaxLinkBandwidth) { + BgpLinkAttrMaxLinkBandwidth other = (BgpLinkAttrMaxLinkBandwidth) obj; + return Objects.equals(maxBandwidth, other.maxBandwidth); + } + return false; + } + + @Override + public int write(ChannelBuffer cb) { + // TODO This will be implemented in the next version + return 0; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(getClass()) + .add("maxBandwidth", maxBandwidth).toString(); + } +} diff --git a/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/attr/BgpLinkAttrOpaqLnkAttrib.java b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/attr/BgpLinkAttrOpaqLnkAttrib.java new file mode 100755 index 00000000..258598be --- /dev/null +++ b/framework/src/onos/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/attr/BgpLinkAttrOpaqLnkAttrib.java @@ -0,0 +1,133 @@ +/* + * 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.types.attr; + +import java.util.Arrays; + +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.Validation; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.MoreObjects; + +/** + * Implements BGP link opaque attribute. + */ +public final class BgpLinkAttrOpaqLnkAttrib implements BGPValueType { + + protected static final Logger log = LoggerFactory + .getLogger(BgpLinkAttrOpaqLnkAttrib.class); + + public static final int ATTRNODE_OPAQUELNKATTRIB = 1097; + + /* Opaque Node Attribute */ + private final byte[] opaqueLinkAttribute; + + /** + * Constructor to initialize the data. + * + * @param opaqueLinkAttribute opaque link attribute + */ + private BgpLinkAttrOpaqLnkAttrib(byte[] opaqueLinkAttribute) { + this.opaqueLinkAttribute = Arrays.copyOf(opaqueLinkAttribute, + opaqueLinkAttribute.length); + } + + /** + * Returns object of this class with specified values. + * + * @param opaqueLinkAttribute opaque link attribute + * @return object of BgpLinkAttrOpaqLnkAttrib + */ + public static BgpLinkAttrOpaqLnkAttrib of(final byte[] opaqueLinkAttribute) { + return new BgpLinkAttrOpaqLnkAttrib(opaqueLinkAttribute); + } + + /** + * Reads the BGP link attributes Opaque link attribute. + * + * @param cb Channel buffer + * @return object of type BgpLinkAttrOpaqLnkAttrib + * @throws BGPParseException while parsing BgpLinkAttrOpaqLnkAttrib + */ + public static BgpLinkAttrOpaqLnkAttrib read(ChannelBuffer cb) + throws BGPParseException { + + byte[] opaqueLinkAttribute; + + short lsAttrLength = cb.readShort(); + + if (cb.readableBytes() < lsAttrLength) { + Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR, + BGPErrorType.ATTRIBUTE_LENGTH_ERROR, + lsAttrLength); + } + + opaqueLinkAttribute = new byte[lsAttrLength]; + cb.readBytes(opaqueLinkAttribute); + + return BgpLinkAttrOpaqLnkAttrib.of(opaqueLinkAttribute); + } + + /** + * Returns the Opaque link attribute. + * + * @return byte array of opaque link attribute. + */ + public byte[] attrOpaqueLnk() { + return opaqueLinkAttribute; + } + + @Override + public short getType() { + return ATTRNODE_OPAQUELNKATTRIB; + } + + @Override + public int hashCode() { + return Arrays.hashCode(opaqueLinkAttribute); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + + if (obj instanceof BgpLinkAttrOpaqLnkAttrib) { + BgpLinkAttrOpaqLnkAttrib other = (BgpLinkAttrOpaqLnkAttrib) obj; + return Arrays + .equals(opaqueLinkAttribute, other.opaqueLinkAttribute); + } + return false; + } + + @Override + public int write(ChannelBuffer cb) { + // TODO This will be implemented in the next version + return 0; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(getClass()).omitNullValues() + .add("opaqueLinkAttribute", opaqueLinkAttribute).toString(); + } +} |