BUG-1116 : application peer API 66/10066/11
authorDana Kutenicsova <dkutenic@cisco.com>
Sun, 31 Aug 2014 14:26:17 +0000 (16:26 +0200)
committerDana Kutenicsova <dkutenic@cisco.com>
Mon, 1 Sep 2014 09:43:25 +0000 (09:43 +0000)
Change-Id: I518938143019acb485ca897a8cecbc52a3d5f568
Signed-off-by: Dana Kutenicsova <dkutenic@cisco.com>
bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/LinkstateAdjRIBsIn.java
bgp/linkstate/src/main/java/org/opendaylight/protocol/bgp/linkstate/LinkstateNlriParser.java
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/Ipv4AdjRIBsIn.java
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/Ipv6AdjRIBsIn.java
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/RIBImpl.java
bgp/rib-spi/src/main/java/org/opendaylight/protocol/bgp/rib/spi/AbstractAdjRIBs.java
bgp/rib-spi/src/main/java/org/opendaylight/protocol/bgp/rib/spi/AdjRIBsIn.java
bgp/rib-spi/src/test/java/org/opendaylight/controller/config/yang/bgp/rib/spi/BestPathSelectionTest.java

index fd5dad0f9192f9e19de141d240459b664a9accdd..576d24a3760b30061c06f82e1673d3bcc4a27242 100644 (file)
@@ -12,6 +12,8 @@ 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.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;
@@ -93,7 +95,7 @@ final class LinkstateAdjRIBsIn extends AbstractAdjRIBs<CLinkstateDestination, Li
             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());
+                new Attributes1Builder().setAttributeType(Preconditions.checkNotNull(createAttributes(this.lsattr))).build()).build());
             builder.setObjectType(Preconditions.checkNotNull(createObject(key)));
 
             return builder.build();
@@ -116,12 +118,12 @@ final class LinkstateAdjRIBsIn extends AbstractAdjRIBs<CLinkstateDestination, Li
         final ByteBuf keyBuf = Unpooled.buffer();
         LinkstateNlriParser.serializeNlri(key, keyBuf);
         return basePath.child(LinkstateRoutes.class).child(LinkstateRoute.class,
-                new LinkstateRouteKey(ByteArray.readAllBytes(keyBuf)));
+            new LinkstateRouteKey(ByteArray.readAllBytes(keyBuf)));
     }
 
     @Override
     public void addRoutes(final AdjRIBsTransaction 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 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributes attributes) {
         LOG.debug("Passed nlri {}", nlri);
         final LinkstateDestination keys = ((DestinationLinkstateCase) nlri.getAdvertizedRoutes().getDestinationType()).getDestinationLinkstate();
         if (keys == null) {
@@ -218,9 +220,10 @@ final class LinkstateAdjRIBsIn extends AbstractAdjRIBs<CLinkstateDestination, Li
     }
 
     @Override
-    protected void addAdvertisement(final MpReachNlriBuilder builder, final LinkstateRoute data) {
+    public void addAdvertisement(final MpReachNlriBuilder builder, final LinkstateRoute data) {
         final CLinkstateDestinationBuilder nlri = new CLinkstateDestinationBuilder();
-
+        nlri.setProtocolId(data.getProtocolId());
+        nlri.setIdentifier(data.getIdentifier());
         final ObjectType type = data.getObjectType();
         if (type instanceof PrefixCase) {
             final PrefixCase prefix = (PrefixCase) type;
@@ -262,16 +265,37 @@ final class LinkstateAdjRIBsIn extends AbstractAdjRIBs<CLinkstateDestination, Li
     }
 
     @Override
-    protected void addWithdrawal(final MpUnreachNlriBuilder builder, final CLinkstateDestination id) {
+    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.rev131125.update.path.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationLinkstateCaseBuilder().setDestinationLinkstate(
-                            new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.destination.linkstate._case.DestinationLinkstateBuilder()
-                            .setCLinkstateDestination(Lists.newArrayList(id)).build()).build()).build());
+                new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationLinkstateCaseBuilder().setDestinationLinkstate(
+                    new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.destination.linkstate._case.DestinationLinkstateBuilder()
+                        .setCLinkstateDestination(Lists.newArrayList(id)).build()).build()).build());
         } else {
             ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationLinkstateCase) 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);
+    }
+
+    @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 dests.get(0);
+    }
 }
