Cleanup eclipse warnings
[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.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.AddressFamily;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.CNextHop;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv4NextHopCase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv6NextHopCase;
29 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;
30 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;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.TopologyTypes;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1Builder;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.IgpNodeAttributes;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.IgpNodeAttributesBuilder;
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.Prefix;
42 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;
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.PrefixKey;
44 import org.opendaylight.yangtools.yang.binding.DataObject;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 /**
51  *
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 = Preconditions.checkNotNull(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.getImplementedInterface());
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 t, final InstanceIdentifier<T> id) {
96         final Optional<T> o;
97         try {
98             o = t.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 o.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).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(final T value);
125
126     protected abstract IpPrefix getPrefix(final 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 }