import java.util.ArrayList;
import java.util.List;
+import org.opendaylight.protocol.bgp.linkstate.attribute.LinkstateAttributeParser;
+import org.opendaylight.protocol.bgp.linkstate.nlri.LinkstateNlriParser;
import org.opendaylight.protocol.bgp.parser.spi.AbstractBGPExtensionProviderActivator;
import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.LinkstateAddressFamily;
this.ianaLinkstateAttributeType = true;
}
- public BGPActivator(boolean ianaLinkstateAttributeType) {
+ public BGPActivator(final boolean ianaLinkstateAttributeType) {
super();
this.ianaLinkstateAttributeType = ianaLinkstateAttributeType;
}
new LinkstateNlriParser(true)));
regs.add(context.registerNlriSerializer(PathAttributes.class, new LinkstateNlriParser(false)));
- regs.add(context.registerAttributeSerializer(PathAttributes1.class, new LinkstateAttributeParser(ianaLinkstateAttributeType)));
- final LinkstateAttributeParser linkstateAttributeParser = new LinkstateAttributeParser(ianaLinkstateAttributeType);
+ regs.add(context.registerAttributeSerializer(PathAttributes1.class, new LinkstateAttributeParser(this.ianaLinkstateAttributeType)));
+ final LinkstateAttributeParser linkstateAttributeParser = new LinkstateAttributeParser(this.ianaLinkstateAttributeType);
regs.add(context.registerAttributeParser(linkstateAttributeParser.getType(), linkstateAttributeParser));
return regs;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import java.util.List;
+import org.opendaylight.protocol.bgp.linkstate.nlri.LinkstateNlriParser;
import org.opendaylight.protocol.bgp.parser.BGPParsingException;
import org.opendaylight.protocol.bgp.rib.spi.AbstractAdjRIBs;
import org.opendaylight.protocol.bgp.rib.spi.AdjRIBsTransaction;
+++ /dev/null
-/*
- * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.protocol.bgp.linkstate;
-
-import com.google.common.base.Charsets;
-import com.google.common.base.Preconditions;
-import com.google.common.collect.HashMultimap;
-import com.google.common.collect.Lists;
-import com.google.common.collect.Multimap;
-import com.google.common.net.InetAddresses;
-import io.netty.buffer.ByteBuf;
-import io.netty.buffer.ByteBufUtil;
-import io.netty.buffer.Unpooled;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.BitSet;
-import java.util.List;
-import java.util.Map.Entry;
-import org.opendaylight.protocol.bgp.parser.BGPParsingException;
-import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
-import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
-import org.opendaylight.protocol.bgp.parser.spi.AttributeUtil;
-import org.opendaylight.protocol.util.ByteArray;
-import org.opendaylight.protocol.util.Ipv4Util;
-import org.opendaylight.protocol.util.Ipv6Util;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.AdministrativeGroup;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.ExtendedRouteTag;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.IgpBits.UpDown;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.Ipv4RouterIdentifier;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.Ipv6RouterIdentifier;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.IsisAreaIdentifier;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.LinkProtectionType;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.MplsProtocolMask;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.NlriType;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.NodeFlagBits;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.PathAttributes1;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.PathAttributes1Builder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.RouteTag;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.TopologyIdentifier;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.link.state.UnreservedBandwidth;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.link.state.UnreservedBandwidthBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.destination.CLinkstateDestination;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.prefix.state.IgpBitsBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.LinkstatePathAttribute;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.LinkstatePathAttributeBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.LinkStateAttribute;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.link.state.attribute.LinkAttributesCase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.link.state.attribute.LinkAttributesCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.link.state.attribute.NodeAttributesCase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.link.state.attribute.NodeAttributesCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.link.state.attribute.PrefixAttributesCase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.link.state.attribute.PrefixAttributesCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.link.state.attribute.link.attributes._case.LinkAttributes;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.link.state.attribute.link.attributes._case.LinkAttributesBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.link.state.attribute.node.attributes._case.NodeAttributes;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.link.state.attribute.node.attributes._case.NodeAttributesBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.link.state.attribute.prefix.attributes._case.PrefixAttributes;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.link.state.attribute.prefix.attributes._case.PrefixAttributesBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLinkstateCase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributes;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes2;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.destination.DestinationType;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.IgpMetric;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Metric;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.TeMetric;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.SrlgId;
-import org.opendaylight.yangtools.yang.binding.DataObject;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * Parser for Link State Path Attribute.
- *
- * @see <a href="http://tools.ietf.org/html/draft-gredler-idr-ls-distribution-04">BGP-LS draft</a>
- */
-public class LinkstateAttributeParser implements AttributeParser, AttributeSerializer {
-
- private static final int TYPE = 29;
-
- private static final int LEGACY_TYPE = 99;
-
- private static final Logger LOG = LoggerFactory.getLogger(LinkstateAttributeParser.class);
-
- private static final int ROUTE_TAG_LENGTH = 4;
-
- private static final int EXTENDED_ROUTE_TAG_LENGTH = 8;
-
- private static final int UNRESERVED_BW_COUNT = 8;
-
- private static final int BANDWIDTH_LENGTH = 4;
-
- // node flag bits
- private static final int OVERLOAD_BIT = 7;
- private static final int ATTACHED_BIT = 6;
- private static final int EXTERNAL_BIT = 5;
- private static final int ABBR_BIT = 4;
-
- // MPLS protection mask bits
- private static final int LDP_BIT = 7;
- private static final int RSVP_BIT = 6;
-
- private static final int UP_DOWN_BIT = 7;
-
- private final int type;
-
- public LinkstateAttributeParser(final boolean isIanaAssignedType) {
- if(isIanaAssignedType) {
- this.type = TYPE;
- } else {
- this.type = LEGACY_TYPE;
- }
- }
-
- public int getType() {
- return this.type;
- }
-
- private NlriType getNlriType(final PathAttributesBuilder pab) {
- final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes1 mpr = pab.getAugmentation(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes1.class);
- if (mpr != null && mpr.getMpReachNlri() != null) {
- final DestinationType dt = mpr.getMpReachNlri().getAdvertizedRoutes().getDestinationType();
- if (dt instanceof DestinationLinkstateCase) {
- for (final CLinkstateDestination d : ((DestinationLinkstateCase) dt).getDestinationLinkstate().getCLinkstateDestination()) {
- return d.getNlriType();
- }
- }
- } else {
- LOG.debug("No MP_REACH attribute present");
- }
- final PathAttributes2 mpu = pab.getAugmentation(PathAttributes2.class);
- if (mpu != null && mpu.getMpUnreachNlri() != null) {
- final DestinationType dt = mpu.getMpUnreachNlri().getWithdrawnRoutes().getDestinationType();
- if (dt instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationLinkstateCase) {
- for (final CLinkstateDestination d : ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationLinkstateCase) dt).getDestinationLinkstate().getCLinkstateDestination()) {
- return d.getNlriType();
- }
- }
- } else {
- LOG.debug("No MP_UNREACH attribute present");
- }
- return null;
- }
-
- @Override
- public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) throws BGPParsingException {
- final NlriType nlriType = getNlriType(builder);
- if (nlriType == null) {
- LOG.warn("No Linkstate NLRI found, not parsing Linkstate attribute");
- return;
- }
- final PathAttributes1 a = new PathAttributes1Builder().setLinkstatePathAttribute(parseLinkState(nlriType, buffer)).build();
- builder.addAugmentation(PathAttributes1.class, a);
- }
-
- private static LinkstatePathAttribute parseLinkState(final NlriType nlri, final ByteBuf buffer) throws BGPParsingException {
- /*
- * e.g. IS-IS Area Identifier TLV can occur multiple times
- */
- final Multimap<Integer, ByteBuf> map = HashMultimap.create();
- while (buffer.isReadable()) {
- final int type = buffer.readUnsignedShort();
- final int length = buffer.readUnsignedShort();
- final ByteBuf value = buffer.slice(buffer.readerIndex(), length);
- map.put(type, value);
- buffer.skipBytes(length);
- }
- final LinkstatePathAttributeBuilder builder = new LinkstatePathAttributeBuilder();
-
- switch (nlri) {
- case Ipv4Prefix:
- case Ipv6Prefix:
- builder.setLinkStateAttribute(parsePrefixAttributes(map));
- return builder.build();
- case Link:
- builder.setLinkStateAttribute(parseLinkAttributes(map));
- return builder.build();
- case Node:
- builder.setLinkStateAttribute(parseNodeAttributes(map));
- return builder.build();
- default:
- throw new IllegalStateException("Unhandled NLRI type " + nlri);
- }
- }
-
- /**
- * Parse Link Attributes.
- *
- * @param attributes key is the tlv type and value is the value of the tlv
- * @return {@link LinkStateAttribute}
- */
- private static LinkStateAttribute parseLinkAttributes(final Multimap<Integer, ByteBuf> attributes) {
- final LinkAttributesBuilder builder = new LinkAttributesBuilder();
- for (final Entry<Integer, ByteBuf> entry : attributes.entries()) {
- LOG.trace("Link attribute TLV {}", entry.getKey());
- final int key = entry.getKey();
- final ByteBuf value = entry.getValue();
- switch (key) {
- case TlvCode.LOCAL_IPV4_ROUTER_ID:
- final Ipv4RouterIdentifier lipv4 = new Ipv4RouterIdentifier(Ipv4Util.addressForByteBuf(value));
- builder.setLocalIpv4RouterId(lipv4);
- LOG.debug("Parsed IPv4 Router-ID of local node: {}", lipv4);
- break;
- case TlvCode.LOCAL_IPV6_ROUTER_ID:
- final Ipv6RouterIdentifier lipv6 = new Ipv6RouterIdentifier(Ipv6Util.addressForByteBuf(value));
- builder.setLocalIpv6RouterId(lipv6);
- LOG.debug("Parsed IPv6 Router-ID of local node: {}", lipv6);
- break;
- case TlvCode.REMOTE_IPV4_ROUTER_ID:
- final Ipv4RouterIdentifier ripv4 = new Ipv4RouterIdentifier(Ipv4Util.addressForByteBuf(value));
- builder.setRemoteIpv4RouterId(ripv4);
- LOG.debug("Parsed IPv4 Router-ID of remote node: {}", ripv4);
- break;
- case TlvCode.REMOTE_IPV6_ROUTER_ID:
- final Ipv6RouterIdentifier ripv6 = new Ipv6RouterIdentifier(Ipv6Util.addressForByteBuf(value));
- builder.setRemoteIpv6RouterId(ripv6);
- LOG.debug("Parsed IPv6 Router-ID of remote node: {}", ripv6);
- break;
- case TlvCode.ADMIN_GROUP:
- builder.setAdminGroup(new AdministrativeGroup(value.readUnsignedInt()));
- LOG.debug("Parsed Administrative Group {}", builder.getAdminGroup());
- break;
- case TlvCode.MAX_BANDWIDTH:
- builder.setMaxLinkBandwidth(new Bandwidth(ByteArray.readAllBytes(value)));
- LOG.debug("Parsed Max Bandwidth {}", builder.getMaxLinkBandwidth());
- break;
- case TlvCode.MAX_RESERVABLE_BANDWIDTH:
- builder.setMaxReservableBandwidth(new Bandwidth(ByteArray.readAllBytes(value)));
- LOG.debug("Parsed Max Reservable Bandwidth {}", builder.getMaxReservableBandwidth());
- break;
- case TlvCode.UNRESERVED_BANDWIDTH:
- final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.link.state.UnreservedBandwidth> unreservedBandwidth = new ArrayList<>(UNRESERVED_BW_COUNT);
- for (int i = 0; i < UNRESERVED_BW_COUNT; i++) {
- final ByteBuf v = value.slice(value.readerIndex(), BANDWIDTH_LENGTH);
- unreservedBandwidth.add(new UnreservedBandwidthBuilder().setBandwidth(new Bandwidth(ByteArray.readAllBytes(v))).setPriority((short) i).build());
- value.skipBytes(BANDWIDTH_LENGTH);
- }
- builder.setUnreservedBandwidth(unreservedBandwidth);
- LOG.debug("Parsed Unreserved Bandwidth {}", builder.getUnreservedBandwidth());
- break;
- case TlvCode.TE_METRIC:
- builder.setTeMetric(new TeMetric(ByteArray.bytesToLong(ByteArray.readAllBytes(value))));
- LOG.debug("Parsed Metric {}", builder.getTeMetric());
- break;
- case TlvCode.LINK_PROTECTION_TYPE:
- final int l = value.readShort();
- final LinkProtectionType lpt = LinkProtectionType.forValue(l);
- if (lpt == null) {
- LOG.warn("Link Protection Type not recognized: {}", l);
- break;
- }
- builder.setLinkProtection(lpt);
- LOG.debug("Parsed Link Protection Type {}", lpt);
- break;
- case TlvCode.MPLS_PROTOCOL:
- final BitSet bits = BitSet.valueOf(ByteArray.readAllBytes(value));
- builder.setMplsProtocol(new MplsProtocolMask(bits.get(LDP_BIT), bits.get(RSVP_BIT)));
- LOG.debug("Parsed MPLS Protocols: {}", builder.getMplsProtocol());
- break;
- case TlvCode.METRIC:
- // length can 3, 2 or 1
- builder.setMetric(new Metric(ByteArray.bytesToLong(ByteArray.readAllBytes(value))));
- LOG.debug("Parsed Metric {}", builder.getMetric());
- break;
- case TlvCode.SHARED_RISK_LINK_GROUP:
- final List<SrlgId> sharedRiskLinkGroups = new ArrayList<>();
- while (value.isReadable()) {
- sharedRiskLinkGroups.add(new SrlgId(value.readUnsignedInt()));
- }
- builder.setSharedRiskLinkGroups(sharedRiskLinkGroups);
- LOG.debug("Parsed Shared Risk Link Groups {}", Arrays.toString(sharedRiskLinkGroups.toArray()));
- break;
- case TlvCode.LINK_OPAQUE:
- LOG.debug("Parsed Opaque value : {}", ByteBufUtil.hexDump(value));
- break;
- case TlvCode.LINK_NAME:
- final String name = new String(ByteArray.readAllBytes(value), Charsets.US_ASCII);
- builder.setLinkName(name);
- LOG.debug("Parsed Link Name : {}", name);
- break;
- default:
- LOG.warn("TLV {} is not a valid link attribute, ignoring it", key);
- }
- }
- LOG.trace("Finished parsing Link Attributes.");
- return new LinkAttributesCaseBuilder().setLinkAttributes(builder.build()).build();
- }
-
- /**
- * Parse Node Attributes.
- *
- * @param attributes key is the tlv type and value is the value of the tlv
- * @return {@link LinkStateAttribute}
- */
- private static LinkStateAttribute parseNodeAttributes(final Multimap<Integer, ByteBuf> attributes) {
- final List<TopologyIdentifier> topologyMembership = Lists.newArrayList();
- final List<IsisAreaIdentifier> areaMembership = Lists.newArrayList();
- final NodeAttributesBuilder builder = new NodeAttributesBuilder();
- for (final Entry<Integer, ByteBuf> entry : attributes.entries()) {
- final int key = entry.getKey();
- final ByteBuf value = entry.getValue();
- LOG.trace("Node attribute TLV {}", key);
- switch (key) {
- case TlvCode.MULTI_TOPOLOGY_ID:
- while (value.isReadable()) {
- final TopologyIdentifier topId = new TopologyIdentifier(value.readUnsignedShort() & LinkstateNlriParser.TOPOLOGY_ID_OFFSET);
- topologyMembership.add(topId);
- LOG.debug("Parsed Topology Identifier: {}", topId);
- }
- break;
- case TlvCode.NODE_FLAG_BITS:
- final BitSet flags = BitSet.valueOf(ByteArray.readAllBytes(value));
- builder.setNodeFlags(new NodeFlagBits(flags.get(OVERLOAD_BIT), flags.get(ATTACHED_BIT), flags.get(EXTERNAL_BIT), flags.get(ABBR_BIT)));
- LOG.debug("Parsed Overload bit: {}, attached bit: {}, external bit: {}, area border router: {}.",
- flags.get(OVERLOAD_BIT), flags.get(ATTACHED_BIT), flags.get(EXTERNAL_BIT), flags.get(ABBR_BIT));
- break;
- case TlvCode.NODE_OPAQUE:
- LOG.debug("Ignoring opaque value: {}.", ByteBufUtil.hexDump(value));
- break;
- case TlvCode.DYNAMIC_HOSTNAME:
- builder.setDynamicHostname(new String(ByteArray.readAllBytes(value), Charsets.US_ASCII));
- LOG.debug("Parsed Node Name {}", builder.getDynamicHostname());
- break;
- case TlvCode.ISIS_AREA_IDENTIFIER:
- final IsisAreaIdentifier ai = new IsisAreaIdentifier(ByteArray.readAllBytes(value));
- areaMembership.add(ai);
- LOG.debug("Parsed AreaIdentifier {}", ai);
- break;
- case TlvCode.LOCAL_IPV4_ROUTER_ID:
- final Ipv4RouterIdentifier ip4 = new Ipv4RouterIdentifier(Ipv4Util.addressForByteBuf(value));
- builder.setIpv4RouterId(ip4);
- LOG.debug("Parsed IPv4 Router Identifier {}", ip4);
- break;
- case TlvCode.LOCAL_IPV6_ROUTER_ID:
- final Ipv6RouterIdentifier ip6 = new Ipv6RouterIdentifier(Ipv6Util.addressForByteBuf(value));
- builder.setIpv6RouterId(ip6);
- LOG.debug("Parsed IPv6 Router Identifier {}", ip6);
- break;
- default:
- LOG.warn("TLV {} is not a valid node attribute, ignoring it", key);
- }
- }
- LOG.trace("Finished parsing Node Attributes.");
- builder.setTopologyIdentifier(topologyMembership);
- builder.setIsisAreaId(areaMembership);
- return new NodeAttributesCaseBuilder().setNodeAttributes(builder.build()).build();
- }
-
- /**
- * Parse prefix attributes.
- *
- * @param attributes key is the tlv type and value are the value bytes of the tlv
- * @return {@link LinkStateAttribute}
- */
- private static LinkStateAttribute parsePrefixAttributes(final Multimap<Integer, ByteBuf> attributes) {
- final PrefixAttributesBuilder builder = new PrefixAttributesBuilder();
- final List<RouteTag> routeTags = Lists.newArrayList();
- final List<ExtendedRouteTag> exRouteTags = Lists.newArrayList();
- for (final Entry<Integer, ByteBuf> entry : attributes.entries()) {
- final int key = entry.getKey();
- final ByteBuf value = entry.getValue();
- LOG.trace("Prefix attribute TLV {}", key);
- switch (key) {
- case TlvCode.IGP_FLAGS:
- final BitSet flags = BitSet.valueOf(ByteArray.readAllBytes(value));
- final boolean upDownBit = flags.get(UP_DOWN_BIT);
- builder.setIgpBits(new IgpBitsBuilder().setUpDown(new UpDown(upDownBit)).build());
- LOG.debug("Parsed IGP flag (up/down bit) : {}", upDownBit);
- break;
- case TlvCode.ROUTE_TAG:
- while (value.isReadable()) {
- final RouteTag routeTag = new RouteTag(ByteArray.readBytes(value, ROUTE_TAG_LENGTH));
- routeTags.add(routeTag);
- LOG.debug("Parsed Route Tag: {}", routeTag);
- }
- break;
- case TlvCode.EXTENDED_ROUTE_TAG:
- while (value.isReadable()) {
- final ExtendedRouteTag exRouteTag = new ExtendedRouteTag(ByteArray.readBytes(value, EXTENDED_ROUTE_TAG_LENGTH));
- exRouteTags.add(exRouteTag);
- LOG.debug("Parsed Extended Route Tag: {}", exRouteTag);
- }
- break;
- case TlvCode.PREFIX_METRIC:
- final IgpMetric metric = new IgpMetric(value.readUnsignedInt());
- builder.setPrefixMetric(metric);
- LOG.debug("Parsed Metric: {}", metric);
- break;
- case TlvCode.FORWARDING_ADDRESS:
- final IpAddress fwdAddress = parseForwardingAddress(value);
- builder.setOspfForwardingAddress(fwdAddress);
- LOG.debug("Parsed FWD Address: {}", fwdAddress);
- break;
- case TlvCode.PREFIX_OPAQUE:
- LOG.debug("Parsed Opaque value: {}, not preserving it", ByteBufUtil.hexDump(value));
- break;
- default:
- LOG.warn("TLV {} is not a valid prefix attribute, ignoring it", key);
- }
- }
- LOG.trace("Finished parsing Prefix Attributes.");
- builder.setRouteTags(routeTags);
- builder.setExtendedTags(exRouteTags);
- return new PrefixAttributesCaseBuilder().setPrefixAttributes(builder.build()).build();
- }
-
- private static IpAddress parseForwardingAddress(final ByteBuf value) {
- IpAddress fwdAddress = null;
- switch (value.readableBytes()) {
- case Ipv4Util.IP4_LENGTH:
- fwdAddress = new IpAddress(Ipv4Util.addressForByteBuf(value));
- break;
- case Ipv6Util.IPV6_LENGTH:
- fwdAddress = new IpAddress(Ipv6Util.addressForByteBuf(value));
- break;
- default:
- LOG.debug("Ignoring unsupported forwarding address length {}", value.readableBytes());
- }
- return fwdAddress;
- }
-
- /**
- * Serialize linkstate attributes.
- *
- * @param attribute DataObject representing LinkstatePathAttribute
- * @param byteAggregator ByteBuf where all serialized data are aggregated
- */
-
- @Override
- public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
- Preconditions.checkArgument(attribute instanceof PathAttributes, "Attribute parameter is not a PathAttribute object.");
- final PathAttributes1 pathAttributes1 = ((PathAttributes) attribute).getAugmentation(PathAttributes1.class);
- if (pathAttributes1 == null) {
- return;
- }
- final LinkStateAttribute linkState = pathAttributes1.getLinkstatePathAttribute().getLinkStateAttribute();
- final ByteBuf lsBuffer = Unpooled.buffer();
- if (linkState instanceof LinkAttributesCase) {
- serializeLinkAttributes((LinkAttributesCase) linkState, lsBuffer);
- } else if (linkState instanceof NodeAttributesCase) {
- serializeNodeAttributes((NodeAttributesCase) linkState, lsBuffer);
- } else if (linkState instanceof PrefixAttributesCase) {
- serializePrefixAttributes((PrefixAttributesCase) linkState, lsBuffer);
- }
- AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL, getType(), lsBuffer, byteAggregator);
- }
-
- private void serializeLinkAttributes(final LinkAttributesCase linkAttributesCase, final ByteBuf byteAggregator) {
- final LinkAttributes linkAttributes = linkAttributesCase.getLinkAttributes();
- LOG.trace("Started serializing Link Attributes");
- if (linkAttributes.getLocalIpv4RouterId() != null) {
- writeTLV(TlvCode.LOCAL_IPV4_ROUTER_ID, Unpooled.wrappedBuffer(InetAddresses.forString(linkAttributes.getLocalIpv4RouterId().getValue()).getAddress()), byteAggregator);
- }
- if (linkAttributes.getLocalIpv6RouterId() != null) {
- writeTLV(TlvCode.LOCAL_IPV6_ROUTER_ID, Unpooled.wrappedBuffer(InetAddresses.forString(linkAttributes.getLocalIpv6RouterId().getValue()).getAddress()), byteAggregator);
- }
- if (linkAttributes.getRemoteIpv4RouterId() != null) {
- writeTLV(TlvCode.REMOTE_IPV4_ROUTER_ID, Unpooled.wrappedBuffer(InetAddresses.forString(linkAttributes.getRemoteIpv4RouterId().getValue()).getAddress()), byteAggregator);
- }
- if (linkAttributes.getRemoteIpv6RouterId() != null) {
- writeTLV(TlvCode.REMOTE_IPV6_ROUTER_ID, Unpooled.wrappedBuffer(InetAddresses.forString(linkAttributes.getRemoteIpv6RouterId().getValue()).getAddress()), byteAggregator);
- }
- if (linkAttributes.getAdminGroup() != null) {
- writeTLV(TlvCode.ADMIN_GROUP, Unpooled.copyInt(linkAttributes.getAdminGroup().getValue().intValue()), byteAggregator);
- }
- if (linkAttributes.getMaxLinkBandwidth() != null) {
- writeTLV(TlvCode.MAX_BANDWIDTH, Unpooled.wrappedBuffer(linkAttributes.getMaxLinkBandwidth().getValue()), byteAggregator);
- }
- if (linkAttributes.getMaxReservableBandwidth() != null) {
- writeTLV(TlvCode.MAX_RESERVABLE_BANDWIDTH, Unpooled.wrappedBuffer(linkAttributes.getMaxReservableBandwidth().getValue()), byteAggregator);
- }
- // this sub-TLV contains eight 32-bit IEEE floating point numbers
- final List<UnreservedBandwidth> ubList = linkAttributes.getUnreservedBandwidth();
- if (ubList != null && !ubList.isEmpty()) {
- final ByteBuf unreservedBandwithBuf = Unpooled.buffer();
- for (final UnreservedBandwidth unreservedBandwidth : ubList) {
- unreservedBandwithBuf.writeBytes(unreservedBandwidth.getBandwidth().getValue());
- }
- writeTLV(TlvCode.UNRESERVED_BANDWIDTH, unreservedBandwithBuf, byteAggregator);
- }
- if (linkAttributes.getTeMetric() != null) {
- writeTLV(TlvCode.TE_METRIC, Unpooled.copyLong(linkAttributes.getTeMetric().getValue().longValue()), byteAggregator);
- }
- if (linkAttributes.getLinkProtection() != null) {
- writeTLV(TlvCode.LINK_PROTECTION_TYPE, Unpooled.copyShort(linkAttributes.getLinkProtection().getIntValue()), byteAggregator);
- }
- serializeMplsProtocolMask(linkAttributes.getMplsProtocol(), byteAggregator);
- if (linkAttributes.getMetric() != null) {
- // size of metric can be 1,2 or 3 depending on the protocol
- writeTLV(TlvCode.METRIC, Unpooled.copyMedium(linkAttributes.getMetric().getValue().intValue()), byteAggregator);
- }
- final List<SrlgId> srlgList = linkAttributes.getSharedRiskLinkGroups();
- if (srlgList != null && !srlgList.isEmpty()) {
- final ByteBuf sharedRLGBuf = Unpooled.buffer();
- for (final SrlgId srlgId : srlgList) {
- sharedRLGBuf.writeInt(srlgId.getValue().intValue());
- }
- writeTLV(TlvCode.SHARED_RISK_LINK_GROUP, sharedRLGBuf, byteAggregator);
- }
- if (linkAttributes.getLinkName() != null) {
- writeTLV(TlvCode.LINK_NAME, Unpooled.wrappedBuffer(Charsets.UTF_8.encode(linkAttributes.getLinkName())), byteAggregator);
- }
- LOG.trace("Finished serializing Link Attributes");
- }
-
- private static void serializeMplsProtocolMask(final MplsProtocolMask mplsProtocolMask, final ByteBuf byteAggregator ) {
- if (mplsProtocolMask != null) {
- final ByteBuf mplsProtocolMaskBuf = Unpooled.buffer();
- final BitSet mask = new BitSet();
- if (mplsProtocolMask.isLdp() != null) {
- mask.set(LDP_BIT, mplsProtocolMask.isLdp());
- }
- if (mplsProtocolMask.isRsvpte() != null) {
- mask.set(RSVP_BIT, mplsProtocolMask.isRsvpte());
- }
- mplsProtocolMaskBuf.writeBytes(mask.toByteArray());
- writeTLV(TlvCode.MPLS_PROTOCOL, mplsProtocolMaskBuf, byteAggregator);
- }
- }
-
- private void serializeNodeAttributes(final NodeAttributesCase nodeAttributesCase, final ByteBuf byteAggregator) {
- LOG.trace("Started serializing Node Attributes");
- final NodeAttributes nodeAttributes = nodeAttributesCase.getNodeAttributes();
- final List<TopologyIdentifier> topList = nodeAttributes.getTopologyIdentifier();
- if (topList != null && !topList.isEmpty()) {
- final ByteBuf mpIdBuf = Unpooled.buffer();
- for (final TopologyIdentifier topologyIdentifier : topList) {
- mpIdBuf.writeShort(topologyIdentifier.getValue());
- }
- writeTLV(TlvCode.MULTI_TOPOLOGY_ID, mpIdBuf, byteAggregator);
- }
- serializeNodeFlagBits(nodeAttributes.getNodeFlags(), byteAggregator);
- if (nodeAttributes.getDynamicHostname() != null) {
- writeTLV(TlvCode.DYNAMIC_HOSTNAME, Unpooled.wrappedBuffer(Charsets.UTF_8.encode(nodeAttributes.getDynamicHostname())), byteAggregator);
- }
- final List<IsisAreaIdentifier> isisList = nodeAttributes.getIsisAreaId();
- if (isisList != null && !isisList.isEmpty()) {
- for (final IsisAreaIdentifier isisAreaIdentifier : isisList) {
- writeTLV(TlvCode.ISIS_AREA_IDENTIFIER, Unpooled.wrappedBuffer(isisAreaIdentifier.getValue()), byteAggregator);
- }
- }
- if (nodeAttributes.getIpv4RouterId() != null) {
- writeTLV(TlvCode.LOCAL_IPV4_ROUTER_ID, Unpooled.wrappedBuffer(InetAddresses.forString(nodeAttributes.getIpv4RouterId().getValue()).getAddress()), byteAggregator);
- }
- if (nodeAttributes.getIpv6RouterId() != null) {
- writeTLV(TlvCode.LOCAL_IPV6_ROUTER_ID, Unpooled.wrappedBuffer(InetAddresses.forString(nodeAttributes.getIpv6RouterId().getValue()).getAddress()), byteAggregator);
- }
- LOG.trace("Finished serializing Node Attributes");
- }
-
- private static void serializeNodeFlagBits(final NodeFlagBits nodeFlagBits, final ByteBuf byteAggregator) {
- if (nodeFlagBits != null) {
- final ByteBuf nodeFlagBuf = Unpooled.buffer();
- final BitSet flags = new BitSet();
- if (nodeFlagBits.isOverload() != null) {
- flags.set(OVERLOAD_BIT, nodeFlagBits.isOverload());
- }
- if (nodeFlagBits.isAttached() != null) {
- flags.set(ATTACHED_BIT, nodeFlagBits.isAttached());
- }
- if (nodeFlagBits.isExternal() != null) {
- flags.set(EXTERNAL_BIT, nodeFlagBits.isExternal());
- }
- if (nodeFlagBits.isAbr() != null) {
- flags.set(ABBR_BIT, nodeFlagBits.isAbr());
- }
- nodeFlagBuf.writeBytes(flags.toByteArray());
- writeTLV(TlvCode.NODE_FLAG_BITS, nodeFlagBuf, byteAggregator);
- }
- }
-
- private void serializePrefixAttributes(final PrefixAttributesCase prefixAttributesCase, final ByteBuf byteAggregator) {
- final PrefixAttributes prefixAtrributes = prefixAttributesCase.getPrefixAttributes();
- if (prefixAtrributes.getIgpBits() != null) {
- final BitSet igpBit = new BitSet();
- final Boolean bit = prefixAtrributes.getIgpBits().getUpDown().isUpDown();
- if (bit != null) {
- igpBit.set(UP_DOWN_BIT, bit);
- }
- writeTLV(TlvCode.IGP_FLAGS, Unpooled.wrappedBuffer(igpBit.toByteArray()), byteAggregator);
- }
- if (prefixAtrributes.getRouteTags() != null) {
- final ByteBuf routeTagsBuf = Unpooled.buffer();
- for (final RouteTag routeTag : prefixAtrributes.getRouteTags()) {
- routeTagsBuf.writeBytes(routeTag.getValue());
- }
- writeTLV(TlvCode.ROUTE_TAG, routeTagsBuf, byteAggregator);
- }
- final List<ExtendedRouteTag> routeTagList = prefixAtrributes.getExtendedTags();
- if (routeTagList != null && !routeTagList.isEmpty()) {
- final ByteBuf extendedBuf = Unpooled.buffer();
- for (final ExtendedRouteTag extendedRouteTag : routeTagList) {
- extendedBuf.writeBytes(extendedRouteTag.getValue());
- }
- writeTLV(TlvCode.EXTENDED_ROUTE_TAG, extendedBuf, byteAggregator);
- }
- if (prefixAtrributes.getPrefixMetric() != null) {
- writeTLV(TlvCode.PREFIX_METRIC, Unpooled.copyInt(prefixAtrributes.getPrefixMetric().getValue().intValue()), byteAggregator);
- }
- serializeForwardingAddress(prefixAtrributes.getOspfForwardingAddress(), byteAggregator);
- }
-
- private static void serializeForwardingAddress(final IpAddress forwardingAddress, final ByteBuf byteAggregator) {
- if (forwardingAddress != null) {
- final ByteBuf ospfBuf = Unpooled.buffer();
- if (forwardingAddress.getIpv4Address() != null) {
- ospfBuf.writeBytes(Ipv4Util.bytesForAddress(forwardingAddress.getIpv4Address()));
- } else if (forwardingAddress.getIpv6Address() != null) {
- ospfBuf.writeBytes(Ipv6Util.bytesForAddress(forwardingAddress.getIpv6Address()));
- }
- writeTLV(TlvCode.FORWARDING_ADDRESS, ospfBuf, byteAggregator);
- }
- }
-
- /**
- * Util method for writing TLV header.
- * @param type TLV type
- * @param value TLV value
- * @param byteAggregator final ByteBuf where the tlv should be serialized
- */
- protected static void writeTLV(final int type, final ByteBuf value, final ByteBuf byteAggregator){
- byteAggregator.writeShort(type);
- byteAggregator.writeShort(value.writerIndex());
- byteAggregator.writeBytes(value);
- value.readerIndex(0);
- LOG.debug("Serialized tlv type {} to: {}", type, ByteBufUtil.hexDump(value));
- }
-}
+++ /dev/null
-/*
- * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.protocol.bgp.linkstate;
-
-import com.google.common.base.Preconditions;
-import com.google.common.collect.Lists;
-import com.google.common.primitives.UnsignedBytes;
-import com.google.common.primitives.UnsignedInteger;
-import io.netty.buffer.ByteBuf;
-import io.netty.buffer.ByteBufUtil;
-import io.netty.buffer.Unpooled;
-import java.math.BigInteger;
-import java.util.List;
-import org.opendaylight.protocol.bgp.parser.BGPParsingException;
-import org.opendaylight.protocol.bgp.parser.spi.NlriParser;
-import org.opendaylight.protocol.bgp.parser.spi.NlriSerializer;
-import org.opendaylight.protocol.util.ByteArray;
-import org.opendaylight.protocol.util.Ipv4Util;
-import org.opendaylight.protocol.util.Ipv6Util;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.AreaIdentifier;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.DomainIdentifier;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.Identifier;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.Ipv4InterfaceIdentifier;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.Ipv6InterfaceIdentifier;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.NlriType;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.NodeIdentifier;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.OspfInterfaceIdentifier;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.OspfRouteType;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.ProtocolId;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.RouteDistinguisher;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.TopologyIdentifier;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.isis.lan.identifier.IsIsRouterIdentifier;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.isis.lan.identifier.IsIsRouterIdentifierBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.destination.CLinkstateDestination;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.destination.CLinkstateDestinationBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.destination.c.linkstate.destination.LinkDescriptors;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.destination.c.linkstate.destination.LinkDescriptorsBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.destination.c.linkstate.destination.LocalNodeDescriptors;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.destination.c.linkstate.destination.LocalNodeDescriptorsBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.destination.c.linkstate.destination.PrefixDescriptors;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.destination.c.linkstate.destination.PrefixDescriptorsBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.destination.c.linkstate.destination.RemoteNodeDescriptors;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.destination.c.linkstate.destination.RemoteNodeDescriptorsBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.CRouterIdentifier;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.IsisNodeCase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.IsisNodeCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.IsisPseudonodeCase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.IsisPseudonodeCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.OspfNodeCase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.OspfNodeCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.OspfPseudonodeCase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.OspfPseudonodeCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.isis.node._case.IsisNode;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.isis.node._case.IsisNodeBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.isis.pseudonode._case.IsisPseudonode;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.isis.pseudonode._case.IsisPseudonodeBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.ospf.node._case.OspfNodeBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.ospf.pseudonode._case.OspfPseudonode;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.ospf.pseudonode._case.OspfPseudonodeBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLinkstateCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.linkstate._case.DestinationLinkstateBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationLinkstateCase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributes;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes1;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes2;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpReachNlriBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpUnreachNlri;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpUnreachNlriBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.mp.reach.nlri.AdvertizedRoutes;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.IsoSystemIdentifier;
-import org.opendaylight.yangtools.yang.binding.DataObject;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * Parser and serializer for Linkstate NLRI.
- */
-public final class LinkstateNlriParser implements NlriParser, NlriSerializer {
- private static final Logger LOG = LoggerFactory.getLogger(LinkstateNlriParser.class);
- private static final int ROUTE_DISTINGUISHER_LENGTH = 8;
- private static final int PROTOCOL_ID_LENGTH = 1;
- private static final int IDENTIFIER_LENGTH = 8;
- private static final int OSPF_PSEUDONODE_ROUTER_ID_LENGTH = 8;
- private static final int OSPF_ROUTER_ID_LENGTH = 4;
- private static final int ISO_SYSTEM_ID_LENGTH = 6;
- private static final int PSN_LENGTH = 1;
-
- private static final int TYPE_LENGTH = 2;
- private static final int LENGTH_SIZE = 2;
-
- static final int TOPOLOGY_ID_OFFSET = 0x3fff;
-
- private final boolean isVpn;
-
- public LinkstateNlriParser(final boolean isVpn) {
- this.isVpn = isVpn;
- }
-
- private static NodeIdentifier parseLink(final CLinkstateDestinationBuilder builder, final ByteBuf buffer) throws BGPParsingException {
- final int type = buffer.readUnsignedShort();
- final int length = buffer.readUnsignedShort();
- final NodeIdentifier remote = null;
- if (type == TlvCode.REMOTE_NODE_DESCRIPTORS) {
- builder.setRemoteNodeDescriptors((RemoteNodeDescriptors) parseNodeDescriptors(buffer.slice(buffer.readerIndex(), length), false));
- buffer.skipBytes(length);
- }
- builder.setLinkDescriptors(parseLinkDescriptors(buffer.slice()));
- return remote;
- }
-
- private static LinkDescriptors parseLinkDescriptors(final ByteBuf buffer) throws BGPParsingException {
- final LinkDescriptorsBuilder builder = new LinkDescriptorsBuilder();
- while (buffer.isReadable()) {
- final int type = buffer.readUnsignedShort();
- final int length = buffer.readUnsignedShort();
- final ByteBuf value = buffer.slice(buffer.readerIndex(), length);
- LOG.trace("Parsing Link Descriptor: {}", ByteBufUtil.hexDump(value));
- switch (type) {
- case TlvCode.LINK_LR_IDENTIFIERS:
- builder.setLinkLocalIdentifier(value.readUnsignedInt());
- builder.setLinkRemoteIdentifier(value.readUnsignedInt());
- LOG.debug("Parsed link local {} remote {} Identifiers.", builder.getLinkLocalIdentifier(),
- builder.getLinkRemoteIdentifier());
- break;
- case TlvCode.IPV4_IFACE_ADDRESS:
- final Ipv4InterfaceIdentifier lipv4 = new Ipv4InterfaceIdentifier(Ipv4Util.addressForByteBuf(value));
- builder.setIpv4InterfaceAddress(lipv4);
- LOG.debug("Parsed IPv4 interface address {}.", lipv4);
- break;
- case TlvCode.IPV4_NEIGHBOR_ADDRESS:
- final Ipv4InterfaceIdentifier ripv4 = new Ipv4InterfaceIdentifier(Ipv4Util.addressForByteBuf(value));
- builder.setIpv4NeighborAddress(ripv4);
- LOG.debug("Parsed IPv4 neighbor address {}.", ripv4);
- break;
- case TlvCode.IPV6_IFACE_ADDRESS:
- final Ipv6InterfaceIdentifier lipv6 = new Ipv6InterfaceIdentifier(Ipv6Util.addressForByteBuf(value));
- builder.setIpv6InterfaceAddress(lipv6);
- LOG.debug("Parsed IPv6 interface address {}.", lipv6);
- break;
- case TlvCode.IPV6_NEIGHBOR_ADDRESS:
- final Ipv6InterfaceIdentifier ripv6 = new Ipv6InterfaceIdentifier(Ipv6Util.addressForByteBuf(value));
- builder.setIpv6NeighborAddress(ripv6);
- LOG.debug("Parsed IPv6 neighbor address {}.", ripv6);
- break;
- case TlvCode.MULTI_TOPOLOGY_ID:
- final TopologyIdentifier topId = new TopologyIdentifier(value.readUnsignedShort() & LinkstateNlriParser.TOPOLOGY_ID_OFFSET);
- builder.setMultiTopologyId(topId);
- LOG.debug("Parsed topology identifier {}.", topId);
- break;
- default:
- throw new BGPParsingException("Link Descriptor not recognized, type: " + type);
- }
- buffer.skipBytes(length);
- }
- LOG.trace("Finished parsing Link descriptors.");
- return builder.build();
- }
-
- private static NodeIdentifier parseNodeDescriptors(final ByteBuf buffer, final boolean local) throws BGPParsingException {
- AsNumber asnumber = null;
- DomainIdentifier bgpId = null;
- AreaIdentifier ai = null;
- CRouterIdentifier routerId = null;
- while (buffer.isReadable()) {
- final int type = buffer.readUnsignedShort();
- final int length = buffer.readUnsignedShort();
- final ByteBuf value = buffer.slice(buffer.readerIndex(), length);
- LOG.trace("Parsing Node Descriptor: {}", ByteBufUtil.hexDump(value));
- switch (type) {
- case TlvCode.AS_NUMBER:
- asnumber = new AsNumber(value.readUnsignedInt());
- LOG.debug("Parsed {}", asnumber);
- break;
- case TlvCode.BGP_LS_ID:
- bgpId = new DomainIdentifier(value.readUnsignedInt());
- LOG.debug("Parsed {}", bgpId);
- break;
- case TlvCode.AREA_ID:
- ai = new AreaIdentifier(value.readUnsignedInt());
- LOG.debug("Parsed area identifier {}", ai);
- break;
- case TlvCode.IGP_ROUTER_ID:
- routerId = parseRouterId(value);
- LOG.debug("Parsed Router Identifier {}", routerId);
- break;
- default:
- throw new BGPParsingException("Node Descriptor not recognized, type: " + type);
- }
- buffer.skipBytes(length);
- }
- LOG.trace("Finished parsing Node descriptors.");
- return (local) ? new LocalNodeDescriptorsBuilder().setAsNumber(asnumber).setDomainId(bgpId).setAreaId(ai).setCRouterIdentifier(
- routerId).build()
- : new RemoteNodeDescriptorsBuilder().setAsNumber(asnumber).setDomainId(bgpId).setAreaId(ai).setCRouterIdentifier(routerId).build();
- }
-
- private static CRouterIdentifier parseRouterId(final ByteBuf value) throws BGPParsingException {
- if (value.readableBytes() == ISO_SYSTEM_ID_LENGTH || (value.readableBytes() == ISO_SYSTEM_ID_LENGTH + PSN_LENGTH && value.getByte(ISO_SYSTEM_ID_LENGTH) == 0)) {
- return new IsisNodeCaseBuilder().setIsisNode(
- new IsisNodeBuilder().setIsoSystemId(new IsoSystemIdentifier(ByteArray.readBytes(value, ISO_SYSTEM_ID_LENGTH))).build()).build();
- }
- if (value.readableBytes() == ISO_SYSTEM_ID_LENGTH + PSN_LENGTH) {
- final IsIsRouterIdentifier iri = new IsIsRouterIdentifierBuilder().setIsoSystemId(
- new IsoSystemIdentifier(ByteArray.readBytes(value, ISO_SYSTEM_ID_LENGTH))).build();
- return new IsisPseudonodeCaseBuilder().setIsisPseudonode(new IsisPseudonodeBuilder().setIsIsRouterIdentifier(iri).setPsn((short) value.readByte()).build()).build();
- }
- if (value.readableBytes() == OSPF_ROUTER_ID_LENGTH) {
- return new OspfNodeCaseBuilder().setOspfNode(
- new OspfNodeBuilder().setOspfRouterId(value.readUnsignedInt()).build()).build();
- }
- if (value.readableBytes() == OSPF_PSEUDONODE_ROUTER_ID_LENGTH) {
- return new OspfPseudonodeCaseBuilder().setOspfPseudonode(
- new OspfPseudonodeBuilder().setOspfRouterId(value.readUnsignedInt()).setLanInterface(new OspfInterfaceIdentifier(value.readUnsignedInt())).build()).build();
- }
- throw new BGPParsingException("Router Id of invalid length " + value.readableBytes());
- }
-
- private static PrefixDescriptors parsePrefixDescriptors(final ByteBuf buffer, final boolean ipv4) throws BGPParsingException {
- final PrefixDescriptorsBuilder builder = new PrefixDescriptorsBuilder();
- while (buffer.isReadable()) {
- final int type = buffer.readUnsignedShort();
- final int length = buffer.readUnsignedShort();
- final ByteBuf value = buffer.slice(buffer.readerIndex(), length);
- LOG.trace("Parsing Prefix Descriptor: {}", ByteBufUtil.hexDump(value));
- switch (type) {
- case TlvCode.MULTI_TOPOLOGY_ID:
- final TopologyIdentifier topologyId = new TopologyIdentifier(value.readShort() & LinkstateNlriParser.TOPOLOGY_ID_OFFSET);
- builder.setMultiTopologyId(topologyId);
- LOG.trace("Parsed Topology Identifier: {}", topologyId);
- break;
- case TlvCode.OSPF_ROUTE_TYPE:
- final int rt = value.readByte();
- final OspfRouteType routeType = OspfRouteType.forValue(rt);
- if (routeType == null) {
- throw new BGPParsingException("Unknown OSPF Route Type: " + rt);
- }
- builder.setOspfRouteType(routeType);
- LOG.trace("Parser RouteType: {}", routeType);
- break;
- case TlvCode.IP_REACHABILITY:
- IpPrefix prefix = null;
- final int prefixLength = value.readByte();
- final int size = prefixLength / Byte.SIZE + ((prefixLength % Byte.SIZE == 0) ? 0 : 1);
- if (size != value.readableBytes()) {
- LOG.debug("Expected length {}, actual length {}.", size, value.readableBytes());
- throw new BGPParsingException("Illegal length of IP reachability TLV: " + (value.readableBytes()));
- }
- if (ipv4) {
- prefix = new IpPrefix(Ipv4Util.prefixForBytes(ByteArray.readBytes(value, size), prefixLength));
- } else {
- prefix = new IpPrefix(Ipv6Util.prefixForBytes(ByteArray.readBytes(value, size), prefixLength));
- }
- builder.setIpReachabilityInformation(prefix);
- LOG.trace("Parsed IP reachability info: {}", prefix);
- break;
- default:
- throw new BGPParsingException("Prefix Descriptor not recognized, type: " + type);
- }
- buffer.skipBytes(length);
- }
- LOG.debug("Finished parsing Prefix descriptors.");
- return builder.build();
- }
-
- /**
- * Parses common parts for Link State Nodes, Links and Prefixes, that includes protocol ID and identifier tlv.
- *
- * @param nlri as byte array
- * @return {@link CLinkstateDestination}
- * @throws BGPParsingException if parsing was unsuccessful
- */
- public static List<CLinkstateDestination> parseNlri(final ByteBuf nlri, final boolean isVpn) throws BGPParsingException {
- if (!nlri.isReadable()) {
- return null;
- }
- final List<CLinkstateDestination> dests = Lists.newArrayList();
-
- CLinkstateDestinationBuilder builder = null;
-
- while (nlri.isReadable()) {
- builder = new CLinkstateDestinationBuilder();
- final NlriType type = NlriType.forValue(nlri.readUnsignedShort());
- builder.setNlriType(type);
-
- // length means total length of the tlvs including route distinguisher not including the type field
- final int length = nlri.readUnsignedShort();
- RouteDistinguisher distinguisher = null;
- if (isVpn) {
- // this parses route distinguisher
- distinguisher = new RouteDistinguisher(BigInteger.valueOf(nlri.readLong()));
- builder.setDistinguisher(distinguisher);
- }
- // parse source protocol
- final ProtocolId sp = ProtocolId.forValue(nlri.readByte());
- builder.setProtocolId(sp);
-
- // parse identifier
- final Identifier identifier = new Identifier(BigInteger.valueOf(nlri.readLong()));
- builder.setIdentifier(identifier);
-
- // if we are dealing with linkstate nodes/links, parse local node descriptor
- NodeIdentifier localDescriptor = null;
- int locallength = 0;
- final int localtype = nlri.readUnsignedShort();
- locallength = nlri.readUnsignedShort();
- if (localtype == TlvCode.LOCAL_NODE_DESCRIPTORS) {
- localDescriptor = parseNodeDescriptors(nlri.slice(nlri.readerIndex(), locallength), true);
- }
- nlri.skipBytes(locallength);
- builder.setLocalNodeDescriptors((LocalNodeDescriptors) localDescriptor);
- final int restLength = length - (isVpn ? ROUTE_DISTINGUISHER_LENGTH : 0) - PROTOCOL_ID_LENGTH - IDENTIFIER_LENGTH
- - TYPE_LENGTH - LENGTH_SIZE - locallength;
- LOG.trace("Restlength {}", restLength);
- final ByteBuf rest = nlri.slice(nlri.readerIndex(), restLength);
- switch (type) {
- case Link:
- parseLink(builder, rest);
- break;
- case Ipv4Prefix:
- builder.setPrefixDescriptors(parsePrefixDescriptors(rest, true));
- break;
- case Ipv6Prefix:
- builder.setPrefixDescriptors(parsePrefixDescriptors(rest, false));
- break;
- case Node:
- // node nlri is already parsed as it contains only the common fields for node and link nlri
- break;
- default:
- break;
- }
- nlri.skipBytes(restLength);
- dests.add(builder.build());
- }
- return dests;
- }
-
- @Override
- public void parseNlri(final ByteBuf nlri, final MpUnreachNlriBuilder builder) throws BGPParsingException {
- if (!nlri.isReadable()) {
- return;
- }
- final List<CLinkstateDestination> dst = parseNlri(nlri, this.isVpn);
-
- builder.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setDestinationType(
- new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationLinkstateCaseBuilder().setDestinationLinkstate(
- new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.destination.linkstate._case.DestinationLinkstateBuilder().setCLinkstateDestination(
- dst).build()).build()).build());
- }
-
- @Override
- public void parseNlri(final ByteBuf nlri, final MpReachNlriBuilder builder) throws BGPParsingException {
- if (!nlri.isReadable()) {
- return;
- }
- final List<CLinkstateDestination> dst = parseNlri(nlri, this.isVpn);
-
- builder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
- new DestinationLinkstateCaseBuilder().setDestinationLinkstate(
- new DestinationLinkstateBuilder().setCLinkstateDestination(dst).build()).build()).build());
- }
-
- /**
- * Serializes Linkstate NLRI to byte array. We need this as NLRI serves as a key in upper layers.
- *
- * @param destination Linkstate NLRI to be serialized
- */
- public static void serializeNlri(final CLinkstateDestination destination, final ByteBuf buffer) {
- final ByteBuf nlriByteBuf = Unpooled.buffer();
- if (destination.getDistinguisher() != null) {
- nlriByteBuf.writeBytes(destination.getDistinguisher().getValue().toByteArray());
- }
- nlriByteBuf.writeByte(destination.getProtocolId().getIntValue());
- nlriByteBuf.writeLong(destination.getIdentifier().getValue().longValue());
-
- // serialize local node descriptors
- final ByteBuf ldescs = Unpooled.buffer();
- serializeNodeDescriptors(destination.getLocalNodeDescriptors(), ldescs);
- LinkstateAttributeParser.writeTLV(TlvCode.LOCAL_NODE_DESCRIPTORS, ldescs, nlriByteBuf);
-
- switch (destination.getNlriType()) {
- case Ipv4Prefix:
- case Ipv6Prefix:
- if (destination.getPrefixDescriptors() != null) {
- serializePrefixDescriptors(destination.getPrefixDescriptors(), nlriByteBuf);
- }
- break;
- case Link:
- final ByteBuf rdescs = Unpooled.buffer();
- serializeNodeDescriptors(destination.getRemoteNodeDescriptors(), rdescs);
- LinkstateAttributeParser.writeTLV(TlvCode.REMOTE_NODE_DESCRIPTORS, rdescs, nlriByteBuf);
- if (destination.getLinkDescriptors() != null) {
- serializeLinkDescriptors(destination.getLinkDescriptors(), nlriByteBuf);
- }
- break;
- case Node:
- break;
- default:
- LOG.warn("Unknown NLRI Type.");
- break;
- }
- LinkstateAttributeParser.writeTLV(destination.getNlriType().getIntValue(), nlriByteBuf, buffer);
- }
-
- private static void serializeNodeDescriptors(final NodeIdentifier descriptors, final ByteBuf buffer) {
- if (descriptors.getAsNumber() != null) {
- LinkstateAttributeParser.writeTLV(TlvCode.AS_NUMBER, Unpooled.copyInt(UnsignedInteger.valueOf(descriptors.getAsNumber().getValue()).intValue()), buffer);
- }
- if (descriptors.getDomainId() != null) {
- LinkstateAttributeParser.writeTLV(TlvCode.BGP_LS_ID, Unpooled.copyInt(UnsignedInteger.valueOf(descriptors.getDomainId().getValue()).intValue()), buffer);
- }
- if (descriptors.getAreaId() != null) {
- LinkstateAttributeParser.writeTLV(TlvCode.AREA_ID, Unpooled.copyInt(UnsignedInteger.valueOf(descriptors.getAreaId().getValue()).intValue()), buffer);
- }
- if (descriptors.getCRouterIdentifier() != null) {
- final ByteBuf routerIdBuf = Unpooled.buffer();
- serializeRouterId(descriptors.getCRouterIdentifier(), routerIdBuf);
- LinkstateAttributeParser.writeTLV(TlvCode.IGP_ROUTER_ID, routerIdBuf, buffer);
- }
- }
-
- private static void serializeRouterId(final CRouterIdentifier routerId, final ByteBuf buffer) {
- if (routerId instanceof IsisNodeCase) {
- final IsisNode isis = ((IsisNodeCase) routerId).getIsisNode();
- buffer.writeBytes(isis.getIsoSystemId().getValue());
- } else if (routerId instanceof IsisPseudonodeCase) {
- final IsisPseudonode isis = ((IsisPseudonodeCase) routerId).getIsisPseudonode();
- buffer.writeBytes(isis.getIsIsRouterIdentifier().getIsoSystemId().getValue());
- buffer.writeByte(((isis.getPsn() != null) ? isis.getPsn() : 0));
- } else if (routerId instanceof OspfNodeCase) {
- buffer.writeInt(UnsignedInteger.valueOf(((OspfNodeCase) routerId).getOspfNode().getOspfRouterId()).intValue());
- } else if (routerId instanceof OspfPseudonodeCase) {
- final OspfPseudonode node = ((OspfPseudonodeCase) routerId).getOspfPseudonode();
- buffer.writeInt(UnsignedInteger.valueOf(node.getOspfRouterId()).intValue());
- buffer.writeInt(UnsignedInteger.valueOf(node.getLanInterface().getValue()).intValue());
- }
- }
-
- private static void serializeLinkDescriptors(final LinkDescriptors descriptors, final ByteBuf buffer) {
- if (descriptors.getLinkLocalIdentifier() != null && descriptors.getLinkRemoteIdentifier() != null) {
- final ByteBuf identifierBuf = Unpooled.buffer();
- identifierBuf.writeInt(descriptors.getLinkLocalIdentifier().intValue());
- identifierBuf.writeInt(descriptors.getLinkRemoteIdentifier().intValue());
- LinkstateAttributeParser.writeTLV(TlvCode.LINK_LR_IDENTIFIERS, identifierBuf, buffer);
- }
- if (descriptors.getIpv4InterfaceAddress() != null) {
- LinkstateAttributeParser.writeTLV(TlvCode.IPV4_IFACE_ADDRESS, Unpooled.wrappedBuffer(Ipv4Util.bytesForAddress(descriptors.getIpv4InterfaceAddress())), buffer);
- }
- if (descriptors.getIpv4NeighborAddress() != null) {
- LinkstateAttributeParser.writeTLV(TlvCode.IPV4_NEIGHBOR_ADDRESS, Unpooled.wrappedBuffer(Ipv4Util.bytesForAddress(descriptors.getIpv4NeighborAddress())), buffer);
- }
- if (descriptors.getIpv6InterfaceAddress() != null) {
- LinkstateAttributeParser.writeTLV(TlvCode.IPV6_IFACE_ADDRESS, Unpooled.wrappedBuffer(Ipv6Util.bytesForAddress(descriptors.getIpv6InterfaceAddress())), buffer);
- }
- if (descriptors.getIpv6NeighborAddress() != null) {
- LinkstateAttributeParser.writeTLV(TlvCode.IPV6_NEIGHBOR_ADDRESS, Unpooled.wrappedBuffer(Ipv6Util.bytesForAddress(descriptors.getIpv6NeighborAddress())), buffer);
- }
- if (descriptors.getMultiTopologyId() != null) {
- LinkstateAttributeParser.writeTLV(TlvCode.MULTI_TOPOLOGY_ID, Unpooled.copyShort(descriptors.getMultiTopologyId().getValue()), buffer);
- }
- }
-
- private static void serializePrefixDescriptors(final PrefixDescriptors descriptors, final ByteBuf buffer) {
- if (descriptors.getMultiTopologyId() != null) {
- LinkstateAttributeParser.writeTLV(TlvCode.MULTI_TOPOLOGY_ID, Unpooled.copyShort(descriptors.getMultiTopologyId().getValue()), buffer);
- }
- if (descriptors.getOspfRouteType() != null) {
- LinkstateAttributeParser.writeTLV(TlvCode.OSPF_ROUTE_TYPE,
- Unpooled.wrappedBuffer(new byte[] {UnsignedBytes.checkedCast(descriptors.getOspfRouteType().getIntValue()) }), buffer);
- }
- if (descriptors.getIpReachabilityInformation() != null) {
- final IpPrefix prefix = descriptors.getIpReachabilityInformation();
- byte[] prefixBytes = null;
- if (prefix.getIpv4Prefix() != null) {
- prefixBytes = Ipv4Util.bytesForPrefixBegin(prefix.getIpv4Prefix());
- } else if (prefix.getIpv6Prefix() != null) {
- prefixBytes = Ipv6Util.bytesForPrefixBegin(prefix.getIpv6Prefix());
- }
- LinkstateAttributeParser.writeTLV(TlvCode.IP_REACHABILITY, Unpooled.wrappedBuffer(prefixBytes), buffer);
- }
- }
-
- @Override
- public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
- Preconditions.checkArgument(attribute instanceof PathAttributes, "Attribute parameter is not a PathAttribute object.");
- final PathAttributes pathAttributes = (PathAttributes) attribute;
- final PathAttributes1 pathAttributes1 = pathAttributes.getAugmentation(PathAttributes1.class);
- final PathAttributes2 pathAttributes2 = pathAttributes.getAugmentation(PathAttributes2.class);
- if (pathAttributes1 != null) {
- final AdvertizedRoutes routes = (pathAttributes1.getMpReachNlri()).getAdvertizedRoutes();
- if (routes != null &&
- routes.getDestinationType()
- instanceof
- org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLinkstateCase) {
- final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLinkstateCase
- linkstateCase = (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLinkstateCase) routes.getDestinationType();
-
- for (final CLinkstateDestination cLinkstateDestination : linkstateCase.getDestinationLinkstate().getCLinkstateDestination()) {
- serializeNlri(cLinkstateDestination, byteAggregator);
- }
- }
- } else if (pathAttributes2 != null) {
- final MpUnreachNlri mpUnreachNlri = pathAttributes2.getMpUnreachNlri();
- if (mpUnreachNlri.getWithdrawnRoutes() != null && mpUnreachNlri.getWithdrawnRoutes().getDestinationType() instanceof DestinationLinkstateCase) {
- final DestinationLinkstateCase linkstateCase = (DestinationLinkstateCase) mpUnreachNlri.getWithdrawnRoutes().getDestinationType();
- for (final CLinkstateDestination cLinkstateDestination : linkstateCase.getDestinationLinkstate().getCLinkstateDestination()) {
- serializeNlri(cLinkstateDestination, byteAggregator);
- }
- }
- }
- }
-}
+++ /dev/null
-/*
- * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.protocol.bgp.linkstate;
-
-final class TlvCode {
-
- private TlvCode() {
- throw new UnsupportedOperationException();
- }
-
- static final int LOCAL_NODE_DESCRIPTORS = 256;
-
- static final int REMOTE_NODE_DESCRIPTORS = 257;
-
- /* Link Descriptor TLVs */
-
- static final int LINK_LR_IDENTIFIERS = 258;
-
- static final int IPV4_IFACE_ADDRESS = 259;
-
- static final int IPV4_NEIGHBOR_ADDRESS = 260;
-
- static final int IPV6_IFACE_ADDRESS = 261;
-
- static final int IPV6_NEIGHBOR_ADDRESS = 262;
-
- /* Link Attribute TLVs */
-
- static final int LOCAL_IPV4_ROUTER_ID = 1028;
-
- static final int LOCAL_IPV6_ROUTER_ID = 1029;
-
- static final int REMOTE_IPV4_ROUTER_ID = 1030;
-
- static final int REMOTE_IPV6_ROUTER_ID = 1031;
-
- static final int ADMIN_GROUP = 1088;
-
- static final int MAX_BANDWIDTH = 1089;
-
- static final int MAX_RESERVABLE_BANDWIDTH = 1090;
-
- static final int UNRESERVED_BANDWIDTH = 1091;
-
- static final int TE_METRIC = 1092;
-
- static final int LINK_PROTECTION_TYPE = 1093;
-
- static final int MPLS_PROTOCOL = 1094;
-
- static final int METRIC = 1095;
-
- static final int SHARED_RISK_LINK_GROUP = 1096;
-
- static final int LINK_OPAQUE = 1097;
-
- static final int LINK_NAME = 1098;
-
- /* Prefix Descriptor TLVs */
-
- static final int MULTI_TOPOLOGY_ID = 263;
-
- static final int OSPF_ROUTE_TYPE = 264;
-
- static final int IP_REACHABILITY = 265;
-
- /* Prefix Attribute TLVs */
-
- static final int IGP_FLAGS = 1152;
-
- static final int ROUTE_TAG = 1153;
-
- static final int EXTENDED_ROUTE_TAG = 1154;
-
- static final int PREFIX_METRIC = 1155;
-
- static final int FORWARDING_ADDRESS = 1156;
-
- static final int PREFIX_OPAQUE = 1157;
-
- /* Node Descriptor TLVs */
-
- static final int AS_NUMBER = 512;
-
- static final int BGP_LS_ID = 513;
-
- static final int AREA_ID = 514;
-
- static final int IGP_ROUTER_ID = 515;
-
- /* Node Attribute TLVs */
-
- static final int NODE_FLAG_BITS = 1024;
-
- static final int NODE_OPAQUE = 1025;
-
- static final int DYNAMIC_HOSTNAME = 1026;
-
- static final int ISIS_AREA_IDENTIFIER = 1027;
-}
--- /dev/null
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.protocol.bgp.linkstate;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public final class TlvUtil {
+
+ private TlvUtil() {
+ throw new UnsupportedOperationException();
+ }
+
+ private static final Logger LOG = LoggerFactory.getLogger(TlvUtil.class);
+
+ public static final int TOPOLOGY_ID_OFFSET = 0x3fff;
+
+ public static final int MULTI_TOPOLOGY_ID = 263;
+ public static final int LOCAL_IPV4_ROUTER_ID = 1028;
+ public static final int LOCAL_IPV6_ROUTER_ID = 1029;
+
+ /**
+ * Util method for writing TLV header.
+ * @param type TLV type
+ * @param value TLV value
+ * @param byteAggregator final ByteBuf where the tlv should be serialized
+ */
+ public static void writeTLV(final int type, final ByteBuf value, final ByteBuf byteAggregator){
+ byteAggregator.writeShort(type);
+ byteAggregator.writeShort(value.writerIndex());
+ byteAggregator.writeBytes(value);
+ value.readerIndex(0);
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Serialized tlv type {} to: {}", type, ByteBufUtil.hexDump(value));
+ }
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.protocol.bgp.linkstate.attribute;
+
+import com.google.common.base.Charsets;
+import com.google.common.collect.Multimap;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufUtil;
+import io.netty.buffer.Unpooled;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.BitSet;
+import java.util.List;
+import java.util.Map.Entry;
+import org.opendaylight.protocol.bgp.linkstate.TlvUtil;
+import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.protocol.util.Ipv4Util;
+import org.opendaylight.protocol.util.Ipv6Util;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.AdministrativeGroup;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.Ipv4RouterIdentifier;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.Ipv6RouterIdentifier;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.LinkProtectionType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.MplsProtocolMask;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.link.state.UnreservedBandwidth;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.link.state.UnreservedBandwidthBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.LinkStateAttribute;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.link.state.attribute.LinkAttributesCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.link.state.attribute.LinkAttributesCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.link.state.attribute.link.attributes._case.LinkAttributes;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.link.state.attribute.link.attributes._case.LinkAttributesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Metric;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.TeMetric;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.SrlgId;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+final class LinkAttributesParser {
+
+ private static final Logger LOG = LoggerFactory.getLogger(LinkAttributesParser.class);
+
+ private static final int UNRESERVED_BW_COUNT = 8;
+
+ private static final int BANDWIDTH_LENGTH = 4;
+
+ // MPLS protection mask bits
+ private static final int LDP_BIT = 7;
+ private static final int RSVP_BIT = 6;
+
+ /* Link Attribute TLVs */
+ private static final int REMOTE_IPV4_ROUTER_ID = 1030;
+ private static final int REMOTE_IPV6_ROUTER_ID = 1031;
+ private static final int ADMIN_GROUP = 1088;
+ private static final int MAX_BANDWIDTH = 1089;
+ private static final int MAX_RESERVABLE_BANDWIDTH = 1090;
+ private static final int UNRESERVED_BANDWIDTH = 1091;
+ private static final int TE_METRIC = 1092;
+ private static final int LINK_PROTECTION_TYPE = 1093;
+ private static final int MPLS_PROTOCOL = 1094;
+ private static final int METRIC = 1095;
+ private static final int SHARED_RISK_LINK_GROUP = 1096;
+ private static final int LINK_OPAQUE = 1097;
+ private static final int LINK_NAME = 1098;
+
+ /**
+ * Parse Link Attributes.
+ *
+ * @param attributes key is the tlv type and value is the value of the tlv
+ * @return {@link LinkStateAttribute}
+ */
+ static LinkStateAttribute parseLinkAttributes(final Multimap<Integer, ByteBuf> attributes) {
+ final LinkAttributesBuilder builder = new LinkAttributesBuilder();
+ for (final Entry<Integer, ByteBuf> entry : attributes.entries()) {
+ LOG.trace("Link attribute TLV {}", entry.getKey());
+ final int key = entry.getKey();
+ final ByteBuf value = entry.getValue();
+ switch (key) {
+ case TlvUtil.LOCAL_IPV4_ROUTER_ID:
+ final Ipv4RouterIdentifier lipv4 = new Ipv4RouterIdentifier(Ipv4Util.addressForByteBuf(value));
+ builder.setLocalIpv4RouterId(lipv4);
+ LOG.debug("Parsed IPv4 Router-ID of local node: {}", lipv4);
+ break;
+ case TlvUtil.LOCAL_IPV6_ROUTER_ID:
+ final Ipv6RouterIdentifier lipv6 = new Ipv6RouterIdentifier(Ipv6Util.addressForByteBuf(value));
+ builder.setLocalIpv6RouterId(lipv6);
+ LOG.debug("Parsed IPv6 Router-ID of local node: {}", lipv6);
+ break;
+ case REMOTE_IPV4_ROUTER_ID:
+ final Ipv4RouterIdentifier ripv4 = new Ipv4RouterIdentifier(Ipv4Util.addressForByteBuf(value));
+ builder.setRemoteIpv4RouterId(ripv4);
+ LOG.debug("Parsed IPv4 Router-ID of remote node: {}", ripv4);
+ break;
+ case REMOTE_IPV6_ROUTER_ID:
+ final Ipv6RouterIdentifier ripv6 = new Ipv6RouterIdentifier(Ipv6Util.addressForByteBuf(value));
+ builder.setRemoteIpv6RouterId(ripv6);
+ LOG.debug("Parsed IPv6 Router-ID of remote node: {}", ripv6);
+ break;
+ case ADMIN_GROUP:
+ builder.setAdminGroup(new AdministrativeGroup(value.readUnsignedInt()));
+ LOG.debug("Parsed Administrative Group {}", builder.getAdminGroup());
+ break;
+ case MAX_BANDWIDTH:
+ builder.setMaxLinkBandwidth(new Bandwidth(ByteArray.readAllBytes(value)));
+ LOG.debug("Parsed Max Bandwidth {}", builder.getMaxLinkBandwidth());
+ break;
+ case MAX_RESERVABLE_BANDWIDTH:
+ builder.setMaxReservableBandwidth(new Bandwidth(ByteArray.readAllBytes(value)));
+ LOG.debug("Parsed Max Reservable Bandwidth {}", builder.getMaxReservableBandwidth());
+ break;
+ case UNRESERVED_BANDWIDTH:
+ final List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.link.state.UnreservedBandwidth> unreservedBandwidth = new ArrayList<>(UNRESERVED_BW_COUNT);
+ for (int i = 0; i < UNRESERVED_BW_COUNT; i++) {
+ final ByteBuf v = value.slice(value.readerIndex(), BANDWIDTH_LENGTH);
+ unreservedBandwidth.add(new UnreservedBandwidthBuilder().setBandwidth(new Bandwidth(ByteArray.readAllBytes(v))).setPriority((short) i).build());
+ value.skipBytes(BANDWIDTH_LENGTH);
+ }
+ builder.setUnreservedBandwidth(unreservedBandwidth);
+ LOG.debug("Parsed Unreserved Bandwidth {}", builder.getUnreservedBandwidth());
+ break;
+ case TE_METRIC:
+ builder.setTeMetric(new TeMetric(ByteArray.bytesToLong(ByteArray.readAllBytes(value))));
+ LOG.debug("Parsed Metric {}", builder.getTeMetric());
+ break;
+ case LINK_PROTECTION_TYPE:
+ final int l = value.readShort();
+ final LinkProtectionType lpt = LinkProtectionType.forValue(l);
+ if (lpt == null) {
+ LOG.warn("Link Protection Type not recognized: {}", l);
+ break;
+ }
+ builder.setLinkProtection(lpt);
+ LOG.debug("Parsed Link Protection Type {}", lpt);
+ break;
+ case MPLS_PROTOCOL:
+ final BitSet bits = BitSet.valueOf(ByteArray.readAllBytes(value));
+ builder.setMplsProtocol(new MplsProtocolMask(bits.get(LDP_BIT), bits.get(RSVP_BIT)));
+ LOG.debug("Parsed MPLS Protocols: {}", builder.getMplsProtocol());
+ break;
+ case METRIC:
+ // length can 3, 2 or 1
+ builder.setMetric(new Metric(ByteArray.bytesToLong(ByteArray.readAllBytes(value))));
+ LOG.debug("Parsed Metric {}", builder.getMetric());
+ break;
+ case SHARED_RISK_LINK_GROUP:
+ final List<SrlgId> sharedRiskLinkGroups = new ArrayList<>();
+ while (value.isReadable()) {
+ sharedRiskLinkGroups.add(new SrlgId(value.readUnsignedInt()));
+ }
+ builder.setSharedRiskLinkGroups(sharedRiskLinkGroups);
+ LOG.debug("Parsed Shared Risk Link Groups {}", Arrays.toString(sharedRiskLinkGroups.toArray()));
+ break;
+ case LINK_OPAQUE:
+ LOG.debug("Parsed Opaque value : {}", ByteBufUtil.hexDump(value));
+ break;
+ case LINK_NAME:
+ final String name = new String(ByteArray.readAllBytes(value), Charsets.US_ASCII);
+ builder.setLinkName(name);
+ LOG.debug("Parsed Link Name : {}", name);
+ break;
+ default:
+ LOG.warn("TLV {} is not a valid link attribute, ignoring it", key);
+ }
+ }
+ LOG.trace("Finished parsing Link Attributes.");
+ return new LinkAttributesCaseBuilder().setLinkAttributes(builder.build()).build();
+ }
+
+ static void serializeLinkAttributes(final LinkAttributesCase linkAttributesCase, final ByteBuf byteAggregator) {
+ final LinkAttributes linkAttributes = linkAttributesCase.getLinkAttributes();
+ LOG.trace("Started serializing Link Attributes");
+ if (linkAttributes.getLocalIpv4RouterId() != null) {
+ TlvUtil.writeTLV(TlvUtil.LOCAL_IPV4_ROUTER_ID, Ipv4Util.byteBufForAddress(linkAttributes.getLocalIpv4RouterId()), byteAggregator);
+ }
+ if (linkAttributes.getLocalIpv6RouterId() != null) {
+ TlvUtil.writeTLV(TlvUtil.LOCAL_IPV6_ROUTER_ID, Ipv6Util.byteBufForAddress(linkAttributes.getLocalIpv6RouterId()), byteAggregator);
+ }
+ if (linkAttributes.getRemoteIpv4RouterId() != null) {
+ TlvUtil.writeTLV(REMOTE_IPV4_ROUTER_ID, Ipv4Util.byteBufForAddress(linkAttributes.getRemoteIpv4RouterId()), byteAggregator);
+ }
+ if (linkAttributes.getRemoteIpv6RouterId() != null) {
+ TlvUtil.writeTLV(REMOTE_IPV6_ROUTER_ID, Ipv6Util.byteBufForAddress(linkAttributes.getRemoteIpv6RouterId()), byteAggregator);
+ }
+ if (linkAttributes.getAdminGroup() != null) {
+ TlvUtil.writeTLV(ADMIN_GROUP, Unpooled.copyInt(linkAttributes.getAdminGroup().getValue().intValue()), byteAggregator);
+ }
+ if (linkAttributes.getMaxLinkBandwidth() != null) {
+ TlvUtil.writeTLV(MAX_BANDWIDTH, Unpooled.wrappedBuffer(linkAttributes.getMaxLinkBandwidth().getValue()), byteAggregator);
+ }
+ if (linkAttributes.getMaxReservableBandwidth() != null) {
+ TlvUtil.writeTLV(MAX_RESERVABLE_BANDWIDTH, Unpooled.wrappedBuffer(linkAttributes.getMaxReservableBandwidth().getValue()), byteAggregator);
+ }
+ // this sub-TLV contains eight 32-bit IEEE floating point numbers
+ final List<UnreservedBandwidth> ubList = linkAttributes.getUnreservedBandwidth();
+ if (ubList != null && !ubList.isEmpty()) {
+ final ByteBuf unreservedBandwithBuf = Unpooled.buffer();
+ for (final UnreservedBandwidth unreservedBandwidth : ubList) {
+ unreservedBandwithBuf.writeBytes(unreservedBandwidth.getBandwidth().getValue());
+ }
+ TlvUtil.writeTLV(UNRESERVED_BANDWIDTH, unreservedBandwithBuf, byteAggregator);
+ }
+ if (linkAttributes.getTeMetric() != null) {
+ TlvUtil.writeTLV(TE_METRIC, Unpooled.copyLong(linkAttributes.getTeMetric().getValue().longValue()), byteAggregator);
+ }
+ if (linkAttributes.getLinkProtection() != null) {
+ TlvUtil.writeTLV(LINK_PROTECTION_TYPE, Unpooled.copyShort(linkAttributes.getLinkProtection().getIntValue()), byteAggregator);
+ }
+ serializeMplsProtocolMask(linkAttributes.getMplsProtocol(), byteAggregator);
+ if (linkAttributes.getMetric() != null) {
+ // size of metric can be 1,2 or 3 depending on the protocol
+ TlvUtil.writeTLV(METRIC, Unpooled.copyMedium(linkAttributes.getMetric().getValue().intValue()), byteAggregator);
+ }
+ final List<SrlgId> srlgList = linkAttributes.getSharedRiskLinkGroups();
+ if (srlgList != null && !srlgList.isEmpty()) {
+ final ByteBuf sharedRLGBuf = Unpooled.buffer();
+ for (final SrlgId srlgId : srlgList) {
+ sharedRLGBuf.writeInt(srlgId.getValue().intValue());
+ }
+ TlvUtil.writeTLV(SHARED_RISK_LINK_GROUP, sharedRLGBuf, byteAggregator);
+ }
+ if (linkAttributes.getLinkName() != null) {
+ TlvUtil.writeTLV(LINK_NAME, Unpooled.wrappedBuffer(Charsets.UTF_8.encode(linkAttributes.getLinkName())), byteAggregator);
+ }
+ LOG.trace("Finished serializing Link Attributes");
+ }
+
+ private static void serializeMplsProtocolMask(final MplsProtocolMask mplsProtocolMask, final ByteBuf byteAggregator ) {
+ if (mplsProtocolMask != null) {
+ final ByteBuf mplsProtocolMaskBuf = Unpooled.buffer(1);
+ final BitSet mask = new BitSet(Byte.SIZE);
+ if (mplsProtocolMask.isLdp() != null) {
+ mask.set(LDP_BIT, mplsProtocolMask.isLdp());
+ }
+ if (mplsProtocolMask.isRsvpte() != null) {
+ mask.set(RSVP_BIT, mplsProtocolMask.isRsvpte());
+ }
+ mplsProtocolMaskBuf.writeBytes(mask.toByteArray());
+ TlvUtil.writeTLV(MPLS_PROTOCOL, mplsProtocolMaskBuf, byteAggregator);
+ }
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.protocol.bgp.linkstate.attribute;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.HashMultimap;
+import com.google.common.collect.Multimap;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import org.opendaylight.protocol.bgp.parser.BGPParsingException;
+import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
+import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
+import org.opendaylight.protocol.bgp.parser.spi.AttributeUtil;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.NlriType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.PathAttributes1;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.PathAttributes1Builder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.destination.CLinkstateDestination;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.LinkstatePathAttribute;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.LinkstatePathAttributeBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.LinkStateAttribute;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.link.state.attribute.LinkAttributesCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.link.state.attribute.NodeAttributesCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.link.state.attribute.PrefixAttributesCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLinkstateCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributes;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes2;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.destination.DestinationType;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Parser for Link State Path Attribute.
+ *
+ * @see <a href="http://tools.ietf.org/html/draft-gredler-idr-ls-distribution-04">BGP-LS draft</a>
+ */
+public class LinkstateAttributeParser implements AttributeParser, AttributeSerializer {
+
+ private static final Logger LOG = LoggerFactory.getLogger(LinkstateAttributeParser.class);
+
+ private static final int TYPE = 29;
+
+ private static final int LEGACY_TYPE = 99;
+
+ private final int type;
+
+ public LinkstateAttributeParser(final boolean isIanaAssignedType) {
+ this.type = (isIanaAssignedType) ? TYPE : LEGACY_TYPE;
+ }
+
+ public int getType() {
+ return this.type;
+ }
+
+ @Override
+ public void parseAttribute(final ByteBuf buffer, final PathAttributesBuilder builder) throws BGPParsingException {
+ final NlriType nlriType = getNlriType(builder);
+ if (nlriType == null) {
+ LOG.warn("No Linkstate NLRI found, not parsing Linkstate attribute");
+ return;
+ }
+ final PathAttributes1 a = new PathAttributes1Builder().setLinkstatePathAttribute(parseLinkState(nlriType, buffer)).build();
+ builder.addAugmentation(PathAttributes1.class, a);
+ }
+
+ private NlriType getNlriType(final PathAttributesBuilder pab) {
+ final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes1 mpr = pab.getAugmentation(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes1.class);
+ if (mpr != null && mpr.getMpReachNlri() != null) {
+ final DestinationType dt = mpr.getMpReachNlri().getAdvertizedRoutes().getDestinationType();
+ if (dt instanceof DestinationLinkstateCase) {
+ for (final CLinkstateDestination d : ((DestinationLinkstateCase) dt).getDestinationLinkstate().getCLinkstateDestination()) {
+ return d.getNlriType();
+ }
+ }
+ }
+ final PathAttributes2 mpu = pab.getAugmentation(PathAttributes2.class);
+ if (mpu != null && mpu.getMpUnreachNlri() != null) {
+ final DestinationType dt = mpu.getMpUnreachNlri().getWithdrawnRoutes().getDestinationType();
+ if (dt instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationLinkstateCase) {
+ for (final CLinkstateDestination d : ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationLinkstateCase) dt).getDestinationLinkstate().getCLinkstateDestination()) {
+ return d.getNlriType();
+ }
+ }
+ }
+ return null;
+ }
+
+ private static LinkstatePathAttribute parseLinkState(final NlriType nlri, final ByteBuf buffer) throws BGPParsingException {
+ /*
+ * e.g. IS-IS Area Identifier TLV can occur multiple times
+ */
+ final Multimap<Integer, ByteBuf> map = HashMultimap.create();
+ while (buffer.isReadable()) {
+ final int type = buffer.readUnsignedShort();
+ final int length = buffer.readUnsignedShort();
+ final ByteBuf value = buffer.slice(buffer.readerIndex(), length);
+ map.put(type, value);
+ buffer.skipBytes(length);
+ }
+ final LinkstatePathAttributeBuilder builder = new LinkstatePathAttributeBuilder();
+
+ switch (nlri) {
+ case Ipv4Prefix:
+ case Ipv6Prefix:
+ builder.setLinkStateAttribute(PrefixAttributesParser.parsePrefixAttributes(map));
+ return builder.build();
+ case Link:
+ builder.setLinkStateAttribute(LinkAttributesParser.parseLinkAttributes(map));
+ return builder.build();
+ case Node:
+ builder.setLinkStateAttribute(NodeAttributesParser.parseNodeAttributes(map));
+ return builder.build();
+ default:
+ throw new IllegalStateException("Unhandled NLRI type " + nlri);
+ }
+ }
+
+ /**
+ * Serialize linkstate attributes.
+ *
+ * @param attribute DataObject representing LinkstatePathAttribute
+ * @param byteAggregator ByteBuf where all serialized data are aggregated
+ */
+
+ @Override
+ public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
+ Preconditions.checkArgument(attribute instanceof PathAttributes, "Attribute parameter is not a PathAttribute object.");
+ final PathAttributes1 pathAttributes1 = ((PathAttributes) attribute).getAugmentation(PathAttributes1.class);
+ if (pathAttributes1 == null) {
+ return;
+ }
+ final LinkStateAttribute linkState = pathAttributes1.getLinkstatePathAttribute().getLinkStateAttribute();
+ final ByteBuf lsBuffer = Unpooled.buffer();
+ if (linkState instanceof LinkAttributesCase) {
+ LinkAttributesParser.serializeLinkAttributes((LinkAttributesCase) linkState, lsBuffer);
+ } else if (linkState instanceof NodeAttributesCase) {
+ NodeAttributesParser.serializeNodeAttributes((NodeAttributesCase) linkState, lsBuffer);
+ } else if (linkState instanceof PrefixAttributesCase) {
+ PrefixAttributesParser.serializePrefixAttributes((PrefixAttributesCase) linkState, lsBuffer);
+ }
+ AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL, getType(), lsBuffer, byteAggregator);
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.protocol.bgp.linkstate.attribute;
+
+import com.google.common.base.Charsets;
+import com.google.common.collect.Multimap;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufUtil;
+import io.netty.buffer.Unpooled;
+import java.util.ArrayList;
+import java.util.BitSet;
+import java.util.List;
+import java.util.Map.Entry;
+import org.opendaylight.protocol.bgp.linkstate.TlvUtil;
+import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.protocol.util.Ipv4Util;
+import org.opendaylight.protocol.util.Ipv6Util;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.Ipv4RouterIdentifier;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.Ipv6RouterIdentifier;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.IsisAreaIdentifier;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.NodeFlagBits;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.TopologyIdentifier;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.LinkStateAttribute;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.link.state.attribute.NodeAttributesCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.link.state.attribute.NodeAttributesCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.link.state.attribute.node.attributes._case.NodeAttributes;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.link.state.attribute.node.attributes._case.NodeAttributesBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+final class NodeAttributesParser {
+
+ private static final Logger LOG = LoggerFactory.getLogger(NodeAttributesParser.class);
+
+ // node flag bits
+ private static final int OVERLOAD_BIT = 7;
+ private static final int ATTACHED_BIT = 6;
+ private static final int EXTERNAL_BIT = 5;
+ private static final int ABBR_BIT = 4;
+
+ /* Node Attribute TLVs */
+ private static final int NODE_FLAG_BITS = 1024;
+ private static final int NODE_OPAQUE = 1025;
+ private static final int DYNAMIC_HOSTNAME = 1026;
+ private static final int ISIS_AREA_IDENTIFIER = 1027;
+
+ /**
+ * Parse Node Attributes.
+ *
+ * @param attributes key is the tlv type and value is the value of the tlv
+ * @return {@link LinkStateAttribute}
+ */
+ static LinkStateAttribute parseNodeAttributes(final Multimap<Integer, ByteBuf> attributes) {
+ final List<TopologyIdentifier> topologyMembership = new ArrayList<>();
+ final List<IsisAreaIdentifier> areaMembership = new ArrayList<>();
+ final NodeAttributesBuilder builder = new NodeAttributesBuilder();
+ for (final Entry<Integer, ByteBuf> entry : attributes.entries()) {
+ final int key = entry.getKey();
+ final ByteBuf value = entry.getValue();
+ LOG.trace("Node attribute TLV {}", key);
+ switch (key) {
+ case TlvUtil.MULTI_TOPOLOGY_ID:
+ while (value.isReadable()) {
+ final TopologyIdentifier topId = new TopologyIdentifier(value.readUnsignedShort() & TlvUtil.TOPOLOGY_ID_OFFSET);
+ topologyMembership.add(topId);
+ LOG.debug("Parsed Topology Identifier: {}", topId);
+ }
+ break;
+ case NODE_FLAG_BITS:
+ final BitSet flags = BitSet.valueOf(ByteArray.readAllBytes(value));
+ builder.setNodeFlags(new NodeFlagBits(flags.get(OVERLOAD_BIT), flags.get(ATTACHED_BIT), flags.get(EXTERNAL_BIT), flags.get(ABBR_BIT)));
+ LOG.debug("Parsed Overload bit: {}, attached bit: {}, external bit: {}, area border router: {}.",
+ flags.get(OVERLOAD_BIT), flags.get(ATTACHED_BIT), flags.get(EXTERNAL_BIT), flags.get(ABBR_BIT));
+ break;
+ case NODE_OPAQUE:
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Ignoring opaque value: {}.", ByteBufUtil.hexDump(value));
+ }
+ break;
+ case DYNAMIC_HOSTNAME:
+ builder.setDynamicHostname(new String(ByteArray.readAllBytes(value), Charsets.US_ASCII));
+ LOG.debug("Parsed Node Name {}", builder.getDynamicHostname());
+ break;
+ case ISIS_AREA_IDENTIFIER:
+ final IsisAreaIdentifier ai = new IsisAreaIdentifier(ByteArray.readAllBytes(value));
+ areaMembership.add(ai);
+ LOG.debug("Parsed AreaIdentifier {}", ai);
+ break;
+ case TlvUtil.LOCAL_IPV4_ROUTER_ID:
+ final Ipv4RouterIdentifier ip4 = new Ipv4RouterIdentifier(Ipv4Util.addressForByteBuf(value));
+ builder.setIpv4RouterId(ip4);
+ LOG.debug("Parsed IPv4 Router Identifier {}", ip4);
+ break;
+ case TlvUtil.LOCAL_IPV6_ROUTER_ID:
+ final Ipv6RouterIdentifier ip6 = new Ipv6RouterIdentifier(Ipv6Util.addressForByteBuf(value));
+ builder.setIpv6RouterId(ip6);
+ LOG.debug("Parsed IPv6 Router Identifier {}", ip6);
+ break;
+ default:
+ LOG.warn("TLV {} is not a valid node attribute, ignoring it", key);
+ }
+ }
+ LOG.trace("Finished parsing Node Attributes.");
+ builder.setTopologyIdentifier(topologyMembership);
+ builder.setIsisAreaId(areaMembership);
+ return new NodeAttributesCaseBuilder().setNodeAttributes(builder.build()).build();
+ }
+
+ static void serializeNodeAttributes(final NodeAttributesCase nodeAttributesCase, final ByteBuf byteAggregator) {
+ LOG.trace("Started serializing Node Attributes");
+ final NodeAttributes nodeAttributes = nodeAttributesCase.getNodeAttributes();
+ final List<TopologyIdentifier> topList = nodeAttributes.getTopologyIdentifier();
+ if (topList != null && !topList.isEmpty()) {
+ final ByteBuf mpIdBuf = Unpooled.buffer();
+ for (final TopologyIdentifier topologyIdentifier : topList) {
+ mpIdBuf.writeShort(topologyIdentifier.getValue());
+ }
+ TlvUtil.writeTLV(TlvUtil.MULTI_TOPOLOGY_ID, mpIdBuf, byteAggregator);
+ }
+ serializeNodeFlagBits(nodeAttributes.getNodeFlags(), byteAggregator);
+ if (nodeAttributes.getDynamicHostname() != null) {
+ TlvUtil.writeTLV(DYNAMIC_HOSTNAME, Unpooled.wrappedBuffer(Charsets.UTF_8.encode(nodeAttributes.getDynamicHostname())), byteAggregator);
+ }
+ final List<IsisAreaIdentifier> isisList = nodeAttributes.getIsisAreaId();
+ if (isisList != null && !isisList.isEmpty()) {
+ for (final IsisAreaIdentifier isisAreaIdentifier : isisList) {
+ TlvUtil.writeTLV(ISIS_AREA_IDENTIFIER, Unpooled.wrappedBuffer(isisAreaIdentifier.getValue()), byteAggregator);
+ }
+ }
+ if (nodeAttributes.getIpv4RouterId() != null) {
+ TlvUtil.writeTLV(TlvUtil.LOCAL_IPV4_ROUTER_ID, Ipv4Util.byteBufForAddress(nodeAttributes.getIpv4RouterId()), byteAggregator);
+ }
+ if (nodeAttributes.getIpv6RouterId() != null) {
+ TlvUtil.writeTLV(TlvUtil.LOCAL_IPV6_ROUTER_ID, Ipv6Util.byteBufForAddress(nodeAttributes.getIpv6RouterId()), byteAggregator);
+ }
+ LOG.trace("Finished serializing Node Attributes");
+ }
+
+ private static void serializeNodeFlagBits(final NodeFlagBits nodeFlagBits, final ByteBuf byteAggregator) {
+ if (nodeFlagBits != null) {
+ final ByteBuf nodeFlagBuf = Unpooled.buffer(1);
+ final BitSet flags = new BitSet(Byte.SIZE);
+ if (nodeFlagBits.isOverload() != null) {
+ flags.set(OVERLOAD_BIT, nodeFlagBits.isOverload());
+ }
+ if (nodeFlagBits.isAttached() != null) {
+ flags.set(ATTACHED_BIT, nodeFlagBits.isAttached());
+ }
+ if (nodeFlagBits.isExternal() != null) {
+ flags.set(EXTERNAL_BIT, nodeFlagBits.isExternal());
+ }
+ if (nodeFlagBits.isAbr() != null) {
+ flags.set(ABBR_BIT, nodeFlagBits.isAbr());
+ }
+ nodeFlagBuf.writeBytes(flags.toByteArray());
+ TlvUtil.writeTLV(NODE_FLAG_BITS, nodeFlagBuf, byteAggregator);
+ }
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.protocol.bgp.linkstate.attribute;
+
+import com.google.common.collect.Multimap;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufUtil;
+import io.netty.buffer.Unpooled;
+import java.util.ArrayList;
+import java.util.BitSet;
+import java.util.List;
+import java.util.Map.Entry;
+import org.opendaylight.protocol.bgp.linkstate.TlvUtil;
+import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.protocol.util.Ipv4Util;
+import org.opendaylight.protocol.util.Ipv6Util;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.ExtendedRouteTag;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.IgpBits.UpDown;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.RouteTag;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.prefix.state.IgpBitsBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.LinkStateAttribute;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.link.state.attribute.PrefixAttributesCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.link.state.attribute.PrefixAttributesCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.link.state.attribute.prefix.attributes._case.PrefixAttributes;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.linkstate.path.attribute.link.state.attribute.prefix.attributes._case.PrefixAttributesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.IgpMetric;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+final class PrefixAttributesParser {
+
+ private static final Logger LOG = LoggerFactory.getLogger(PrefixAttributesParser.class);
+
+ private static final int ROUTE_TAG_LENGTH = 4;
+ private static final int EXTENDED_ROUTE_TAG_LENGTH = 8;
+ private static final int UP_DOWN_BIT = 7;
+
+ /* Prefix Attribute TLVs */
+ private static final int IGP_FLAGS = 1152;
+ private static final int ROUTE_TAG = 1153;
+ private static final int EXTENDED_ROUTE_TAG = 1154;
+ private static final int PREFIX_METRIC = 1155;
+ private static final int FORWARDING_ADDRESS = 1156;
+ private static final int PREFIX_OPAQUE = 1157;
+
+ /**
+ * Parse prefix attributes.
+ *
+ * @param attributes key is the tlv type and value are the value bytes of the tlv
+ * @return {@link LinkStateAttribute}
+ */
+ static LinkStateAttribute parsePrefixAttributes(final Multimap<Integer, ByteBuf> attributes) {
+ final PrefixAttributesBuilder builder = new PrefixAttributesBuilder();
+ final List<RouteTag> routeTags = new ArrayList<>();
+ final List<ExtendedRouteTag> exRouteTags = new ArrayList<>();
+ for (final Entry<Integer, ByteBuf> entry : attributes.entries()) {
+ final int key = entry.getKey();
+ final ByteBuf value = entry.getValue();
+ LOG.trace("Prefix attribute TLV {}", key);
+ switch (key) {
+ case IGP_FLAGS:
+ final BitSet flags = BitSet.valueOf(ByteArray.readAllBytes(value));
+ final boolean upDownBit = flags.get(UP_DOWN_BIT);
+ builder.setIgpBits(new IgpBitsBuilder().setUpDown(new UpDown(upDownBit)).build());
+ LOG.debug("Parsed IGP flag (up/down bit) : {}", upDownBit);
+ break;
+ case ROUTE_TAG:
+ while (value.isReadable()) {
+ final RouteTag routeTag = new RouteTag(ByteArray.readBytes(value, ROUTE_TAG_LENGTH));
+ routeTags.add(routeTag);
+ LOG.debug("Parsed Route Tag: {}", routeTag);
+ }
+ break;
+ case EXTENDED_ROUTE_TAG:
+ while (value.isReadable()) {
+ final ExtendedRouteTag exRouteTag = new ExtendedRouteTag(ByteArray.readBytes(value, EXTENDED_ROUTE_TAG_LENGTH));
+ exRouteTags.add(exRouteTag);
+ LOG.debug("Parsed Extended Route Tag: {}", exRouteTag);
+ }
+ break;
+ case PREFIX_METRIC:
+ final IgpMetric metric = new IgpMetric(value.readUnsignedInt());
+ builder.setPrefixMetric(metric);
+ LOG.debug("Parsed Metric: {}", metric);
+ break;
+ case FORWARDING_ADDRESS:
+ final IpAddress fwdAddress = parseForwardingAddress(value);
+ builder.setOspfForwardingAddress(fwdAddress);
+ LOG.debug("Parsed FWD Address: {}", fwdAddress);
+ break;
+ case PREFIX_OPAQUE:
+ LOG.debug("Parsed Opaque value: {}, not preserving it", ByteBufUtil.hexDump(value));
+ break;
+ default:
+ LOG.warn("TLV {} is not a valid prefix attribute, ignoring it", key);
+ }
+ }
+ LOG.trace("Finished parsing Prefix Attributes.");
+ builder.setRouteTags(routeTags);
+ builder.setExtendedTags(exRouteTags);
+ return new PrefixAttributesCaseBuilder().setPrefixAttributes(builder.build()).build();
+ }
+
+ private static IpAddress parseForwardingAddress(final ByteBuf value) {
+ IpAddress fwdAddress = null;
+ switch (value.readableBytes()) {
+ case Ipv4Util.IP4_LENGTH:
+ fwdAddress = new IpAddress(Ipv4Util.addressForByteBuf(value));
+ break;
+ case Ipv6Util.IPV6_LENGTH:
+ fwdAddress = new IpAddress(Ipv6Util.addressForByteBuf(value));
+ break;
+ default:
+ LOG.debug("Ignoring unsupported forwarding address length {}", value.readableBytes());
+ }
+ return fwdAddress;
+ }
+
+ static void serializePrefixAttributes(final PrefixAttributesCase prefixAttributesCase, final ByteBuf byteAggregator) {
+ final PrefixAttributes prefixAtrributes = prefixAttributesCase.getPrefixAttributes();
+ if (prefixAtrributes.getIgpBits() != null) {
+ final BitSet igpBit = new BitSet();
+ final Boolean bit = prefixAtrributes.getIgpBits().getUpDown().isUpDown();
+ if (bit != null) {
+ igpBit.set(UP_DOWN_BIT, bit);
+ }
+ TlvUtil.writeTLV(IGP_FLAGS, Unpooled.wrappedBuffer(igpBit.toByteArray()), byteAggregator);
+ }
+ if (prefixAtrributes.getRouteTags() != null) {
+ final ByteBuf routeTagsBuf = Unpooled.buffer();
+ for (final RouteTag routeTag : prefixAtrributes.getRouteTags()) {
+ routeTagsBuf.writeBytes(routeTag.getValue());
+ }
+ TlvUtil.writeTLV(ROUTE_TAG, routeTagsBuf, byteAggregator);
+ }
+ final List<ExtendedRouteTag> routeTagList = prefixAtrributes.getExtendedTags();
+ if (routeTagList != null && !routeTagList.isEmpty()) {
+ final ByteBuf extendedBuf = Unpooled.buffer();
+ for (final ExtendedRouteTag extendedRouteTag : routeTagList) {
+ extendedBuf.writeBytes(extendedRouteTag.getValue());
+ }
+ TlvUtil.writeTLV(EXTENDED_ROUTE_TAG, extendedBuf, byteAggregator);
+ }
+ if (prefixAtrributes.getPrefixMetric() != null) {
+ TlvUtil.writeTLV(PREFIX_METRIC, Unpooled.copyInt(prefixAtrributes.getPrefixMetric().getValue().intValue()), byteAggregator);
+ }
+ serializeForwardingAddress(prefixAtrributes.getOspfForwardingAddress(), byteAggregator);
+ }
+
+ private static void serializeForwardingAddress(final IpAddress forwardingAddress, final ByteBuf byteAggregator) {
+ if (forwardingAddress != null) {
+ final ByteBuf ospfBuf = Unpooled.buffer();
+ if (forwardingAddress.getIpv4Address() != null) {
+ ospfBuf.writeBytes(Ipv4Util.bytesForAddress(forwardingAddress.getIpv4Address()));
+ } else if (forwardingAddress.getIpv6Address() != null) {
+ ospfBuf.writeBytes(Ipv6Util.bytesForAddress(forwardingAddress.getIpv6Address()));
+ }
+ TlvUtil.writeTLV(FORWARDING_ADDRESS, ospfBuf, byteAggregator);
+ }
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.protocol.bgp.linkstate.nlri;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufUtil;
+import io.netty.buffer.Unpooled;
+import org.opendaylight.protocol.bgp.linkstate.TlvUtil;
+import org.opendaylight.protocol.bgp.parser.BGPParsingException;
+import org.opendaylight.protocol.util.Ipv4Util;
+import org.opendaylight.protocol.util.Ipv6Util;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.Ipv4InterfaceIdentifier;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.Ipv6InterfaceIdentifier;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.TopologyIdentifier;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.destination.c.linkstate.destination.LinkDescriptors;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.destination.c.linkstate.destination.LinkDescriptorsBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+final class LinkNlriParser {
+
+ private static final Logger LOG = LoggerFactory.getLogger(LinkNlriParser.class);
+
+ /* Link Descriptor TLVs */
+ private static final int LINK_LR_IDENTIFIERS = 258;
+ private static final int IPV4_IFACE_ADDRESS = 259;
+ private static final int IPV4_NEIGHBOR_ADDRESS = 260;
+ private static final int IPV6_IFACE_ADDRESS = 261;
+ private static final int IPV6_NEIGHBOR_ADDRESS = 262;
+
+ static LinkDescriptors parseLinkDescriptors(final ByteBuf buffer) throws BGPParsingException {
+ final LinkDescriptorsBuilder builder = new LinkDescriptorsBuilder();
+ while (buffer.isReadable()) {
+ final int type = buffer.readUnsignedShort();
+ final int length = buffer.readUnsignedShort();
+ final ByteBuf value = buffer.slice(buffer.readerIndex(), length);
+ LOG.trace("Parsing Link Descriptor: {}", ByteBufUtil.hexDump(value));
+ switch (type) {
+ case LINK_LR_IDENTIFIERS:
+ builder.setLinkLocalIdentifier(value.readUnsignedInt());
+ builder.setLinkRemoteIdentifier(value.readUnsignedInt());
+ LOG.debug("Parsed link local {} remote {} Identifiers.", builder.getLinkLocalIdentifier(),
+ builder.getLinkRemoteIdentifier());
+ break;
+ case IPV4_IFACE_ADDRESS:
+ final Ipv4InterfaceIdentifier lipv4 = new Ipv4InterfaceIdentifier(Ipv4Util.addressForByteBuf(value));
+ builder.setIpv4InterfaceAddress(lipv4);
+ LOG.debug("Parsed IPv4 interface address {}.", lipv4);
+ break;
+ case IPV4_NEIGHBOR_ADDRESS:
+ final Ipv4InterfaceIdentifier ripv4 = new Ipv4InterfaceIdentifier(Ipv4Util.addressForByteBuf(value));
+ builder.setIpv4NeighborAddress(ripv4);
+ LOG.debug("Parsed IPv4 neighbor address {}.", ripv4);
+ break;
+ case IPV6_IFACE_ADDRESS:
+ final Ipv6InterfaceIdentifier lipv6 = new Ipv6InterfaceIdentifier(Ipv6Util.addressForByteBuf(value));
+ builder.setIpv6InterfaceAddress(lipv6);
+ LOG.debug("Parsed IPv6 interface address {}.", lipv6);
+ break;
+ case IPV6_NEIGHBOR_ADDRESS:
+ final Ipv6InterfaceIdentifier ripv6 = new Ipv6InterfaceIdentifier(Ipv6Util.addressForByteBuf(value));
+ builder.setIpv6NeighborAddress(ripv6);
+ LOG.debug("Parsed IPv6 neighbor address {}.", ripv6);
+ break;
+ case TlvUtil.MULTI_TOPOLOGY_ID:
+ final TopologyIdentifier topId = new TopologyIdentifier(value.readUnsignedShort() & TlvUtil.TOPOLOGY_ID_OFFSET);
+ builder.setMultiTopologyId(topId);
+ LOG.debug("Parsed topology identifier {}.", topId);
+ break;
+ default:
+ throw new BGPParsingException("Link Descriptor not recognized, type: " + type);
+ }
+ buffer.skipBytes(length);
+ }
+ LOG.trace("Finished parsing Link descriptors.");
+ return builder.build();
+ }
+
+ static void serializeLinkDescriptors(final LinkDescriptors descriptors, final ByteBuf buffer) {
+ if (descriptors.getLinkLocalIdentifier() != null && descriptors.getLinkRemoteIdentifier() != null) {
+ final ByteBuf identifierBuf = Unpooled.buffer();
+ identifierBuf.writeInt(descriptors.getLinkLocalIdentifier().intValue());
+ identifierBuf.writeInt(descriptors.getLinkRemoteIdentifier().intValue());
+ TlvUtil.writeTLV(LINK_LR_IDENTIFIERS, identifierBuf, buffer);
+ }
+ if (descriptors.getIpv4InterfaceAddress() != null) {
+ TlvUtil.writeTLV(IPV4_IFACE_ADDRESS, Ipv4Util.byteBufForAddress(descriptors.getIpv4InterfaceAddress()), buffer);
+ }
+ if (descriptors.getIpv4NeighborAddress() != null) {
+ TlvUtil.writeTLV(IPV4_NEIGHBOR_ADDRESS, Ipv4Util.byteBufForAddress(descriptors.getIpv4NeighborAddress()), buffer);
+ }
+ if (descriptors.getIpv6InterfaceAddress() != null) {
+ TlvUtil.writeTLV(IPV6_IFACE_ADDRESS, Ipv6Util.byteBufForAddress(descriptors.getIpv6InterfaceAddress()), buffer);
+ }
+ if (descriptors.getIpv6NeighborAddress() != null) {
+ TlvUtil.writeTLV(IPV6_NEIGHBOR_ADDRESS, Ipv6Util.byteBufForAddress(descriptors.getIpv6NeighborAddress()), buffer);
+ }
+ if (descriptors.getMultiTopologyId() != null) {
+ TlvUtil.writeTLV(TlvUtil.MULTI_TOPOLOGY_ID, Unpooled.copyShort(descriptors.getMultiTopologyId().getValue()), buffer);
+ }
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.protocol.bgp.linkstate.nlri;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import java.math.BigInteger;
+import java.util.List;
+import org.opendaylight.protocol.bgp.linkstate.TlvUtil;
+import org.opendaylight.protocol.bgp.parser.BGPParsingException;
+import org.opendaylight.protocol.bgp.parser.spi.NlriParser;
+import org.opendaylight.protocol.bgp.parser.spi.NlriSerializer;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.Identifier;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.NlriType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.NodeIdentifier;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.ProtocolId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.RouteDistinguisher;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.destination.CLinkstateDestination;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.destination.CLinkstateDestinationBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.destination.c.linkstate.destination.LocalNodeDescriptors;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.destination.c.linkstate.destination.RemoteNodeDescriptors;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLinkstateCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.linkstate._case.DestinationLinkstateBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationLinkstateCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributes;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes1;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes2;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpReachNlriBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpUnreachNlri;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpUnreachNlriBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.mp.reach.nlri.AdvertizedRoutes;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Parser and serializer for Linkstate NLRI.
+ */
+public final class LinkstateNlriParser implements NlriParser, NlriSerializer {
+ private static final Logger LOG = LoggerFactory.getLogger(LinkstateNlriParser.class);
+ private static final int ROUTE_DISTINGUISHER_LENGTH = 8;
+ private static final int PROTOCOL_ID_LENGTH = 1;
+ private static final int IDENTIFIER_LENGTH = 8;
+
+ private static final int TYPE_LENGTH = 2;
+ private static final int LENGTH_SIZE = 2;
+
+
+ private static final int LOCAL_NODE_DESCRIPTORS = 256;
+ private static final int REMOTE_NODE_DESCRIPTORS = 257;
+
+ private final boolean isVpn;
+
+ public LinkstateNlriParser(final boolean isVpn) {
+ this.isVpn = isVpn;
+ }
+
+ private static NodeIdentifier parseLink(final CLinkstateDestinationBuilder builder, final ByteBuf buffer) throws BGPParsingException {
+ final int type = buffer.readUnsignedShort();
+ final int length = buffer.readUnsignedShort();
+ final NodeIdentifier remote = null;
+ if (type == REMOTE_NODE_DESCRIPTORS) {
+ builder.setRemoteNodeDescriptors((RemoteNodeDescriptors) NodeNlriParser.parseNodeDescriptors(buffer.slice(buffer.readerIndex(), length), false));
+ buffer.skipBytes(length);
+ }
+ builder.setLinkDescriptors(LinkNlriParser.parseLinkDescriptors(buffer.slice()));
+ return remote;
+ }
+
+ /**
+ * Parses common parts for Link State Nodes, Links and Prefixes, that includes protocol ID and identifier tlv.
+ *
+ * @param nlri as byte array
+ * @return {@link CLinkstateDestination}
+ * @throws BGPParsingException if parsing was unsuccessful
+ */
+ public static List<CLinkstateDestination> parseNlri(final ByteBuf nlri, final boolean isVpn) throws BGPParsingException {
+ if (!nlri.isReadable()) {
+ return null;
+ }
+ final List<CLinkstateDestination> dests = Lists.newArrayList();
+
+ CLinkstateDestinationBuilder builder = null;
+
+ while (nlri.isReadable()) {
+ builder = new CLinkstateDestinationBuilder();
+ final NlriType type = NlriType.forValue(nlri.readUnsignedShort());
+ builder.setNlriType(type);
+
+ // length means total length of the tlvs including route distinguisher not including the type field
+ final int length = nlri.readUnsignedShort();
+ RouteDistinguisher distinguisher = null;
+ if (isVpn) {
+ // this parses route distinguisher
+ distinguisher = new RouteDistinguisher(BigInteger.valueOf(nlri.readLong()));
+ builder.setDistinguisher(distinguisher);
+ }
+ // parse source protocol
+ final ProtocolId sp = ProtocolId.forValue(nlri.readByte());
+ builder.setProtocolId(sp);
+
+ // parse identifier
+ final Identifier identifier = new Identifier(BigInteger.valueOf(nlri.readLong()));
+ builder.setIdentifier(identifier);
+
+ // if we are dealing with linkstate nodes/links, parse local node descriptor
+ NodeIdentifier localDescriptor = null;
+ int locallength = 0;
+ final int localtype = nlri.readUnsignedShort();
+ locallength = nlri.readUnsignedShort();
+ if (localtype == LOCAL_NODE_DESCRIPTORS) {
+ localDescriptor = NodeNlriParser.parseNodeDescriptors(nlri.slice(nlri.readerIndex(), locallength), true);
+ }
+ nlri.skipBytes(locallength);
+ builder.setLocalNodeDescriptors((LocalNodeDescriptors) localDescriptor);
+ final int restLength = length - (isVpn ? ROUTE_DISTINGUISHER_LENGTH : 0) - PROTOCOL_ID_LENGTH - IDENTIFIER_LENGTH
+ - TYPE_LENGTH - LENGTH_SIZE - locallength;
+ LOG.trace("Restlength {}", restLength);
+ final ByteBuf rest = nlri.slice(nlri.readerIndex(), restLength);
+ switch (type) {
+ case Link:
+ parseLink(builder, rest);
+ break;
+ case Ipv4Prefix:
+ builder.setPrefixDescriptors(PrefixNlriParser.parsePrefixDescriptors(rest, true));
+ break;
+ case Ipv6Prefix:
+ builder.setPrefixDescriptors(PrefixNlriParser.parsePrefixDescriptors(rest, false));
+ break;
+ case Node:
+ // node nlri is already parsed as it contains only the common fields for node and link nlri
+ break;
+ default:
+ break;
+ }
+ nlri.skipBytes(restLength);
+ dests.add(builder.build());
+ }
+ return dests;
+ }
+
+ @Override
+ public void parseNlri(final ByteBuf nlri, final MpUnreachNlriBuilder builder) throws BGPParsingException {
+ if (!nlri.isReadable()) {
+ return;
+ }
+ final List<CLinkstateDestination> dst = parseNlri(nlri, this.isVpn);
+
+ builder.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setDestinationType(
+ new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationLinkstateCaseBuilder().setDestinationLinkstate(
+ new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.destination.linkstate._case.DestinationLinkstateBuilder().setCLinkstateDestination(
+ dst).build()).build()).build());
+ }
+
+ @Override
+ public void parseNlri(final ByteBuf nlri, final MpReachNlriBuilder builder) throws BGPParsingException {
+ if (!nlri.isReadable()) {
+ return;
+ }
+ final List<CLinkstateDestination> dst = parseNlri(nlri, this.isVpn);
+
+ builder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
+ new DestinationLinkstateCaseBuilder().setDestinationLinkstate(
+ new DestinationLinkstateBuilder().setCLinkstateDestination(dst).build()).build()).build());
+ }
+
+ /**
+ * Serializes Linkstate NLRI to byte array. We need this as NLRI serves as a key in upper layers.
+ *
+ * @param destination Linkstate NLRI to be serialized
+ */
+ public static void serializeNlri(final CLinkstateDestination destination, final ByteBuf buffer) {
+ final ByteBuf nlriByteBuf = Unpooled.buffer();
+ if (destination.getDistinguisher() != null) {
+ nlriByteBuf.writeBytes(destination.getDistinguisher().getValue().toByteArray());
+ }
+ nlriByteBuf.writeByte(destination.getProtocolId().getIntValue());
+ nlriByteBuf.writeLong(destination.getIdentifier().getValue().longValue());
+
+ // serialize local node descriptors
+ final ByteBuf ldescs = Unpooled.buffer();
+ NodeNlriParser.serializeNodeDescriptors(destination.getLocalNodeDescriptors(), ldescs);
+ TlvUtil.writeTLV(LOCAL_NODE_DESCRIPTORS, ldescs, nlriByteBuf);
+
+ switch (destination.getNlriType()) {
+ case Ipv4Prefix:
+ case Ipv6Prefix:
+ if (destination.getPrefixDescriptors() != null) {
+ PrefixNlriParser.serializePrefixDescriptors(destination.getPrefixDescriptors(), nlriByteBuf);
+ }
+ break;
+ case Link:
+ final ByteBuf rdescs = Unpooled.buffer();
+ NodeNlriParser.serializeNodeDescriptors(destination.getRemoteNodeDescriptors(), rdescs);
+ TlvUtil.writeTLV(REMOTE_NODE_DESCRIPTORS, rdescs, nlriByteBuf);
+ if (destination.getLinkDescriptors() != null) {
+ LinkNlriParser.serializeLinkDescriptors(destination.getLinkDescriptors(), nlriByteBuf);
+ }
+ break;
+ case Node:
+ break;
+ default:
+ LOG.warn("Unknown NLRI Type.");
+ break;
+ }
+ TlvUtil.writeTLV(destination.getNlriType().getIntValue(), nlriByteBuf, buffer);
+ }
+
+ @Override
+ public void serializeAttribute(final DataObject attribute, final ByteBuf byteAggregator) {
+ Preconditions.checkArgument(attribute instanceof PathAttributes, "Attribute parameter is not a PathAttribute object.");
+ final PathAttributes pathAttributes = (PathAttributes) attribute;
+ final PathAttributes1 pathAttributes1 = pathAttributes.getAugmentation(PathAttributes1.class);
+ final PathAttributes2 pathAttributes2 = pathAttributes.getAugmentation(PathAttributes2.class);
+ if (pathAttributes1 != null) {
+ final AdvertizedRoutes routes = (pathAttributes1.getMpReachNlri()).getAdvertizedRoutes();
+ if (routes != null &&
+ routes.getDestinationType()
+ instanceof
+ org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLinkstateCase) {
+ final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLinkstateCase
+ linkstateCase = (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLinkstateCase) routes.getDestinationType();
+
+ for (final CLinkstateDestination cLinkstateDestination : linkstateCase.getDestinationLinkstate().getCLinkstateDestination()) {
+ serializeNlri(cLinkstateDestination, byteAggregator);
+ }
+ }
+ } else if (pathAttributes2 != null) {
+ final MpUnreachNlri mpUnreachNlri = pathAttributes2.getMpUnreachNlri();
+ if (mpUnreachNlri.getWithdrawnRoutes() != null && mpUnreachNlri.getWithdrawnRoutes().getDestinationType() instanceof DestinationLinkstateCase) {
+ final DestinationLinkstateCase linkstateCase = (DestinationLinkstateCase) mpUnreachNlri.getWithdrawnRoutes().getDestinationType();
+ for (final CLinkstateDestination cLinkstateDestination : linkstateCase.getDestinationLinkstate().getCLinkstateDestination()) {
+ serializeNlri(cLinkstateDestination, byteAggregator);
+ }
+ }
+ }
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.protocol.bgp.linkstate.nlri;
+
+import com.google.common.primitives.UnsignedInteger;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufUtil;
+import io.netty.buffer.Unpooled;
+import org.opendaylight.protocol.bgp.linkstate.TlvUtil;
+import org.opendaylight.protocol.bgp.parser.BGPParsingException;
+import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.AreaIdentifier;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.DomainIdentifier;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.NodeIdentifier;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.OspfInterfaceIdentifier;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.isis.lan.identifier.IsIsRouterIdentifier;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.isis.lan.identifier.IsIsRouterIdentifierBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.destination.c.linkstate.destination.LocalNodeDescriptorsBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.destination.c.linkstate.destination.RemoteNodeDescriptorsBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.CRouterIdentifier;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.IsisNodeCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.IsisNodeCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.IsisPseudonodeCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.IsisPseudonodeCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.OspfNodeCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.OspfNodeCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.OspfPseudonodeCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.OspfPseudonodeCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.isis.node._case.IsisNode;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.isis.node._case.IsisNodeBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.isis.pseudonode._case.IsisPseudonode;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.isis.pseudonode._case.IsisPseudonodeBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.ospf.node._case.OspfNodeBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.ospf.pseudonode._case.OspfPseudonode;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.ospf.pseudonode._case.OspfPseudonodeBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.IsoSystemIdentifier;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+final class NodeNlriParser {
+
+ private static final Logger LOG = LoggerFactory.getLogger(NodeNlriParser.class);
+
+ private static final int OSPF_PSEUDONODE_ROUTER_ID_LENGTH = 8;
+ private static final int OSPF_ROUTER_ID_LENGTH = 4;
+ private static final int ISO_SYSTEM_ID_LENGTH = 6;
+ private static final int PSN_LENGTH = 1;
+
+ /* Node Descriptor TLVs */
+ private static final int AS_NUMBER = 512;
+ private static final int BGP_LS_ID = 513;
+ private static final int AREA_ID = 514;
+ private static final int IGP_ROUTER_ID = 515;
+
+ static NodeIdentifier parseNodeDescriptors(final ByteBuf buffer, final boolean local) throws BGPParsingException {
+ AsNumber asnumber = null;
+ DomainIdentifier bgpId = null;
+ AreaIdentifier ai = null;
+ CRouterIdentifier routerId = null;
+ while (buffer.isReadable()) {
+ final int type = buffer.readUnsignedShort();
+ final int length = buffer.readUnsignedShort();
+ final ByteBuf value = buffer.slice(buffer.readerIndex(), length);
+ if (LOG.isTraceEnabled()) {
+ LOG.trace("Parsing Node Descriptor: {}", ByteBufUtil.hexDump(value));
+ }
+ switch (type) {
+ case AS_NUMBER:
+ asnumber = new AsNumber(value.readUnsignedInt());
+ LOG.debug("Parsed {}", asnumber);
+ break;
+ case BGP_LS_ID:
+ bgpId = new DomainIdentifier(value.readUnsignedInt());
+ LOG.debug("Parsed {}", bgpId);
+ break;
+ case AREA_ID:
+ ai = new AreaIdentifier(value.readUnsignedInt());
+ LOG.debug("Parsed area identifier {}", ai);
+ break;
+ case IGP_ROUTER_ID:
+ routerId = parseRouterId(value);
+ LOG.debug("Parsed Router Identifier {}", routerId);
+ break;
+ default:
+ throw new BGPParsingException("Node Descriptor not recognized, type: " + type);
+ }
+ buffer.skipBytes(length);
+ }
+ LOG.trace("Finished parsing Node descriptors.");
+ return (local) ? new LocalNodeDescriptorsBuilder().setAsNumber(asnumber).setDomainId(bgpId).setAreaId(ai).setCRouterIdentifier(
+ routerId).build()
+ : new RemoteNodeDescriptorsBuilder().setAsNumber(asnumber).setDomainId(bgpId).setAreaId(ai).setCRouterIdentifier(routerId).build();
+ }
+
+ private static CRouterIdentifier parseRouterId(final ByteBuf value) throws BGPParsingException {
+ if (value.readableBytes() == ISO_SYSTEM_ID_LENGTH || (value.readableBytes() == ISO_SYSTEM_ID_LENGTH + PSN_LENGTH && value.getByte(ISO_SYSTEM_ID_LENGTH) == 0)) {
+ return new IsisNodeCaseBuilder().setIsisNode(
+ new IsisNodeBuilder().setIsoSystemId(new IsoSystemIdentifier(ByteArray.readBytes(value, ISO_SYSTEM_ID_LENGTH))).build()).build();
+ }
+ if (value.readableBytes() == ISO_SYSTEM_ID_LENGTH + PSN_LENGTH) {
+ final IsIsRouterIdentifier iri = new IsIsRouterIdentifierBuilder().setIsoSystemId(
+ new IsoSystemIdentifier(ByteArray.readBytes(value, ISO_SYSTEM_ID_LENGTH))).build();
+ return new IsisPseudonodeCaseBuilder().setIsisPseudonode(new IsisPseudonodeBuilder().setIsIsRouterIdentifier(iri).setPsn((short) value.readByte()).build()).build();
+ }
+ if (value.readableBytes() == OSPF_ROUTER_ID_LENGTH) {
+ return new OspfNodeCaseBuilder().setOspfNode(
+ new OspfNodeBuilder().setOspfRouterId(value.readUnsignedInt()).build()).build();
+ }
+ if (value.readableBytes() == OSPF_PSEUDONODE_ROUTER_ID_LENGTH) {
+ return new OspfPseudonodeCaseBuilder().setOspfPseudonode(
+ new OspfPseudonodeBuilder().setOspfRouterId(value.readUnsignedInt()).setLanInterface(new OspfInterfaceIdentifier(value.readUnsignedInt())).build()).build();
+ }
+ throw new BGPParsingException("Router Id of invalid length " + value.readableBytes());
+ }
+
+ static void serializeNodeDescriptors(final NodeIdentifier descriptors, final ByteBuf buffer) {
+ if (descriptors.getAsNumber() != null) {
+ TlvUtil.writeTLV(AS_NUMBER, Unpooled.copyInt(UnsignedInteger.valueOf(descriptors.getAsNumber().getValue()).intValue()), buffer);
+ }
+ if (descriptors.getDomainId() != null) {
+ TlvUtil.writeTLV(BGP_LS_ID, Unpooled.copyInt(UnsignedInteger.valueOf(descriptors.getDomainId().getValue()).intValue()), buffer);
+ }
+ if (descriptors.getAreaId() != null) {
+ TlvUtil.writeTLV(AREA_ID, Unpooled.copyInt(UnsignedInteger.valueOf(descriptors.getAreaId().getValue()).intValue()), buffer);
+ }
+ if (descriptors.getCRouterIdentifier() != null) {
+ final ByteBuf routerIdBuf = Unpooled.buffer();
+ serializeRouterId(descriptors.getCRouterIdentifier(), routerIdBuf);
+ TlvUtil.writeTLV(IGP_ROUTER_ID, routerIdBuf, buffer);
+ }
+ }
+
+ private static void serializeRouterId(final CRouterIdentifier routerId, final ByteBuf buffer) {
+ if (routerId instanceof IsisNodeCase) {
+ final IsisNode isis = ((IsisNodeCase) routerId).getIsisNode();
+ buffer.writeBytes(isis.getIsoSystemId().getValue());
+ } else if (routerId instanceof IsisPseudonodeCase) {
+ final IsisPseudonode isis = ((IsisPseudonodeCase) routerId).getIsisPseudonode();
+ buffer.writeBytes(isis.getIsIsRouterIdentifier().getIsoSystemId().getValue());
+ buffer.writeByte(((isis.getPsn() != null) ? isis.getPsn() : 0));
+ } else if (routerId instanceof OspfNodeCase) {
+ buffer.writeInt(UnsignedInteger.valueOf(((OspfNodeCase) routerId).getOspfNode().getOspfRouterId()).intValue());
+ } else if (routerId instanceof OspfPseudonodeCase) {
+ final OspfPseudonode node = ((OspfPseudonodeCase) routerId).getOspfPseudonode();
+ buffer.writeInt(UnsignedInteger.valueOf(node.getOspfRouterId()).intValue());
+ buffer.writeInt(UnsignedInteger.valueOf(node.getLanInterface().getValue()).intValue());
+ }
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.protocol.bgp.linkstate.nlri;
+
+import com.google.common.primitives.UnsignedBytes;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufUtil;
+import io.netty.buffer.Unpooled;
+import org.opendaylight.protocol.bgp.linkstate.TlvUtil;
+import org.opendaylight.protocol.bgp.parser.BGPParsingException;
+import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.protocol.util.Ipv4Util;
+import org.opendaylight.protocol.util.Ipv6Util;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.OspfRouteType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.TopologyIdentifier;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.destination.c.linkstate.destination.PrefixDescriptors;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.destination.c.linkstate.destination.PrefixDescriptorsBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+final class PrefixNlriParser {
+
+ private static final Logger LOG = LoggerFactory.getLogger(PrefixNlriParser.class);
+
+ /* Prefix Descriptor TLVs */
+ private static final int OSPF_ROUTE_TYPE = 264;
+ private static final int IP_REACHABILITY = 265;
+
+ static PrefixDescriptors parsePrefixDescriptors(final ByteBuf buffer, final boolean ipv4) throws BGPParsingException {
+ final PrefixDescriptorsBuilder builder = new PrefixDescriptorsBuilder();
+ while (buffer.isReadable()) {
+ final int type = buffer.readUnsignedShort();
+ final int length = buffer.readUnsignedShort();
+ final ByteBuf value = buffer.slice(buffer.readerIndex(), length);
+ if (LOG.isTraceEnabled()) {
+ LOG.trace("Parsing Prefix Descriptor: {}", ByteBufUtil.hexDump(value));
+ }
+ switch (type) {
+ case TlvUtil.MULTI_TOPOLOGY_ID:
+ final TopologyIdentifier topologyId = new TopologyIdentifier(value.readShort() & TlvUtil.TOPOLOGY_ID_OFFSET);
+ builder.setMultiTopologyId(topologyId);
+ LOG.trace("Parsed Topology Identifier: {}", topologyId);
+ break;
+ case OSPF_ROUTE_TYPE:
+ final int rt = value.readByte();
+ final OspfRouteType routeType = OspfRouteType.forValue(rt);
+ if (routeType == null) {
+ throw new BGPParsingException("Unknown OSPF Route Type: " + rt);
+ }
+ builder.setOspfRouteType(routeType);
+ LOG.trace("Parser RouteType: {}", routeType);
+ break;
+ case IP_REACHABILITY:
+ IpPrefix prefix = null;
+ final int prefixLength = value.readByte();
+ final int size = prefixLength / Byte.SIZE + ((prefixLength % Byte.SIZE == 0) ? 0 : 1);
+ if (size != value.readableBytes()) {
+ LOG.debug("Expected length {}, actual length {}.", size, value.readableBytes());
+ throw new BGPParsingException("Illegal length of IP reachability TLV: " + (value.readableBytes()));
+ }
+ prefix = (ipv4) ? new IpPrefix(Ipv4Util.prefixForBytes(ByteArray.readBytes(value, size), prefixLength)):
+ new IpPrefix(Ipv6Util.prefixForBytes(ByteArray.readBytes(value, size), prefixLength));
+ builder.setIpReachabilityInformation(prefix);
+ LOG.trace("Parsed IP reachability info: {}", prefix);
+ break;
+ default:
+ throw new BGPParsingException("Prefix Descriptor not recognized, type: " + type);
+ }
+ buffer.skipBytes(length);
+ }
+ LOG.debug("Finished parsing Prefix descriptors.");
+ return builder.build();
+ }
+
+ static void serializePrefixDescriptors(final PrefixDescriptors descriptors, final ByteBuf buffer) {
+ if (descriptors.getMultiTopologyId() != null) {
+ TlvUtil.writeTLV(TlvUtil.MULTI_TOPOLOGY_ID, Unpooled.copyShort(descriptors.getMultiTopologyId().getValue()), buffer);
+ }
+ if (descriptors.getOspfRouteType() != null) {
+ TlvUtil.writeTLV(OSPF_ROUTE_TYPE,
+ Unpooled.wrappedBuffer(new byte[] {UnsignedBytes.checkedCast(descriptors.getOspfRouteType().getIntValue()) }), buffer);
+ }
+ if (descriptors.getIpReachabilityInformation() != null) {
+ final IpPrefix prefix = descriptors.getIpReachabilityInformation();
+ byte[] prefixBytes = null;
+ if (prefix.getIpv4Prefix() != null) {
+ prefixBytes = Ipv4Util.bytesForPrefixBegin(prefix.getIpv4Prefix());
+ } else if (prefix.getIpv6Prefix() != null) {
+ prefixBytes = Ipv6Util.bytesForPrefixBegin(prefix.getIpv6Prefix());
+ }
+ TlvUtil.writeTLV(IP_REACHABILITY, Unpooled.wrappedBuffer(prefixBytes), buffer);
+ }
+ }
+}
public void setUp() {
MockitoAnnotations.initMocks(this);
final WriteTransaction wTx = getDataBroker().newWriteOnlyTransaction();
- final InstanceIdentifier<Rib> iid = InstanceIdentifier.builder(BgpRib.class).child(Rib.class, new RibKey(new RibId("test-rib"))).toInstance();
+ final InstanceIdentifier<Rib> iid = InstanceIdentifier.builder(BgpRib.class).child(Rib.class, new RibKey(new RibId("test-rib"))).build();
final KeyedInstanceIdentifier<Tables, TablesKey> key = iid.child(LocRib.class).child(Tables.class, new TablesKey(LinkstateAddressFamily.class,
LinkstateSubsequentAddressFamily.class));
import io.netty.buffer.Unpooled;
import java.util.Arrays;
import org.junit.Test;
+import org.opendaylight.protocol.bgp.linkstate.attribute.LinkstateAttributeParser;
import org.opendaylight.protocol.bgp.parser.BGPParsingException;
import org.opendaylight.protocol.util.ByteArray;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.LinkProtectionType;
import java.math.BigInteger;
import java.util.List;
import org.junit.Test;
+import org.opendaylight.protocol.bgp.linkstate.nlri.LinkstateNlriParser;
import org.opendaylight.protocol.bgp.parser.BGPParsingException;
import org.opendaylight.protocol.util.ByteArray;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
import java.lang.reflect.InvocationTargetException;
import org.junit.Test;
-public class TlvCodeTest {
+public class TlvUtilTest {
@Test(expected=UnsupportedOperationException.class)
public void testPrivateConstructor() throws Throwable {
- final Constructor<TlvCode> c = TlvCode.class.getDeclaredConstructor();
+ final Constructor<TlvUtil> c = TlvUtil.class.getDeclaredConstructor();
c.setAccessible(true);
try {
c.newInstance();
- } catch (InvocationTargetException e) {
+ } catch (final InvocationTargetException e) {
throw e.getCause();
}
}
public RIBImpl(final RibId ribId, final AsNumber localAs, final Ipv4Address localBgpId, final RIBExtensionConsumerContext extensions,
final BGPDispatcher dispatcher, final ReconnectStrategyFactory tcpStrategyFactory,
final ReconnectStrategyFactory sessionStrategyFactory, final DataBroker dps, final List<BgpTableType> localTables) {
- super(InstanceIdentifier.builder(BgpRib.class).child(Rib.class, new RibKey(Preconditions.checkNotNull(ribId))).toInstance());
+ super(InstanceIdentifier.builder(BgpRib.class).child(Rib.class, new RibKey(Preconditions.checkNotNull(ribId))).build());
this.chain = dps.createTransactionChain(this);
this.localAs = Preconditions.checkNotNull(localAs);
this.comparator = new BGPObjectComparator(localAs);
private WriteTransaction trans;
- private final DefaultRibReference rib = new DefaultRibReference(InstanceIdentifier.builder(BgpRib.class).child(Rib.class, new RibKey(new RibId("test"))).toInstance());
+ private final DefaultRibReference rib = new DefaultRibReference(InstanceIdentifier.builder(BgpRib.class).child(Rib.class, new RibKey(new RibId("test"))).build());
private final RIBActivator act = new RIBActivator();
private static final Ipv4Prefix IPV4_PREFIX2 = new Ipv4Prefix("2.2.2.2/32");
- private static final InstanceIdentifier<Rib> RIB_IID = InstanceIdentifier.builder(BgpRib.class).child(Rib.class, new RibKey(new RibId("test-rib"))).toInstance();
+ private static final InstanceIdentifier<Rib> RIB_IID = InstanceIdentifier.builder(BgpRib.class).child(Rib.class, new RibKey(new RibId("test-rib"))).build();
private static final KeyedInstanceIdentifier<Tables, TablesKey> TABLES_IID = RIB_IID.child(LocRib.class).child(Tables.class, new TablesKey(Ipv4AddressFamily.class,
UnicastSubsequentAddressFamily.class));
this.chain = dataProvider.createTransactionChain(this);
final TopologyKey tk = new TopologyKey(Preconditions.checkNotNull(topologyId));
- this.topology = InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class, tk).toInstance();
+ this.topology = InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class, tk).build();
LOG.debug("Initiating topology builder from {} at {}", locRibReference, this.topology);
public final InstanceIdentifier<Tables> tableInstanceIdentifier(final Class<? extends AddressFamily> afi,
final Class<? extends SubsequentAddressFamily> safi) {
- return this.locRibReference.getInstanceIdentifier().builder().child(LocRib.class).child(Tables.class, new TablesKey(afi, safi)).toInstance();
+ return this.locRibReference.getInstanceIdentifier().builder().child(LocRib.class).child(Tables.class, new TablesKey(afi, safi)).build();
}
protected abstract void createObject(ReadWriteTransaction trans, InstanceIdentifier<T> id, T value);
public abstract class AbstractTopologyBuilderTest extends AbstractDataBrokerTest {
protected static final TopologyId TEST_TOPOLOGY_ID = new TopologyId("test-topo");
- protected static RibReference LOC_RIB_REF = new DefaultRibReference(InstanceIdentifier.builder(BgpRib.class).child(Rib.class, new RibKey(Preconditions.checkNotNull(new RibId("test-rib")))).toInstance());
+ protected static RibReference LOC_RIB_REF = new DefaultRibReference(InstanceIdentifier.builder(BgpRib.class).child(Rib.class, new RibKey(Preconditions.checkNotNull(new RibId("test-rib")))).build());
protected ListenerRegistration<DataChangeListener> reg;
protected void createEmptyTopology() {
final WriteTransaction wTx = getDataBroker().newWriteOnlyTransaction();
- wTx.put(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.builder(NetworkTopology.class).toInstance(), new NetworkTopologyBuilder().setTopology(Collections.<Topology>emptyList()).build());
+ wTx.put(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.builder(NetworkTopology.class).build(), new NetworkTopologyBuilder().setTopology(Collections.<Topology>emptyList()).build());
wTx.submit();
}
wTx.put(LogicalDatastoreType.OPERATIONAL, path, new TablesBuilder().setAfi(Ipv4AddressFamily.class).setSafi(UnicastSubsequentAddressFamily.class)
.setAttributes(new AttributesBuilder().setUptodate(Boolean.TRUE).build()).setRoutes(new Ipv4RoutesCaseBuilder().build()).build(), true);
wTx.submit();
- this.ipv4RouteIID = path.builder().child(Ipv4Routes.class).child(Ipv4Route.class, new Ipv4RouteKey(new Ipv4Prefix(ROUTE_IP4PREFIX))).toInstance();
+ this.ipv4RouteIID = path.builder().child(Ipv4Routes.class).child(Ipv4Route.class, new Ipv4RouteKey(new Ipv4Prefix(ROUTE_IP4PREFIX))).build();
}
@Test
wTx.put(LogicalDatastoreType.OPERATIONAL, path, new TablesBuilder().setAfi(Ipv6AddressFamily.class).setSafi(UnicastSubsequentAddressFamily.class)
.setAttributes(new AttributesBuilder().setUptodate(Boolean.TRUE).build()).setRoutes(new Ipv6RoutesCaseBuilder().build()).build(), true);
wTx.submit();
- this.ipv6RouteIID = path.builder().child(Ipv6Routes.class).child(Ipv6Route.class, new Ipv6RouteKey(new Ipv6Prefix(ROUTE_IP6PREFIX))).toInstance();
+ this.ipv6RouteIID = path.builder().child(Ipv6Routes.class).child(Ipv6Route.class, new Ipv6RouteKey(new Ipv6Prefix(ROUTE_IP6PREFIX))).build();
}
@Test
wTx.put(LogicalDatastoreType.OPERATIONAL, path, new TablesBuilder().setAfi(LinkstateAddressFamily.class).setSafi(LinkstateSubsequentAddressFamily.class)
.setAttributes(new AttributesBuilder().setUptodate(Boolean.TRUE).build()).setRoutes(new LinkstateRoutesCaseBuilder().build()).build(), true);
wTx.submit();
- this.linkstateRouteIID = (InstanceIdentifier<LinkstateRoute>) path.builder().child((Class)LinkstateRoutes.class).child(LinkstateRoute.class, new LinkstateRouteKey(LINKSTATE_ROUTE_KEY)).toInstance();
+ this.linkstateRouteIID = (InstanceIdentifier<LinkstateRoute>) path.builder().child((Class)LinkstateRoutes.class).child(LinkstateRoute.class, new LinkstateRouteKey(LINKSTATE_ROUTE_KEY)).build();
}
@Test
public class DefaultInstanceReferenceTest {
- private static final InstanceIdentifier<NetworkTopology> IID = InstanceIdentifier.builder(NetworkTopology.class).toInstance();
+ private static final InstanceIdentifier<NetworkTopology> IID = InstanceIdentifier.builder(NetworkTopology.class).build();
@Test
public void testDefaultInstanceReference() {
public java.lang.AutoCloseable createInstance() {
final TopologyDataChangeCounter counter = new TopologyDataChangeCounter(getDataProviderDependency());
final InstanceIdentifier<Topology> topoIId = InstanceIdentifier.builder(NetworkTopology.class)
- .child(Topology.class, new TopologyKey(new TopologyId(getTopologyName()))).toInstance();
+ .child(Topology.class, new TopologyKey(new TopologyId(getTopologyName()))).build();
final ListenerRegistration<DataChangeListener> registration = getDataProviderDependency().registerDataChangeListener(
LogicalDatastoreType.OPERATIONAL, topoIId, counter, DataBroker.DataChangeScope.SUBTREE);
return new DataChangeCounterCloseable(counter, registration);
private static final Logger LOG = LoggerFactory.getLogger(TopologyDataChangeCounter.class);
protected static final InstanceIdentifier<DataChangeCounter> IID = InstanceIdentifier
- .builder(DataChangeCounter.class).toInstance();
+ .builder(DataChangeCounter.class).build();
private final DataBroker dataBroker;
private AtomicLong count;
assertNotNull(getBroker());
final InstanceIdentifier<Topology> topology = InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class,
- new TopologyKey(getTopologyId("Topo1"))).toInstance();
+ new TopologyKey(getTopologyId("Topo1"))).build();
BindingAwareProvider provider1 = new AbstractTestProvider() {
broker.registerProvider(provider1, getBundleContext());
final InstanceIdentifier<Topology> topology2 = InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class,
- new TopologyKey(getTopologyId("Topo2"))).toInstance();
+ new TopologyKey(getTopologyId("Topo2"))).build();
BindingAwareProvider provider2 = new AbstractTestProvider() {
assertNotNull(getBroker());
final InstanceIdentifier<Topology> topology = InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class,
- new TopologyKey(getTopologyId("Topo1"))).toInstance();
+ new TopologyKey(getTopologyId("Topo1"))).build();
BindingAwareProvider provider1 = new AbstractTestProvider() {
broker.registerProvider(provider1, getBundleContext());
final InstanceIdentifier<Topology> topology2 = InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class,
- new TopologyKey(getTopologyId("Topo2"))).toInstance();
+ new TopologyKey(getTopologyId("Topo2"))).build();
BindingAwareProvider provider2 = new AbstractTestProvider() {
assertNotNull(getBroker());
final InstanceIdentifier<Topology> topology = InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class,
- new TopologyKey(getTopologyId("Topo1"))).toInstance();
+ new TopologyKey(getTopologyId("Topo1"))).build();
BindingAwareProvider provider1 = new AbstractTestProvider() {
broker.registerProvider(provider1, getBundleContext());
final InstanceIdentifier<Topology> topology2 = InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class,
- new TopologyKey(getTopologyId("Topo2"))).toInstance();
+ new TopologyKey(getTopologyId("Topo2"))).build();
BindingAwareProvider provider2 = new AbstractTestProvider() {
<arguments>
</arguments>
</buildCommand>
+ <buildCommand>
+ <name>net.sf.eclipsecs.core.CheckstyleBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
+ <nature>net.sf.eclipsecs.core.CheckstyleNature</nature>
</natures>
</projectDescription>
@Override
public java.lang.AutoCloseable createInstance() {
final InstanceIdentifier<Topology> topology = InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class,
- new TopologyKey(getTopologyId())).toInstance();
+ new TopologyKey(getTopologyId())).build();
final InetSocketAddress address = new InetSocketAddress(listenAddress(), getListenPort().getValue());
final KeyMapping keys = contructKeys();
protected static final String TEST_TOPOLOGY_NAME = "testtopo";
protected static final InstanceIdentifier<Topology> TOPO_IID = InstanceIdentifier.builder(NetworkTopology.class).child(
- Topology.class, new TopologyKey(new TopologyId(TEST_TOPOLOGY_NAME))).toInstance();
+ Topology.class, new TopologyKey(new TopologyId(TEST_TOPOLOGY_NAME))).build();
protected static final String TEST_ADDRESS = "127.0.0.1";
protected static final NodeId NODE_ID = new NodeId("pcc://" + TEST_ADDRESS);
protected static final String TEST_LSP_NAME = "tunnel0";
}
for (final ReportedLsp l : pccnode.getPathComputationClient().getReportedLsp()) {
- lsps.add(id.builder().augmentation(Node1.class).child(PathComputationClient.class).child(ReportedLsp.class, l.getKey()).toInstance());
+ lsps.add(id.builder().augmentation(Node1.class).child(PathComputationClient.class).child(ReportedLsp.class, l.getKey()).build());
}
}
for (final IpAddress a : ((Ip) tpt).getIpAddress()) {
if (addr.equals(a)) {
handleSni(sni, n, inControl, trans);
- return this.target.builder().child(Node.class, n.getKey()).child(TerminationPoint.class, tp.getKey()).toInstance();
+ return this.target.builder().child(Node.class, n.getKey()).child(TerminationPoint.class, tp.getKey()).build();
}
}
} else {
}
private InstanceIdentifier<TerminationPoint> tpIdentifier(final NodeId node, final TpId tp) {
- return this.target.builder().child(Node.class, new NodeKey(node)).child(TerminationPoint.class, new TerminationPointKey(tp)).toInstance();
+ return this.target.builder().child(Node.class, new NodeKey(node)).child(TerminationPoint.class, new TerminationPointKey(tp)).build();
}
private InstanceIdentifier<Node> nodeIdentifier(final NodeId node) {
public static PCEPTunnelTopologyProvider create(final DataBroker dataProvider,
final InstanceIdentifier<Topology> sourceTopology, final TopologyId targetTopology) {
final InstanceIdentifier<Topology> dst = InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class,
- new TopologyKey(targetTopology)).toInstance();
+ new TopologyKey(targetTopology)).build();
final NodeChangedListener ncl = new NodeChangedListener(dataProvider, dst);
final InstanceIdentifier<Node> src = sourceTopology.child(Node.class);
final BindingAwareBroker.RoutedRpcRegistration<TopologyTunnelPcepProgrammingService> reg = getRpcRegistryDependency().addRoutedRpcImplementation(
TopologyTunnelPcepProgrammingService.class, tp);
final InstanceIdentifier<Topology> topology = InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class,
- new TopologyKey(getTopologyId())).toInstance();
+ new TopologyKey(getTopologyId())).build();
reg.registerPath(NetworkTopologyContext.class, topology);
final class TunnelTopologyReferenceCloseable extends DefaultTopologyReference implements AutoCloseable {
private static final String LSP2_NAME = "lsp2";
private static final long LSP2_ID = 2;
- private static final InstanceIdentifier<Topology> PCEP_TOPO_IID = InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class, new TopologyKey(PCEP_TOPOLOGY_ID)).toInstance();
- private static final InstanceIdentifier<Topology> TUNNEL_TOPO_IID = InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class, new TopologyKey(TUNNEL_TOPOLOGY_ID)).toInstance();
+ private static final InstanceIdentifier<Topology> PCEP_TOPO_IID = InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class, new TopologyKey(PCEP_TOPOLOGY_ID)).build();
+ private static final InstanceIdentifier<Topology> TUNNEL_TOPO_IID = InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class, new TopologyKey(TUNNEL_TOPOLOGY_ID)).build();
private ListenerRegistration<DataChangeListener> listenerRegistration;
node1Builder.setPathComputationClient(new PathComputationClientBuilder().setStateSync(PccSyncState.Synchronized).setReportedLsp(Lists.newArrayList(reportedLps)).setIpAddress(new IpAddress(new Ipv4Address(ipv4Address))).build());
nodeBuilder.addAugmentation(Node1.class, node1Builder.build());
final WriteTransaction wTx = getDataBroker().newWriteOnlyTransaction();
- wTx.put(LogicalDatastoreType.OPERATIONAL, PCEP_TOPO_IID.builder().child(Node.class, new NodeKey(nodeId)).toInstance(), nodeBuilder.build());
+ wTx.put(LogicalDatastoreType.OPERATIONAL, PCEP_TOPO_IID.builder().child(Node.class, new NodeKey(nodeId)).build(), nodeBuilder.build());
wTx.submit().checkedGet();
}
private void removeNode(final NodeId nodeId) throws TransactionCommitFailedException {
final WriteTransaction wTx = getDataBroker().newWriteOnlyTransaction();
- wTx.delete(LogicalDatastoreType.OPERATIONAL, PCEP_TOPO_IID.builder().child(Node.class, new NodeKey(nodeId)).toInstance());
+ wTx.delete(LogicalDatastoreType.OPERATIONAL, PCEP_TOPO_IID.builder().child(Node.class, new NodeKey(nodeId)).build());
wTx.submit().checkedGet();
}
public class TunnelProgrammingTest extends AbstractDataBrokerTest {
private static final TopologyId TOPOLOGY_ID = new TopologyId("tunnel-topo");
- private static final InstanceIdentifier<Topology> TOPO_IID = InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class, new TopologyKey(TOPOLOGY_ID)).toInstance();
+ private static final InstanceIdentifier<Topology> TOPO_IID = InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class, new TopologyKey(TOPOLOGY_ID)).build();
private static final String NODE1_IPV4 = "127.0.0.1";
private static final NodeId NODE1_ID = new NodeId("pcc://" + NODE1_IPV4);
linkBuilder.setKey(new LinkKey(LINK1_ID));
linkBuilder.addAugmentation(Link1.class, new Link1Builder().setSymbolicPathName(LINK1_ID.getValue()).build());
final WriteTransaction wTx = getDataBroker().newWriteOnlyTransaction();
- wTx.put(LogicalDatastoreType.OPERATIONAL, TOPO_IID.builder().child(Link.class, new LinkKey(LINK1_ID)).toInstance(), linkBuilder.build(), true);
+ wTx.put(LogicalDatastoreType.OPERATIONAL, TOPO_IID.builder().child(Link.class, new LinkKey(LINK1_ID)).build(), linkBuilder.build(), true);
wTx.submit().checkedGet();
}
<artifactId>releasepom</artifactId>
<packaging>pom</packaging>
- <name>BGPCEP release</name>
+ <name>bgpcep</name> <!-- Used by Sonar to set project name -->
<description>BGPCEP top-level pom</description>
<modules>
this.notifs = Preconditions.checkNotNull(notifs);
this.executor = Preconditions.checkNotNull(executor);
this.timer = Preconditions.checkNotNull(timer);
- this.qid = InstanceIdentifier.builder(InstructionsQueue.class).toInstance();
+ this.qid = InstanceIdentifier.builder(InstructionsQueue.class).build();
final ReadWriteTransaction t = dataProvider.newReadWriteTransaction();
try {
private boolean assertInstructionExists(final InstructionId id) {
try {
- return getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.builder(InstructionsQueue.class).toInstance().child(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev130930.instruction.queue.Instruction.class,
+ return getDataBroker().newReadOnlyTransaction().read(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.builder(InstructionsQueue.class).build().child(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.programming.rev130930.instruction.queue.Instruction.class,
new InstructionKey(id))).get().isPresent();
} catch (InterruptedException | ExecutionException e) {
return false;