index eb538e677d6ba459cfd4dc50e5c2824022206ac3..723d52c479c24335486546c1b94d3d9e5693644a 100644 (file)
@@ -278,7 +278,7 @@ public final class LinkstateNlriParser implements NlriParser, NlriSerializer {
      * @return {@link CLinkstateDestination}
      * @throws BGPParsingException if parsing was unsuccessful
      */
-    private List<CLinkstateDestination> parseNlri(final ByteBuf nlri) throws BGPParsingException {
+    public static List<CLinkstateDestination> parseNlri(final ByteBuf nlri, final boolean isVpn) throws BGPParsingException {
         if (!nlri.isReadable()) {
             return null;
         }
@@ -294,7 +294,7 @@ public final class LinkstateNlriParser implements NlriParser, NlriSerializer {
             // length means total length of the tlvs including route distinguisher not including the type field
             final int length = nlri.readUnsignedShort();
             RouteDistinguisher distinguisher = null;
-            if (this.isVpn) {
+            if (isVpn) {
                 // this parses route distinguisher
                 distinguisher = new RouteDistinguisher(BigInteger.valueOf(nlri.readLong()));
                 builder.setDistinguisher(distinguisher);
@@ -317,7 +317,7 @@ public final class LinkstateNlriParser implements NlriParser, NlriSerializer {
             }
             nlri.skipBytes(locallength);
             builder.setLocalNodeDescriptors((LocalNodeDescriptors) localDescriptor);
-            final int restLength = length - (this.isVpn ? ROUTE_DISTINGUISHER_LENGTH : 0) - PROTOCOL_ID_LENGTH - IDENTIFIER_LENGTH
+            final int restLength = length - (isVpn ? ROUTE_DISTINGUISHER_LENGTH : 0) - PROTOCOL_ID_LENGTH - IDENTIFIER_LENGTH
                 - TYPE_LENGTH - LENGTH_SIZE - locallength;
             LOG.trace("Restlength {}", restLength);
             final ByteBuf rest = nlri.slice(nlri.readerIndex(), restLength);
@@ -346,7 +346,7 @@ public final class LinkstateNlriParser implements NlriParser, NlriSerializer {
         if (!nlri.isReadable()) {
             return;
         }
-        final List<CLinkstateDestination> dst = parseNlri(nlri);
+        final List<CLinkstateDestination> dst = parseNlri(nlri, this.isVpn);
 
         builder.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setDestinationType(
             new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.update.path.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationLinkstateCaseBuilder().setDestinationLinkstate(
@@ -359,7 +359,7 @@ public final class LinkstateNlriParser implements NlriParser, NlriSerializer {
         if (!nlri.isReadable()) {
             return;
         }
-        final List<CLinkstateDestination> dst = parseNlri(nlri);
+        final List<CLinkstateDestination> dst = parseNlri(nlri, this.isVpn);
 
         builder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
             new DestinationLinkstateCaseBuilder().setDestinationLinkstate(
index 00d2472ef87ce6d372eb4ea2dad877c40fabec31..7ce722b2445d89a2a52ff2b1af7b4522e270a27b 100644 (file)
@@ -8,7 +8,6 @@
 package org.opendaylight.protocol.bgp.rib.impl;
 
 import com.google.common.collect.Lists;
-
 import org.opendaylight.protocol.bgp.rib.spi.AbstractAdjRIBs;
 import org.opendaylight.protocol.bgp.rib.spi.AdjRIBsTransaction;
 import org.opendaylight.protocol.bgp.rib.spi.Peer;
@@ -47,7 +46,7 @@ final class Ipv4AdjRIBsIn extends AbstractAdjRIBs<Ipv4Prefix, Ipv4Route, Ipv4Rou
 
     @Override
     public void addRoutes(final AdjRIBsTransaction 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 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributes attributes) {
         final RIBEntryData<Ipv4Prefix, Ipv4Route, Ipv4RouteKey> data = new RIBEntryData<Ipv4Prefix, Ipv4Route, Ipv4RouteKey>(peer, attributes) {
             @Override
             protected Ipv4Route getDataObject(final Ipv4Prefix key, final Ipv4RouteKey id) {
@@ -68,26 +67,37 @@ final class Ipv4AdjRIBsIn extends AbstractAdjRIBs<Ipv4Prefix, Ipv4Route, Ipv4Rou
     }
 
     @Override
-    protected void addAdvertisement(final MpReachNlriBuilder builder, final Ipv4Route data) {
+    public void addAdvertisement(final MpReachNlriBuilder builder, final Ipv4Route data) {
         final AdvertizedRoutes ar = builder.getAdvertizedRoutes();
         if (ar == null) {
             builder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
-                    new DestinationIpv4CaseBuilder().setDestinationIpv4(new DestinationIpv4Builder().setIpv4Prefixes(
-                            Lists.newArrayList(data.getPrefix())).build()).build()).build());
+                new DestinationIpv4CaseBuilder().setDestinationIpv4(new DestinationIpv4Builder().setIpv4Prefixes(
+                    Lists.newArrayList(data.getPrefix())).build()).build()).build());
         } else {
             ((DestinationIpv4) ar.getDestinationType()).getIpv4Prefixes().add(data.getPrefix());
         }
     }
 
     @Override
-    protected void addWithdrawal(final MpUnreachNlriBuilder builder, final Ipv4Prefix id) {
+    public void addWithdrawal(final MpUnreachNlriBuilder builder, final Ipv4Prefix id) {
         final WithdrawnRoutes wr = builder.getWithdrawnRoutes();
         if (wr == null) {
             builder.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setDestinationType(
-                    new DestinationIpv4CaseBuilder().setDestinationIpv4(new DestinationIpv4Builder().setIpv4Prefixes(
-                            Lists.newArrayList(id)).build()).build()).build());
+                new DestinationIpv4CaseBuilder().setDestinationIpv4(new DestinationIpv4Builder().setIpv4Prefixes(
+                    Lists.newArrayList(id)).build()).build()).build());
         } else {
             ((DestinationIpv4Case) wr.getDestinationType()).getDestinationIpv4().getIpv4Prefixes().add(id);
         }
     }
+
+    @SuppressWarnings("unchecked")
+    @Override
+    public KeyedInstanceIdentifier<Ipv4Route, Ipv4RouteKey> routeIdentifier(final InstanceIdentifier<?> id) {
+        return (KeyedInstanceIdentifier<Ipv4Route, Ipv4RouteKey>)id.firstIdentifierOf(Ipv4Route.class);
+    }
+
+    @Override
+    public Ipv4Prefix keyForIdentifier(final KeyedInstanceIdentifier<Ipv4Route, Ipv4RouteKey> id) {
+        return id.getKey().getPrefix();
+    }
 }
index 3f1ebc61c162abfdc701287dba5e8c96b294c87c..3a5ff4d78ba36cbef8d63353bacf032e581639b6 100644 (file)
@@ -9,7 +9,6 @@ package org.opendaylight.protocol.bgp.rib.impl;
 
 import com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
-
 import org.opendaylight.protocol.bgp.rib.spi.AbstractAdjRIBs;
 import org.opendaylight.protocol.bgp.rib.spi.AdjRIBsTransaction;
 import org.opendaylight.protocol.bgp.rib.spi.Peer;
@@ -48,7 +47,7 @@ final class Ipv6AdjRIBsIn extends AbstractAdjRIBs<Ipv6Prefix, Ipv6Route, Ipv6Rou
 
     @Override
     public void addRoutes(final AdjRIBsTransaction 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 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributes attributes) {
         final RIBEntryData<Ipv6Prefix, Ipv6Route, Ipv6RouteKey> data = new RIBEntryData<Ipv6Prefix, Ipv6Route, Ipv6RouteKey>(peer, attributes) {
             @Override
             protected Ipv6Route getDataObject(final Ipv6Prefix key, final Ipv6RouteKey id) {
@@ -69,26 +68,37 @@ final class Ipv6AdjRIBsIn extends AbstractAdjRIBs<Ipv6Prefix, Ipv6Route, Ipv6Rou
     }
 
     @Override
-    protected void addAdvertisement(final MpReachNlriBuilder builder, final Ipv6Route data) {
+    public void addAdvertisement(final MpReachNlriBuilder builder, final Ipv6Route data) {
         final AdvertizedRoutes ar = builder.getAdvertizedRoutes();
         if (ar == null) {
             builder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
-                    new DestinationIpv6CaseBuilder().setDestinationIpv6(new DestinationIpv6Builder().setIpv6Prefixes(
-                            Lists.newArrayList(data.getPrefix())).build()).build()).build());
+                new DestinationIpv6CaseBuilder().setDestinationIpv6(new DestinationIpv6Builder().setIpv6Prefixes(
+                    Lists.newArrayList(data.getPrefix())).build()).build()).build());
         } else {
             ((DestinationIpv6) ar.getDestinationType()).getIpv6Prefixes().add(data.getPrefix());
         }
     }
 
     @Override
-    protected void addWithdrawal(final MpUnreachNlriBuilder builder, final Ipv6Prefix id) {
+    public void addWithdrawal(final MpUnreachNlriBuilder builder, final Ipv6Prefix id) {
         final WithdrawnRoutes wr = builder.getWithdrawnRoutes();
         if (wr == null) {
             builder.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setDestinationType(
-                    new DestinationIpv6CaseBuilder().setDestinationIpv6(new DestinationIpv6Builder().setIpv6Prefixes(
-                            Lists.newArrayList(id)).build()).build()).build());
+                new DestinationIpv6CaseBuilder().setDestinationIpv6(new DestinationIpv6Builder().setIpv6Prefixes(
+                    Lists.newArrayList(id)).build()).build()).build());
         } else {
             ((DestinationIpv6Case) wr.getDestinationType()).getDestinationIpv6().getIpv6Prefixes().add(id);
         }
     }
+
+    @SuppressWarnings("unchecked")
+    @Override
+    public KeyedInstanceIdentifier<Ipv6Route, Ipv6RouteKey> routeIdentifier(final InstanceIdentifier<?> id) {
+        return (KeyedInstanceIdentifier<Ipv6Route, Ipv6RouteKey>)id.firstIdentifierOf(Ipv6Route.class);
+    }
+
+    @Override
+    public Ipv6Prefix keyForIdentifier(final KeyedInstanceIdentifier<Ipv6Route, Ipv6RouteKey> id) {
+        return id.getKey().getPrefix();
+    }
 }
index 39f81cd7d8a3d7854425ad524a7ceba8584acaca..c3786f4bd5dddc00d2b592974c25e886dcd7ed18 100644 (file)
@@ -14,7 +14,6 @@ import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableList;
 import com.google.common.util.concurrent.FutureCallback;
 import com.google.common.util.concurrent.Futures;
-
 import java.util.Collections;
 import java.util.List;
 import java.util.concurrent.BlockingQueue;
@@ -23,7 +22,6 @@ import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.LinkedBlockingQueue;
 import javax.annotation.concurrent.ThreadSafe;
-
 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
@@ -64,6 +62,7 @@ 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.rib.rev130925.BgpRib;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.BgpRibBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.RibId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.Route;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.Rib;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.RibBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.RibKey;
@@ -122,8 +121,8 @@ public final class RIBImpl extends DefaultRibReference implements AutoCloseable,
     });
 
     public RIBImpl(final RibId ribId, final AsNumber localAs, final Ipv4Address localBgpId, final RIBExtensionConsumerContext extensions,
-            final BGPDispatcher dispatcher, final ReconnectStrategyFactory tcpStrategyFactory,
-            final ReconnectStrategyFactory sessionStrategyFactory, final DataBroker dps, final List<BgpTableType> localTables) {
+        final BGPDispatcher dispatcher, final ReconnectStrategyFactory tcpStrategyFactory,
+        final ReconnectStrategyFactory sessionStrategyFactory, final DataBroker dps, final List<BgpTableType> localTables) {
         super(InstanceIdentifier.builder(BgpRib.class).child(Rib.class, new RibKey(Preconditions.checkNotNull(ribId))).toInstance());
         this.chain = dps.createTransactionChain(this);
         this.localAs = Preconditions.checkNotNull(localAs);
@@ -138,7 +137,7 @@ public final class RIBImpl extends DefaultRibReference implements AutoCloseable,
 
         LOG.debug("Instantiating RIB table {} at {}", ribId, getInstanceIdentifier());
 
-        final ReadWriteTransaction trans = chain.newReadWriteTransaction();
+        final ReadWriteTransaction trans = this.chain.newReadWriteTransaction();
         Optional<Rib> o;
         try {
             o = trans.read(LogicalDatastoreType.OPERATIONAL, getInstanceIdentifier()).get();
@@ -150,9 +149,9 @@ public final class RIBImpl extends DefaultRibReference implements AutoCloseable,
         // put empty BgpRib if not exists
         trans.merge(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.builder(BgpRib.class).build(), new BgpRibBuilder().build());
         trans.put(LogicalDatastoreType.OPERATIONAL, getInstanceIdentifier(), new RibBuilder().setKey(new RibKey(ribId)).setId(ribId).setLocRib(
-                new LocRibBuilder().setTables(Collections.<Tables> emptyList()).build()).build());
+            new LocRibBuilder().setTables(Collections.<Tables> emptyList()).build()).build());
 
-        for (BgpTableType t : localTables) {
+        for (final BgpTableType t : localTables) {
             final TablesKey key = new TablesKey(t.getAfi(), t.getSafi());
             if (this.tables.create(trans, this, key) == null) {
                 LOG.debug("Did not create local table for unhandled table type {}", t);
@@ -177,20 +176,23 @@ public final class RIBImpl extends DefaultRibReference implements AutoCloseable,
 
     @Override
     public synchronized void updateTables(final Peer peer, final Update message) {
-        final AdjRIBsTransactionImpl trans = new AdjRIBsTransactionImpl(ribOuts, this.comparator, this.chain.newWriteOnlyTransaction());
+        final AdjRIBsTransactionImpl trans = new AdjRIBsTransactionImpl(this.ribOuts, this.comparator, this.chain.newWriteOnlyTransaction());
 
         if (!EOR.equals(message)) {
             final WithdrawnRoutes wr = message.getWithdrawnRoutes();
             if (wr != null) {
                 final AdjRIBsIn<?, ?> ari = this.tables.get(IPV4_UNICAST_TABLE);
                 if (ari != null) {
+                    /*
+                     * create MPUnreach for the routes to be handled in the same way as linkstate routes
+                     */
                     ari.removeRoutes(
-                            trans,
-                            peer,
-                            new MpUnreachNlriBuilder().setAfi(Ipv4AddressFamily.class).setSafi(UnicastSubsequentAddressFamily.class).setWithdrawnRoutes(
-                                    new WithdrawnRoutesBuilder().setDestinationType(
-                                            new DestinationIpv4CaseBuilder().setDestinationIpv4(
-                                                    new DestinationIpv4Builder().setIpv4Prefixes(wr.getWithdrawnRoutes()).build()).build()).build()).build());
+                        trans,
+                        peer,
+                        new MpUnreachNlriBuilder().setAfi(Ipv4AddressFamily.class).setSafi(UnicastSubsequentAddressFamily.class).setWithdrawnRoutes(
+                            new WithdrawnRoutesBuilder().setDestinationType(
+                                new DestinationIpv4CaseBuilder().setDestinationIpv4(
+                                    new DestinationIpv4Builder().setIpv4Prefixes(wr.getWithdrawnRoutes()).build()).build()).build()).build());
                 } else {
                     LOG.debug("Not removing objects from unhandled IPv4 Unicast");
                 }
@@ -215,11 +217,14 @@ public final class RIBImpl extends DefaultRibReference implements AutoCloseable,
             if (ar != null) {
                 final AdjRIBsIn<?, ?> ari = this.tables.get(IPV4_UNICAST_TABLE);
                 if (ari != null) {
+                    /*
+                     * create MPReach for the routes to be handled in the same way as linkstate routes
+                     */
                     final MpReachNlriBuilder b = new MpReachNlriBuilder().setAfi(Ipv4AddressFamily.class).setSafi(
-                            UnicastSubsequentAddressFamily.class).setAdvertizedRoutes(
-                                    new AdvertizedRoutesBuilder().setDestinationType(
-                                            new DestinationIpv4CaseBuilder().setDestinationIpv4(
-                                                    new DestinationIpv4Builder().setIpv4Prefixes(ar.getNlri()).build()).build()).build());
+                        UnicastSubsequentAddressFamily.class).setAdvertizedRoutes(
+                            new AdvertizedRoutesBuilder().setDestinationType(
+                                new DestinationIpv4CaseBuilder().setDestinationIpv4(
+                                    new DestinationIpv4Builder().setIpv4Prefixes(ar.getNlri()).build()).build()).build());
                     if (attrs != null) {
                         b.setCNextHop(attrs.getCNextHop());
                     }
@@ -273,7 +278,7 @@ public final class RIBImpl extends DefaultRibReference implements AutoCloseable,
     public synchronized void clearTable(final Peer peer, final TablesKey key) {
         final AdjRIBsIn<?, ?> ari = this.tables.get(key);
         if (ari != null) {
-            final AdjRIBsTransactionImpl trans = new AdjRIBsTransactionImpl(ribOuts, comparator, this.chain.newWriteOnlyTransaction());
+            final AdjRIBsTransactionImpl trans = new AdjRIBsTransactionImpl(this.ribOuts, this.comparator, this.chain.newWriteOnlyTransaction());
             ari.clear(trans, peer);
 
             Futures.addCallback(trans.commit(), new FutureCallback<Void>() {
@@ -299,12 +304,17 @@ public final class RIBImpl extends DefaultRibReference implements AutoCloseable,
         return toStringHelper;
     }
 
+    @SuppressWarnings("unchecked")
+    protected <K, V extends Route> AdjRIBsIn<K, V> getTable(final TablesKey key) {
+        return (AdjRIBsIn<K, V>) this.tables.get(key);
+    }
+
     @Override
     public void close() throws InterruptedException, ExecutionException {
         final WriteTransaction t = this.chain.newWriteOnlyTransaction();
         t.delete(LogicalDatastoreType.OPERATIONAL, getInstanceIdentifier());
         t.submit().get();
-        chain.close();
+        this.chain.close();
     }
 
     @Override
@@ -347,7 +357,7 @@ public final class RIBImpl extends DefaultRibReference implements AutoCloseable,
         final AdjRIBsOutRegistration reg = new AdjRIBsOutRegistration(aro) {
             @Override
             protected void removeRegistration() {
-                ribOuts.remove(peer, aro);
+                RIBImpl.this.ribOuts.remove(peer, aro);
             }
         };
 
index f9ce6f1373fd28c62033b590b39f23d7f6f7a6f2..d242103e89b04b2a24c08ad199210a7f8eb1542e 100644 (file)
@@ -10,14 +10,13 @@ package org.opendaylight.protocol.bgp.rib.spi;
 import com.google.common.base.Objects;
 import com.google.common.base.Objects.ToStringHelper;
 import com.google.common.base.Preconditions;
-
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.Map.Entry;
+import javax.annotation.Nullable;
 import javax.annotation.concurrent.GuardedBy;
 import javax.annotation.concurrent.ThreadSafe;
-
 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
 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.message.rev130919.Update;
@@ -109,6 +108,13 @@ public abstract class AbstractAdjRIBs<I, D extends Identifiable<K> & Route, K ex
             return this.name;
         }
 
+        /**
+         * Based on given comparator, finds a new best candidate for initial route.
+         *
+         * @param comparator
+         * @param initial
+         * @return
+         */
         private RIBEntryData<I, D, K> findCandidate(final BGPObjectComparator comparator, final RIBEntryData<I, D, K> initial) {
             RIBEntryData<I, D, K> newState = initial;
             for (final RIBEntryData<I, D, K> s : this.candidates.values()) {
@@ -120,6 +126,12 @@ public abstract class AbstractAdjRIBs<I, D extends Identifiable<K> & Route, K ex
             return newState;
         }
 
+        /**
+         * Advertize newly elected best candidate to datastore.
+         *
+         * @param transaction
+         * @param candidate
+         */
         private void electCandidate(final AdjRIBsTransaction transaction, final RIBEntryData<I, D, K> candidate) {
             LOG.trace("Electing state {} to supersede {}", candidate, this.currentState);
 
@@ -130,6 +142,13 @@ public abstract class AbstractAdjRIBs<I, D extends Identifiable<K> & Route, K ex
             }
         }
 
+        /**
+         * Removes RIBEntry from database. If we are removing best path, elect another candidate (using BPS).
+         * If there are no other candidates, remove the path completely.
+         * @param transaction
+         * @param peer
+         * @return true if the list of the candidates for this path is empty
+         */
         synchronized boolean removeState(final AdjRIBsTransaction transaction, final Peer peer) {
             final RIBEntryData<I, D, K> data = this.candidates.remove(peer);
             LOG.trace("Removed data {}", data);
@@ -202,13 +221,9 @@ public abstract class AbstractAdjRIBs<I, D extends Identifiable<K> & Route, K ex
      */
     protected abstract KeyedInstanceIdentifier<D, K> identifierForKey(InstanceIdentifier<Tables> basePath, I id);
 
-    /**
-     * Transform an advertised data object into the corresponding NLRI in MP_REACH attribute.
-     *
-     * @param data Data object
-     * @param builder MP_REACH attribute builder
-     */
-    protected abstract void addAdvertisement(MpReachNlriBuilder builder, D data);
+    public void addWith(final MpUnreachNlriBuilder builder, final InstanceIdentifier<?> key) {
+        this.addWithdrawal(builder, keyForIdentifier(this.routeIdentifier(key)));
+    }
 
     /**
      * Transform a withdrawn identifier into a the corresponding NLRI in MP_UNREACH attribute.
@@ -217,8 +232,25 @@ public abstract class AbstractAdjRIBs<I, D extends Identifiable<K> & Route, K ex
      */
     protected abstract void addWithdrawal(MpUnreachNlriBuilder builder, I id);
 
+    /**
+     * Creates router identifier out of instance identifier
+     * @param id instance identifier
+     * @return router identifier
+     */
+    public abstract @Nullable KeyedInstanceIdentifier<D, K> routeIdentifier(InstanceIdentifier<?> id);
+
+    /**
+     * Craates route key out of instance identifier
+     * @param id instance identifier
+     * @return route key
+     */
+    public abstract I keyForIdentifier(KeyedInstanceIdentifier<D, K> id);
+
     /**
      * Common backend for {@link AdjRIBsIn#addRoutes()} implementations.
+     * If a new route is added, check first for its existence in Map of entries.
+     * If the route is already there, change it's state. Then check for peer in
+     * Map of peers, if it's not there, add it.
      *
      * @param trans Transaction context
      * @param peer Originating peer
index 0c48e496b869a63d2f1bc54f0c1ee45cb6315af0..72f1e03edccde7217a1f89490121683ef8ff6239 100644 (file)
@@ -10,17 +10,53 @@ package org.opendaylight.protocol.bgp.rib.spi;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpReachNlri;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpReachNlriBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpUnreachNlri;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.Route;
 
 public interface AdjRIBsIn<K, V extends Route> {
-    void addRoutes(AdjRIBsTransaction trans, Peer peer, MpReachNlri nlri, PathAttributes attributes);
 
-    void removeRoutes(AdjRIBsTransaction trans, Peer peer, MpUnreachNlri nlri);
+    /**
+     * Adds routes to this adjacency rib.
+     * @param trans data-store transaction
+     * @param peer advertising peer
+     * @param nlri routes
+     * @param attributes route attributes
+     */
+    void addRoutes(final AdjRIBsTransaction trans, final Peer peer, final MpReachNlri nlri, final PathAttributes attributes);
 
-    void clear(AdjRIBsTransaction trans, Peer peer);
+    /**
+     * Removes routes from this adjacency rib.
+     * @param trans data-store transaction
+     * @param peer advertising peer
+     * @param nlri routes
+     */
+    void removeRoutes(final AdjRIBsTransaction trans, final Peer peer, final MpUnreachNlri nlri);
 
-    void markUptodate(AdjRIBsTransaction trans, Peer peer);
+    /**
+     * Clears adjacency rib tables.
+     * @param trans data-store transaction
+     * @param peer advertising peer
+     */
+    void clear(final AdjRIBsTransaction trans, final Peer peer);
 
+    /**
+     * Marks true or false the state of this adjacency rib.
+     * @param trans data-store transaction
+     * @param peer advertising peer
+     */
+    void markUptodate(final AdjRIBsTransaction trans, final Peer peer);
+
+    /**
+     * Transform an advertised data object into the corresponding NLRI in MP_REACH attribute.
+     * @param builder MP_REACH attribute builder
+     * @param data Data object
+     */
+    void addAdvertisement(final MpReachNlriBuilder builder, final V data);
+
+    /**
+     * Creates end-of-rib message for this adjacency rib.
+     * @return BGP Update message
+     */
     Update endOfRib();
 }
index cc6abc41e7b4a21b2f373e04aa4ac1d44179695d..b615ace12e9da3b7024fd3fcca32e4b1738c7ebf 100644 (file)
@@ -120,34 +120,34 @@ public class BestPathSelectionTest {
     @Test
     public void testCompare() {
         assertTrue(this.comparator.compare(new TestIpv4AdjRIBsIn.TestIpv4RIBEntryData(this.peer, this.attr1),
-                new TestIpv4AdjRIBsIn.TestIpv4RIBEntryData(this.peer, this.attr2)) < 0);
+            new TestIpv4AdjRIBsIn.TestIpv4RIBEntryData(this.peer, this.attr2)) < 0);
         assertTrue(this.comparator.compare(new TestIpv4AdjRIBsIn.TestIpv4RIBEntryData(this.peer, this.attr2),
-                new TestIpv4AdjRIBsIn.TestIpv4RIBEntryData(this.peer, this.attr1)) > 0);
+            new TestIpv4AdjRIBsIn.TestIpv4RIBEntryData(this.peer, this.attr1)) > 0);
 
         assertTrue(this.comparator.compare(new TestIpv4AdjRIBsIn.TestIpv4RIBEntryData(this.peer, this.attr2),
-                new TestIpv4AdjRIBsIn.TestIpv4RIBEntryData(this.peer, this.attr3)) < 0);
+            new TestIpv4AdjRIBsIn.TestIpv4RIBEntryData(this.peer, this.attr3)) < 0);
         assertTrue(this.comparator.compare(new TestIpv4AdjRIBsIn.TestIpv4RIBEntryData(this.peer, this.attr3),
-                new TestIpv4AdjRIBsIn.TestIpv4RIBEntryData(this.peer, this.attr2)) > 0);
+            new TestIpv4AdjRIBsIn.TestIpv4RIBEntryData(this.peer, this.attr2)) > 0);
 
         assertTrue(this.comparator.compare(new TestIpv4AdjRIBsIn.TestIpv4RIBEntryData(this.peer, this.attr3),
-                new TestIpv4AdjRIBsIn.TestIpv4RIBEntryData(this.peer, this.attr4)) < 0);
+            new TestIpv4AdjRIBsIn.TestIpv4RIBEntryData(this.peer, this.attr4)) < 0);
         assertTrue(this.comparator.compare(new TestIpv4AdjRIBsIn.TestIpv4RIBEntryData(this.peer, this.attr4),
-                new TestIpv4AdjRIBsIn.TestIpv4RIBEntryData(this.peer, this.attr3)) > 0);
+            new TestIpv4AdjRIBsIn.TestIpv4RIBEntryData(this.peer, this.attr3)) > 0);
 
         assertTrue(this.comparator.compare(new TestIpv4AdjRIBsIn.TestIpv4RIBEntryData(this.peer, this.attr4),
-                new TestIpv4AdjRIBsIn.TestIpv4RIBEntryData(this.peer, this.attr5)) < 0);
+            new TestIpv4AdjRIBsIn.TestIpv4RIBEntryData(this.peer, this.attr5)) < 0);
         assertTrue(this.comparator.compare(new TestIpv4AdjRIBsIn.TestIpv4RIBEntryData(this.peer, this.attr5),
-                new TestIpv4AdjRIBsIn.TestIpv4RIBEntryData(this.peer, this.attr4)) > 0);
+            new TestIpv4AdjRIBsIn.TestIpv4RIBEntryData(this.peer, this.attr4)) > 0);
 
         assertTrue(this.comparator.compare(new TestIpv4AdjRIBsIn.TestIpv4RIBEntryData(this.peer, this.attr5),
-                new TestIpv4AdjRIBsIn.TestIpv4RIBEntryData(this.peer, this.attr6)) < 0);
+            new TestIpv4AdjRIBsIn.TestIpv4RIBEntryData(this.peer, this.attr6)) < 0);
         assertTrue(this.comparator.compare(new TestIpv4AdjRIBsIn.TestIpv4RIBEntryData(this.peer, this.attr6),
-                new TestIpv4AdjRIBsIn.TestIpv4RIBEntryData(this.peer, this.attr5)) > 0);
+            new TestIpv4AdjRIBsIn.TestIpv4RIBEntryData(this.peer, this.attr5)) > 0);
 
         assertTrue(this.comparator.compare(new TestIpv4AdjRIBsIn.TestIpv4RIBEntryData(this.peer, this.attr6),
-                new TestIpv4AdjRIBsIn.TestIpv4RIBEntryData(this.peer, this.attr7)) < 0);
+            new TestIpv4AdjRIBsIn.TestIpv4RIBEntryData(this.peer, this.attr7)) < 0);
         assertTrue(this.comparator.compare(new TestIpv4AdjRIBsIn.TestIpv4RIBEntryData(this.peer, this.attr7),
-                new TestIpv4AdjRIBsIn.TestIpv4RIBEntryData(this.peer, this.attr6)) > 0);
+            new TestIpv4AdjRIBsIn.TestIpv4RIBEntryData(this.peer, this.attr6)) > 0);
     }
 
     @Test
