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