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.Preconditions;
13 import java.util.HashMap;
15 import java.util.Optional;
16 import java.util.concurrent.ExecutionException;
17 import org.opendaylight.mdsal.binding.api.DataBroker;
18 import org.opendaylight.mdsal.binding.api.ReadOperations;
19 import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
20 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
21 import org.opendaylight.protocol.bgp.rib.RibReference;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.Attributes;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.Route;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.AddressFamily;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.SubsequentAddressFamily;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.next.hop.CNextHop;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.next.hop.c.next.hop.Ipv4NextHopCase;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.next.hop.c.next.hop.Ipv6NextHopCase;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.next.hop.c.next.hop.ipv4.next.hop._case.Ipv4NextHop;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.next.hop.c.next.hop.ipv6.next.hop._case.Ipv6NextHop;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.TopologyTypes;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1Builder;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.IgpNodeAttributes;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.IgpNodeAttributesBuilder;
42 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;
43 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;
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.PrefixKey;
45 import org.opendaylight.yangtools.yang.binding.DataObject;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
51 abstract class AbstractReachabilityTopologyBuilder<T extends Route> extends AbstractTopologyBuilder<T> {
52 private static final Logger LOG = LoggerFactory.getLogger(AbstractReachabilityTopologyBuilder.class);
53 private final Map<NodeId, NodeUsage> nodes = new HashMap<>();
55 private static final class NodeUsage {
56 private final InstanceIdentifier<IgpNodeAttributes> attrId;
57 private int useCount = 1;
59 NodeUsage(final InstanceIdentifier<IgpNodeAttributes> attrId) {
60 this.attrId = requireNonNull(attrId);
64 protected AbstractReachabilityTopologyBuilder(final DataBroker dataProvider, final RibReference locRibReference,
65 final TopologyId topologyId, final TopologyTypes topologyTypes, final AddressFamily afi,
66 final SubsequentAddressFamily safi) {
67 super(dataProvider, locRibReference, topologyId, topologyTypes, afi, safi);
70 private static NodeId advertizingNode(final Attributes attrs) {
71 final CNextHop nh = attrs.getCNextHop();
73 LOG.warn("Next hop value is null");
75 } else if (nh instanceof Ipv4NextHopCase) {
76 final Ipv4NextHop ipv4 = ((Ipv4NextHopCase) nh).getIpv4NextHop();
78 return new NodeId(ipv4.getGlobal().getValue());
79 } else if (nh instanceof Ipv6NextHopCase) {
80 final Ipv6NextHop ipv6 = ((Ipv6NextHopCase) nh).getIpv6NextHop();
82 return new NodeId(ipv6.getGlobal().getValue());
84 LOG.warn("Unhandled next hop class {}", nh.implementedInterface());
89 private KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceId(final NodeId ni) {
90 return getInstanceIdentifier().child(Node.class, new NodeKey(ni));
93 private static <T extends DataObject> T read(final ReadOperations rt, final InstanceIdentifier<T> id) {
94 final Optional<T> optional;
96 optional = rt.read(LogicalDatastoreType.OPERATIONAL, id).get();
97 } catch (InterruptedException | ExecutionException e) {
98 LOG.warn("Failed to read {}, assuming non-existent", id, e);
102 return optional.orElse(null);
105 private InstanceIdentifier<IgpNodeAttributes> ensureNodePresent(final ReadWriteTransaction trans, final NodeId ni) {
106 final NodeUsage present = this.nodes.get(ni);
107 if (present != null) {
108 return present.attrId;
111 final KeyedInstanceIdentifier<Node, NodeKey> nii = nodeInstanceId(ni);
112 final InstanceIdentifier<IgpNodeAttributes> ret = nii.builder().augmentation(Node1.class)
113 .child(IgpNodeAttributes.class).build();
115 trans.merge(LogicalDatastoreType.OPERATIONAL, nii, new NodeBuilder().withKey(nii.getKey()).setNodeId(ni)
116 .addAugmentation(new Node1Builder().setIgpNodeAttributes(
117 new IgpNodeAttributesBuilder().setPrefix(Map.of()).build()).build()).build());
119 this.nodes.put(ni, new NodeUsage(ret));
123 protected abstract Attributes getAttributes(T value);
125 protected abstract IpPrefix getPrefix(T value);
128 protected final void createObject(final ReadWriteTransaction trans, final InstanceIdentifier<T> id, final T value) {
129 final NodeId ni = advertizingNode(getAttributes(value));
133 final InstanceIdentifier<IgpNodeAttributes> nii = ensureNodePresent(trans, ni);
135 final IpPrefix prefix = getPrefix(value);
136 final PrefixKey pk = new PrefixKey(prefix);
138 trans.put(LogicalDatastoreType.OPERATIONAL,
139 nii.child(Prefix.class, pk), new PrefixBuilder().withKey(pk).setPrefix(prefix).build());
143 protected final void removeObject(final ReadWriteTransaction trans, final InstanceIdentifier<T> id, final T value) {
145 LOG.error("Empty before-data received in delete data change notification for instance id {}", id);
149 final NodeId ni = advertizingNode(getAttributes(value));
153 final NodeUsage present = this.nodes.get(ni);
154 Preconditions.checkState(present != null, "Removing prefix from non-existent node %s", present);
156 final PrefixKey pk = new PrefixKey(getPrefix(value));
157 trans.delete(LogicalDatastoreType.OPERATIONAL, present.attrId.child(Prefix.class, pk));
160 * This is optimization magic: we are reading a list and we want to remove it once it
161 * hits zero. We may be in a transaction, so the read is costly, especially since we
162 * have just modified the list.
164 * Once we have performed the read, though, we can check the number of nodes, and reuse
165 * it for that number of removals. Note that since we do not track data and thus have
166 * no understanding about the difference between replace and add, we do not ever increase
167 * the life of this in createObject().
170 if (present.useCount == 0) {
171 final IgpNodeAttributes attrs = read(trans, present.attrId);
173 present.useCount = attrs.nonnullPrefix().size();
175 present.useCount = 0;
177 if (present.useCount == 0) {
178 trans.delete(LogicalDatastoreType.OPERATIONAL, nodeInstanceId(ni));
179 this.nodes.remove(ni);
185 protected void clearTopology() {