Replace Preconditions.CheckNotNull per RequireNonNull
[bgpcep.git] / bgp / topology-provider / src / main / java / org / opendaylight / bgpcep / bgp / topology / provider / LinkstateTopologyBuilder.java
old mode 100755 (executable)
new mode 100644 (file)
index edeeed2..ba9b989
@@ -7,7 +7,8 @@
  */
 package org.opendaylight.bgpcep.bgp.topology.provider;
 
-import com.google.common.base.Function;
+import static java.util.Objects.requireNonNull;
+
 import com.google.common.base.Preconditions;
 import com.google.common.collect.Collections2;
 import com.google.common.collect.Lists;
@@ -25,12 +26,14 @@ import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.protocol.bgp.rib.RibReference;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.DomainName;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
-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.rev130715.DomainName;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
 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.IsisAreaIdentifier;
+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.NodeFlagBits;
 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.TopologyIdentifier;
@@ -100,6 +103,7 @@ import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TerminationPoint1;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TerminationPoint1Builder;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.link.attributes.IgpLinkAttributesBuilder;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.IgpNodeAttributes;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.IgpNodeAttributesBuilder;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.igp.node.attributes.Prefix;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.igp.node.attributes.PrefixBuilder;
@@ -120,12 +124,12 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public final class LinkstateTopologyBuilder extends AbstractTopologyBuilder<LinkstateRoute> {
-    private static final TopologyTypes LINKSTATE_TOPOLOGY_TYPE = new TopologyTypesBuilder()
-            .addAugmentation(TopologyTypes1.class,
-                    new TopologyTypes1Builder()
-                            .setBgpLinkstateTopology(new BgpLinkstateTopologyBuilder().build()).build())
-            .build();
+public class LinkstateTopologyBuilder extends AbstractTopologyBuilder<LinkstateRoute> {
+    public static final TopologyTypes LINKSTATE_TOPOLOGY_TYPE = new TopologyTypesBuilder()
+    .addAugmentation(TopologyTypes1.class,
+            new TopologyTypes1Builder()
+    .setBgpLinkstateTopology(new BgpLinkstateTopologyBuilder().build()).build())
+    .build();
 
     private static final String UNHANDLED_OBJECT_CLASS = "Unhandled object class {}";
 
@@ -136,7 +140,7 @@ public final class LinkstateTopologyBuilder extends AbstractTopologyBuilder<Link
         private final TerminationPoint tp;
 
         private TpHolder(final TerminationPoint tp) {
-            this.tp = Preconditions.checkNotNull(tp);
+            this.tp = requireNonNull(tp);
         }
 
         private synchronized void addLink(final LinkId id, final boolean isRemote) {
@@ -185,9 +189,7 @@ public final class LinkstateTopologyBuilder extends AbstractTopologyBuilder<Link
          * @return True if the node has been purged, false otherwise.
          */
         private boolean syncState(final WriteTransaction trans) {
-            final InstanceIdentifier<Node> nid = getInstanceIdentifier().child(
-                org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node.class,
-                this.nb.getKey());
+            final InstanceIdentifier<Node> nid = getNodeInstanceIdentifier(this.nb.getKey());
 
             /*
              * Transaction's putOperationalData() does a merge. Force it onto a replace
@@ -205,12 +207,7 @@ public final class LinkstateTopologyBuilder extends AbstractTopologyBuilder<Link
             }
 
             // Re-generate termination points
-            this.nb.setTerminationPoint(Lists.newArrayList(Collections2.transform(this.tps.values(), new Function<TpHolder, TerminationPoint>() {
-                @Override
-                public TerminationPoint apply(final TpHolder input) {
-                    return input.getTp();
-                }
-            })));
+            this.nb.setTerminationPoint(Lists.newArrayList(Collections2.transform(this.tps.values(), TpHolder::getTp)));
 
             // Re-generate prefixes
             this.inab.setPrefix(Lists.newArrayList(this.prefixes.values()));
@@ -222,6 +219,21 @@ public final class LinkstateTopologyBuilder extends AbstractTopologyBuilder<Link
             return false;
         }
 
+        private boolean checkForRemoval(final WriteTransaction trans) {
+            final InstanceIdentifier<Node> nid = getNodeInstanceIdentifier(this.nb.getKey());
+
+                if (!this.advertized) {
+                    if (this.tps.isEmpty() && this.prefixes.isEmpty()) {
+                        trans.delete(LogicalDatastoreType.OPERATIONAL, nid);
+                        LOG.debug("Removing unadvertized unused node {}", this.nb.getNodeId());
+                        return true;
+                    }
+
+                    LOG.debug("Node {} is still implied by {} TPs and {} prefixes", this.nb.getNodeId(), this.tps.size(), this.prefixes.size());
+                }
+                return false;
+        }
+
         private synchronized void removeTp(final TpId tp, final LinkId link, final boolean isRemote) {
             final TpHolder h = this.tps.get(tp);
             if (h != null) {
@@ -235,12 +247,7 @@ public final class LinkstateTopologyBuilder extends AbstractTopologyBuilder<Link
         }
 
         private void addTp(final TerminationPoint tp, final LinkId link, final boolean isRemote) {
-            TpHolder h = this.tps.get(tp.getTpId());
-            if (h == null) {
-                h = new TpHolder(tp);
-                this.tps.put(tp.getTpId(), h);
-            }
-
+            final TpHolder h = this.tps.computeIfAbsent(tp.getTpId(), k -> new TpHolder(tp));
             h.addLink(link, isRemote);
         }
 
@@ -260,13 +267,13 @@ public final class LinkstateTopologyBuilder extends AbstractTopologyBuilder<Link
         }
 
         private void advertized(final NodeBuilder nb, final IgpNodeAttributesBuilder inab) {
-            this.nb = Preconditions.checkNotNull(nb);
-            this.inab = Preconditions.checkNotNull(inab);
+            this.nb = requireNonNull(nb);
+            this.inab = requireNonNull(inab);
             this.advertized = true;
             LOG.debug("Node {} is advertized", nb.getNodeId());
         }
 
-        private Object getNodeId() {
+        private NodeId getNodeId() {
             return this.nb.getNodeId();
         }
     }
@@ -275,7 +282,13 @@ public final class LinkstateTopologyBuilder extends AbstractTopologyBuilder<Link
     private final Map<NodeId, NodeHolder> nodes = new HashMap<>();
 
     public LinkstateTopologyBuilder(final DataBroker dataProvider, final RibReference locRibReference, final TopologyId topologyId) {
-        super(dataProvider, locRibReference, topologyId, LINKSTATE_TOPOLOGY_TYPE);
+        super(dataProvider, locRibReference, topologyId, LINKSTATE_TOPOLOGY_TYPE, LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class);
+    }
+
+    public LinkstateTopologyBuilder(final DataBroker dataProvider, final RibReference locRibReference,
+            final TopologyId topologyId, final long listenerResetLimitInMillsec, final int listenerResetEnforceCounter) {
+        super(dataProvider, locRibReference, topologyId, LINKSTATE_TOPOLOGY_TYPE, LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class,
+            listenerResetLimitInMillsec, listenerResetEnforceCounter);
     }
 
     private static LinkId buildLinkId(final UriBuilder base, final LinkCase link) {
@@ -288,7 +301,7 @@ public final class LinkstateTopologyBuilder extends AbstractTopologyBuilder<Link
     }
 
     private static TpId buildTpId(final UriBuilder base, final TopologyIdentifier topologyIdentifier,
-        final Ipv4InterfaceIdentifier ipv4InterfaceIdentifier, final Ipv6InterfaceIdentifier ipv6InterfaceIdentifier, final Long id) {
+            final Ipv4InterfaceIdentifier ipv4InterfaceIdentifier, final Ipv6InterfaceIdentifier ipv6InterfaceIdentifier, final Long id) {
         final UriBuilder b = new UriBuilder(base, "tp");
         if (topologyIdentifier != null) {
             b.add("mt", topologyIdentifier.getValue());
@@ -305,7 +318,7 @@ public final class LinkstateTopologyBuilder extends AbstractTopologyBuilder<Link
 
     private static TpId buildLocalTpId(final UriBuilder base, final LinkDescriptors linkDescriptors) {
         return buildTpId(base, linkDescriptors.getMultiTopologyId(), linkDescriptors.getIpv4InterfaceAddress(),
-            linkDescriptors.getIpv6InterfaceAddress(), linkDescriptors.getLinkLocalIdentifier());
+                linkDescriptors.getIpv6InterfaceAddress(), linkDescriptors.getLinkLocalIdentifier());
     }
 
     private static TerminationPoint buildTp(final TpId id, final TerminationPointType type) {
@@ -315,14 +328,14 @@ public final class LinkstateTopologyBuilder extends AbstractTopologyBuilder<Link
 
         if (type != null) {
             stpb.addAugmentation(TerminationPoint1.class, new TerminationPoint1Builder().setIgpTerminationPointAttributes(
-                new IgpTerminationPointAttributesBuilder().setTerminationPointType(null).build()).build());
+                    new IgpTerminationPointAttributesBuilder().setTerminationPointType(type).build()).build());
         }
 
         return stpb.build();
     }
 
     private static TerminationPointType getTpType(final Ipv4InterfaceIdentifier ipv4InterfaceIdentifier,
-        final Ipv6InterfaceIdentifier ipv6InterfaceIdentifier, final Long id) {
+            final Ipv6InterfaceIdentifier ipv6InterfaceIdentifier, final Long id) {
         // Order of preference: Unnumbered first, then IP
         if (id != null) {
             LOG.debug("Unnumbered termination point type: {}", id);
@@ -349,28 +362,28 @@ public final class LinkstateTopologyBuilder extends AbstractTopologyBuilder<Link
     private static TerminationPoint buildLocalTp(final UriBuilder base, final LinkDescriptors linkDescriptors) {
         final TpId id = buildLocalTpId(base, linkDescriptors);
         final TerminationPointType t = getTpType(linkDescriptors.getIpv4InterfaceAddress(), linkDescriptors.getIpv6InterfaceAddress(),
-            linkDescriptors.getLinkLocalIdentifier());
+                linkDescriptors.getLinkLocalIdentifier());
 
         return buildTp(id, t);
     }
 
     private static TpId buildRemoteTpId(final UriBuilder base, final LinkDescriptors linkDescriptors) {
         return buildTpId(base, linkDescriptors.getMultiTopologyId(), linkDescriptors.getIpv4NeighborAddress(),
-            linkDescriptors.getIpv6NeighborAddress(), linkDescriptors.getLinkRemoteIdentifier());
+                linkDescriptors.getIpv6NeighborAddress(), linkDescriptors.getLinkRemoteIdentifier());
     }
 
     private static TerminationPoint buildRemoteTp(final UriBuilder base, final LinkDescriptors linkDescriptors) {
         final TpId id = buildRemoteTpId(base, linkDescriptors);
         final TerminationPointType t = getTpType(linkDescriptors.getIpv4NeighborAddress(), linkDescriptors.getIpv6NeighborAddress(),
-            linkDescriptors.getLinkRemoteIdentifier());
+                linkDescriptors.getLinkRemoteIdentifier());
 
         return buildTp(id, t);
     }
 
     private InstanceIdentifier<Link> buildLinkIdentifier(final LinkId id) {
         return getInstanceIdentifier().child(
-            org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link.class,
-            new LinkKey(id));
+                org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link.class,
+                new LinkKey(id));
     }
 
     private static Float bandwidthToFloat(final Bandwidth bandwidth) {
@@ -382,19 +395,19 @@ public final class LinkstateTopologyBuilder extends AbstractTopologyBuilder<Link
     }
 
     private static List<UnreservedBandwidth> unreservedBandwidthList(
-        final List<? extends org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.UnreservedBandwidth> input) {
+            final List<? extends org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.UnreservedBandwidth> input) {
         final List<UnreservedBandwidth> ret = new ArrayList<>(input.size());
 
         for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.UnreservedBandwidth i : input) {
             ret.add(new UnreservedBandwidthBuilder().setBandwidth(bandwidthToBigDecimal(i.getBandwidth())).setKey(
-                new UnreservedBandwidthKey(i.getPriority())).build());
+                    new UnreservedBandwidthKey(i.getPriority())).build());
         }
 
         return ret;
     }
 
     private static org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpLinkAttributes1 isisLinkAttributes(
-        final TopologyIdentifier topologyIdentifier, final LinkAttributes la) {
+            final TopologyIdentifier topologyIdentifier, final LinkAttributes la) {
         final org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.link.attributes.isis.link.attributes.TedBuilder tb = new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.link.attributes.isis.link.attributes.TedBuilder();
 
         if (la != null) {
@@ -429,11 +442,11 @@ public final class LinkstateTopologyBuilder extends AbstractTopologyBuilder<Link
         }
 
         return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpLinkAttributes1Builder().setIsisLinkAttributes(
-            ilab.build()).build();
+                ilab.build()).build();
     }
 
     private static org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1 ospfLinkAttributes(
-        final TopologyIdentifier topologyIdentifier, final LinkAttributes la) {
+            final TopologyIdentifier topologyIdentifier, final LinkAttributes la) {
         final org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.link.attributes.ospf.link.attributes.TedBuilder tb = new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.link.attributes.ospf.link.attributes.TedBuilder();
 
         if (la != null) {
@@ -468,7 +481,7 @@ public final class LinkstateTopologyBuilder extends AbstractTopologyBuilder<Link
         }
 
         return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1Builder().setOspfLinkAttributes(
-            ilab.build()).build();
+                ilab.build()).build();
     }
 
     private NodeHolder getNode(final NodeId id) {
@@ -488,22 +501,27 @@ public final class LinkstateTopologyBuilder extends AbstractTopologyBuilder<Link
         }
     }
 
+    private void checkNodeForRemoval(final WriteTransaction trans, final NodeHolder holder) {
+        if (holder.checkForRemoval(trans)) {
+            this.nodes.remove(holder.getNodeId());
+        }
+    }
+
     private static void augmentProtocolId(final LinkstateRoute value, final IgpLinkAttributesBuilder ilab,
             final LinkAttributes la, final LinkDescriptors ld) {
         switch (value.getProtocolId()) {
         case Direct:
         case Static:
-        case Unknown:
-            break;
         case IsisLevel1:
         case IsisLevel2:
             ilab.addAugmentation(
-                org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpLinkAttributes1.class,
-                isisLinkAttributes(ld.getMultiTopologyId(), la));
+                    org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpLinkAttributes1.class,
+                    isisLinkAttributes(ld.getMultiTopologyId(), la));
             break;
+        case OspfV3:
         case Ospf:
             ilab.addAugmentation(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1.class,
-                ospfLinkAttributes(ld.getMultiTopologyId(), la));
+                    ospfLinkAttributes(ld.getMultiTopologyId(), la));
             break;
         default:
             break;
@@ -511,7 +529,7 @@ public final class LinkstateTopologyBuilder extends AbstractTopologyBuilder<Link
     }
 
     private void createLink(final WriteTransaction trans, final UriBuilder base,
-        final LinkstateRoute value, final LinkCase l, final Attributes attributes) {
+            final LinkstateRoute value, final LinkCase l, final Attributes attributes) {
         // defensive lookup
         final LinkAttributes la;
         final Attributes1 attr = attributes.getAugmentation(Attributes1.class);
@@ -556,15 +574,31 @@ public final class LinkstateTopologyBuilder extends AbstractTopologyBuilder<Link
         lb.setSource(new SourceBuilder().setSourceNode(srcNode).setSourceTp(srcTp.getTpId()).build());
         lb.setDestination(new DestinationBuilder().setDestNode(dstNode).setDestTp(dstTp.getTpId()).build());
 
-        final NodeHolder snh = getNode(srcNode);
-        snh.addTp(srcTp, lb.getLinkId(), false);
         LOG.debug("Created TP {} as link source", srcTp);
-        putNode(trans, snh);
+        NodeHolder snh = this.nodes.get(srcNode);
+        if (snh == null) {
+            snh = getNode(srcNode);
+            snh.addTp(srcTp, lb.getLinkId(), false);
+            putNode(trans, snh);
+        } else {
+            snh.addTp(srcTp, lb.getLinkId(), false);
+            final InstanceIdentifier<Node> nid = getNodeInstanceIdentifier(new NodeKey(snh.getNodeId()));
+            trans.put(LogicalDatastoreType.OPERATIONAL, nid.child(TerminationPoint.class, srcTp.getKey()), srcTp);
+        }
 
-        final NodeHolder dnh = getNode(dstNode);
-        dnh.addTp(dstTp, lb.getLinkId(), true);
         LOG.debug("Created TP {} as link destination", dstTp);
-        putNode(trans, dnh);
+        NodeHolder dnh = this.nodes.get(dstNode);
+        if (dnh == null) {
+            dnh = getNode(dstNode);
+            dnh.addTp(dstTp, lb.getLinkId(), true);
+            putNode(trans, dnh);
+        } else {
+            dnh.addTp(dstTp, lb.getLinkId(), true);
+            final InstanceIdentifier<Node> nid = getInstanceIdentifier().child(
+                    org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node.class,
+                    new NodeKey(dnh.getNodeId()));
+            trans.put(LogicalDatastoreType.OPERATIONAL, nid.child(TerminationPoint.class, dstTp.getKey()), dstTp);
+        }
 
         final InstanceIdentifier<Link> lid = buildLinkIdentifier(lb.getLinkId());
         final Link link = lb.build();
@@ -574,11 +608,13 @@ public final class LinkstateTopologyBuilder extends AbstractTopologyBuilder<Link
     }
 
     private void removeTp(final WriteTransaction trans, final NodeId node, final TpId tp,
-        final LinkId link, final boolean isRemote) {
+            final LinkId link, final boolean isRemote) {
         final NodeHolder nh = this.nodes.get(node);
         if (nh != null) {
+            final InstanceIdentifier<Node> nid = getNodeInstanceIdentifier(new NodeKey(nh.getNodeId()));
+            trans.delete(LogicalDatastoreType.OPERATIONAL, nid.child(TerminationPoint.class, new TerminationPointKey(tp)));
             nh.removeTp(tp, link, isRemote);
-            putNode(trans, nh);
+            checkNodeForRemoval(trans, nh);
         } else {
             LOG.warn("Removed non-existent node {}", node);
         }
@@ -603,7 +639,7 @@ public final class LinkstateTopologyBuilder extends AbstractTopologyBuilder<Link
     }
 
     private static org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1 isisNodeAttributes(
-        final NodeIdentifier node, final NodeAttributes na) {
+            final NodeIdentifier node, final NodeAttributes na) {
         final org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.node.attributes.isis.node.attributes.TedBuilder tb = new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.node.attributes.isis.node.attributes.TedBuilder();
         final IsisNodeAttributesBuilder ab = new IsisNodeAttributesBuilder();
 
@@ -642,20 +678,15 @@ public final class LinkstateTopologyBuilder extends AbstractTopologyBuilder<Link
         ab.setTed(tb.build());
 
         return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1Builder().setIsisNodeAttributes(
-            ab.build()).build();
+                ab.build()).build();
     }
 
     private static List<IsoNetId> toIsoNetIds(final List<IsisAreaIdentifier> areaIds, final String systemId) {
-        return Lists.transform(areaIds, new Function<IsisAreaIdentifier, IsoNetId>() {
-            @Override
-            public IsoNetId apply(final IsisAreaIdentifier input) {
-                return new IsoNetId(UriBuilder.toIsoNetId(input, systemId));
-            }
-        });
+        return Lists.transform(areaIds, input -> new IsoNetId(UriBuilder.toIsoNetId(input, systemId)));
     }
 
     private static org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1 ospfNodeAttributes(
-        final NodeIdentifier node, final NodeAttributes na) {
+            final NodeIdentifier node, final NodeAttributes na) {
         final org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.ospf.node.attributes.TedBuilder tb = new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.ospf.node.attributes.TedBuilder();
         final OspfNodeAttributesBuilder ab = new OspfNodeAttributesBuilder();
         if (na != null) {
@@ -687,7 +718,7 @@ public final class LinkstateTopologyBuilder extends AbstractTopologyBuilder<Link
         }
         ab.setTed(tb.build());
         return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1Builder().setOspfNodeAttributes(
-            ab.build()).build();
+                ab.build()).build();
     }
 
     private static void augmentProtocolId(final LinkstateRoute value, final IgpNodeAttributesBuilder inab,
@@ -695,17 +726,15 @@ public final class LinkstateTopologyBuilder extends AbstractTopologyBuilder<Link
         switch (value.getProtocolId()) {
         case Direct:
         case Static:
-        case Unknown:
-            break;
         case IsisLevel1:
         case IsisLevel2:
             inab.addAugmentation(
-                org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1.class,
-                isisNodeAttributes(nd, na));
+                    org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1.class,
+                    isisNodeAttributes(nd, na));
             break;
         case Ospf:
             inab.addAugmentation(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1.class,
-                ospfNodeAttributes(nd, na));
+                    ospfNodeAttributes(nd, na));
             break;
         default:
             break;
@@ -713,7 +742,7 @@ public final class LinkstateTopologyBuilder extends AbstractTopologyBuilder<Link
     }
 
     private void createNode(final WriteTransaction trans, final UriBuilder base,
-        final LinkstateRoute value, final NodeCase n, final Attributes attributes) {
+            final LinkstateRoute value, final NodeCase n, final Attributes attributes) {
         final NodeAttributes na;
         //defensive lookup
         final Attributes1 attr = attributes.getAugmentation(Attributes1.class);
@@ -779,14 +808,12 @@ public final class LinkstateTopologyBuilder extends AbstractTopologyBuilder<Link
         case IsisLevel1:
         case IsisLevel2:
         case Static:
-        case Unknown:
-            break;
         case Ospf:
             if (pa != null && pa.getOspfForwardingAddress() != null) {
                 pb.addAugmentation(
-                    Prefix1.class,
-                    new Prefix1Builder().setOspfPrefixAttributes(
-                        new OspfPrefixAttributesBuilder().setForwardingAddress(pa.getOspfForwardingAddress().getIpv4Address()).build()).build());
+                        Prefix1.class,
+                        new Prefix1Builder().setOspfPrefixAttributes(
+                                new OspfPrefixAttributesBuilder().setForwardingAddress(pa.getOspfForwardingAddress().getIpv4Address()).build()).build());
             }
             break;
         default:
@@ -795,14 +822,15 @@ public final class LinkstateTopologyBuilder extends AbstractTopologyBuilder<Link
     }
 
     private void createPrefix(final WriteTransaction trans, final UriBuilder base,
-        final LinkstateRoute value, final PrefixCase p, final Attributes attributes) {
+            final LinkstateRoute value, final PrefixCase p, final Attributes attributes) {
         final IpPrefix ippfx = p.getPrefixDescriptors().getIpReachabilityInformation();
         if (ippfx == null) {
             LOG.warn("IP reachability not present in prefix {} route {}, skipping it", p, value);
             return;
         }
         final PrefixBuilder pb = new PrefixBuilder();
-        pb.setKey(new PrefixKey(ippfx));
+        final PrefixKey pk = new PrefixKey(ippfx);
+        pb.setKey(pk);
         pb.setPrefix(ippfx);
 
         final PrefixAttributes pa;
@@ -826,32 +854,55 @@ public final class LinkstateTopologyBuilder extends AbstractTopologyBuilder<Link
         augmentProtocolId(value, pa, pb);
 
         final Prefix pfx = pb.build();
+        LOG.debug("Created prefix {} for {}", pfx, p);
 
         /*
          * All set, but... the hosting node may not exist, we may need to fake it.
          */
         final NodeId node = buildNodeId(base, p.getAdvertisingNodeDescriptors());
-        final NodeHolder nh = getNode(node);
-        nh.addPrefix(pfx);
-        LOG.debug("Created prefix {} for {}", pfx, p);
-        putNode(trans, nh);
+        NodeHolder nh = this.nodes.get(node);
+        if (nh == null) {
+            nh = getNode(node);
+            nh.addPrefix(pfx);
+            putNode(trans, nh);
+        } else {
+            nh.addPrefix(pfx);
+            final InstanceIdentifier<Node> nid = getNodeInstanceIdentifier(new NodeKey(nh.getNodeId()));
+            final InstanceIdentifier<IgpNodeAttributes> inaId = nid.builder().augmentation(Node1.class).child(IgpNodeAttributes.class).build();
+            trans.put(LogicalDatastoreType.OPERATIONAL, inaId.child(Prefix.class, pk), pfx);
+        }
     }
 
     private void removePrefix(final WriteTransaction trans, final UriBuilder base, final PrefixCase p) {
         final NodeId node = buildNodeId(base, p.getAdvertisingNodeDescriptors());
         final NodeHolder nh = this.nodes.get(node);
         if (nh != null) {
-            nh.removePrefix(p);
             LOG.debug("Removed prefix {}", p);
-            putNode(trans, nh);
+            final InstanceIdentifier<Node> nid = getNodeInstanceIdentifier(new NodeKey(nh.getNodeId()));
+            final InstanceIdentifier<IgpNodeAttributes> inaId = nid.builder().augmentation(Node1.class).child(IgpNodeAttributes.class).build();
+            final IpPrefix ippfx = p.getPrefixDescriptors().getIpReachabilityInformation();
+            if (ippfx == null) {
+                LOG.warn("IP reachability not present in prefix {}, skipping it", p);
+                return;
+            }
+            final PrefixKey pk = new PrefixKey(ippfx);
+            trans.delete(LogicalDatastoreType.OPERATIONAL, inaId.child(Prefix.class, pk));
+            nh.removePrefix(p);
+            checkNodeForRemoval(trans, nh);
         } else {
             LOG.warn("Removing prefix from non-existing node {}", node);
         }
     }
 
+    private InstanceIdentifier<Node> getNodeInstanceIdentifier(final NodeKey nodeKey) {
+        return getInstanceIdentifier().child(
+                org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node.class,
+                nodeKey);
+    }
+
     @Override
     protected void createObject(final ReadWriteTransaction trans,
-        final InstanceIdentifier<LinkstateRoute> id, final LinkstateRoute value) {
+            final InstanceIdentifier<LinkstateRoute> id, final LinkstateRoute value) {
         final UriBuilder base = new UriBuilder(value);
 
         final ObjectType t = value.getObjectType();
@@ -870,7 +921,12 @@ public final class LinkstateTopologyBuilder extends AbstractTopologyBuilder<Link
 
     @Override
     protected void removeObject(final ReadWriteTransaction trans,
-        final InstanceIdentifier<LinkstateRoute> id, final LinkstateRoute value) {
+            final InstanceIdentifier<LinkstateRoute> id, final LinkstateRoute value) {
+        if (value == null) {
+            LOG.error("Empty before-data received in delete data change notification for instance id {}", id);
+            return;
+        }
+
         final UriBuilder base = new UriBuilder(value);
 
         final ObjectType t = value.getObjectType();
@@ -890,4 +946,9 @@ public final class LinkstateTopologyBuilder extends AbstractTopologyBuilder<Link
     protected InstanceIdentifier<LinkstateRoute> getRouteWildcard(final InstanceIdentifier<Tables> tablesId) {
         return tablesId.child((Class)LinkstateRoutes.class).child(LinkstateRoute.class);
     }
+
+    @Override
+    protected void clearTopology() {
+        this.nodes.clear();
+    }
 }