BUG-2982 : moved path-attributes container to grouping
[bgpcep.git] / bgp / linkstate / src / main / java / org / opendaylight / protocol / bgp / linkstate / LinkstateAdjRIBsIn.java
index fd8ef23f5f03f36d87dcb1a6d046edf05b503011..32e1254053541268e52d8544cd71637e0bdeeb3c 100644 (file)
  */
 package org.opendaylight.protocol.bgp.linkstate;
 
-import java.util.Comparator;
-
-import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
-import org.opendaylight.protocol.bgp.rib.RibReference;
-import org.opendaylight.protocol.bgp.rib.spi.AbstractAdjRIBsIn;
+import com.google.common.base.MoreObjects.ToStringHelper;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
+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;
 import org.opendaylight.protocol.bgp.rib.spi.Peer;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.LinkstateDestination;
-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.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.LinkstateRoutes;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.LinkstateRoute;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.LinkstateRouteBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.LinkstateRouteKey;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.Attributes1;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.Attributes1Builder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.ObjectType;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.attributes.AttributeType;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.object.type.LinkCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.object.type.NodeCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.object.type.PrefixCaseBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.object.type.link._case.LinkDescriptorsBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.object.type.link._case.LocalNodeDescriptorsBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.object.type.link._case.RemoteNodeDescriptorsBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.object.type.node._case.NodeDescriptorsBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.object.type.prefix._case.AdvertisingNodeDescriptorsBuilder;
-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.c.linkstate.destination.LocalNodeDescriptors;
-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.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.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.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.prefix.attributes._case.PrefixAttributes;
-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.PathAttributes;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpReachNlri;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpUnreachNlri;
+import org.opendaylight.protocol.util.ByteArray;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.LinkstateDestination;
+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.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.path.attribute.LinkStateAttribute;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.LinkAttributesCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.LinkAttributesCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.NodeAttributesCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.NodeAttributesCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.PrefixAttributesCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.PrefixAttributesCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.link.attributes._case.LinkAttributesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.node.attributes._case.NodeAttributesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.prefix.attributes._case.PrefixAttributesBuilder;
+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.linkstate.rev150210.linkstate.routes.linkstate.routes.LinkstateRoute;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.linkstate.routes.LinkstateRouteBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.linkstate.routes.LinkstateRouteKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.linkstate.routes.linkstate.route.Attributes1;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.linkstate.routes.linkstate.route.Attributes1Builder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLinkstateCase;
+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.destination.linkstate._case.DestinationLinkstate;
+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.message.rev130919.path.attributes.AttributesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlri;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlriBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlri;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlriBuilder;
+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.WithdrawnRoutes;
+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.bgp.rib.rev130925.rib.Tables;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.route.AttributesBuilder;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.base.Preconditions;
+final class LinkstateAdjRIBsIn extends AbstractAdjRIBs<CLinkstateDestination, LinkstateRoute, LinkstateRouteKey> {
+
+    private abstract static class LinkstateRIBEntryData<A extends LinkStateAttribute> extends RIBEntryData<CLinkstateDestination, LinkstateRoute, LinkstateRouteKey> {
+        private final A lsattr;
+
+        protected LinkstateRIBEntryData(final Peer peer, final Attributes attributes, final A lsattr) {
+            super(peer, attributes);
+            this.lsattr = lsattr;
+        }
+
+        protected abstract LinkStateAttribute createAttributes(A lsattr);
+
+        @Override
+        protected final LinkstateRoute getDataObject(final CLinkstateDestination key, final LinkstateRouteKey id) {
+            final LinkstateRouteBuilder builder = new LinkstateRouteBuilder();
+
+            builder.setKey(id);
+            builder.setIdentifier(key.getIdentifier());
+            builder.setProtocolId(key.getProtocolId());
+            builder.setDistinguisher(key.getDistinguisher());
+            builder.setAttributes(new AttributesBuilder(getAttributes()).addAugmentation(Attributes1.class,
+                new Attributes1Builder().setLinkStateAttribute(Preconditions.checkNotNull(createAttributes(this.lsattr))).build()).build());
+            builder.setObjectType(Preconditions.checkNotNull(key.getObjectType()));
+
+            return builder.build();
+        }
+
+        @Override
+        protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
+            return toStringHelper.add("lsattr", this.lsattr);
+        }
+    }
+
+    private static final Logger LOG = LoggerFactory.getLogger(LinkstateAdjRIBsIn.class);
+    private final InstanceIdentifier<LinkstateRoutes> routesBasePath;
+
+    LinkstateAdjRIBsIn(final KeyedInstanceIdentifier<Tables, TablesKey> basePath) {
+        super(basePath);
+        this.routesBasePath = basePath.builder().child((Class)LinkstateRoutes.class).build();
+    }
+
+    @Override
+    @Deprecated
+    public KeyedInstanceIdentifier<LinkstateRoute, LinkstateRouteKey> identifierForKey(final InstanceIdentifier<Tables> basePath, final CLinkstateDestination key) {
+        final ByteBuf keyBuf = Unpooled.buffer();
+        LinkstateNlriParser.serializeNlri(key, keyBuf);
+        return basePath.child((Class)LinkstateRoutes.class).child(LinkstateRoute.class,
+            new LinkstateRouteKey(ByteArray.readAllBytes(keyBuf)));
+    }
+
+    @Override
+    public KeyedInstanceIdentifier<LinkstateRoute, LinkstateRouteKey> identifierForKey(final CLinkstateDestination key) {
+        final ByteBuf keyBuf = Unpooled.buffer();
+        LinkstateNlriParser.serializeNlri(key, keyBuf);
+        return this.routesBasePath.child(LinkstateRoute.class,
+            new LinkstateRouteKey(ByteArray.readAllBytes(keyBuf)));
+    }
+
+    private static LinkstateRIBEntryData<PrefixAttributesCase> createPrefixData(final Peer peer,
+        final Attributes attributes, final LinkStateAttribute lsattr) {
+        return new LinkstateRIBEntryData<PrefixAttributesCase>(peer, attributes, (PrefixAttributesCase) lsattr) {
+            @Override
+            protected LinkStateAttribute createAttributes(final PrefixAttributesCase lsattr) {
+                final PrefixAttributesCaseBuilder b = new PrefixAttributesCaseBuilder();
+                if (lsattr != null && lsattr.getPrefixAttributes() != null) {
+                    b.setPrefixAttributes(new PrefixAttributesBuilder(lsattr.getPrefixAttributes()).build());
+                }
+                return b.build();
+            }
+        };
+    }
+
+    private static LinkstateRIBEntryData<LinkAttributesCase> createLinkData(final Peer peer,
+        final Attributes attributes, final LinkStateAttribute lsattr) {
+        return new LinkstateRIBEntryData<LinkAttributesCase>(peer, attributes, (LinkAttributesCase) lsattr) {
+            @Override
+            protected LinkStateAttribute createAttributes(final LinkAttributesCase lsattr) {
+                final LinkAttributesCaseBuilder b = new LinkAttributesCaseBuilder();
+                if (lsattr != null && lsattr.getLinkAttributes() != null) {
+                    b.setLinkAttributes(new LinkAttributesBuilder(lsattr.getLinkAttributes()).build());
+                }
+                return b.build();
+            }
+        };
+    }
+
+    private static LinkstateRIBEntryData<NodeAttributesCase> createNodeData(final Peer peer,
+        final Attributes attributes, final LinkStateAttribute lsattr) {
+        return new LinkstateRIBEntryData<NodeAttributesCase>(peer, attributes, (NodeAttributesCase) lsattr) {
+            @Override
+            protected LinkStateAttribute createAttributes(final NodeAttributesCase lsattr) {
+                final NodeAttributesCaseBuilder b = new NodeAttributesCaseBuilder();
+                if (lsattr != null && lsattr.getNodeAttributes() != null) {
+                    b.setNodeAttributes(new NodeAttributesBuilder(lsattr.getNodeAttributes()).build());
+                }
+                return b.build();
+            }
+        };
+    }
+
+    @Override
+    public void addRoutes(final AdjRIBsTransaction trans, final Peer peer, final MpReachNlri nlri,
+        final Attributes attributes) {
+        LOG.debug("Passed nlri {}", nlri);
+        final LinkstateDestination keys = ((DestinationLinkstateCase) nlri.getAdvertizedRoutes().getDestinationType()).getDestinationLinkstate();
+        if (keys == null) {
+            LOG.debug("No destinations present in advertized routes");
+            return;
+        }
+        LOG.debug("Iterating over route destinations {}", keys);
+        for (final CLinkstateDestination key : keys.getCLinkstateDestination()) {
+            LOG.debug("Processing route key {}", key);
+            LinkStateAttribute lsattr = null;
+            final Attributes1 pa = attributes.getAugmentation(Attributes1.class);
+            if (pa != null) {
+                lsattr = pa.getLinkStateAttribute();
+            }
+            RIBEntryData<CLinkstateDestination, LinkstateRoute, LinkstateRouteKey> data = null;
+            final ObjectType ot = key.getObjectType();
+            if (ot instanceof LinkCase) {
+                data = createLinkData(peer, attributes, lsattr);
+            } else if (ot instanceof NodeCase) {
+                data = createNodeData(peer, attributes, lsattr);
+            } else if (ot instanceof PrefixCase) {
+                data = createPrefixData(peer, attributes, lsattr);
+            }
+            super.add(trans, peer, key, data);
+        }
+    }
+
+    @Override
+    public void removeRoutes(final AdjRIBsTransaction trans, final Peer peer, final MpUnreachNlri nlri) {
+        final DestinationLinkstate keys = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationLinkstateCase) nlri.getWithdrawnRoutes().getDestinationType()).getDestinationLinkstate();
+
+        for (final CLinkstateDestination key : keys.getCLinkstateDestination()) {
+            super.remove(trans, peer, key);
+        }
+    }
+
+    @Override
+    public void addAdvertisement(final MpReachNlriBuilder builder, final LinkstateRoute data) {
+        final CLinkstateDestinationBuilder nlri = new CLinkstateDestinationBuilder();
+        nlri.setProtocolId(data.getProtocolId());
+        nlri.setIdentifier(data.getIdentifier());
+        final Attributes a = data.getAttributes();
+        if (a != null && a.getCNextHop() != null) {
+            builder.setCNextHop(a.getCNextHop());
+        }
+        final ObjectType type = data.getObjectType();
+        nlri.setObjectType(type);
+        final AdvertizedRoutes ar = builder.getAdvertizedRoutes();
+        if (ar == null) {
+            builder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
+                new DestinationLinkstateCaseBuilder().setDestinationLinkstate(new DestinationLinkstateBuilder()
+                    .setCLinkstateDestination(Lists.newArrayList(nlri.build())).build()).build()).build());
+        } else {
+            ((DestinationLinkstateCase) ar.getDestinationType()).getDestinationLinkstate().getCLinkstateDestination().add(nlri.build());
+        }
+    }
+
+    @Override
+    public void addWithdrawal(final MpUnreachNlriBuilder builder, final CLinkstateDestination id) {
+        final WithdrawnRoutes wr = builder.getWithdrawnRoutes();
+        if (wr == null) {
+            builder.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setDestinationType(
+                new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationLinkstateCaseBuilder().setDestinationLinkstate(
+                    new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.destination.linkstate._case.DestinationLinkstateBuilder()
+                        .setCLinkstateDestination(Lists.newArrayList(id)).build()).build()).build());
+        } else {
+            ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationLinkstateCase) wr.getDestinationType())
+            .getDestinationLinkstate().getCLinkstateDestination().add(id);
+        }
+    }
+
+    @SuppressWarnings("unchecked")
+    @Override
+    public KeyedInstanceIdentifier<LinkstateRoute, LinkstateRouteKey> routeIdentifier(final InstanceIdentifier<?> id) {
+        return (KeyedInstanceIdentifier<LinkstateRoute, LinkstateRouteKey>)id.firstIdentifierOf(LinkstateRoute.class);
+    }
 
