2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.bgpcep.bgp.topology.provider;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import java.util.Collections;
15 import java.util.HashMap;
16 import java.util.List;
18 import java.util.concurrent.ExecutionException;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.ReadTransaction;
21 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.protocol.bgp.rib.RibReference;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.Route;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.AddressFamily;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.SubsequentAddressFamily;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.CNextHop;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.Ipv4NextHopCase;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.Ipv6NextHopCase;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.ipv4.next.hop._case.Ipv4NextHop;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.ipv6.next.hop._case.Ipv6NextHop;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.TopologyTypes;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1Builder;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.IgpNodeAttributes;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.IgpNodeAttributesBuilder;
44 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;
45 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;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.igp.node.attributes.PrefixKey;
47 import org.opendaylight.yangtools.yang.binding.DataObject;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
53 abstract class AbstractReachabilityTopologyBuilder<T extends Route> extends AbstractTopologyBuilder<T> {
54 private static final Logger LOG = LoggerFactory.getLogger(AbstractReachabilityTopologyBuilder.class);
55 private final Map<NodeId, NodeUsage> nodes = new HashMap<>();
57 private static final class NodeUsage {
58 private final InstanceIdentifier<IgpNodeAttributes> attrId;
59 private int useCount = 1;
61 NodeUsage(final InstanceIdentifier<IgpNodeAttributes> attrId) {
62 this.attrId = requireNonNull(attrId);
66 protected AbstractReachabilityTopologyBuilder(final DataBroker dataProvider, final RibReference locRibReference,
67 final TopologyId topologyId, final TopologyTypes topologyTypes, final Class<? extends AddressFamily> afi,
68 final Class<? extends SubsequentAddressFamily> safi) {
69 super(dataProvider, locRibReference, topologyId, topologyTypes, afi, safi);
72 private static NodeId advertizingNode(final Attributes attrs) {
73 final CNextHop nh = attrs.getCNextHop();
75 LOG.warn("Next hop value is null");
77 } else if (nh instanceof Ipv4NextHopCase) {
78 final Ipv4NextHop ipv4 = ((Ipv4NextHopCase) nh).getIpv4NextHop();
80 return new NodeId(ipv4.getGlobal().getValue());
81 } else if (nh instanceof Ipv6NextHopCase) {
82 final Ipv6NextHop ipv6 = ((Ipv6NextHopCase) nh).getIpv6NextHop();
84 return new NodeId(ipv6.getGlobal().getValue());
86 LOG.warn("Unhandled next hop class {}", nh.getImplementedInterface());
91 private KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceId(final NodeId ni) {
92 return getInstanceIdentifier().child(Node.class, new NodeKey(ni));
95 private static <T extends DataObject> T read(final ReadTransaction rt, final InstanceIdentifier<T> id) {
96 final Optional<T> optional;
98 optional = rt.read(LogicalDatastoreType.OPERATIONAL, id).get();
99 } catch (InterruptedException | ExecutionException e) {
100 LOG.warn("Failed to read {}, assuming non-existent", id, e);
104 return optional.orNull();
107 private InstanceIdentifier<IgpNodeAttributes> ensureNodePresent(final ReadWriteTransaction trans, final NodeId ni) {
108 final NodeUsage present = this.nodes.get(ni);
109 if (present != null) {
110 return present.attrId;
113 final KeyedInstanceIdentifier<Node, NodeKey> nii = nodeInstanceId(ni);
114 final InstanceIdentifier<IgpNodeAttributes> ret = nii.builder().augmentation(Node1.class)
115 .child(IgpNodeAttributes.class).build();
117 trans.merge(LogicalDatastoreType.OPERATIONAL, nii, new NodeBuilder().withKey(nii.getKey()).setNodeId(ni)
118 .addAugmentation(Node1.class, new Node1Builder().setIgpNodeAttributes(
119 new IgpNodeAttributesBuilder().setPrefix(Collections.emptyList()).build()).build()).build());
121 this.nodes.put(ni, new NodeUsage(ret));
125 protected abstract Attributes getAttributes(T value);
127 protected abstract IpPrefix getPrefix(T value);
130 protected final void createObject(final ReadWriteTransaction trans, final InstanceIdentifier<T> id, final T value) {
131 final NodeId ni = advertizingNode(getAttributes(value));
135 final InstanceIdentifier<IgpNodeAttributes> nii = ensureNodePresent(trans, ni);
137 final IpPrefix prefix = getPrefix(value);
138 final PrefixKey pk = new PrefixKey(prefix);
140 trans.put(LogicalDatastoreType.OPERATIONAL,
141 nii.child(Prefix.class, pk), new PrefixBuilder().withKey(pk).setPrefix(prefix).build());
145 protected final void removeObject(final ReadWriteTransaction trans, final InstanceIdentifier<T> id, final T value) {
147 LOG.error("Empty before-data received in delete data change notification for instance id {}", id);
151 final NodeId ni = advertizingNode(getAttributes(value));
155 final NodeUsage present = this.nodes.get(ni);
156 Preconditions.checkState(present != null, "Removing prefix from non-existent node %s", present);
158 final PrefixKey pk = new PrefixKey(getPrefix(value));
159 trans.delete(LogicalDatastoreType.OPERATIONAL, present.attrId.child(Prefix.class, pk));
162 * This is optimization magic: we are reading a list and we want to remove it once it
163 * hits zero. We may be in a transaction, so the read is costly, especially since we
164 * have just modified the list.
166 * Once we have performed the read, though, we can check the number of nodes, and reuse
167 * it for that number of removals. Note that since we do not track data and thus have
168 * no understanding about the difference between replace and add, we do not ever increase
169 * the life of this in createObject().
172 if (present.useCount == 0) {
173 final IgpNodeAttributes attrs = read(trans, present.attrId);
175 final List<Prefix> prefix = attrs.getPrefix();
176 present.useCount = prefix == null ? 0 : prefix.size();
178 present.useCount = 0;
180 if (present.useCount == 0) {
181 trans.delete(LogicalDatastoreType.OPERATIONAL, nodeInstanceId(ni));
182 this.nodes.remove(ni);
188 protected void clearTopology() {