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.rev130715.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.TopologyTypes;
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, final TopologyTypes topologyTypes) {
66 super(dataProvider, locRibReference, topologyId, topologyTypes);
69 private NodeId advertizingNode(final Attributes attrs) {
70 final CNextHop nh = attrs.getCNextHop();
72 LOG.warn("Next hop value is null");
74 } else if (nh instanceof Ipv4NextHopCase) {
75 final Ipv4NextHop ipv4 = ((Ipv4NextHopCase) nh).getIpv4NextHop();
77 return new NodeId(ipv4.getGlobal().getValue());
78 } else if (nh instanceof Ipv6NextHopCase) {
79 final Ipv6NextHop ipv6 = ((Ipv6NextHopCase) nh).getIpv6NextHop();
81 return new NodeId(ipv6.getGlobal().getValue());
83 LOG.warn("Unhandled next hop class {}", nh.getImplementedInterface());
88 private KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceId(final NodeId ni) {
89 return getInstanceIdentifier().child(Node.class, new NodeKey(ni));
92 private static <T extends DataObject> T read(final ReadTransaction t, final InstanceIdentifier<T> id) {
95 o = t.read(LogicalDatastoreType.OPERATIONAL, id).get();
96 } catch (InterruptedException | ExecutionException e) {
97 LOG.warn("Failed to read {}, assuming non-existent", id, e);
104 private InstanceIdentifier<IgpNodeAttributes> ensureNodePresent(final ReadWriteTransaction trans, final NodeId ni) {
105 final NodeUsage present = this.nodes.get(ni);
106 if (present != null) {
107 return present.attrId;
110 final KeyedInstanceIdentifier<Node, NodeKey> nii = nodeInstanceId(ni);
111 final InstanceIdentifier<IgpNodeAttributes> ret = nii.builder().augmentation(Node1.class).child(IgpNodeAttributes.class).build();
113 trans.merge(LogicalDatastoreType.OPERATIONAL, nii, new NodeBuilder().setKey(nii.getKey()).setNodeId(ni)
114 .addAugmentation(Node1.class, new Node1Builder().setIgpNodeAttributes(
115 new IgpNodeAttributesBuilder().setPrefix(Collections.<Prefix>emptyList()).build()).build()).build());
117 this.nodes.put(ni, new NodeUsage(ret));
121 protected abstract Attributes getAttributes(final T value);
123 protected abstract IpPrefix getPrefix(final T value);
126 protected final void createObject(final ReadWriteTransaction trans, final InstanceIdentifier<T> id, final T value) {
127 final NodeId ni = advertizingNode(getAttributes(value));
131 final InstanceIdentifier<IgpNodeAttributes> nii = ensureNodePresent(trans, ni);
133 final IpPrefix prefix = getPrefix(value);
134 final PrefixKey pk = new PrefixKey(prefix);
136 trans.put(LogicalDatastoreType.OPERATIONAL,
137 nii.child(Prefix.class, pk), new PrefixBuilder().setKey(pk).setPrefix(prefix).build());
141 protected final void removeObject(final ReadWriteTransaction trans, final InstanceIdentifier<T> id, final T value) {
142 final NodeId ni = advertizingNode(getAttributes(value));
146 final NodeUsage present = this.nodes.get(ni);
147 Preconditions.checkState(present != null, "Removing prefix from non-existent node %s", present);
149 final PrefixKey pk = new PrefixKey(getPrefix(value));
150 trans.delete(LogicalDatastoreType.OPERATIONAL, present.attrId.child(Prefix.class, pk));
153 * This is optimization magic: we are reading a list and we want to remove it once it
154 * hits zero. We may be in a transaction, so the read is costly, especially since we
155 * have just modified the list.
157 * Once we have performed the read, though, we can check the number of nodes, and reuse
158 * it for that number of removals. Note that since we do not track data and thus have
159 * no understanding about the difference between replace and add, we do not ever increase
160 * the life of this in createObject().
163 if (present.useCount == 0) {
164 final IgpNodeAttributes attrs = read(trans, present.attrId);
166 present.useCount = attrs.getPrefix().size();
167 if (present.useCount == 0) {
168 trans.delete(LogicalDatastoreType.OPERATIONAL, nodeInstanceId(ni));
169 this.nodes.remove(ni);