@@ -174,7 +174,7 @@ public class BestPathSelectionTest {
 
             @Override
             protected Ipv4Route getDataObject(final Ipv4Prefix key, final Ipv4RouteKey id) {
-                return new Ipv4RouteBuilder().setKey(id).setAttributes(new AttributesBuilder(attributes).build()).build();
+                return new Ipv4RouteBuilder().setKey(id).setAttributes(new AttributesBuilder(this.attributes).build()).build();
             }
 
         }
@@ -186,7 +186,7 @@ public class BestPathSelectionTest {
 
         @Override
         public void addRoutes(final AdjRIBsTransaction 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 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributes attributes) {
             return;
         }
 
@@ -196,13 +196,23 @@ public class BestPathSelectionTest {
         }
 
         @Override
-        protected void addAdvertisement(final MpReachNlriBuilder builder, final Ipv4Route data) {
-            // TODO Auto-generated method stub
+        public void addAdvertisement(final MpReachNlriBuilder builder, final Ipv4Route data) {
+            // no-op
         }
 
         @Override
-        protected void addWithdrawal(final MpUnreachNlriBuilder builder, final Ipv4Prefix id) {
-            // TODO Auto-generated method stub
+        public void addWithdrawal(final MpUnreachNlriBuilder builder, final Ipv4Prefix id) {
+            // no-op
+        }
+
+        @Override
+        public KeyedInstanceIdentifier<Ipv4Route, Ipv4RouteKey> routeIdentifier(final InstanceIdentifier<?> id) {
+            return null;
+        }
+
+        @Override
+        public Ipv4Prefix keyForIdentifier(final KeyedInstanceIdentifier<Ipv4Route, Ipv4RouteKey> id) {
+            return null;
         }
     }
 }