-final class LinkstateAdjRIBsIn extends AbstractAdjRIBsIn<CLinkstateDestination, LinkstateRoute> {
-       private static final Logger LOG = LoggerFactory.getLogger(LinkstateAdjRIBsIn.class);
-
-       private abstract static class LinkstateRIBEntryData<A extends LinkStateAttribute> extends
-       RIBEntryData<CLinkstateDestination, LinkstateRoute> {
-               private final A lsattr;
-
-               protected LinkstateRIBEntryData(final PathAttributes attributes, final A lsattr) {
-                       super(attributes);
-                       this.lsattr = lsattr;
-               }
-
-               protected abstract AttributeType createAttributes(A lsattr);
-
-               protected abstract ObjectType createObject(CLinkstateDestination key);
-
-               @Override
-               protected final LinkstateRoute getDataObject(final CLinkstateDestination key) {
-                       final LinkstateRouteBuilder builder = new LinkstateRouteBuilder();
-
-                       builder.setIdentifier(key.getIdentifier());
-                       builder.setProtocolId(key.getProtocolId());
-                       builder.setDistinguisher(key.getDistinguisher());
-                       builder.setAttributes(new AttributesBuilder(getPathAttributes()).addAugmentation(Attributes1.class,
-                                       new Attributes1Builder().setAttributeType(Preconditions.checkNotNull(createAttributes(this.lsattr))).build()).build());
-                       builder.setObjectType(Preconditions.checkNotNull(createObject(key)));
-
-                       return builder.build();
-               }
-       }
-
-       LinkstateAdjRIBsIn(final DataModificationTransaction trans, final RibReference rib, final Comparator<PathAttributes> comparator, final TablesKey key) {
-               super(trans, rib, comparator, key);
-       }
-
-       @Override
-       public InstanceIdentifier<?> identifierForKey(final InstanceIdentifier<Tables> basePath, final CLinkstateDestination key) {
-               return InstanceIdentifier.builder(basePath).child(LinkstateRoutes.class).child(LinkstateRoute.class,
-                               new LinkstateRouteKey(LinkstateNlriParser.serializeNlri(key))).toInstance();
-       }
-
-       @Override
-       public void addRoutes(final DataModificationTransaction trans, final Peer peer, final MpReachNlri nlri,
-                       final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributes attributes) {
-               final LinkstateDestination keys = ((DestinationLinkstateCase) nlri.getAdvertizedRoutes().getDestinationType()).getDestinationLinkstate();
-               if (keys == null) {
-                       LOG.debug("No destinations present in advertized routes");
-                       return;
-               }
-
-               LOG.debug("Iterating over route destinations {}", keys);
-
-               for (final CLinkstateDestination key : keys.getCLinkstateDestination()) {
-                       LinkStateAttribute lsattr = null;
-                       final PathAttributes1 pa = attributes.getAugmentation(PathAttributes1.class);
-                       if (pa != null) {
-                               final LinkstatePathAttribute lpa = pa.getLinkstatePathAttribute();
-                               if (lpa != null) {
-                                       lsattr = lpa.getLinkStateAttribute();
-                               }
-                       }
-
-                       RIBEntryData<CLinkstateDestination, LinkstateRoute> data = null;
-                       switch (key.getNlriType()) {
-                       case Ipv4Prefix:
-                       case Ipv6Prefix:
-                               data = new LinkstateRIBEntryData<PrefixAttributesCase>(attributes, (PrefixAttributesCase) lsattr) {
-                                       @Override
-                                       protected AttributeType createAttributes(final PrefixAttributesCase lsattr) {
-                                               final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.attributes.attribute.type.PrefixCaseBuilder b = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.attributes.attribute.type.PrefixCaseBuilder();
-
-                                               if (lsattr != null) {
-                                                       final PrefixAttributes a = lsattr.getPrefixAttributes();
-                                                       if (a != null) {
-                                                               final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.attributes.attribute.type.prefix._case.PrefixAttributesBuilder builder = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.attributes.attribute.type.prefix._case.PrefixAttributesBuilder(a);
-                                                               b.setPrefixAttributes(builder.build());
-                                                       }
-
-                                               }
-
-                                               return b.build();
-                                       }
-
-                                       @Override
-                                       protected ObjectType createObject(final CLinkstateDestination key) {
-                                               return new PrefixCaseBuilder().setAdvertisingNodeDescriptors(
-                                                               new AdvertisingNodeDescriptorsBuilder(key.getLocalNodeDescriptors()).build()).build();
-                                       }
-                               };
-                               break;
-                       case Link:
-                               data = new LinkstateRIBEntryData<LinkAttributesCase>(attributes, (LinkAttributesCase) lsattr) {
-                                       @Override
-                                       protected AttributeType createAttributes(final LinkAttributesCase lsattr) {
-                                               final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.attributes.attribute.type.LinkCaseBuilder b = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.attributes.attribute.type.LinkCaseBuilder();
-
-                                               if (lsattr != null) {
-                                                       final LinkAttributes la = lsattr.getLinkAttributes();
-                                                       if (la != null) {
-                                                               final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.attributes.attribute.type.link._case.LinkAttributesBuilder builder = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.attributes.attribute.type.link._case.LinkAttributesBuilder(la);
-                                                               b.setLinkAttributes(builder.build());
-                                                       }
-                                               }
-
-                                               return  b.build();
-                                       }
-
-                                       @Override
-                                       protected ObjectType createObject(final CLinkstateDestination key) {
-                                               final LinkCaseBuilder b = new LinkCaseBuilder();
-                                               b.setLinkDescriptors(new LinkDescriptorsBuilder(key.getLinkDescriptors()).build());
-                                               b.setLocalNodeDescriptors(new LocalNodeDescriptorsBuilder(key.getLocalNodeDescriptors()).build());
-                                               b.setRemoteNodeDescriptors(new RemoteNodeDescriptorsBuilder(key.getRemoteNodeDescriptors()).build());
-                                               return b.build();
-                                       }
-                               };
-                               break;
-                       case Node:
-                               data = new LinkstateRIBEntryData<NodeAttributesCase>(attributes, (NodeAttributesCase) lsattr) {
-                                       @Override
-                                       protected AttributeType createAttributes(final NodeAttributesCase lsattr) {
-                                               final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.attributes.attribute.type.NodeCaseBuilder b = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.attributes.attribute.type.NodeCaseBuilder();
-                                               if (lsattr != null) {
-                                                       final NodeAttributes n = lsattr.getNodeAttributes();
-                                                       if (n != null) {
-                                                               final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.attributes.attribute.type.node._case.NodeAttributesBuilder builder = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.attributes.attribute.type.node._case.NodeAttributesBuilder(n);
-                                                               b.setNodeAttributes(builder.build());
-                                                       }
-                                               }
-                                               return b.build();
-                                       }
-
-                                       @Override
-                                       protected ObjectType createObject(final CLinkstateDestination key) {
-                                               final LocalNodeDescriptors d = key.getLocalNodeDescriptors();
-                                               return new NodeCaseBuilder().setNodeDescriptors(new NodeDescriptorsBuilder(d).build()).build();
-                                       }
-                               };
-                               break;
-                       }
-                       super.add(trans, peer, key, data);
-               }
-       }
-
-       @Override
-       public void removeRoutes(final DataModificationTransaction trans, final Peer peer, final MpUnreachNlri nlri) {
-               final CLinkstateDestination key = (CLinkstateDestination) ((LinkstateDestination) nlri.getWithdrawnRoutes().getDestinationType()).getCLinkstateDestination();
-
-               super.remove(trans, peer, key);
-       }
-}
\ No newline at end of file
+    @Override
+    public CLinkstateDestination keyForIdentifier(final KeyedInstanceIdentifier<LinkstateRoute, LinkstateRouteKey> id) {
+        final LinkstateRouteKey route = id.getKey();
+        List<CLinkstateDestination> dests = null;
+        try {
+            dests = LinkstateNlriParser.parseNlri(Unpooled.wrappedBuffer(route.getRouteKey()), false);
+        } catch (final BGPParsingException e) {
+            LOG.warn("Unable to parse LinkstateRoute Key {}", route, e);
+        }
+        if (dests == null || dests.size() != 1) {
+            LOG.warn("Null or more than one LinkstateRoute Key was parsed");
+            return null;
+        }
+        return dests.get(0);
+    }
+}