BUG 5031: 18/36218/5
authorOmair <of8826@att.com>
Mon, 14 Mar 2016 20:12:55 +0000 (13:12 -0700)
committerOmair <of8826@att.com>
Tue, 22 Mar 2016 18:54:54 +0000 (11:54 -0700)
Bring order into the BGP-LS encoders/decoders by introducing simple codecs
and their registers for BGP-LS's NLRI types, to make it more extensible.

Change-Id: Idd0740db3d3dc1828b5d5459a75bf0799b70d6ef
Signed-off-by: Omair <of8826@att.com>
16 files changed:
bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/BGPActivator.java
bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/nlri/LinkNlriParser.java
bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/nlri/LinkstateNlriParser.java
bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/nlri/NlriTypeCaseParser.java [new file with mode: 0644]
bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/nlri/NlriTypeCaseSerializer.java [new file with mode: 0644]
bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/nlri/NodeNlriParser.java
bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/nlri/PrefixIpv4NlriParser.java [new file with mode: 0644]
bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/nlri/PrefixIpv6NlriParser.java [new file with mode: 0644]
bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/nlri/PrefixNlriSerializer.java [moved from bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/nlri/PrefixNlriParser.java with 73% similarity]
bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/nlri/SimpleNlriTypeRegistry.java [new file with mode: 0644]
bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/nlri/TeLspIpv4NlriParser.java [new file with mode: 0644]
bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/nlri/TeLspIpv6NlriParser.java [new file with mode: 0644]
bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/nlri/TeLspNlriSerializer.java [moved from bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/nlri/TeLspNlriParser.java with 92% similarity]
bgp/linkstate/src/test/java/org/opendaylight/protocol/bgp/linkstate/ActivatorTest.java
bgp/linkstate/src/test/java/org/opendaylight/protocol/bgp/linkstate/LinkstateNlriParserTest.java
bgp/linkstate/src/test/java/org/opendaylight/protocol/bgp/linkstate/LinkstateRIBSupportTest.java

index 9c25bb7af9b984134766b75f600e1094c27362af..b5e4c59cfe5e71058ec9d5f18fa5b56ae1849d78 100644 (file)
@@ -11,13 +11,27 @@ import java.util.ArrayList;
 import java.util.List;
 import org.opendaylight.protocol.bgp.linkstate.attribute.LinkstateAttributeParser;
 import org.opendaylight.protocol.bgp.linkstate.attribute.next.hop.LinkstateNextHopParser;
+import org.opendaylight.protocol.bgp.linkstate.nlri.LinkNlriParser;
 import org.opendaylight.protocol.bgp.linkstate.nlri.LinkstateNlriParser;
+import org.opendaylight.protocol.bgp.linkstate.nlri.NodeNlriParser;
+import org.opendaylight.protocol.bgp.linkstate.nlri.PrefixIpv4NlriParser;
+import org.opendaylight.protocol.bgp.linkstate.nlri.PrefixIpv6NlriParser;
+import org.opendaylight.protocol.bgp.linkstate.nlri.PrefixNlriSerializer;
+import org.opendaylight.protocol.bgp.linkstate.nlri.SimpleNlriTypeRegistry;
+import org.opendaylight.protocol.bgp.linkstate.nlri.TeLspIpv4NlriParser;
+import org.opendaylight.protocol.bgp.linkstate.nlri.TeLspIpv6NlriParser;
+import org.opendaylight.protocol.bgp.linkstate.nlri.TeLspNlriSerializer;
 import org.opendaylight.protocol.bgp.parser.spi.AbstractBGPExtensionProviderActivator;
 import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
 import org.opendaylight.protocol.rsvp.parser.spi.RSVPTeObjectRegistry;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.Attributes1;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.LinkstateAddressFamily;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.LinkstateSubsequentAddressFamily;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NlriType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.LinkCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.NodeCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.PrefixCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.TeLspCase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.LinkstateRoutes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.MplsLabeledVpnSubsequentAddressFamily;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv4NextHopCase;
