Migrate to DataTreeChangeListener
[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 com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import java.util.Collections;
13 import java.util.HashMap;
14 import java.util.Map;
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;
47
48 /**
49  *
50  */
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<>();
54
55     private static final class NodeUsage {
56         private final InstanceIdentifier<IgpNodeAttributes> attrId;
57         private int useCount = 1;
58
59         NodeUsage(final InstanceIdentifier<IgpNodeAttributes> attrId) {
60             this.attrId = Preconditions.checkNotNull(attrId);
61         }
62     }
63
64     protected AbstractReachabilityTopologyBuilder(final DataBroker dataProvider, final RibReference locRibReference,
65             final TopologyId topologyId) {
66         super(dataProvider, locRibReference, topologyId, new TopologyTypesBuilder().build());
67     }
68
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();
73
74             return new NodeId(ipv4.getGlobal().getValue());
75         } else if (nh instanceof Ipv6NextHopCase) {
76             final Ipv6NextHop ipv6 = ((Ipv6NextHopCase) nh).getIpv6NextHop();
77
78             return new NodeId(ipv6.getGlobal().getValue());
79         } else {
80             LOG.warn("Unhandled next hop class {}", nh.getImplementedInterface());
81             return null;
82         }
83     }
84
85     private KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceId(final NodeId ni) {
86         return getInstanceIdentifier().child(Node.class, new NodeKey(ni));
87     }
88
89     private static <T extends DataObject> T read(final ReadTransaction t, final InstanceIdentifier<T> id) {
90         final Optional<T> o;
91         try {
92             o = t.read(LogicalDatastoreType.OPERATIONAL, id).get();
93         } catch (InterruptedException | ExecutionException e) {
94             LOG.warn("Failed to read {}, assuming non-existent", id, e);
95             return null;
96         }
97
98         return o.orNull();
99     }
100
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;
105         }
106
107         final KeyedInstanceIdentifier<Node, NodeKey> nii = nodeInstanceId(ni);
108         final InstanceIdentifier<IgpNodeAttributes> ret = nii.builder().augmentation(Node1.class).child(IgpNodeAttributes.class).build();
109
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());
113
114         this.nodes.put(ni, new NodeUsage(ret));
115         return ret;
116     }
117
118     protected abstract Attributes getAttributes(final T value);
119
120     protected abstract IpPrefix getPrefix(final T value);
121
122     @Override
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);
126
127         final IpPrefix prefix = getPrefix(value);
128         final PrefixKey pk = new PrefixKey(prefix);
129
130         trans.put(LogicalDatastoreType.OPERATIONAL,
131                 nii.child(Prefix.class, pk), new PrefixBuilder().setKey(pk).setPrefix(prefix).build());
132     }
133
134     @Override
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);
139
140         final PrefixKey pk = new PrefixKey(getPrefix(value));
141         trans.delete(LogicalDatastoreType.OPERATIONAL, present.attrId.child(Prefix.class, pk));
142
143         /*
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.
147          *
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().
152          */
153         present.useCount--;
154         if (present.useCount == 0) {
155             final IgpNodeAttributes attrs = read(trans, present.attrId);
156             if (attrs != null) {
157                 present.useCount = attrs.getPrefix().size();
158                 if (present.useCount == 0) {
159                     trans.delete(LogicalDatastoreType.OPERATIONAL, nodeInstanceId(ni));
160                     this.nodes.remove(ni);
161                 }
162             }
163         }
164     }
165 }