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 com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import java.util.Collections;
13 import java.util.HashMap;
15 import java.util.concurrent.ExecutionException;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.ReadTransaction;
18 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.protocol.bgp.rib.RibReference;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.Route;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.CNextHop;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv4NextHopCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv6NextHopCase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.ipv4.next.hop._case.Ipv4NextHop;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.ipv6.next.hop._case.Ipv6NextHop;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.TopologyTypesBuilder;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1Builder;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.IgpNodeAttributes;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.IgpNodeAttributesBuilder;
39 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;
40 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;
41 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;
42 import org.opendaylight.yangtools.yang.binding.DataObject;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
45 import org.slf4j.Logger;
46 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 = Preconditions.checkNotNull(attrId);
64 protected AbstractReachabilityTopologyBuilder(final DataBroker dataProvider, final RibReference locRibReference,
65 final TopologyId topologyId) {
66 super(dataProvider, locRibReference, topologyId, new TopologyTypesBuilder().build());
69 private NodeId advertizingNode(final Attributes attrs) {
70 final CNextHop nh = attrs.getCNextHop();
71 if (nh instanceof Ipv4NextHopCase) {
72 final Ipv4NextHop ipv4 = ((Ipv4NextHopCase) nh).getIpv4NextHop();
74 return new NodeId(ipv4.getGlobal().getValue());
75 } else if (nh instanceof Ipv6NextHopCase) {
76 final Ipv6NextHop ipv6 = ((Ipv6NextHopCase) nh).getIpv6NextHop();
78 return new NodeId(ipv6.getGlobal().getValue());
80 LOG.warn("Unhandled next hop class {}", nh.getImplementedInterface());
85 private KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceId(final NodeId ni) {
86 return getInstanceIdentifier().child(Node.class, new NodeKey(ni));
89 private static <T extends DataObject> T read(final ReadTransaction t, final InstanceIdentifier<T> id) {
92 o = t.read(LogicalDatastoreType.OPERATIONAL, id).get();
93 } catch (InterruptedException | ExecutionException e) {
94 LOG.warn("Failed to read {}, assuming non-existent", id, e);
101 private InstanceIdentifier<IgpNodeAttributes> ensureNodePresent(final ReadWriteTransaction trans, final NodeId ni) {
102 final NodeUsage present = this.nodes.get(ni);
103 if (present != null) {
104 return present.attrId;
107 final KeyedInstanceIdentifier<Node, NodeKey> nii = nodeInstanceId(ni);
108 final InstanceIdentifier<IgpNodeAttributes> ret = nii.builder().augmentation(Node1.class).child(IgpNodeAttributes.class).build();
110 trans.merge(LogicalDatastoreType.OPERATIONAL, nii, new NodeBuilder().setKey(nii.getKey()).setNodeId(ni)
111 .addAugmentation(Node1.class, new Node1Builder().setIgpNodeAttributes(
112 new IgpNodeAttributesBuilder().setPrefix(Collections.<Prefix>emptyList()).build()).build()).build());
114 this.nodes.put(ni, new NodeUsage(ret));
118 protected abstract Attributes getAttributes(final T value);
120 protected abstract IpPrefix getPrefix(final T value);
123 protected final void createObject(final ReadWriteTransaction trans, final InstanceIdentifier<T> id, final T value) {
124 final NodeId ni = advertizingNode(getAttributes(value));
125 final InstanceIdentifier<IgpNodeAttributes> nii = ensureNodePresent(trans, ni);
127 final IpPrefix prefix = getPrefix(value);
128 final PrefixKey pk = new PrefixKey(prefix);
130 trans.put(LogicalDatastoreType.OPERATIONAL,
131 nii.child(Prefix.class, pk), new PrefixBuilder().setKey(pk).setPrefix(prefix).build());
135 protected final void removeObject(final ReadWriteTransaction trans, final InstanceIdentifier<T> id, final T value) {
136 final NodeId ni = advertizingNode(getAttributes(value));
137 final NodeUsage present = this.nodes.get(ni);
138 Preconditions.checkState(present != null, "Removing prefix from non-existent node %s", present);
140 final PrefixKey pk = new PrefixKey(getPrefix(value));
141 trans.delete(LogicalDatastoreType.OPERATIONAL, present.attrId.child(Prefix.class, pk));
144 * This is optimization magic: we are reading a list and we want to remove it once it
145 * hits zero. We may be in a transaction, so the read is costly, especially since we
146 * have just modified the list.
148 * Once we have performed the read, though, we can check the number of nodes, and reuse
149 * it for that number of removals. Note that since we do not track data and thus have
150 * no understanding about the difference between replace and add, we do not ever increase
151 * the life of this in createObject().
154 if (present.useCount == 0) {
155 final IgpNodeAttributes attrs = read(trans, present.attrId);
157 present.useCount = attrs.getPrefix().size();
158 if (present.useCount == 0) {
159 trans.delete(LogicalDatastoreType.OPERATIONAL, nodeInstanceId(ni));
160 this.nodes.remove(ni);