Bug-6562: Support add-path in base BGP NLRI
[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.rev171207.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 /**
53  *
54  */
55 abstract class AbstractReachabilityTopologyBuilder<T extends Route> extends AbstractTopologyBuilder<T> {
56     private static final Logger LOG = LoggerFactory.getLogger(AbstractReachabilityTopologyBuilder.class);
57     private final Map<NodeId, NodeUsage> nodes = new HashMap<>();
58
59     private static final class NodeUsage {
60         private final InstanceIdentifier<IgpNodeAttributes> attrId;
61         private int useCount = 1;
62
63         NodeUsage(final InstanceIdentifier<IgpNodeAttributes> attrId) {
64             this.attrId = requireNonNull(attrId);
65         }
66     }
67
68     protected AbstractReachabilityTopologyBuilder(final DataBroker dataProvider, final RibReference locRibReference,
69             final TopologyId topologyId, final TopologyTypes topologyTypes, final Class<? extends AddressFamily> afi,
70             final Class<? extends SubsequentAddressFamily> safi) {
71         super(dataProvider, locRibReference, topologyId, topologyTypes, afi, safi);
72     }
73
74     private static NodeId advertizingNode(final Attributes attrs) {
75         final CNextHop nh = attrs.getCNextHop();
76         if (nh == null) {
77             LOG.warn("Next hop value is null");
78             return null;
79         } else if (nh instanceof Ipv4NextHopCase) {
80             final Ipv4NextHop ipv4 = ((Ipv4NextHopCase) nh).getIpv4NextHop();
81
82             return new NodeId(ipv4.getGlobal().getValue());
83         } else if (nh instanceof Ipv6NextHopCase) {
84             final Ipv6NextHop ipv6 = ((Ipv6NextHopCase) nh).getIpv6NextHop();
85
86             return new NodeId(ipv6.getGlobal().getValue());
87         } else {
88             LOG.warn("Unhandled next hop class {}", nh.getImplementedInterface());
89             return null;
90         }
91     }
92
93     private KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceId(final NodeId ni) {
94         return getInstanceIdentifier().child(Node.class, new NodeKey(ni));
95     }
96
97     private static <T extends DataObject> T read(final ReadTransaction t, final InstanceIdentifier<T> id) {
98         final Optional<T> o;
99         try {
100             o = t.read(LogicalDatastoreType.OPERATIONAL, id).get();
101         } catch (InterruptedException | ExecutionException e) {
102             LOG.warn("Failed to read {}, assuming non-existent", id, e);
103             return null;
104         }
105
106         return o.orNull();
107     }
108
109     private InstanceIdentifier<IgpNodeAttributes> ensureNodePresent(final ReadWriteTransaction trans, final NodeId ni) {
110         final NodeUsage present = this.nodes.get(ni);
111         if (present != null) {
112             return present.attrId;
113         }
114
115         final KeyedInstanceIdentifier<Node, NodeKey> nii = nodeInstanceId(ni);
116         final InstanceIdentifier<IgpNodeAttributes> ret = nii.builder().augmentation(Node1.class).child(IgpNodeAttributes.class).build();
117
118         trans.merge(LogicalDatastoreType.OPERATIONAL, nii, new NodeBuilder().setKey(nii.getKey()).setNodeId(ni)
119             .addAugmentation(Node1.class, new Node1Builder().setIgpNodeAttributes(
120                 new IgpNodeAttributesBuilder().setPrefix(Collections.emptyList()).build()).build()).build());
121
122         this.nodes.put(ni, new NodeUsage(ret));
123         return ret;
124     }
125
126     protected abstract Attributes getAttributes(final T value);
127
128     protected abstract IpPrefix getPrefix(final T value);
129
130     @Override
131     protected final void createObject(final ReadWriteTransaction trans, final InstanceIdentifier<T> id, final T value) {
132         final NodeId ni = advertizingNode(getAttributes(value));
133         if (ni == null) {
134             return;
135         }
136         final InstanceIdentifier<IgpNodeAttributes> nii = ensureNodePresent(trans, ni);
137
138         final IpPrefix prefix = getPrefix(value);
139         final PrefixKey pk = new PrefixKey(prefix);
140
141         trans.put(LogicalDatastoreType.OPERATIONAL,
142                 nii.child(Prefix.class, pk), new PrefixBuilder().setKey(pk).setPrefix(prefix).build());
143     }
144
145     @Override
146     protected final void removeObject(final ReadWriteTransaction trans, final InstanceIdentifier<T> id, final T value) {
147         if (value == null) {
148             LOG.error("Empty before-data received in delete data change notification for instance id {}", id);
149             return;
150         }
151
152         final NodeId ni = advertizingNode(getAttributes(value));
153         if (ni == null) {
154             return;
155         }
156         final NodeUsage present = this.nodes.get(ni);
157         Preconditions.checkState(present != null, "Removing prefix from non-existent node %s", present);
158
159         final PrefixKey pk = new PrefixKey(getPrefix(value));
160         trans.delete(LogicalDatastoreType.OPERATIONAL, present.attrId.child(Prefix.class, pk));
161
162         /*
163          * This is optimization magic: we are reading a list and we want to remove it once it
164          * hits zero. We may be in a transaction, so the read is costly, especially since we
165          * have just modified the list.
166          *
167          * Once we have performed the read, though, we can check the number of nodes, and reuse
168          * it for that number of removals. Note that since we do not track data and thus have
169          * no understanding about the difference between replace and add, we do not ever increase
170          * the life of this in createObject().
171          */
172         present.useCount--;
173         if (present.useCount == 0) {
174             final IgpNodeAttributes attrs = read(trans, present.attrId);
175             if (attrs != null) {
176                 present.useCount = attrs.getPrefix().size();
177                 if (present.useCount == 0) {
178                     trans.delete(LogicalDatastoreType.OPERATIONAL, nodeInstanceId(ni));
179                     this.nodes.remove(ni);
180                 }
181             }
182         }
183     }
184
185     @Override
186     protected void clearTopology() {
187         this.nodes.clear();
188     }
189 }