package org.opendaylight.bgpcep.bgp.topology.provider;
import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
import java.util.concurrent.ExecutionException;
import org.opendaylight.controller.md.sal.binding.api.DataBroker;
import org.opendaylight.controller.md.sal.binding.api.ReadTransaction;
import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
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.IpPrefix;
+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.message.rev130919.path.attributes.Attributes;
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.route.Attributes;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.CNextHop;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv4NextHopCase;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv6NextHopCase;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
-import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.TopologyTypesBuilder;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.TopologyTypes;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1Builder;
import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.IgpNodeAttributes;
*/
abstract class AbstractReachabilityTopologyBuilder<T extends Route> extends AbstractTopologyBuilder<T> {
private static final Logger LOG = LoggerFactory.getLogger(AbstractReachabilityTopologyBuilder.class);
+ private final Map<NodeId, NodeUsage> nodes = new HashMap<>();
+
+ private static final class NodeUsage {
+ private final InstanceIdentifier<IgpNodeAttributes> attrId;
+ private int useCount = 1;
+
+ NodeUsage(final InstanceIdentifier<IgpNodeAttributes> attrId) {
+ this.attrId = Preconditions.checkNotNull(attrId);
+ }
+ }
protected AbstractReachabilityTopologyBuilder(final DataBroker dataProvider, final RibReference locRibReference,
- final TopologyId topologyId, final Class<T> idClass) {
- super(dataProvider, locRibReference, topologyId, new TopologyTypesBuilder().build(), idClass);
+ final TopologyId topologyId, final TopologyTypes topologyTypes) {
+ super(dataProvider, locRibReference, topologyId, topologyTypes);
}
private NodeId advertizingNode(final Attributes attrs) {
final CNextHop nh = attrs.getCNextHop();
- if (nh instanceof Ipv4NextHopCase) {
+ if (nh == null) {
+ LOG.warn("Next hop value is null");
+ return null;
+ } else if (nh instanceof Ipv4NextHopCase) {
final Ipv4NextHop ipv4 = ((Ipv4NextHopCase) nh).getIpv4NextHop();
return new NodeId(ipv4.getGlobal().getValue());
return o.orNull();
}
- private InstanceIdentifier<Node1> ensureNodePresent(final ReadWriteTransaction trans, final NodeId ni) {
+ private InstanceIdentifier<IgpNodeAttributes> ensureNodePresent(final ReadWriteTransaction trans, final NodeId ni) {
+ final NodeUsage present = this.nodes.get(ni);
+ if (present != null) {
+ return present.attrId;
+ }
+
final KeyedInstanceIdentifier<Node, NodeKey> nii = nodeInstanceId(ni);
- final InstanceIdentifier<Node1> ret = nii.augmentation(Node1.class);
+ final InstanceIdentifier<IgpNodeAttributes> ret = nii.builder().augmentation(Node1.class).child(IgpNodeAttributes.class).build();
trans.merge(LogicalDatastoreType.OPERATIONAL, nii, new NodeBuilder().setKey(nii.getKey()).setNodeId(ni)
.addAugmentation(Node1.class, new Node1Builder().setIgpNodeAttributes(
new IgpNodeAttributesBuilder().setPrefix(Collections.<Prefix>emptyList()).build()).build()).build());
+ this.nodes.put(ni, new NodeUsage(ret));
return ret;
}
- private void removeEmptyNode(final ReadWriteTransaction trans, final InstanceIdentifier<Node> nii) {
- final IgpNodeAttributes attrs = read(trans, nii.augmentation(Node1.class).child(IgpNodeAttributes.class));
- if (attrs != null && attrs.getPrefix().isEmpty()) {
- trans.delete(LogicalDatastoreType.OPERATIONAL, nii);
- }
- }
-
protected abstract Attributes getAttributes(final T value);
protected abstract IpPrefix getPrefix(final T value);
@Override
protected final void createObject(final ReadWriteTransaction trans, final InstanceIdentifier<T> id, final T value) {
final NodeId ni = advertizingNode(getAttributes(value));
- final InstanceIdentifier<Node1> nii = ensureNodePresent(trans, ni);
+ if (ni == null) {
+ return;
+ }
+ final InstanceIdentifier<IgpNodeAttributes> nii = ensureNodePresent(trans, ni);
final IpPrefix prefix = getPrefix(value);
final PrefixKey pk = new PrefixKey(prefix);
trans.put(LogicalDatastoreType.OPERATIONAL,
- nii.child(IgpNodeAttributes.class).child(
- Prefix.class, pk), new PrefixBuilder().setKey(pk).setPrefix(prefix).build());
+ nii.child(Prefix.class, pk), new PrefixBuilder().setKey(pk).setPrefix(prefix).build());
}
@Override
protected final void removeObject(final ReadWriteTransaction trans, final InstanceIdentifier<T> id, final T value) {
final NodeId ni = advertizingNode(getAttributes(value));
- final InstanceIdentifier<Node> nii = nodeInstanceId(ni);
-
- final IpPrefix prefix = getPrefix(value);
- final PrefixKey pk = new PrefixKey(prefix);
-
- trans.delete(LogicalDatastoreType.OPERATIONAL, nii.augmentation(Node1.class).child(IgpNodeAttributes.class).child(
- Prefix.class, pk));
-
- removeEmptyNode(trans, nii);
+ if (ni == null) {
+ return;
+ }
+ final NodeUsage present = this.nodes.get(ni);
+ Preconditions.checkState(present != null, "Removing prefix from non-existent node %s", present);
+
+ final PrefixKey pk = new PrefixKey(getPrefix(value));
+ trans.delete(LogicalDatastoreType.OPERATIONAL, present.attrId.child(Prefix.class, pk));
+
+ /*
+ * This is optimization magic: we are reading a list and we want to remove it once it
+ * hits zero. We may be in a transaction, so the read is costly, especially since we
+ * have just modified the list.
+ *
+ * Once we have performed the read, though, we can check the number of nodes, and reuse
+ * it for that number of removals. Note that since we do not track data and thus have
+ * no understanding about the difference between replace and add, we do not ever increase
+ * the life of this in createObject().
+ */
+ present.useCount--;
+ if (present.useCount == 0) {
+ final IgpNodeAttributes attrs = read(trans, present.attrId);
+ if (attrs != null) {
+ present.useCount = attrs.getPrefix().size();
+ if (present.useCount == 0) {
+ trans.delete(LogicalDatastoreType.OPERATIONAL, nodeInstanceId(ni));
+ this.nodes.remove(ni);
+ }
+ }
+ }
}
-
}