Bump MDSAL to 4.0.0
[bgpcep.git] / bgp / topology-provider / src / main / java / org / opendaylight / bgpcep / bgp / topology / provider / LinkstateTopologyBuilder.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.annotations.VisibleForTesting;
13 import com.google.common.base.Preconditions;
14 import com.google.common.collect.Collections2;
15 import com.google.common.collect.Lists;
16 import java.util.ArrayList;
17 import java.util.HashMap;
18 import java.util.HashSet;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Set;
22 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
23 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
24 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
25 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
26 import org.opendaylight.protocol.bgp.rib.RibReference;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.DomainName;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.Ipv4InterfaceIdentifier;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.Ipv6InterfaceIdentifier;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.LinkstateAddressFamily;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.LinkstateSubsequentAddressFamily;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.TopologyIdentifier;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.bgp.rib.rib.loc.rib.tables.routes.LinkstateRoutesCase;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.ObjectType;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.object.type.LinkCase;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.object.type.NodeCase;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.object.type.PrefixCase;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.object.type.link._case.LinkDescriptors;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.LinkStateAttribute;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.link.state.attribute.LinkAttributesCase;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.link.state.attribute.NodeAttributesCase;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.link.state.attribute.PrefixAttributesCase;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.link.state.attribute.link.attributes._case.LinkAttributes;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.link.state.attribute.node.attributes._case.NodeAttributes;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.link.state.attribute.prefix.attributes._case.PrefixAttributes;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.routes.LinkstateRoutes;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.routes.linkstate.routes.LinkstateRoute;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.routes.linkstate.routes.linkstate.route.Attributes1;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.Tables;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp.topology.types.rev160524.TopologyTypes1;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp.topology.types.rev160524.TopologyTypes1Builder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp.topology.types.rev160524.bgp.linkstate.topology.type.BgpLinkstateTopologyBuilder;
56 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.LinkId;
57 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
58 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
59 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
60 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.DestinationBuilder;
61 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.SourceBuilder;
62 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
63 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkBuilder;
64 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkKey;
65 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
66 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
67 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
68 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.TopologyTypes;
69 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.TopologyTypesBuilder;
70 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
71 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
72 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
73 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Link1;
74 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Link1Builder;
75 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1;
76 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1Builder;
77 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TerminationPoint1;
78 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TerminationPoint1Builder;
79 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.link.attributes.IgpLinkAttributesBuilder;
80 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.IgpNodeAttributes;
81 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.IgpNodeAttributesBuilder;
82 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;
83 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;
84 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;
85 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.termination.point.attributes.IgpTerminationPointAttributesBuilder;
86 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.termination.point.attributes.igp.termination.point.attributes.TerminationPointType;
87 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.termination.point.attributes.igp.termination.point.attributes.termination.point.type.IpBuilder;
88 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.termination.point.attributes.igp.termination.point.attributes.termination.point.type.UnnumberedBuilder;
89 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
90 import org.slf4j.Logger;
91 import org.slf4j.LoggerFactory;
92
93 public class LinkstateTopologyBuilder extends AbstractTopologyBuilder<LinkstateRoute> {
94     private static final TopologyTypes LINKSTATE_TOPOLOGY_TYPE = new TopologyTypesBuilder()
95             .addAugmentation(TopologyTypes1.class, new TopologyTypes1Builder()
96                     .setBgpLinkstateTopology(new BgpLinkstateTopologyBuilder().build()).build()).build();
97
98     private static final String UNHANDLED_OBJECT_CLASS = "Unhandled object class {}";
99
100     private static final class TpHolder {
101         private final Set<LinkId> local = new HashSet<>();
102         private final Set<LinkId> remote = new HashSet<>();
103
104         private final TerminationPoint tp;
105
106         private TpHolder(final TerminationPoint tp) {
107             this.tp = requireNonNull(tp);
108         }
109
110         private synchronized void addLink(final LinkId id, final boolean isRemote) {
111             if (isRemote) {
112                 this.remote.add(id);
113             } else {
114                 this.local.add(id);
115             }
116         }
117
118         private synchronized boolean removeLink(final LinkId id, final boolean isRemote) {
119             final boolean removed;
120             if (isRemote) {
121                 removed = this.remote.remove(id);
122             } else {
123                 removed = this.local.remove(id);
124             }
125             if (!removed) {
126                 LOG.warn("Removed non-reference link {} from TP {} isRemote {}", this.tp.getTpId(), id, isRemote);
127             }
128
129             return this.local.isEmpty() && this.remote.isEmpty();
130         }
131
132         private TerminationPoint getTp() {
133             return this.tp;
134         }
135     }
136
137     private final class NodeHolder {
138         private final Map<PrefixKey, Prefix> prefixes = new HashMap<>();
139         private final Map<TpId, TpHolder> tps = new HashMap<>();
140         private boolean advertized = false;
141         private IgpNodeAttributesBuilder inab;
142         private NodeBuilder nb;
143
144         private NodeHolder(final NodeId id) {
145             this.inab = new IgpNodeAttributesBuilder();
146             this.nb = new NodeBuilder().withKey(new NodeKey(id)).setNodeId(id);
147         }
148
149         /**
150          * Synchronized in-core state of a node into the backing store using the transaction.
151          *
152          * @param trans data modification transaction which to use
153          * @return True if the node has been purged, false otherwise.
154          */
155         private boolean syncState(final WriteTransaction trans) {
156             final InstanceIdentifier<Node> nid = getNodeInstanceIdentifier(this.nb.key());
157
158             /*
159              * Transaction's putOperationalData() does a merge. Force it onto a replace
160              * by removing the data. If we decide to remove the node -- we just skip the put.
161              */
162             trans.delete(LogicalDatastoreType.OPERATIONAL, nid);
163
164             if (!this.advertized) {
165                 if (this.tps.isEmpty() && this.prefixes.isEmpty()) {
166                     LOG.trace("Removing unadvertized unused node {}", this.nb.getNodeId());
167                     return true;
168                 }
169
170                 LOG.trace("Node {} is still implied by {} TPs and {} prefixes", this.nb.getNodeId(), this.tps.size(),
171                         this.prefixes.size());
172             }
173
174             // Re-generate termination points
175             this.nb.setTerminationPoint(Lists.newArrayList(Collections2.transform(this.tps.values(), TpHolder::getTp)));
176
177             // Re-generate prefixes
178             this.inab.setPrefix(Lists.newArrayList(this.prefixes.values()));
179
180             // Write the node out
181             final Node n = this.nb.addAugmentation(Node1.class, new Node1Builder()
182                     .setIgpNodeAttributes(this.inab.build()).build()).build();
183             trans.put(LogicalDatastoreType.OPERATIONAL, nid, n);
184             LOG.trace("Created node {} at {}", n, nid);
185             return false;
186         }
187
188         private boolean checkForRemoval(final WriteTransaction trans) {
189             final InstanceIdentifier<Node> nid = getNodeInstanceIdentifier(this.nb.key());
190
191             if (!this.advertized) {
192                 if (this.tps.isEmpty() && this.prefixes.isEmpty()) {
193                     trans.delete(LogicalDatastoreType.OPERATIONAL, nid);
194                     LOG.trace("Removing unadvertized unused node {}", this.nb.getNodeId());
195                     return true;
196                 }
197
198                 LOG.trace("Node {} is still implied by {} TPs and {} prefixes", this.nb.getNodeId(),
199                         this.tps.size(), this.prefixes.size());
200             }
201             return false;
202         }
203
204         private synchronized void removeTp(final TpId tp, final LinkId link, final boolean isRemote) {
205             final TpHolder h = this.tps.get(tp);
206             if (h != null) {
207                 if (h.removeLink(link, isRemote)) {
208                     this.tps.remove(tp);
209                     LOG.trace("Removed TP {}", tp);
210                 }
211             } else {
212                 LOG.warn("Removed non-present TP {} by link {}", tp, link);
213             }
214         }
215
216         private void addTp(final TerminationPoint tp, final LinkId link, final boolean isRemote) {
217             final TpHolder h = this.tps.computeIfAbsent(tp.getTpId(), k -> new TpHolder(tp));
218             h.addLink(link, isRemote);
219         }
220
221         private void addPrefix(final Prefix pfx) {
222             this.prefixes.put(pfx.key(), pfx);
223         }
224
225         private void removePrefix(final PrefixCase prefixCase) {
226             this.prefixes.remove(new PrefixKey(prefixCase.getPrefixDescriptors().getIpReachabilityInformation()));
227         }
228
229         private void unadvertized() {
230             this.inab = new IgpNodeAttributesBuilder();
231             this.nb = new NodeBuilder().withKey(this.nb.key()).setNodeId(this.nb.getNodeId());
232             this.advertized = false;
233             LOG.debug("Node {} is unadvertized", this.nb.getNodeId());
234         }
235
236         private void advertized(final NodeBuilder nodeBuilder, final IgpNodeAttributesBuilder igpNodeAttBuilder) {
237             this.nb = requireNonNull(nodeBuilder);
238             this.inab = requireNonNull(igpNodeAttBuilder);
239             this.advertized = true;
240             LOG.debug("Node {} is advertized", nodeBuilder.getNodeId());
241         }
242
243         private NodeId getNodeId() {
244             return this.nb.getNodeId();
245         }
246     }
247
248     private static final Logger LOG = LoggerFactory.getLogger(LinkstateTopologyBuilder.class);
249     private final Map<NodeId, NodeHolder> nodes = new HashMap<>();
250
251     public LinkstateTopologyBuilder(final DataBroker dataProvider, final RibReference locRibReference,
252             final TopologyId topologyId) {
253         super(dataProvider, locRibReference, topologyId, LINKSTATE_TOPOLOGY_TYPE, LinkstateAddressFamily.class,
254                 LinkstateSubsequentAddressFamily.class);
255     }
256
257     @VisibleForTesting
258     LinkstateTopologyBuilder(final DataBroker dataProvider, final RibReference locRibReference,
259             final TopologyId topologyId, final long listenerResetLimitInMillsec,
260             final int listenerResetEnforceCounter) {
261         super(dataProvider, locRibReference, topologyId, LINKSTATE_TOPOLOGY_TYPE, LinkstateAddressFamily.class,
262                 LinkstateSubsequentAddressFamily.class,
263                 listenerResetLimitInMillsec, listenerResetEnforceCounter);
264     }
265
266     private static LinkId buildLinkId(final UriBuilder base, final LinkCase link) {
267         return new LinkId(new UriBuilder(base, "link").add(link).toString());
268     }
269
270     private static NodeId buildNodeId(final UriBuilder base, final org.opendaylight.yang.gen.v1.urn.opendaylight
271             .params.xml.ns.yang.bgp.linkstate.rev180329.NodeIdentifier node) {
272         return new NodeId(new UriBuilder(base, "node").addPrefix("", node).toString());
273     }
274
275     private static TpId buildTpId(final UriBuilder base, final TopologyIdentifier topologyIdentifier,
276             final Ipv4InterfaceIdentifier ipv4InterfaceIdentifier,
277             final Ipv6InterfaceIdentifier ipv6InterfaceIdentifier, final Long id) {
278         final UriBuilder b = new UriBuilder(base, "tp");
279         if (topologyIdentifier != null) {
280             b.add("mt", topologyIdentifier.getValue());
281         }
282         if (ipv4InterfaceIdentifier != null) {
283             b.add("ipv4", ipv4InterfaceIdentifier.getValue());
284         }
285         if (ipv6InterfaceIdentifier != null) {
286             b.add("ipv6", ipv6InterfaceIdentifier.getValue());
287         }
288
289         return new TpId(b.add("id", id).toString());
290     }
291
292     private static TpId buildLocalTpId(final UriBuilder base, final LinkDescriptors linkDescriptors) {
293         return buildTpId(base, linkDescriptors.getMultiTopologyId(), linkDescriptors.getIpv4InterfaceAddress(),
294                 linkDescriptors.getIpv6InterfaceAddress(), linkDescriptors.getLinkLocalIdentifier());
295     }
296
297     private static TerminationPoint buildTp(final TpId id, final TerminationPointType type) {
298         final TerminationPointBuilder stpb = new TerminationPointBuilder();
299         stpb.withKey(new TerminationPointKey(id));
300         stpb.setTpId(id);
301
302         if (type != null) {
303             stpb.addAugmentation(TerminationPoint1.class, new TerminationPoint1Builder()
304                     .setIgpTerminationPointAttributes(
305                     new IgpTerminationPointAttributesBuilder().setTerminationPointType(type).build()).build());
306         }
307
308         return stpb.build();
309     }
310
311     private static TerminationPointType getTpType(final Ipv4InterfaceIdentifier ipv4InterfaceIdentifier,
312             final Ipv6InterfaceIdentifier ipv6InterfaceIdentifier, final Long id) {
313         // Order of preference: Unnumbered first, then IP
314         if (id != null) {
315             LOG.debug("Unnumbered termination point type: {}", id);
316             return new UnnumberedBuilder().setUnnumberedId(id).build();
317         }
318
319         final IpAddress ip;
320         if (ipv6InterfaceIdentifier != null) {
321             ip = new IpAddress(ipv6InterfaceIdentifier);
322         } else if (ipv4InterfaceIdentifier != null) {
323             ip = new IpAddress(ipv4InterfaceIdentifier);
324         } else {
325             ip = null;
326         }
327
328         if (ip != null) {
329             LOG.debug("IP termination point type: {}", ip);
330             return new IpBuilder().setIpAddress(Lists.newArrayList(ip)).build();
331         }
332
333         return null;
334     }
335
336     private static TerminationPoint buildLocalTp(final UriBuilder base, final LinkDescriptors linkDescriptors) {
337         final TpId id = buildLocalTpId(base, linkDescriptors);
338         final TerminationPointType t = getTpType(linkDescriptors.getIpv4InterfaceAddress(),
339                 linkDescriptors.getIpv6InterfaceAddress(),
340                 linkDescriptors.getLinkLocalIdentifier());
341
342         return buildTp(id, t);
343     }
344
345     private static TpId buildRemoteTpId(final UriBuilder base, final LinkDescriptors linkDescriptors) {
346         return buildTpId(base, linkDescriptors.getMultiTopologyId(), linkDescriptors.getIpv4NeighborAddress(),
347                 linkDescriptors.getIpv6NeighborAddress(), linkDescriptors.getLinkRemoteIdentifier());
348     }
349
350     private static TerminationPoint buildRemoteTp(final UriBuilder base, final LinkDescriptors linkDescriptors) {
351         final TpId id = buildRemoteTpId(base, linkDescriptors);
352         final TerminationPointType t = getTpType(linkDescriptors.getIpv4NeighborAddress(),
353                 linkDescriptors.getIpv6NeighborAddress(),
354                 linkDescriptors.getLinkRemoteIdentifier());
355
356         return buildTp(id, t);
357     }
358
359     private InstanceIdentifier<Link> buildLinkIdentifier(final LinkId id) {
360         return getInstanceIdentifier().child(
361                 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology
362                         .topology.Link.class, new LinkKey(id));
363     }
364
365     private NodeHolder getNode(final NodeId id) {
366         if (this.nodes.containsKey(id)) {
367             LOG.debug("Node {} is already present", id);
368             return this.nodes.get(id);
369         }
370
371         final NodeHolder ret = new NodeHolder(id);
372         this.nodes.put(id, ret);
373         return ret;
374     }
375
376     private void putNode(final WriteTransaction trans, final NodeHolder holder) {
377         if (holder.syncState(trans)) {
378             this.nodes.remove(holder.getNodeId());
379         }
380     }
381
382     private void checkNodeForRemoval(final WriteTransaction trans, final NodeHolder holder) {
383         if (holder.checkForRemoval(trans)) {
384             this.nodes.remove(holder.getNodeId());
385         }
386     }
387
388     private void createLink(final WriteTransaction trans, final UriBuilder base,
389             final LinkstateRoute value, final LinkCase linkCase, final Attributes attributes) {
390         // defensive lookup
391         final LinkAttributes la;
392         final Attributes1 attr = attributes.augmentation(Attributes1.class);
393         if (attr != null) {
394             final LinkStateAttribute attrType = attr.getLinkStateAttribute();
395             if (attrType != null) {
396                 la = ((LinkAttributesCase)attrType).getLinkAttributes();
397             } else {
398                 LOG.debug("Missing attribute type in link {} route {}, skipping it", linkCase, value);
399                 la = null;
400             }
401         } else {
402             LOG.debug("Missing attributes in link {} route {}, skipping it", linkCase, value);
403             la = null;
404         }
405
406         final IgpLinkAttributesBuilder ilab = new IgpLinkAttributesBuilder();
407         if (la != null) {
408             if (la.getMetric() != null) {
409                 ilab.setMetric(la.getMetric().getValue());
410             }
411             ilab.setName(la.getLinkName());
412         }
413         ProtocolUtil.augmentProtocolId(value, ilab, la, linkCase.getLinkDescriptors());
414
415         final LinkBuilder lb = new LinkBuilder();
416         lb.setLinkId(buildLinkId(base, linkCase));
417         lb.addAugmentation(Link1.class, new Link1Builder().setIgpLinkAttributes(ilab.build()).build());
418
419         final NodeId srcNode = buildNodeId(base, linkCase.getLocalNodeDescriptors());
420         LOG.trace("Link {} implies source node {}", linkCase, srcNode);
421
422         final NodeId dstNode = buildNodeId(base, linkCase.getRemoteNodeDescriptors());
423         LOG.trace("Link {} implies destination node {}", linkCase, dstNode);
424
425         final TerminationPoint srcTp = buildLocalTp(base, linkCase.getLinkDescriptors());
426         LOG.trace("Link {} implies source TP {}", linkCase, srcTp);
427
428         final TerminationPoint dstTp = buildRemoteTp(base, linkCase.getLinkDescriptors());
429         LOG.trace("Link {} implies destination TP {}", linkCase, dstTp);
430
431         lb.setSource(new SourceBuilder().setSourceNode(srcNode).setSourceTp(srcTp.getTpId()).build());
432         lb.setDestination(new DestinationBuilder().setDestNode(dstNode).setDestTp(dstTp.getTpId()).build());
433
434         LOG.trace("Created TP {} as link source", srcTp);
435         NodeHolder snh = this.nodes.get(srcNode);
436         if (snh == null) {
437             snh = getNode(srcNode);
438             snh.addTp(srcTp, lb.getLinkId(), false);
439             putNode(trans, snh);
440         } else {
441             snh.addTp(srcTp, lb.getLinkId(), false);
442             final InstanceIdentifier<Node> nid = getNodeInstanceIdentifier(new NodeKey(snh.getNodeId()));
443             trans.put(LogicalDatastoreType.OPERATIONAL, nid.child(TerminationPoint.class, srcTp.key()), srcTp);
444         }
445
446         LOG.debug("Created TP {} as link destination", dstTp);
447         NodeHolder dnh = this.nodes.get(dstNode);
448         if (dnh == null) {
449             dnh = getNode(dstNode);
450             dnh.addTp(dstTp, lb.getLinkId(), true);
451             putNode(trans, dnh);
452         } else {
453             dnh.addTp(dstTp, lb.getLinkId(), true);
454             final InstanceIdentifier<Node> nid = getInstanceIdentifier().child(
455                     org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network
456                             .topology.topology.Node.class, new NodeKey(dnh.getNodeId()));
457             trans.put(LogicalDatastoreType.OPERATIONAL, nid.child(TerminationPoint.class, dstTp.key()), dstTp);
458         }
459
460         final InstanceIdentifier<Link> lid = buildLinkIdentifier(lb.getLinkId());
461         final Link link = lb.build();
462
463         trans.put(LogicalDatastoreType.OPERATIONAL, lid, link);
464         LOG.debug("Created link {} at {} for {}", link, lid, linkCase);
465     }
466
467     private void removeTp(final WriteTransaction trans, final NodeId node, final TpId tp,
468             final LinkId link, final boolean isRemote) {
469         final NodeHolder nh = this.nodes.get(node);
470         if (nh != null) {
471             final InstanceIdentifier<Node> nid = getNodeInstanceIdentifier(new NodeKey(nh.getNodeId()));
472             trans.delete(LogicalDatastoreType.OPERATIONAL, nid.child(TerminationPoint.class,
473                     new TerminationPointKey(tp)));
474             nh.removeTp(tp, link, isRemote);
475             checkNodeForRemoval(trans, nh);
476         } else {
477             LOG.warn("Removed non-existent node {}", node);
478         }
479     }
480
481     private void removeLink(final WriteTransaction trans, final UriBuilder base, final LinkCase linkCase) {
482         final LinkId id = buildLinkId(base, linkCase);
483         final InstanceIdentifier<?> lid = buildLinkIdentifier(id);
484         trans.delete(LogicalDatastoreType.OPERATIONAL, lid);
485         LOG.debug("Removed link {}", lid);
486
487         removeTp(trans, buildNodeId(base, linkCase.getLocalNodeDescriptors()),
488                 buildLocalTpId(base, linkCase.getLinkDescriptors()), id, false);
489         removeTp(trans, buildNodeId(base, linkCase.getRemoteNodeDescriptors()),
490                 buildRemoteTpId(base, linkCase.getLinkDescriptors()), id, true);
491     }
492
493     private void createNode(final WriteTransaction trans, final UriBuilder base,
494             final LinkstateRoute value, final NodeCase nodeCase, final Attributes attributes) {
495         final NodeAttributes na;
496         //defensive lookup
497         final Attributes1 attr = attributes.augmentation(Attributes1.class);
498         if (attr != null) {
499             final LinkStateAttribute attrType = attr.getLinkStateAttribute();
500             if (attrType != null) {
501                 na = ((NodeAttributesCase)attrType).getNodeAttributes();
502             } else {
503                 LOG.debug("Missing attribute type in node {} route {}, skipping it", nodeCase, value);
504                 na = null;
505             }
506         } else {
507             LOG.debug("Missing attributes in node {} route {}, skipping it", nodeCase, value);
508             na = null;
509         }
510         final IgpNodeAttributesBuilder inab = new IgpNodeAttributesBuilder();
511         final List<IpAddress> ids = new ArrayList<>();
512         if (na != null) {
513             if (na.getIpv4RouterId() != null) {
514                 ids.add(new IpAddress(na.getIpv4RouterId()));
515             }
516             if (na.getIpv6RouterId() != null) {
517                 ids.add(new IpAddress(na.getIpv6RouterId()));
518             }
519             if (na.getDynamicHostname() != null) {
520                 inab.setName(new DomainName(na.getDynamicHostname()));
521             }
522         }
523         if (!ids.isEmpty()) {
524             inab.setRouterId(ids);
525         }
526         ProtocolUtil.augmentProtocolId(value, inab, na, nodeCase.getNodeDescriptors());
527
528         final NodeId nid = buildNodeId(base, nodeCase.getNodeDescriptors());
529         final NodeHolder nh = getNode(nid);
530         /*
531          *  Eventhough the the holder creates a dummy structure, we need to duplicate it here,
532          *  as that is the API requirement. The reason for it is the possible presence of supporting
533          *  node -- something which the holder does not track.
534          */
535         final NodeBuilder nb = new NodeBuilder();
536         nb.setNodeId(nid);
537         nb.withKey(new NodeKey(nb.getNodeId()));
538
539         nh.advertized(nb, inab);
540         putNode(trans, nh);
541     }
542
543     private void removeNode(final WriteTransaction trans, final UriBuilder base, final NodeCase nodeCase) {
544         final NodeId id = buildNodeId(base, nodeCase.getNodeDescriptors());
545         final NodeHolder nh = this.nodes.get(id);
546         if (nh != null) {
547             nh.unadvertized();
548             putNode(trans, nh);
549         } else {
550             LOG.warn("Node {} does not have a holder", id);
551         }
552     }
553
554     private void createPrefix(final WriteTransaction trans, final UriBuilder base,
555             final LinkstateRoute value, final PrefixCase prefixCase, final Attributes attributes) {
556         final IpPrefix ippfx = prefixCase.getPrefixDescriptors().getIpReachabilityInformation();
557         if (ippfx == null) {
558             LOG.warn("IP reachability not present in prefix {} route {}, skipping it", prefixCase, value);
559             return;
560         }
561         final PrefixBuilder pb = new PrefixBuilder();
562         final PrefixKey pk = new PrefixKey(ippfx);
563         pb.withKey(pk);
564         pb.setPrefix(ippfx);
565
566         final PrefixAttributes pa;
567         // Very defensive lookup
568         final Attributes1 attr = attributes.augmentation(Attributes1.class);
569         if (attr != null) {
570             final LinkStateAttribute attrType = attr.getLinkStateAttribute();
571             if (attrType != null) {
572                 pa = ((PrefixAttributesCase)attrType).getPrefixAttributes();
573             } else {
574                 LOG.debug("Missing attribute type in IP {} prefix {} route {}, skipping it", ippfx, prefixCase, value);
575                 pa = null;
576             }
577         } else {
578             LOG.debug("Missing attributes in IP {} prefix {} route {}, skipping it", ippfx, prefixCase, value);
579             pa = null;
580         }
581         if (pa != null && pa.getPrefixMetric() != null) {
582             pb.setMetric(pa.getPrefixMetric().getValue());
583         }
584         ProtocolUtil.augmentProtocolId(value, pa, pb);
585
586         final Prefix pfx = pb.build();
587         LOG.debug("Created prefix {} for {}", pfx, prefixCase);
588
589         /*
590          * All set, but... the hosting node may not exist, we may need to fake it.
591          */
592         final NodeId node = buildNodeId(base, prefixCase.getAdvertisingNodeDescriptors());
593         NodeHolder nh = this.nodes.get(node);
594         if (nh == null) {
595             nh = getNode(node);
596             nh.addPrefix(pfx);
597             putNode(trans, nh);
598         } else {
599             nh.addPrefix(pfx);
600             final InstanceIdentifier<Node> nid = getNodeInstanceIdentifier(new NodeKey(nh.getNodeId()));
601             final InstanceIdentifier<IgpNodeAttributes> inaId = nid.builder().augmentation(Node1.class)
602                     .child(IgpNodeAttributes.class).build();
603             trans.put(LogicalDatastoreType.OPERATIONAL, inaId.child(Prefix.class, pk), pfx);
604         }
605     }
606
607     private void removePrefix(final WriteTransaction trans, final UriBuilder base, final PrefixCase prefixCase) {
608         final NodeId node = buildNodeId(base, prefixCase.getAdvertisingNodeDescriptors());
609         final NodeHolder nh = this.nodes.get(node);
610         if (nh != null) {
611             LOG.debug("Removed prefix {}", prefixCase);
612             final InstanceIdentifier<Node> nid = getNodeInstanceIdentifier(new NodeKey(nh.getNodeId()));
613             final InstanceIdentifier<IgpNodeAttributes> inaId = nid.builder().augmentation(Node1.class)
614                     .child(IgpNodeAttributes.class).build();
615             final IpPrefix ippfx = prefixCase.getPrefixDescriptors().getIpReachabilityInformation();
616             if (ippfx == null) {
617                 LOG.warn("IP reachability not present in prefix {}, skipping it", prefixCase);
618                 return;
619             }
620             final PrefixKey pk = new PrefixKey(ippfx);
621             trans.delete(LogicalDatastoreType.OPERATIONAL, inaId.child(Prefix.class, pk));
622             nh.removePrefix(prefixCase);
623             checkNodeForRemoval(trans, nh);
624         } else {
625             LOG.warn("Removing prefix from non-existing node {}", node);
626         }
627     }
628
629     private InstanceIdentifier<Node> getNodeInstanceIdentifier(final NodeKey nodeKey) {
630         return getInstanceIdentifier().child(
631                 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network
632                         .topology.topology.Node.class, nodeKey);
633     }
634
635     @Override
636     protected void createObject(final ReadWriteTransaction trans,
637             final InstanceIdentifier<LinkstateRoute> id, final LinkstateRoute value) {
638         final UriBuilder base = new UriBuilder(value);
639
640         final ObjectType t = value.getObjectType();
641         Preconditions.checkArgument(t != null, "Route %s value %s has null object type", id, value);
642
643         if (t instanceof LinkCase) {
644             createLink(trans, base, value, (LinkCase) t, value.getAttributes());
645         } else if (t instanceof NodeCase) {
646             createNode(trans, base, value, (NodeCase) t, value.getAttributes());
647         } else if (t instanceof PrefixCase) {
648             createPrefix(trans, base, value, (PrefixCase) t, value.getAttributes());
649         } else {
650             LOG.debug(UNHANDLED_OBJECT_CLASS, t.implementedInterface());
651         }
652     }
653
654     @Override
655     protected void removeObject(final ReadWriteTransaction trans,
656             final InstanceIdentifier<LinkstateRoute> id, final LinkstateRoute value) {
657         if (value == null) {
658             LOG.error("Empty before-data received in delete data change notification for instance id {}", id);
659             return;
660         }
661
662         final UriBuilder base = new UriBuilder(value);
663
664         final ObjectType t = value.getObjectType();
665         if (t instanceof LinkCase) {
666             removeLink(trans, base, (LinkCase) t);
667         } else if (t instanceof NodeCase) {
668             removeNode(trans, base, (NodeCase) t);
669         } else if (t instanceof PrefixCase) {
670             removePrefix(trans, base, (PrefixCase) t);
671         } else {
672             LOG.debug(UNHANDLED_OBJECT_CLASS, t.implementedInterface());
673         }
674     }
675
676     @Override
677     protected InstanceIdentifier<LinkstateRoute> getRouteWildcard(final InstanceIdentifier<Tables> tablesId) {
678         return tablesId.child(LinkstateRoutesCase.class, LinkstateRoutes.class).child(LinkstateRoute.class);
679     }
680
681     @Override
682     protected void clearTopology() {
683         this.nodes.clear();
684     }
685 }