@@ -52,6 +66,8 @@ public final class BGPActivator extends AbstractBGPExtensionProviderActivator {
     protected List<AutoCloseable> startImpl(final BGPExtensionProviderContext context) {
         final List<AutoCloseable> regs = new ArrayList<>();
 
+        final SimpleNlriTypeRegistry nlriTypeReg = SimpleNlriTypeRegistry.getInstance();
+
         regs.add(context.registerAddressFamily(LinkstateAddressFamily.class, LINKSTATE_AFI));
         regs.add(context.registerSubsequentAddressFamily(LinkstateSubsequentAddressFamily.class, LINKSTATE_SAFI));
 
@@ -67,6 +83,32 @@ public final class BGPActivator extends AbstractBGPExtensionProviderActivator {
         final LinkstateAttributeParser linkstateAttributeParser = new LinkstateAttributeParser(this.ianaLinkstateAttributeType, this.rsvpTeObjectRegistry);
         regs.add(context.registerAttributeParser(linkstateAttributeParser.getType(), linkstateAttributeParser));
 
+        final NodeNlriParser nodeParser = new NodeNlriParser();
+        regs.add(nlriTypeReg.registerNlriTypeParser(NlriType.Node, nodeParser));
+        regs.add(nlriTypeReg.registerNlriTypeSerializer(NodeCase.class, nodeParser));
+
+        final LinkNlriParser linkParser = new LinkNlriParser();
+        regs.add(nlriTypeReg.registerNlriTypeParser(NlriType.Link, linkParser));
+        regs.add( nlriTypeReg.registerNlriTypeSerializer(LinkCase.class, linkParser));
+
+        final PrefixIpv4NlriParser ipv4PrefParser = new PrefixIpv4NlriParser();
+        regs.add(nlriTypeReg.registerNlriTypeParser(NlriType.Ipv4Prefix, ipv4PrefParser));
+
+        final PrefixIpv6NlriParser ipv6PrefParser = new PrefixIpv6NlriParser();
+        regs.add(nlriTypeReg.registerNlriTypeParser(NlriType.Ipv6Prefix, ipv6PrefParser));
+
+        final PrefixNlriSerializer prefixSerializer = new PrefixNlriSerializer();
+        regs.add(nlriTypeReg.registerNlriTypeSerializer(PrefixCase.class, prefixSerializer));
+
+        final TeLspIpv4NlriParser telSPipv4Parser = new TeLspIpv4NlriParser();
+        regs.add(nlriTypeReg.registerNlriTypeParser(NlriType.Ipv4TeLsp, telSPipv4Parser));
+
+        final TeLspIpv6NlriParser telSPipv6Parser = new TeLspIpv6NlriParser();
+        regs.add(nlriTypeReg.registerNlriTypeParser(NlriType.Ipv6TeLsp, telSPipv6Parser));
+
+        final TeLspNlriSerializer telSpSerializer = new TeLspNlriSerializer();
+        regs.add(nlriTypeReg.registerNlriTypeSerializer(TeLspCase.class, telSpSerializer));
+
         return regs;
     }
 }
index f40a9e106ea56284e75dd73dd1d23698a22cf5a7..4427de48ed2e0e57c6532d3fa1b5413ee7c4102e 100644 (file)
@@ -17,9 +17,16 @@ 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.rev150210.Ipv4InterfaceIdentifier;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.Ipv6InterfaceIdentifier;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NlriType;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.TopologyIdentifier;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.ObjectType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.destination.CLinkstateDestination;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.LinkCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.LinkCaseBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.link._case.LinkDescriptors;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.link._case.LinkDescriptorsBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.link._case.LocalNodeDescriptors;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.link._case.RemoteNodeDescriptors;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
@@ -27,13 +34,10 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 @VisibleForTesting
-public final class LinkNlriParser {
+public final class LinkNlriParser implements NlriTypeCaseParser, NlriTypeCaseSerializer {
 
     private static final Logger LOG = LoggerFactory.getLogger(LinkNlriParser.class);
 
-    private LinkNlriParser() {
-        throw new UnsupportedOperationException();
-    }
     /* Link Descriptor TLVs */
     private static final int LINK_LR_IDENTIFIERS = 258;
     private static final int IPV4_IFACE_ADDRESS = 259;
@@ -41,6 +45,11 @@ public final class LinkNlriParser {
     private static final int IPV6_IFACE_ADDRESS = 261;
     private static final int IPV6_NEIGHBOR_ADDRESS = 262;
 
+    /* Node Descriptor Type */
+    private static final int LOCAL_NODE_DESCRIPTORS_TYPE = 256;
+    private static final int REMOTE_NODE_DESCRIPTORS_TYPE = 257;
+
+
     /* Link Descriptor QNames */
     @VisibleForTesting
     public static final NodeIdentifier IPV4_IFACE_NID = new NodeIdentifier(QName.create(LinkDescriptors.QNAME, "ipv4-interface-address").intern());
@@ -148,4 +157,42 @@ public final class LinkNlriParser {
         }
         return linkDescBuilder.build();
     }
+
+
+    @Override
+    public ObjectType parseTypeNlri(final ByteBuf nlri, final NlriType type, final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NodeIdentifier localdescriptor, final ByteBuf restNlri) throws BGPParsingException {
+
+        final int nodetype = restNlri.readUnsignedShort();
+        final int length = restNlri.readUnsignedShort();
+
+        RemoteNodeDescriptors remoteDescriptors = null;
+
+        if (nodetype == REMOTE_NODE_DESCRIPTORS_TYPE) {
+            remoteDescriptors = (RemoteNodeDescriptors) NodeNlriParser.parseNodeDescriptors(restNlri.readSlice(length), type, false);
+        }
+        LinkDescriptors linkdescriptor = parseLinkDescriptors(restNlri.slice());
+        LinkCaseBuilder linkbuilder = new LinkCaseBuilder();
+        LinkCase linkcase = linkbuilder.setLocalNodeDescriptors((LocalNodeDescriptors) localdescriptor).setRemoteNodeDescriptors(remoteDescriptors).setLinkDescriptors(linkdescriptor).build();
+        return linkcase;
+    }
+
+    @Override
+    public NlriType serializeTypeNlri(final CLinkstateDestination destination, final ByteBuf localdescs, final ByteBuf byteAggregator) {
+
+        final LinkCase lCase = ((LinkCase)destination.getObjectType());
+        NodeNlriParser.serializeNodeIdentifier(lCase.getLocalNodeDescriptors(), localdescs);
+        NodeNlriParser.serializeEpeNodeDescriptors(lCase.getLocalNodeDescriptors(), localdescs);
+        TlvUtil.writeTLV(LOCAL_NODE_DESCRIPTORS_TYPE, localdescs, byteAggregator);
+        final ByteBuf rdescs = Unpooled.buffer();
+        NodeNlriParser.serializeNodeIdentifier(lCase.getRemoteNodeDescriptors(), rdescs);
+        NodeNlriParser.serializeEpeNodeDescriptors(lCase.getRemoteNodeDescriptors(), rdescs);
+        TlvUtil.writeTLV(REMOTE_NODE_DESCRIPTORS_TYPE, rdescs, byteAggregator);
+        if (lCase.getLinkDescriptors() != null) {
+            LinkNlriParser.serializeLinkDescriptors(lCase.getLinkDescriptors(), byteAggregator);
+        }
+        return NlriType.Link;
+    }
+
+
+
 }
index 7af1e65d0523533df000a69edc486c6bd5935e18..7aca24bef6a37f14d506aee208f917b0e4d2dd64 100644 (file)
@@ -19,8 +19,6 @@ import org.opendaylight.protocol.bgp.linkstate.spi.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.protocol.util.Ipv4Util;
-import org.opendaylight.protocol.util.Ipv6Util;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.Identifier;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NlriType;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.ProtocolId;
@@ -28,23 +26,15 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.link
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.ObjectType;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.destination.CLinkstateDestination;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.destination.CLinkstateDestinationBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.LinkCase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.LinkCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.NodeCase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.NodeCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.PrefixCase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.PrefixCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.TeLspCase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.TeLspCaseBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.link._case.LinkDescriptors;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.link._case.LocalNodeDescriptors;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.link._case.RemoteNodeDescriptors;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.node._case.NodeDescriptors;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.prefix._case.AdvertisingNodeDescriptors;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.prefix._case.PrefixDescriptors;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.te.lsp._case.AddressFamily;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.te.lsp._case.address.family.Ipv4CaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.te.lsp._case.address.family.Ipv6CaseBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLinkstateCaseBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.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.rev150210.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationLinkstateCase;
@@ -57,8 +47,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.mult
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.reach.nlri.AdvertizedRoutes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.LspId;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.TunnelId;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
@@ -115,21 +103,6 @@ public final class LinkstateNlriParser implements NlriParser, NlriSerializer {
         this.isVpn = isVpn;
     }
 
-    private static org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NodeIdentifier parseLink(final CLinkstateDestinationBuilder builder, final ByteBuf buffer, final LocalNodeDescriptors localDescriptors)
-        throws BGPParsingException {
-        final int type = buffer.readUnsignedShort();
-        final int length = buffer.readUnsignedShort();
-        final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NodeIdentifier remote = null;
-        RemoteNodeDescriptors remoteDescriptors = null;
-        if (type == REMOTE_NODE_DESCRIPTORS_TYPE) {
-            remoteDescriptors = (RemoteNodeDescriptors) NodeNlriParser.parseNodeDescriptors(buffer.readSlice(length), NlriType.Link, false);
-        }
-        builder.setObjectType(new LinkCaseBuilder()
-            .setLocalNodeDescriptors(localDescriptors)
-            .setRemoteNodeDescriptors(remoteDescriptors)
-            .setLinkDescriptors(LinkNlriParser.parseLinkDescriptors(buffer.slice())).build());
-        return remote;
-    }
 
     /**
      * Parses common parts for Link State Nodes, Links and Prefixes, that includes protocol ID and identifier tlv.
@@ -175,61 +148,12 @@ public final class LinkstateNlriParser implements NlriParser, NlriSerializer {
                 LOG.trace("Restlength {}", restLength);
                 rest = nlri.readSlice(restLength);
             }
-            setObjectType(nlri, builder, type, localDescriptor, rest);
-
+            builder.setObjectType(SimpleNlriTypeRegistry.getInstance().parseNlriType(nlri, type, localDescriptor, rest));
             dests.add(builder.build());
         }
         return dests;
     }
 
-    private static void setObjectType(final ByteBuf nlri, final CLinkstateDestinationBuilder builder, final NlriType type, final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NodeIdentifier localDescriptor, final ByteBuf rest) throws BGPParsingException {
-        switch (type) {
-        case Link:
-            parseLink(builder, rest, (LocalNodeDescriptors) localDescriptor);
-            break;
-        case Ipv4Prefix:
-            builder.setObjectType(new PrefixCaseBuilder()
-                .setAdvertisingNodeDescriptors((AdvertisingNodeDescriptors) localDescriptor)
-                .setPrefixDescriptors(PrefixNlriParser.parsePrefixDescriptors(rest, true)).build());
-            break;
-        case Ipv6Prefix:
-            builder.setObjectType(new PrefixCaseBuilder()
-                .setAdvertisingNodeDescriptors((AdvertisingNodeDescriptors) localDescriptor)
-                .setPrefixDescriptors(PrefixNlriParser.parsePrefixDescriptors(rest, false)).build());
-            break;
-        case Node:
-            // node nlri is already parsed as it contains only the common fields for node and link nlri
-            builder.setObjectType(new NodeCaseBuilder().setNodeDescriptors((NodeDescriptors) localDescriptor).build());
-            break;
-        case Ipv4TeLsp:
-            builder.setObjectType(parseIpv4TeLsp(nlri));
-            break;
-        case Ipv6TeLsp:
-            builder.setObjectType(parseIpv6TeLsp(nlri));
-            break;
-        default:
-            break;
-        }
-    }
-
-    private static TeLspCase parseIpv6TeLsp(final ByteBuf nlri) {
-        final Ipv6CaseBuilder ipv6Builder = new Ipv6CaseBuilder();
-        ipv6Builder.setIpv6TunnelSenderAddress(Ipv6Util.addressForByteBuf(nlri));
-        final TunnelId tunnelId6 = new TunnelId(nlri.readUnsignedShort());
-        final LspId lspId6 = new LspId((long)nlri.readUnsignedShort());
-        ipv6Builder.setIpv6TunnelEndpointAddress(Ipv6Util.addressForByteBuf(nlri));
-        return new TeLspCaseBuilder().setAddressFamily(ipv6Builder.build()).setLspId(lspId6).setTunnelId(tunnelId6).build();
-    }
-
-    private static TeLspCase parseIpv4TeLsp(final ByteBuf nlri) {
-        final Ipv4CaseBuilder ipv4Builder = new Ipv4CaseBuilder();
-        ipv4Builder.setIpv4TunnelSenderAddress(Ipv4Util.addressForByteBuf(nlri));
-        final TunnelId tunnelId = new TunnelId(nlri.readUnsignedShort());
-        final LspId lspId = new LspId((long)nlri.readUnsignedShort());
-        ipv4Builder.setIpv4TunnelEndpointAddress(Ipv4Util.addressForByteBuf(nlri));
-        return new TeLspCaseBuilder().setAddressFamily(ipv4Builder.build()).setLspId(lspId).setTunnelId(tunnelId).build();
-    }
-
     @Override
     public void parseNlri(final ByteBuf nlri, final MpUnreachNlriBuilder builder) throws BGPParsingException {
         if (!nlri.isReadable()) {
@@ -269,48 +193,8 @@ public final class LinkstateNlriParser implements NlriParser, NlriSerializer {
         nlriByteBuf.writeByte(destination.getProtocolId().getIntValue());
         nlriByteBuf.writeLong(destination.getIdentifier().getValue().longValue());
         final ByteBuf ldescs = Unpooled.buffer();
-        final ObjectType ot = destination.getObjectType();
-        NlriType nlriType = null;
-        if (ot instanceof PrefixCase) {
-            final PrefixCase pCase = (PrefixCase) destination.getObjectType();
-            NodeNlriParser.serializeNodeIdentifier(pCase.getAdvertisingNodeDescriptors(), ldescs);
-            TlvUtil.writeTLV(LOCAL_NODE_DESCRIPTORS_TYPE, ldescs, nlriByteBuf);
-            if (pCase.getPrefixDescriptors() != null) {
-                PrefixNlriParser.serializePrefixDescriptors(pCase.getPrefixDescriptors(), nlriByteBuf);
-                if (pCase.getPrefixDescriptors().getIpReachabilityInformation().getIpv4Prefix() != null) {
-                    nlriType = NlriType.Ipv4Prefix;
-                } else {
-                    nlriType = NlriType.Ipv6Prefix;
-                }
-            }
-        } else if (ot instanceof LinkCase) {
-            final LinkCase lCase = (LinkCase) destination.getObjectType();
-            NodeNlriParser.serializeNodeIdentifier(lCase.getLocalNodeDescriptors(), ldescs);
-            NodeNlriParser.serializeEpeNodeDescriptors(lCase.getLocalNodeDescriptors(), ldescs);
-            TlvUtil.writeTLV(LOCAL_NODE_DESCRIPTORS_TYPE, ldescs, nlriByteBuf);
-            final ByteBuf rdescs = Unpooled.buffer();
-            NodeNlriParser.serializeNodeIdentifier(lCase.getRemoteNodeDescriptors(), rdescs);
-            NodeNlriParser.serializeEpeNodeDescriptors(lCase.getRemoteNodeDescriptors(), rdescs);
-            TlvUtil.writeTLV(REMOTE_NODE_DESCRIPTORS_TYPE, rdescs, nlriByteBuf);
-            if (lCase.getLinkDescriptors() != null) {
-                LinkNlriParser.serializeLinkDescriptors(lCase.getLinkDescriptors(), nlriByteBuf);
-            }
-            nlriType = NlriType.Link;
-        } else if (ot instanceof NodeCase) {
-            final NodeCase nCase = (NodeCase) destination.getObjectType();
-            NodeNlriParser.serializeNodeIdentifier(nCase.getNodeDescriptors(), ldescs);
-            TlvUtil.writeTLV(LOCAL_NODE_DESCRIPTORS_TYPE, ldescs, nlriByteBuf);
-            nlriType = NlriType.Node;
-        } else if (ot instanceof TeLspCase) {
-            final TeLspCase teLSP = ((TeLspCase) destination.getObjectType());
-            final AddressFamily afi = teLSP.getAddressFamily();
-            nlriType = TeLspNlriParser.serializeIpvTSA(afi, ldescs);
-            TeLspNlriParser.serializeTunnelID(teLSP.getTunnelId(), ldescs);
-            TeLspNlriParser.serializeLspID(teLSP.getLspId(), ldescs);
-            TeLspNlriParser.serializeTEA(afi, ldescs);
-        } else {
-            LOG.warn("Unknown NLRI Type.");
-        }
+        NlriType nlriType = SimpleNlriTypeRegistry.getInstance().serializeNlriType(destination, ldescs, nlriByteBuf);
+        Preconditions.checkNotNull(nlriType, "NLRI Type value should not be null.");
         TlvUtil.writeTLV(nlriType.getIntValue(), nlriByteBuf, buffer);
     }
 
@@ -385,8 +269,8 @@ public final class LinkstateNlriParser implements NlriParser, NlriSerializer {
             } else {
                 LOG.warn("Unknown Object Type.");
             }
-        } else if (TeLspNlriParser.isTeLsp(objectType)) {
-            builder.setObjectType(TeLspNlriParser.serializeTeLsp(objectType));
+        } else if (TeLspNlriSerializer.isTeLsp(objectType)) {
+            builder.setObjectType(TeLspNlriSerializer.serializeTeLsp(objectType));
         }
         return builder.build();
     }
@@ -424,7 +308,7 @@ public final class LinkstateNlriParser implements NlriParser, NlriSerializer {
         // prefix descriptors
         final Optional<DataContainerChild<? extends PathArgument, ?>> prefixDescriptors = objectType.getChild(PREFIX_DESCRIPTORS_NID);
         if (prefixDescriptors.isPresent()) {
-            prefixBuilder.setPrefixDescriptors(PrefixNlriParser.serializePrefixDescriptors((ContainerNode) prefixDescriptors.get()));
+            prefixBuilder.setPrefixDescriptors(PrefixNlriSerializer.serializePrefixDescriptors((ContainerNode) prefixDescriptors.get()));
         }
         builder.setObjectType(prefixBuilder.build());
     }
diff --git a/bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/nlri/NlriTypeCaseParser.java b/bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/nlri/NlriTypeCaseParser.java
new file mode 100644 (file)
index 0000000..9289a7f
--- /dev/null
@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2016 AT&T 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 org.opendaylight.protocol.bgp.parser.BGPParsingException;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NlriType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NodeIdentifier;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.ObjectType;
+
+/**
+ * Common interface for encoding different NLRI types.
+ */
+public interface NlriTypeCaseParser {
+
+    ObjectType parseTypeNlri(ByteBuf nlri, NlriType type, NodeIdentifier localdescriptor, ByteBuf restNlri) throws BGPParsingException;
+
+}
diff --git a/bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/nlri/NlriTypeCaseSerializer.java b/bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/nlri/NlriTypeCaseSerializer.java
new file mode 100644 (file)
index 0000000..daf1f49
--- /dev/null
@@ -0,0 +1,21 @@
+/*
+ * Copyright (c) 2016 AT&T 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 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NlriType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.destination.CLinkstateDestination;
+
+/**
+ * Common interface for decoding different NLRI types.
+ */
+public interface NlriTypeCaseSerializer {
+
+    NlriType serializeTypeNlri(CLinkstateDestination destination, ByteBuf localdescs, ByteBuf byteAggregator);
+
+}
index 600a0535844d31abe17398ca261fc7596b69ff0f..c8efba11d81d0e10c266a4a9b3ff977ebd6b3572 100644 (file)
@@ -13,6 +13,7 @@ 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.spi.TlvUtil;
 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
 import org.opendaylight.protocol.util.ByteArray;
@@ -26,6 +27,10 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.link
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.OspfInterfaceIdentifier;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.isis.lan.identifier.IsIsRouterIdentifier;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.isis.lan.identifier.IsIsRouterIdentifierBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.ObjectType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.destination.CLinkstateDestination;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.NodeCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.NodeCaseBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.link._case.LocalNodeDescriptors;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.link._case.LocalNodeDescriptorsBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.link._case.RemoteNodeDescriptors;
@@ -62,12 +67,11 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 @VisibleForTesting
-public final class NodeNlriParser {
+public final class NodeNlriParser implements NlriTypeCaseParser, NlriTypeCaseSerializer {
 
     private static final Logger LOG = LoggerFactory.getLogger(NodeNlriParser.class);
 
-    private NodeNlriParser() {
-        throw new UnsupportedOperationException();
+    public NodeNlriParser() {
     }
 
     private static final int OSPF_PSEUDONODE_ROUTER_ID_LENGTH = 8;
@@ -83,6 +87,9 @@ public final class NodeNlriParser {
     private static final int BGP_ROUTER_ID = 516;
     private static final int MEMBER_AS_NUMBER = 517;
 
+    /* Node Descriptor Type */
+    private static final int LOCAL_NODE_DESCRIPTORS_TYPE = 256;
+
     /* Node Descriptor QNames */
     @VisibleForTesting
     public static final NodeIdentifier AS_NUMBER_NID = new NodeIdentifier(QName.create(NodeDescriptors.QNAME, "as-number").intern());
@@ -394,4 +401,22 @@ public final class NodeNlriParser {
         builder.setCRouterIdentifier(serializeRouterId(descriptorsData));
         return builder.build();
     }
+
+    @Override
+    public ObjectType parseTypeNlri(final ByteBuf nlri, final NlriType type, final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NodeIdentifier localdescriptor, final ByteBuf restNlri) throws BGPParsingException {
+
+        NodeCaseBuilder nodebuilder = new NodeCaseBuilder();
+        NodeCase nodecase = nodebuilder.setNodeDescriptors((NodeDescriptors) localdescriptor).build();
+        return nodecase;
+
+    }
+
+    @Override
+    public NlriType serializeTypeNlri(final CLinkstateDestination destination, final ByteBuf localdescs, final ByteBuf byteAggregator) {
+        final NodeCase nCase = ((NodeCase)destination.getObjectType());
+        NodeNlriParser.serializeNodeIdentifier(nCase.getNodeDescriptors(), localdescs);
+        TlvUtil.writeTLV(LOCAL_NODE_DESCRIPTORS_TYPE, localdescs, byteAggregator);
+        return NlriType.Node;
+    }
+
 }
diff --git a/bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/nlri/PrefixIpv4NlriParser.java b/bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/nlri/PrefixIpv4NlriParser.java
new file mode 100644 (file)
index 0000000..062120a
--- /dev/null
@@ -0,0 +1,91 @@
+/*
+ * 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.annotations.VisibleForTesting;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufUtil;
+import org.opendaylight.protocol.bgp.linkstate.spi.TlvUtil;
+import org.opendaylight.protocol.bgp.parser.BGPParsingException;
+import org.opendaylight.protocol.util.Ipv4Util;
+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.rev150210.NlriType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.OspfRouteType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.TopologyIdentifier;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.ObjectType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.PrefixCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.PrefixCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.prefix._case.AdvertisingNodeDescriptors;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.prefix._case.PrefixDescriptors;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.prefix._case.PrefixDescriptorsBuilder;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@VisibleForTesting
+public final class PrefixIpv4NlriParser implements NlriTypeCaseParser  {
+
+    private static final Logger LOG = LoggerFactory.getLogger(PrefixIpv4NlriParser.class);
+
+    /* Prefix Descriptor TLVs */
+    private static final int OSPF_ROUTE_TYPE = 264;
+    private static final int IP_REACHABILITY = 265;
+
+    /* Prefix Descriptor QNames */
+    @VisibleForTesting
+    public static final NodeIdentifier OSPF_ROUTE_NID = new NodeIdentifier(QName.create(PrefixDescriptors.QNAME, "ospf-route-type").intern());
+    @VisibleForTesting
+    public static final NodeIdentifier IP_REACH_NID = new NodeIdentifier(QName.create(PrefixDescriptors.QNAME, "ip-reachability-information").intern());
+
+    static PrefixDescriptors parseIpv4PrefixDescriptors(final ByteBuf buffer) throws BGPParsingException {
+        final PrefixDescriptorsBuilder builder = new PrefixDescriptorsBuilder();
+        while (buffer.isReadable()) {
+            final int type = buffer.readUnsignedShort();
+            final int length = buffer.readUnsignedShort();
+            final ByteBuf value = buffer.readSlice(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:
+                final IpPrefix prefix = new IpPrefix(Ipv4Util.prefixForByteBuf(value));
+                builder.setIpReachabilityInformation(prefix);
+                LOG.trace("Parsed IP reachability info: {}", prefix);
+                break;
+            default:
+                throw new BGPParsingException("Prefix Descriptor not recognized, type: " + type);
+            }
+        }
+        LOG.debug("Finished parsing Ipv4 Prefix descriptors.");
+        return builder.build();
+    }
+
+    @Override
+    public ObjectType parseTypeNlri(final ByteBuf nlri, final NlriType type, final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NodeIdentifier localdescriptor, final ByteBuf restNlri) throws BGPParsingException {
+        PrefixCaseBuilder prefixbuilder = new PrefixCaseBuilder();
+        PrefixDescriptors prefdesc = parseIpv4PrefixDescriptors(restNlri);
+        PrefixCase prefixcase = prefixbuilder.setAdvertisingNodeDescriptors((AdvertisingNodeDescriptors) localdescriptor).setPrefixDescriptors(prefdesc).build();
+        return prefixcase;
+    }
+
+}
diff --git a/bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/nlri/PrefixIpv6NlriParser.java b/bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/nlri/PrefixIpv6NlriParser.java
new file mode 100644 (file)
index 0000000..32c15d4
--- /dev/null
@@ -0,0 +1,91 @@
+/*
+ * 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.annotations.VisibleForTesting;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufUtil;
+import org.opendaylight.protocol.bgp.linkstate.spi.TlvUtil;
+import org.opendaylight.protocol.bgp.parser.BGPParsingException;
+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.rev150210.NlriType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.OspfRouteType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.TopologyIdentifier;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.ObjectType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.PrefixCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.PrefixCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.prefix._case.AdvertisingNodeDescriptors;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.prefix._case.PrefixDescriptors;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.prefix._case.PrefixDescriptorsBuilder;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@VisibleForTesting
+public final class PrefixIpv6NlriParser implements NlriTypeCaseParser {
+
+    private static final Logger LOG = LoggerFactory.getLogger(PrefixIpv6NlriParser.class);
+
+    /* Prefix Descriptor TLVs */
+    private static final int OSPF_ROUTE_TYPE = 264;
+    private static final int IP_REACHABILITY = 265;
+
+    /* Prefix Descriptor QNames */
+    @VisibleForTesting
+    public static final NodeIdentifier OSPF_ROUTE_NID = new NodeIdentifier(QName.create(PrefixDescriptors.QNAME, "ospf-route-type").intern());
+    @VisibleForTesting
+    public static final NodeIdentifier IP_REACH_NID = new NodeIdentifier(QName.create(PrefixDescriptors.QNAME, "ip-reachability-information").intern());
+
+    static PrefixDescriptors parseIpv6PrefixDescriptors(final ByteBuf buffer) throws BGPParsingException {
+        final PrefixDescriptorsBuilder builder = new PrefixDescriptorsBuilder();
+        while (buffer.isReadable()) {
+            final int type = buffer.readUnsignedShort();
+            final int length = buffer.readUnsignedShort();
+            final ByteBuf value = buffer.readSlice(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:
+                final IpPrefix prefix = new IpPrefix(Ipv6Util.prefixForByteBuf(value));
+                builder.setIpReachabilityInformation(prefix);
+                LOG.trace("Parsed IP reachability info: {}", prefix);
+                break;
+            default:
+                throw new BGPParsingException("Prefix Descriptor not recognized, type: " + type);
+            }
+        }
+        LOG.debug("Finished parsing Ipv6 Prefix descriptors.");
+        return builder.build();
+    }
+
+    @Override
+    public ObjectType parseTypeNlri(final ByteBuf nlri, final NlriType type, final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NodeIdentifier localdescriptor, final ByteBuf restNlri) throws BGPParsingException {
+        PrefixCaseBuilder prefixbuilder = new PrefixCaseBuilder();
+        PrefixDescriptors prefdesc = parseIpv6PrefixDescriptors(restNlri);
+        PrefixCase prefixcase = prefixbuilder.setAdvertisingNodeDescriptors((AdvertisingNodeDescriptors) localdescriptor).setPrefixDescriptors(prefdesc).build();
+        return prefixcase;
+    }
+
+}
similarity index 73%
rename from bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/nlri/PrefixNlriParser.java
rename to bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/nlri/PrefixNlriSerializer.java
index 0dea9ccb78d2fe88070c68ee52fe55568a914e27..131da71289b303c97a4862f7cc1bf15e8c3b2056 100644 (file)
@@ -11,18 +11,19 @@ import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Optional;
 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.spi.TlvUtil;
-import org.opendaylight.protocol.bgp.parser.BGPParsingException;
 import org.opendaylight.protocol.util.ByteBufWriteUtil;
 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.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NlriType;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.OspfRouteType;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.TopologyIdentifier;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.destination.CLinkstateDestination;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.PrefixCase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.prefix._case.PrefixDescriptors;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.prefix._case.PrefixDescriptorsBuilder;
 import org.opendaylight.yangtools.yang.common.QName;
@@ -33,62 +34,24 @@ import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-@VisibleForTesting
-public final class PrefixNlriParser {
+public final class PrefixNlriSerializer implements NlriTypeCaseSerializer {
 
-    private static final Logger LOG = LoggerFactory.getLogger(PrefixNlriParser.class);
+    private static final Logger LOG = LoggerFactory.getLogger(PrefixNlriSerializer.class);
 
-    private PrefixNlriParser() {
-        throw new UnsupportedOperationException();
-    }
 
     /* Prefix Descriptor TLVs */
     private static final int OSPF_ROUTE_TYPE = 264;
     private static final int IP_REACHABILITY = 265;
 
+    /* Node Descriptor Type */
+    private static final int LOCAL_NODE_DESCRIPTORS_TYPE = 256;
+
     /* Prefix Descriptor QNames */
     @VisibleForTesting
     public static final NodeIdentifier OSPF_ROUTE_NID = new NodeIdentifier(QName.create(PrefixDescriptors.QNAME, "ospf-route-type").intern());
     @VisibleForTesting
     public static final NodeIdentifier IP_REACH_NID = new NodeIdentifier(QName.create(PrefixDescriptors.QNAME, "ip-reachability-information").intern());
 
-    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.readSlice(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:
-                final IpPrefix prefix = (ipv4) ? new IpPrefix(Ipv4Util.prefixForByteBuf(value)) : new IpPrefix(Ipv6Util.prefixForByteBuf(value));
-                builder.setIpReachabilityInformation(prefix);
-                LOG.trace("Parsed IP reachability info: {}", prefix);
-                break;
-            default:
-                throw new BGPParsingException("Prefix Descriptor not recognized, type: " + type);
-            }
-        }
-        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);
@@ -156,4 +119,21 @@ public final class PrefixNlriParser {
         }
         return prefixDescBuilder.build();
     }
+
+    @Override
+    public NlriType serializeTypeNlri(final CLinkstateDestination destination, final ByteBuf localdescs, final ByteBuf byteAggregator)  {
+        final PrefixCase pCase = ((PrefixCase)destination.getObjectType());
+        NodeNlriParser.serializeNodeIdentifier(pCase.getAdvertisingNodeDescriptors(), localdescs);
+        TlvUtil.writeTLV(LOCAL_NODE_DESCRIPTORS_TYPE, localdescs, byteAggregator);
+        if (pCase.getPrefixDescriptors() != null) {
+            serializePrefixDescriptors(pCase.getPrefixDescriptors(), byteAggregator);
+            if (pCase.getPrefixDescriptors().getIpReachabilityInformation().getIpv4Prefix() != null) {
+                return NlriType.Ipv4Prefix;
+            } else {
+                return NlriType.Ipv6Prefix;
+            }
+        }
+        return null;
+    }
+
 }
diff --git a/bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/nlri/SimpleNlriTypeRegistry.java b/bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/nlri/SimpleNlriTypeRegistry.java
new file mode 100644 (file)
index 0000000..57010ef
--- /dev/null
@@ -0,0 +1,97 @@
+/*
+ * Copyright (c) 2016 AT&T 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.annotations.VisibleForTesting;
+import com.google.common.base.Preconditions;
+import io.netty.buffer.ByteBuf;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import org.opendaylight.protocol.bgp.parser.BGPParsingException;
+import org.opendaylight.protocol.concepts.AbstractRegistration;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NlriType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NodeIdentifier;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.ObjectType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.destination.CLinkstateDestination;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public final class SimpleNlriTypeRegistry {
+
+    private static final Logger LOG = LoggerFactory.getLogger(SimpleNlriTypeRegistry.class);
+
+    private final ConcurrentMap<NlriType, NlriTypeCaseParser> parsers = new ConcurrentHashMap<>();
+    private final ConcurrentMap<Class<? extends ObjectType>, NlriTypeCaseSerializer> serializers = new ConcurrentHashMap<>();
+    private static final SimpleNlriTypeRegistry SINGLETON = new SimpleNlriTypeRegistry();
+
+    private SimpleNlriTypeRegistry () {
+
+    }
+
+    public static SimpleNlriTypeRegistry getInstance() {
+        return SINGLETON;
+    }
+
+    public synchronized AutoCloseable registerNlriTypeSerializer(final Class<? extends ObjectType> classKey, final NlriTypeCaseSerializer serializer) {
+        this.serializers.put(classKey, serializer);
+
+        final Object lock = this;
+        return new AbstractRegistration() {
+            @Override
+            protected void removeRegistration() {
+                synchronized (lock) {
+                    SimpleNlriTypeRegistry.this.serializers.remove(classKey);
+                }
+            }
+        };
+
+    }
+
+    public synchronized AutoCloseable registerNlriTypeParser(final NlriType key, final NlriTypeCaseParser parser) {
+        this.parsers.put(key, parser);
+
+        final Object lock = this;
+        return new AbstractRegistration() {
+            @Override
+            protected void removeRegistration() {
+                synchronized (lock) {
+                    SimpleNlriTypeRegistry.this.parsers.remove(key);
+                }
+            }
+        };
+
+    }
+
+    @VisibleForTesting
+    public NlriTypeCaseSerializer getSerializer(Class<? extends ObjectType> objtyp) {
+        return this.serializers.get(objtyp);
+    }
+
+    @VisibleForTesting
+    public NlriTypeCaseParser getParser(NlriType nlrityp) {
+        return this.parsers.get(nlrityp);
+    }
+
+
+    public ObjectType parseNlriType (final ByteBuf buffer, final NlriType type, final NodeIdentifier localdescriptor, final ByteBuf restBuffer) throws BGPParsingException {
+        final NlriTypeCaseParser parser = this.parsers.get(type);
+        Preconditions.checkNotNull(parser, "Parser for Nlri type %s not found.", type);
+        ObjectType nlriObjectType = parser.parseTypeNlri(buffer, type, localdescriptor, restBuffer);
+        return nlriObjectType;
+    }
+
+    public NlriType serializeNlriType (final CLinkstateDestination destination, final ByteBuf localdesc, final ByteBuf byteAggregator) {
+        final NlriTypeCaseSerializer serializer = this.serializers.get(destination.getObjectType().getImplementedInterface());
+        Preconditions.checkNotNull(serializer, "Serializer for %s not found.", destination.getObjectType().getImplementedInterface().getSimpleName());
+        NlriType nlriTypeVal = serializer.serializeTypeNlri(destination, localdesc, byteAggregator);
+        if (nlriTypeVal == null) {
+            LOG.warn("NLRI Type value is null.");
+        }
+        return nlriTypeVal;
+    }
+}
diff --git a/bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/nlri/TeLspIpv4NlriParser.java b/bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/nlri/TeLspIpv4NlriParser.java
new file mode 100644 (file)
index 0000000..e6a39ef
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2015 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 org.opendaylight.protocol.bgp.parser.BGPParsingException;
+import org.opendaylight.protocol.util.Ipv4Util;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NlriType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NodeIdentifier;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.ObjectType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.TeLspCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.TeLspCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.te.lsp._case.address.family.Ipv4CaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.LspId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.TunnelId;
+
+public final class TeLspIpv4NlriParser implements NlriTypeCaseParser {
+
+    @Override
+    public ObjectType parseTypeNlri(final ByteBuf nlri, final NlriType type, final NodeIdentifier localdescriptor, final ByteBuf restNlri) throws BGPParsingException {
+        final TeLspCaseBuilder telspipv4builder = new TeLspCaseBuilder();
+        final Ipv4CaseBuilder ipv4Builder = new Ipv4CaseBuilder();
+        ipv4Builder.setIpv4TunnelSenderAddress(Ipv4Util.addressForByteBuf(nlri));
+        final TunnelId tunnelId = new TunnelId(nlri.readUnsignedShort());
+        final LspId lspId = new LspId((long)nlri.readUnsignedShort());
+        ipv4Builder.setIpv4TunnelEndpointAddress(Ipv4Util.addressForByteBuf(nlri));
+        TeLspCase telspipv4Case = telspipv4builder.setAddressFamily(ipv4Builder.build()).setLspId(lspId).setTunnelId(tunnelId).build();
+        return telspipv4Case;
+    }
+
+}
diff --git a/bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/nlri/TeLspIpv6NlriParser.java b/bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/nlri/TeLspIpv6NlriParser.java
new file mode 100644 (file)
index 0000000..6cd4bd3
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2015 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 org.opendaylight.protocol.bgp.parser.BGPParsingException;
+import org.opendaylight.protocol.util.Ipv6Util;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NlriType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NodeIdentifier;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.ObjectType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.TeLspCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.TeLspCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.te.lsp._case.address.family.Ipv6CaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.LspId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.TunnelId;
+
+public final class TeLspIpv6NlriParser implements NlriTypeCaseParser {
+
+    @Override
+    public ObjectType parseTypeNlri(ByteBuf nlri, NlriType type, NodeIdentifier localdescriptor, ByteBuf restNlri) throws BGPParsingException {
+        final TeLspCaseBuilder telspipv6builder = new TeLspCaseBuilder();
+        final Ipv6CaseBuilder ipv6Builder = new Ipv6CaseBuilder();
+        ipv6Builder.setIpv6TunnelSenderAddress(Ipv6Util.addressForByteBuf(nlri));
+        final TunnelId tunnelId6 = new TunnelId(nlri.readUnsignedShort());
+        final LspId lspId6 = new LspId((long)nlri.readUnsignedShort());
+        ipv6Builder.setIpv6TunnelEndpointAddress(Ipv6Util.addressForByteBuf(nlri));
+        TeLspCase telspipv6Case = telspipv6builder.setAddressFamily(ipv6Builder.build()).setLspId(lspId6).setTunnelId(tunnelId6).build();
+        return telspipv6Case;
+    }
+
+}
similarity index 92%
rename from bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/nlri/TeLspNlriParser.java
rename to bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/nlri/TeLspNlriSerializer.java
index 0a19ea40bfcf8cba16b05c88b2b645cd54b12f60..3d8abbb26f62d49328d43b54cb98838c181be3f5 100644 (file)
@@ -5,7 +5,6 @@
  * 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 static org.opendaylight.protocol.util.ByteBufWriteUtil.writeIpv4Address;
@@ -34,7 +33,7 @@ import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
 
 @VisibleForTesting
-public final class TeLspNlriParser {
+public final class TeLspNlriSerializer implements NlriTypeCaseSerializer {
 
     @VisibleForTesting
     public static final YangInstanceIdentifier.NodeIdentifier LSP_ID = new YangInstanceIdentifier.NodeIdentifier(
@@ -58,10 +57,6 @@ public final class TeLspNlriParser {
     @VisibleForTesting
     public static final YangInstanceIdentifier.NodeIdentifier ADDRESS_FAMILY = new YangInstanceIdentifier.NodeIdentifier(AddressFamily.QNAME);
 
-    private TeLspNlriParser() {
-        throw new UnsupportedOperationException();
-    }
-
     public static NlriType serializeIpvTSA(final AddressFamily addressFamily, final ByteBuf body) {
         if (addressFamily.equals(Ipv6Case.class)) {
             final Ipv6Address ipv6 = ((Ipv6Case) addressFamily).getIpv6TunnelSenderAddress();
@@ -128,4 +123,16 @@ public final class TeLspNlriParser {
             .setIpv6TunnelEndpointAddress(new Ipv6Address((String) addressFamily.getChild(IPV6_TUNNEL_ENDPOINT_ADDRESS).get().getValue()))
             .build();
     }
+
+    @Override
+    public NlriType serializeTypeNlri(final CLinkstateDestination destination, final ByteBuf localdescs, final ByteBuf byteAggregator) {
+        final TeLspCase teLSP = ((TeLspCase) destination.getObjectType());
+        final AddressFamily afi = teLSP.getAddressFamily();
+        NlriType nlriType = serializeIpvTSA(afi, localdescs);
+        serializeTunnelID(teLSP.getTunnelId(), localdescs);
+        serializeLspID(teLSP.getLspId(), localdescs);
+        serializeTEA(afi, localdescs);
+        return nlriType;
+    }
+
 }
index 551d09a97a3786d91e31e29a5238eff28cc0a1cc..27c84ea78246153636a606ccb934087ff65e17b6 100644 (file)
@@ -10,7 +10,17 @@ package org.opendaylight.protocol.bgp.linkstate;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
 import org.junit.Test;
+import org.opendaylight.protocol.bgp.linkstate.nlri.LinkNlriParser;
+import org.opendaylight.protocol.bgp.linkstate.nlri.NodeNlriParser;
+import org.opendaylight.protocol.bgp.linkstate.nlri.PrefixIpv4NlriParser;
+import org.opendaylight.protocol.bgp.linkstate.nlri.PrefixIpv6NlriParser;
+import org.opendaylight.protocol.bgp.linkstate.nlri.PrefixNlriSerializer;
+import org.opendaylight.protocol.bgp.linkstate.nlri.SimpleNlriTypeRegistry;
+import org.opendaylight.protocol.bgp.linkstate.nlri.TeLspIpv4NlriParser;
+import org.opendaylight.protocol.bgp.linkstate.nlri.TeLspIpv6NlriParser;
+import org.opendaylight.protocol.bgp.linkstate.nlri.TeLspNlriSerializer;
 import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
 import org.opendaylight.protocol.bgp.parser.spi.pojo.SimpleBGPExtensionProviderContext;
 import org.opendaylight.protocol.bgp.rib.spi.RIBExtensionProviderContext;
@@ -18,6 +28,11 @@ import org.opendaylight.protocol.bgp.rib.spi.SimpleRIBExtensionProviderContext;
 import org.opendaylight.protocol.rsvp.parser.spi.pojo.ServiceLoaderRSVPExtensionProviderContext;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.LinkstateAddressFamily;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.LinkstateSubsequentAddressFamily;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NlriType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.LinkCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.NodeCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.PrefixCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.TeLspCase;
 
 public class ActivatorTest {
 
@@ -25,6 +40,7 @@ public class ActivatorTest {
     public void testActivator() throws Exception {
         final BGPActivator act = new BGPActivator(true, ServiceLoaderRSVPExtensionProviderContext.getSingletonInstance().getRsvpRegistry());
         final BGPExtensionProviderContext context = new SimpleBGPExtensionProviderContext();
+        final SimpleNlriTypeRegistry typeReg = SimpleNlriTypeRegistry.getInstance();
 
         assertNull(context.getAddressFamilyRegistry().classForFamily(16388));
         assertNull(context.getSubsequentAddressFamilyRegistry().classForFamily(71));
@@ -33,6 +49,16 @@ public class ActivatorTest {
 
         assertEquals(LinkstateAddressFamily.class, context.getAddressFamilyRegistry().classForFamily(16388));
         assertEquals(LinkstateSubsequentAddressFamily.class, context.getSubsequentAddressFamilyRegistry().classForFamily(71));
+        assertTrue(typeReg.getParser(NlriType.Node) instanceof NodeNlriParser);
+        assertTrue(typeReg.getParser(NlriType.Link) instanceof LinkNlriParser);
+        assertTrue(typeReg.getParser(NlriType.Ipv4Prefix) instanceof PrefixIpv4NlriParser);
+        assertTrue(typeReg.getParser(NlriType.Ipv6Prefix) instanceof PrefixIpv6NlriParser);
+        assertTrue(typeReg.getParser(NlriType.Ipv4TeLsp) instanceof TeLspIpv4NlriParser);
+        assertTrue(typeReg.getParser(NlriType.Ipv6TeLsp) instanceof TeLspIpv6NlriParser);
+        assertTrue(typeReg.getSerializer(NodeCase.class) instanceof NodeNlriParser);
+        assertTrue(typeReg.getSerializer(LinkCase.class) instanceof LinkNlriParser);
+        assertTrue(typeReg.getSerializer(PrefixCase.class) instanceof PrefixNlriSerializer);
+        assertTrue(typeReg.getSerializer(TeLspCase.class) instanceof TeLspNlriSerializer);
 
         act.close();
     }
index d42b0e8fc42713fda1bb6fa28677fd132a106f46..099a08b2ec40b58e2c9f64cf7de5dc7ea7e5dd28 100644 (file)
@@ -14,16 +14,19 @@ import static org.junit.Assert.assertNull;
 import com.google.common.collect.Lists;
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
-import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationTargetException;
 import java.math.BigInteger;
 import java.util.List;
 import org.junit.Test;
 import org.opendaylight.protocol.bgp.linkstate.nlri.LinkNlriParser;
 import org.opendaylight.protocol.bgp.linkstate.nlri.LinkstateNlriParser;
 import org.opendaylight.protocol.bgp.linkstate.nlri.NodeNlriParser;
-import org.opendaylight.protocol.bgp.linkstate.nlri.PrefixNlriParser;
-import org.opendaylight.protocol.bgp.linkstate.nlri.TeLspNlriParser;
+import org.opendaylight.protocol.bgp.linkstate.nlri.PrefixIpv4NlriParser;
+import org.opendaylight.protocol.bgp.linkstate.nlri.PrefixIpv6NlriParser;
+import org.opendaylight.protocol.bgp.linkstate.nlri.PrefixNlriSerializer;
+import org.opendaylight.protocol.bgp.linkstate.nlri.SimpleNlriTypeRegistry;
+import org.opendaylight.protocol.bgp.linkstate.nlri.TeLspIpv4NlriParser;
+import org.opendaylight.protocol.bgp.linkstate.nlri.TeLspIpv6NlriParser;
+import org.opendaylight.protocol.bgp.linkstate.nlri.TeLspNlriSerializer;
 import org.opendaylight.protocol.bgp.linkstate.spi.TlvUtil;
 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
 import org.opendaylight.protocol.util.ByteArray;
@@ -31,6 +34,7 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.DomainIdentifier;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NlriType;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.OspfRouteType;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.ProtocolId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.TopologyIdentifier;
@@ -140,6 +144,34 @@ public class LinkstateNlriParserTest {
     private void setUp(final byte[] data) throws BGPParsingException {
         final LinkstateNlriParser parser = new LinkstateNlriParser(false);
         final MpReachNlriBuilder builder = new MpReachNlriBuilder();
+        final SimpleNlriTypeRegistry testreg = SimpleNlriTypeRegistry.getInstance();
+
+        final NodeNlriParser nodeParser = new NodeNlriParser();
+        testreg.registerNlriTypeParser(NlriType.Node, nodeParser);
+        testreg.registerNlriTypeSerializer(NodeCase.class, nodeParser);
+
+        final LinkNlriParser linkParser = new LinkNlriParser();
+        testreg.registerNlriTypeParser(NlriType.Link, linkParser);
+        testreg.registerNlriTypeSerializer(LinkCase.class, linkParser);
+
+        final PrefixIpv4NlriParser ipv4PrefParser = new PrefixIpv4NlriParser();
+        testreg.registerNlriTypeParser(NlriType.Ipv4Prefix, ipv4PrefParser);
+
+        final PrefixIpv6NlriParser ipv6PrefParser = new PrefixIpv6NlriParser();
+        testreg.registerNlriTypeParser(NlriType.Ipv6Prefix, ipv6PrefParser);
+
+        final TeLspIpv4NlriParser telSPipv4Parser = new TeLspIpv4NlriParser();
+        testreg.registerNlriTypeParser(NlriType.Ipv4TeLsp, telSPipv4Parser);
+
+        final TeLspIpv6NlriParser telSPipv6Parser = new TeLspIpv6NlriParser();
+        testreg.registerNlriTypeParser(NlriType.Ipv6TeLsp, telSPipv6Parser);
+
+        final PrefixNlriSerializer prefixSerializer = new PrefixNlriSerializer();
+        testreg.registerNlriTypeSerializer(PrefixCase.class, prefixSerializer);
+
+        final TeLspNlriSerializer telSpSerializer = new TeLspNlriSerializer();
+        testreg.registerNlriTypeSerializer(TeLspCase.class, telSpSerializer);
+
         parser.parseNlri(Unpooled.copiedBuffer(data), builder);
 
         final DestinationLinkstate ls = ((DestinationLinkstateCase) builder.getAdvertizedRoutes().getDestinationType()).getDestinationLinkstate();
@@ -476,13 +508,13 @@ public class LinkstateNlriParserTest {
         prefixDescriptors.addChild(multiTopologyId.build());
 
         final ImmutableLeafNodeBuilder<String> ipReachabilityInformation = new ImmutableLeafNodeBuilder<>();
-        ipReachabilityInformation.withNodeIdentifier(PrefixNlriParser.IP_REACH_NID);
+        ipReachabilityInformation.withNodeIdentifier(PrefixIpv4NlriParser.IP_REACH_NID);
         ipReachabilityInformation.withValue("255.255.0.0/16");
 
         prefixDescriptors.addChild(ipReachabilityInformation.build());
 
         final ImmutableLeafNodeBuilder<String> ospfRouteType = new ImmutableLeafNodeBuilder<>();
-        ospfRouteType.withNodeIdentifier(PrefixNlriParser.OSPF_ROUTE_NID);
+        ospfRouteType.withNodeIdentifier(PrefixIpv4NlriParser.OSPF_ROUTE_NID);
         ospfRouteType.withValue("external1");
 
         prefixDescriptors.addChild(ospfRouteType.build());
@@ -526,22 +558,22 @@ public class LinkstateNlriParserTest {
         objectType.withNodeIdentifier(LinkstateNlriParser.OBJECT_TYPE_NID);
 
         final ImmutableLeafNodeBuilder<Long> lspId = new ImmutableLeafNodeBuilder<>();
-        lspId.withNodeIdentifier(TeLspNlriParser.LSP_ID);
+        lspId.withNodeIdentifier(TeLspNlriSerializer.LSP_ID);
         lspId.withValue(1L);
 
         final ImmutableLeafNodeBuilder<Integer> tunnelId = new ImmutableLeafNodeBuilder<>();
-        tunnelId.withNodeIdentifier(TeLspNlriParser.TUNNEL_ID);
+        tunnelId.withNodeIdentifier(TeLspNlriSerializer.TUNNEL_ID);
         tunnelId.withValue(1);
 
         final DataContainerNodeBuilder<NodeIdentifier, ChoiceNode> addressFamily = Builders.choiceBuilder();
-        addressFamily.withNodeIdentifier(TeLspNlriParser.ADDRESS_FAMILY);
+        addressFamily.withNodeIdentifier(TeLspNlriSerializer.ADDRESS_FAMILY);
 
         final ImmutableLeafNodeBuilder<String> ipv4TunnelSenderAddress = new ImmutableLeafNodeBuilder<>();
-        ipv4TunnelSenderAddress.withNodeIdentifier(TeLspNlriParser.IPV4_TUNNEL_SENDER_ADDRESS);
+        ipv4TunnelSenderAddress.withNodeIdentifier(TeLspNlriSerializer.IPV4_TUNNEL_SENDER_ADDRESS);
         ipv4TunnelSenderAddress.withValue("1.2.3.4");
 
         final ImmutableLeafNodeBuilder<String> ipv4TunnelEndPointAddress = new ImmutableLeafNodeBuilder<>();
-        ipv4TunnelEndPointAddress.withNodeIdentifier(TeLspNlriParser.IPV4_TUNNEL_ENDPOINT_ADDRESS);
+        ipv4TunnelEndPointAddress.withNodeIdentifier(TeLspNlriSerializer.IPV4_TUNNEL_ENDPOINT_ADDRESS);
         ipv4TunnelEndPointAddress.withValue("4.3.2.1");
 
         addressFamily.addChild(ipv4TunnelSenderAddress.build());
@@ -584,47 +616,4 @@ public class LinkstateNlriParserTest {
         assertArrayEquals(this.nodeNlri, ByteArray.getAllBytes(result));
     }
 
-    @Test(expected = UnsupportedOperationException.class)
-    public void testLinkNlriPrivateConstructor() throws Throwable {
-        final Constructor<LinkNlriParser> c = LinkNlriParser.class.getDeclaredConstructor();
-        c.setAccessible(true);
-        try {
-            c.newInstance();
-        } catch (final InvocationTargetException e) {
-            throw e.getCause();
-        }
-    }
-
-    @Test(expected = UnsupportedOperationException.class)
-    public void testNodeNlriPrivateConstructor() throws Throwable {
-        final Constructor<NodeNlriParser> c = NodeNlriParser.class.getDeclaredConstructor();
-        c.setAccessible(true);
-        try {
-            c.newInstance();
-        } catch (final InvocationTargetException e) {
-            throw e.getCause();
-        }
-    }
-
-    @Test(expected = UnsupportedOperationException.class)
-    public void testPrefixNlriPrivateConstructor() throws Throwable {
-        final Constructor<PrefixNlriParser> c = PrefixNlriParser.class.getDeclaredConstructor();
-        c.setAccessible(true);
-        try {
-            c.newInstance();
-        } catch (final InvocationTargetException e) {
-            throw e.getCause();
-        }
-    }
-
-    @Test(expected = UnsupportedOperationException.class)
-    public void testTeLspNlriPrivateConstructor() throws Throwable {
-        final Constructor<TeLspNlriParser> c = TeLspNlriParser.class.getDeclaredConstructor();
-        c.setAccessible(true);
-        try {
-            c.newInstance();
-        } catch (final InvocationTargetException e) {
-            throw e.getCause();
-        }
-    }
 }
index e0f53b31a36583e71740652ee8561864598912a3..5f3a7d21cf9e21284333f5df47e4a2d396aa1bed 100644 (file)
@@ -24,11 +24,13 @@ import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
 import org.opendaylight.protocol.bgp.linkstate.nlri.LinkstateNlriParser;
 import org.opendaylight.protocol.bgp.linkstate.nlri.NodeNlriParser;
+import org.opendaylight.protocol.bgp.linkstate.nlri.SimpleNlriTypeRegistry;
 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.LinkstateAddressFamily;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.LinkstateSubsequentAddressFamily;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.destination.CLinkstateDestination;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.NodeCase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.CRouterIdentifier;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlri;
@@ -107,6 +109,9 @@ public class LinkstateRIBSupportTest {
     public void testDestinationRoutes() {
         final YangInstanceIdentifier yangIdentifier = YangInstanceIdentifier.of(Routes.QNAME);
 
+        final NodeNlriParser nodeParser = new NodeNlriParser();
+        SimpleNlriTypeRegistry.getInstance().registerNlriTypeSerializer(NodeCase.class, nodeParser);
+
         final DataContainerNodeAttrBuilder<NodeIdentifier, UnkeyedListEntryNode> linkstateBI = ImmutableUnkeyedListEntryNodeBuilder.create();
         linkstateBI.withNodeIdentifier(new NodeIdentifier(CLinkstateDestination.QNAME));