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