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