Provide Add Path support for all AFI/SAFI
[bgpcep.git] / bgp / topology-provider / src / main / java / org / opendaylight / bgpcep / bgp / topology / provider / AbstractReachabilityTopologyBuilder.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.bgpcep.bgp.topology.provider;
9
10 import static java.util.Objects.requireNonNull;
11
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.Map;
17 import java.util.concurrent.ExecutionException;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.ReadTransaction;
20 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.protocol.bgp.rib.RibReference;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.Attributes;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.Route;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.CNextHop;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv4NextHopCase;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv6NextHopCase;
31 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;
32 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;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.TopologyTypes;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1Builder;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.IgpNodeAttributes;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.IgpNodeAttributesBuilder;
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.Prefix;
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.PrefixBuilder;
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.PrefixKey;
46 import org.opendaylight.yangtools.yang.binding.DataObject;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 abstract class AbstractReachabilityTopologyBuilder<T extends Route> extends AbstractTopologyBuilder<T> {
53     private static final Logger LOG = LoggerFactory.getLogger(AbstractReachabilityTopologyBuilder.class);
54     private final Map<NodeId, NodeUsage> nodes = new HashMap<>();
55
56     private static final class NodeUsage {
57         private final InstanceIdentifier<IgpNodeAttributes> attrId;
58         private int useCount = 1;
59
60         NodeUsage(final InstanceIdentifier<IgpNodeAttributes> attrId) {
61             this.attrId = requireNonNull(attrId);
62         }
63     }
64
65     protected AbstractReachabilityTopologyBuilder(final DataBroker dataProvider, final RibReference locRibReference,
66             final TopologyId topologyId, final TopologyTypes topologyTypes, final Class<? extends AddressFamily> afi,
67             final Class<? extends SubsequentAddressFamily> safi) {
68         super(dataProvider, locRibReference, topologyId, topologyTypes, afi, safi);
69     }
70
71     private static NodeId advertizingNode(final Attributes attrs) {
72         final CNextHop nh = attrs.getCNextHop();
73         if (nh == null) {
74             LOG.warn("Next hop value is null");
75             return null;
76         } else if (nh instanceof Ipv4NextHopCase) {
77             final Ipv4NextHop ipv4 = ((Ipv4NextHopCase) nh).getIpv4NextHop();
78
79             return new NodeId(ipv4.getGlobal().getValue());
80         } else if (nh instanceof Ipv6NextHopCase) {
81             final Ipv6NextHop ipv6 = ((Ipv6NextHopCase) nh).getIpv6NextHop();
82
83             return new NodeId(ipv6.getGlobal().getValue());
84         } else {
85             LOG.warn("Unhandled next hop class {}", nh.getImplementedInterface());
86             return null;
87         }
88     }
89
90     private KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceId(final NodeId ni) {
91         return getInstanceIdentifier().child(Node.class, new NodeKey(ni));
92     }
93
94     private static <T extends DataObject> T read(final ReadTransaction rt, final InstanceIdentifier<T> id) {
95         final Optional<T> optional;
96         try {
97             optional = rt.read(LogicalDatastoreType.OPERATIONAL, id).get();
98         } catch (InterruptedException | ExecutionException e) {
99             LOG.warn("Failed to read {}, assuming non-existent", id, e);
100             return null;
101         }
102
103         return optional.orNull();
104     }
105
106     private InstanceIdentifier<IgpNodeAttributes> ensureNodePresent(final ReadWriteTransaction trans, final NodeId ni) {
107         final NodeUsage present = this.nodes.get(ni);
108         if (present != null) {
109             return present.attrId;
110         }
111
112         final KeyedInstanceIdentifier<Node, NodeKey> nii = nodeInstanceId(ni);
113         final InstanceIdentifier<IgpNodeAttributes> ret = nii.builder().augmentation(Node1.class)
114                 .child(IgpNodeAttributes.class).build();
115
116         trans.merge(LogicalDatastoreType.OPERATIONAL, nii, new NodeBuilder().setKey(nii.getKey()).setNodeId(ni)
117             .addAugmentation(Node1.class, new Node1Builder().setIgpNodeAttributes(
118                 new IgpNodeAttributesBuilder().setPrefix(Collections.emptyList()).build()).build()).build());
119
120         this.nodes.put(ni, new NodeUsage(ret));
121         return ret;
122     }
123
124     protected abstract Attributes getAttributes(T value);
125
126     protected abstract IpPrefix getPrefix(T value);
127
128     @Override
129     protected final void createObject(final ReadWriteTransaction trans, final InstanceIdentifier<T> id, final T value) {
130         final NodeId ni = advertizingNode(getAttributes(value));
131         if (ni == null) {
132             return;
133         }
134         final InstanceIdentifier<IgpNodeAttributes> nii = ensureNodePresent(trans, ni);
135
136         final IpPrefix prefix = getPrefix(value);
137         final PrefixKey pk = new PrefixKey(prefix);
138
139         trans.put(LogicalDatastoreType.OPERATIONAL,
140                 nii.child(Prefix.class, pk), new PrefixBuilder().setKey(pk).setPrefix(prefix).build());
141     }
142
143     @Override
144     protected final void removeObject(final ReadWriteTransaction trans, final InstanceIdentifier<T> id, final T value) {
145         if (value == null) {
146             LOG.error("Empty before-data received in delete data change notification for instance id {}", id);
147             return;
148         }
149
150         final NodeId ni = advertizingNode(getAttributes(value));
151         if (ni == null) {
152             return;
153         }
154         final NodeUsage present = this.nodes.get(ni);
155         Preconditions.checkState(present != null, "Removing prefix from non-existent node %s", present);
156
157         final PrefixKey pk = new PrefixKey(getPrefix(value));
158         trans.delete(LogicalDatastoreType.OPERATIONAL, present.attrId.child(Prefix.class, pk));
159
160         /*
161          * This is optimization magic: we are reading a list and we want to remove it once it
162          * hits zero. We may be in a transaction, so the read is costly, especially since we
163          * have just modified the list.
164          *
165          * Once we have performed the read, though, we can check the number of nodes, and reuse
166          * it for that number of removals. Note that since we do not track data and thus have
167          * no understanding about the difference between replace and add, we do not ever increase
168          * the life of this in createObject().
169          */
170         present.useCount--;
171         if (present.useCount == 0) {
172             final IgpNodeAttributes attrs = read(trans, present.attrId);
173             if (attrs != null) {
174                 present.useCount = attrs.getPrefix().size();
175                 if (present.useCount == 0) {
176                     trans.delete(LogicalDatastoreType.OPERATIONAL, nodeInstanceId(ni));
177                     this.nodes.remove(ni);
178                 }
179             }
180         }
181     }
182
183     @Override
184     protected void clearTopology() {
185         this.nodes.clear();
186     }
187 }