cd6cd1cbf15b0aff03321cc7f38501fb706c2311
[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 com.google.common.base.Function;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Collections2;
13 import com.google.common.collect.Lists;
14 import java.math.BigDecimal;
15 import java.nio.ByteBuffer;
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.apache.commons.codec.binary.Hex;
23 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
24 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
25 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
26 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
27 import org.opendaylight.protocol.bgp.rib.RibReference;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.DomainName;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.Ipv4InterfaceIdentifier;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.Ipv6InterfaceIdentifier;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NodeFlagBits;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NodeIdentifier;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.TopologyIdentifier;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.ObjectType;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.LinkCase;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.NodeCase;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.PrefixCase;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.link._case.LinkDescriptors;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.linkstate.routes.LinkstateRoute;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.linkstate.routes.linkstate.route.Attributes1;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.linkstate.routes.linkstate.route.attributes.AttributeType;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.linkstate.routes.linkstate.route.attributes.attribute.type.link._case.LinkAttributes;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.linkstate.routes.linkstate.route.attributes.attribute.type.node._case.NodeAttributes;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.linkstate.routes.linkstate.route.attributes.attribute.type.prefix._case.PrefixAttributes;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.CRouterIdentifier;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.IsisNodeCase;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.IsisPseudonodeCase;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.OspfNodeCase;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.OspfPseudonodeCase;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.isis.node._case.IsisNode;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.isis.pseudonode._case.IsisPseudonode;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.ospf.pseudonode._case.OspfPseudonode;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.route.Attributes;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.SrlgId;
58 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IsoPseudonodeId;
59 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IsoSystemId;
60 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.link.attributes.IsisLinkAttributesBuilder;
61 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.node.attributes.IsisNodeAttributesBuilder;
62 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.node.attributes.isis.node.attributes.IsoBuilder;
63 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.srlg.attributes.SrlgValues;
64 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.srlg.attributes.SrlgValuesBuilder;
65 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.SrlgBuilder;
66 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.UnreservedBandwidth;
67 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.UnreservedBandwidthBuilder;
68 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.UnreservedBandwidthKey;
69 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.LinkId;
70 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
71 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
72 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
73 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.DestinationBuilder;
74 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.SourceBuilder;
75 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
76 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkBuilder;
77 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkKey;
78 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
79 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
80 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
81 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.TopologyTypesBuilder;
82 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
83 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
84 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
85 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Link1;
86 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Link1Builder;
87 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1;
88 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1Builder;
89 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TerminationPoint1;
90 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TerminationPoint1Builder;
91 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TopologyTypes1;
92 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TopologyTypes1Builder;
93 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.link.attributes.IgpLinkAttributesBuilder;
94 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.IgpNodeAttributesBuilder;
95 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;
96 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;
97 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;
98 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.termination.point.attributes.IgpTerminationPointAttributesBuilder;
99 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;
100 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;
101 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;
102 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.Prefix1;
103 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.Prefix1Builder;
104 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.link.attributes.OspfLinkAttributesBuilder;
105 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.OspfNodeAttributesBuilder;
106 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.ospf.node.attributes.router.type.AbrBuilder;
107 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.ospf.node.attributes.router.type.InternalBuilder;
108 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.ospf.node.attributes.router.type.PseudonodeBuilder;
109 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.prefix.attributes.OspfPrefixAttributesBuilder;
110 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
111 import org.slf4j.Logger;
112 import org.slf4j.LoggerFactory;
113
114 public final class LinkstateTopologyBuilder extends AbstractTopologyBuilder<LinkstateRoute> {
115     private static final class TpHolder {
116         private final Set<LinkId> local = new HashSet<>();
117         private final Set<LinkId> remote = new HashSet<>();
118
119         private final TerminationPoint tp;
120
121         private TpHolder(final TerminationPoint tp) {
122             this.tp = Preconditions.checkNotNull(tp);
123         }
124
125         private synchronized void addLink(final LinkId id, final boolean isRemote) {
126             if (isRemote) {
127                 this.remote.add(id);
128             } else {
129                 this.local.add(id);
130             }
131         }
132
133         private synchronized boolean removeLink(final LinkId id, final boolean isRemote) {
134             final boolean removed;
135             if (isRemote) {
136                 removed = this.remote.remove(id);
137             } else {
138                 removed = this.local.remove(id);
139             }
140             if (!removed) {
141                 LOG.warn("Removed non-reference link {} from TP {} isRemote {}", this.tp.getTpId(), id, isRemote);
142             }
143
144             return this.local.isEmpty() && this.remote.isEmpty();
145         }
146
147         private TerminationPoint getTp() {
148             return this.tp;
149         }
150     }
151
152     private final class NodeHolder {
153         private final Map<PrefixKey, Prefix> prefixes = new HashMap<>();
154         private final Map<TpId, TpHolder> tps = new HashMap<>();
155         private boolean advertized = false;
156         private IgpNodeAttributesBuilder inab;
157         private NodeBuilder nb;
158
159         private NodeHolder(final NodeId id) {
160             this.inab = new IgpNodeAttributesBuilder();
161             this.nb = new NodeBuilder().setKey(new NodeKey(id)).setNodeId(id);
162         }
163
164         /**
165          * Synchronized in-core state of a node into the backing store using the transaction
166          *
167          * @param trans data modification transaction which to use
168          * @return True if the node has been purged, false otherwise.
169          */
170         private boolean syncState(final WriteTransaction trans) {
171             final InstanceIdentifier<Node> nid = getInstanceIdentifier().child(
172                 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node.class,
173                 this.nb.getKey());
174
175             /*
176              * Transaction's putOperationalData() does a merge. Force it onto a replace
177              * by removing the data. If we decide to remove the node -- we just skip the put.
178              */
179             trans.delete(LogicalDatastoreType.OPERATIONAL, nid);
180
181             if (!this.advertized) {
182                 if (this.tps.isEmpty() && this.prefixes.isEmpty()) {
183                     LOG.debug("Removing unadvertized unused node {}", this.nb.getNodeId());
184                     return true;
185                 }
186
187                 LOG.debug("Node {} is still implied by {} TPs and {} prefixes", this.nb.getNodeId(), this.tps.size(), this.prefixes.size());
188             }
189
190             // Re-generate termination points
191             this.nb.setTerminationPoint(Lists.newArrayList(Collections2.transform(this.tps.values(), new Function<TpHolder, TerminationPoint>() {
192                 @Override
193                 public TerminationPoint apply(final TpHolder input) {
194                     return input.getTp();
195                 }
196             })));
197
198             // Re-generate prefixes
199             this.inab.setPrefix(Lists.newArrayList(this.prefixes.values()));
200
201             // Write the node out
202             final Node n = this.nb.addAugmentation(Node1.class, new Node1Builder().setIgpNodeAttributes(this.inab.build()).build()).build();
203             trans.put(LogicalDatastoreType.OPERATIONAL, nid, n);
204             LOG.debug("Created node {} at {}", n, nid);
205             return false;
206         }
207
208         private synchronized void removeTp(final TpId tp, final LinkId link, final boolean isRemote) {
209             final TpHolder h = this.tps.get(tp);
210             if (h != null) {
211                 if (h.removeLink(link, isRemote)) {
212                     this.tps.remove(tp);
213                     LOG.debug("Removed TP {}", tp);
214                 }
215             } else {
216                 LOG.warn("Removed non-present TP {} by link {}", tp, link);
217             }
218         }
219
220         private void addTp(final TerminationPoint tp, final LinkId link, final boolean isRemote) {
221             TpHolder h = this.tps.get(tp.getTpId());
222             if (h == null) {
223                 h = new TpHolder(tp);
224                 this.tps.put(tp.getTpId(), h);
225             }
226
227             h.addLink(link, isRemote);
228         }
229
230         private void addPrefix(final Prefix pfx) {
231             this.prefixes.put(pfx.getKey(), pfx);
232         }
233
234         private void removePrefix(final PrefixCase p) {
235             this.prefixes.remove(new PrefixKey(p.getPrefixDescriptors().getIpReachabilityInformation()));
236         }
237
238         private void unadvertized() {
239             this.inab = new IgpNodeAttributesBuilder();
240             this.nb = new NodeBuilder().setKey(this.nb.getKey()).setNodeId(this.nb.getNodeId());
241             this.advertized = false;
242             LOG.debug("Node {} is unadvertized", this.nb.getNodeId());
243         }
244
245         private void advertized(final NodeBuilder nb, final IgpNodeAttributesBuilder inab) {
246             this.nb = Preconditions.checkNotNull(nb);
247             this.inab = Preconditions.checkNotNull(inab);
248             this.advertized = true;
249             LOG.debug("Node {} is advertized", nb.getNodeId());
250         }
251
252         private Object getNodeId() {
253             return this.nb.getNodeId();
254         }
255     }
256
257     private static final Logger LOG = LoggerFactory.getLogger(LinkstateTopologyBuilder.class);
258     private final Map<NodeId, NodeHolder> nodes = new HashMap<>();
259
260     public LinkstateTopologyBuilder(final DataBroker dataProvider, final RibReference locRibReference, final TopologyId topologyId) {
261         super(dataProvider, locRibReference, topologyId, new TopologyTypesBuilder().addAugmentation(TopologyTypes1.class,
262             new TopologyTypes1Builder().build()).build(), LinkstateRoute.class);
263     }
264
265     private LinkId buildLinkId(final UriBuilder base, final LinkCase link) {
266         return new LinkId(new UriBuilder(base, "link").add(link).toString());
267     }
268
269     private NodeId buildNodeId(final UriBuilder base, final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NodeIdentifier node) {
270         return new NodeId(new UriBuilder(base, "node").add("", node).toString());
271     }
272
273     private TpId buildTpId(final UriBuilder base, final TopologyIdentifier topologyIdentifier,
274         final Ipv4InterfaceIdentifier ipv4InterfaceIdentifier, final Ipv6InterfaceIdentifier ipv6InterfaceIdentifier, final Long id) {
275         final UriBuilder b = new UriBuilder(base, "tp");
276         if (topologyIdentifier != null) {
277             b.add("mt", topologyIdentifier.getValue());
278         }
279         if (ipv4InterfaceIdentifier != null) {
280             b.add("ipv4", ipv4InterfaceIdentifier.getValue());
281         }
282         if (ipv6InterfaceIdentifier != null) {
283             b.add("ipv6", ipv6InterfaceIdentifier.getValue());
284         }
285
286         return new TpId(b.add("id", id).toString());
287     }
288
289     private TpId buildLocalTpId(final UriBuilder base, final LinkDescriptors linkDescriptors) {
290         return buildTpId(base, linkDescriptors.getMultiTopologyId(), linkDescriptors.getIpv4InterfaceAddress(),
291             linkDescriptors.getIpv6InterfaceAddress(), linkDescriptors.getLinkLocalIdentifier());
292     }
293
294     private TerminationPoint buildTp(final TpId id, final TerminationPointType type) {
295         final TerminationPointBuilder stpb = new TerminationPointBuilder();
296         stpb.setKey(new TerminationPointKey(id));
297         stpb.setTpId(id);
298
299         if (type != null) {
300             stpb.addAugmentation(TerminationPoint1.class, new TerminationPoint1Builder().setIgpTerminationPointAttributes(
301                 new IgpTerminationPointAttributesBuilder().setTerminationPointType(null).build()).build());
302         }
303
304         return stpb.build();
305     }
306
307     private TerminationPointType getTpType(final Ipv4InterfaceIdentifier ipv4InterfaceIdentifier,
308         final Ipv6InterfaceIdentifier ipv6InterfaceIdentifier, final Long id) {
309         // Order of preference: Unnumbered first, then IP
310         if (id != null) {
311             LOG.debug("Unnumbered termination point type: {}", id);
312             return new UnnumberedBuilder().setUnnumberedId(id).build();
313         }
314
315         final IpAddress ip;
316         if (ipv6InterfaceIdentifier != null) {
317             ip = new IpAddress(ipv6InterfaceIdentifier);
318         } else if (ipv4InterfaceIdentifier != null) {
319             ip = new IpAddress(ipv4InterfaceIdentifier);
320         } else {
321             ip = null;
322         }
323
324         if (ip != null) {
325             LOG.debug("IP termination point type: {}", ip);
326             return new IpBuilder().setIpAddress(Lists.newArrayList(ip)).build();
327         }
328
329         return null;
330     }
331
332     private TerminationPoint buildLocalTp(final UriBuilder base, final LinkDescriptors linkDescriptors) {
333         final TpId id = buildLocalTpId(base, linkDescriptors);
334         final TerminationPointType t = getTpType(linkDescriptors.getIpv4InterfaceAddress(), linkDescriptors.getIpv6InterfaceAddress(),
335             linkDescriptors.getLinkLocalIdentifier());
336
337         return buildTp(id, t);
338     }
339
340     private TpId buildRemoteTpId(final UriBuilder base, final LinkDescriptors linkDescriptors) {
341         return buildTpId(base, linkDescriptors.getMultiTopologyId(), linkDescriptors.getIpv4NeighborAddress(),
342             linkDescriptors.getIpv6NeighborAddress(), linkDescriptors.getLinkRemoteIdentifier());
343     }
344
345     private TerminationPoint buildRemoteTp(final UriBuilder base, final LinkDescriptors linkDescriptors) {
346         final TpId id = buildRemoteTpId(base, linkDescriptors);
347         final TerminationPointType t = getTpType(linkDescriptors.getIpv4NeighborAddress(), linkDescriptors.getIpv6NeighborAddress(),
348             linkDescriptors.getLinkRemoteIdentifier());
349
350         return buildTp(id, t);
351     }
352
353     private InstanceIdentifier<Link> buildLinkIdentifier(final LinkId id) {
354         return getInstanceIdentifier().child(
355             org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link.class,
356             new LinkKey(id));
357     }
358
359     private static Float bandwidthToFloat(final Bandwidth bandwidth) {
360         return ByteBuffer.wrap(bandwidth.getValue()).getFloat();
361     }
362
363     private static BigDecimal bandwidthToBigDecimal(final Bandwidth bandwidth) {
364         return BigDecimal.valueOf(bandwidthToFloat(bandwidth));
365     }
366
367     private static List<UnreservedBandwidth> unreservedBandwidthList(
368         final List<? extends org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.UnreservedBandwidth> input) {
369         final List<UnreservedBandwidth> ret = new ArrayList<>(input.size());
370
371         for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.UnreservedBandwidth i : input) {
372             ret.add(new UnreservedBandwidthBuilder().setBandwidth(bandwidthToBigDecimal(i.getBandwidth())).setKey(
373                 new UnreservedBandwidthKey(i.getPriority())).build());
374         }
375
376         return ret;
377     }
378
379     private org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpLinkAttributes1 isisLinkAttributes(
380         final TopologyIdentifier topologyIdentifier, final LinkAttributes la) {
381         final org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.link.attributes.isis.link.attributes.TedBuilder tb = new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.link.attributes.isis.link.attributes.TedBuilder();
382
383         if (la != null) {
384             if (la.getAdminGroup() != null) {
385                 tb.setColor(la.getAdminGroup().getValue());
386             }
387             if (la.getTeMetric() != null) {
388                 tb.setTeDefaultMetric(la.getTeMetric().getValue());
389             }
390             if (la.getUnreservedBandwidth() != null) {
391                 tb.setUnreservedBandwidth(unreservedBandwidthList(la.getUnreservedBandwidth()));
392             }
393             if (la.getMaxLinkBandwidth() != null) {
394                 tb.setMaxLinkBandwidth(bandwidthToBigDecimal(la.getMaxLinkBandwidth()));
395             }
396             if (la.getMaxReservableBandwidth() != null) {
397                 tb.setMaxResvLinkBandwidth(bandwidthToBigDecimal(la.getMaxReservableBandwidth()));
398             }
399             if (la.getSharedRiskLinkGroups() != null) {
400                 final List<SrlgValues> srlgs = new ArrayList<>();
401                 for (final SrlgId id : la.getSharedRiskLinkGroups()) {
402                     srlgs.add(new SrlgValuesBuilder().setSrlgValue(id.getValue()).build());
403                 }
404                 tb.setSrlg(new SrlgBuilder().setSrlgValues(srlgs).build());
405             }
406         }
407
408         final IsisLinkAttributesBuilder ilab = new IsisLinkAttributesBuilder();
409         ilab.setTed(tb.build());
410         if (topologyIdentifier != null) {
411             ilab.setMultiTopologyId(topologyIdentifier.getValue().shortValue());
412         }
413
414         return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpLinkAttributes1Builder().setIsisLinkAttributes(
415             ilab.build()).build();
416     }
417
418     private org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1 ospfLinkAttributes(
419         final TopologyIdentifier topologyIdentifier, final LinkAttributes la) {
420         final org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.link.attributes.ospf.link.attributes.TedBuilder tb = new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.link.attributes.ospf.link.attributes.TedBuilder();
421
422         if (la != null) {
423             if (la.getAdminGroup() != null) {
424                 tb.setColor(la.getAdminGroup().getValue());
425             }
426             if (la.getTeMetric() != null) {
427                 tb.setTeDefaultMetric(la.getTeMetric().getValue());
428             }
429             if (la.getUnreservedBandwidth() != null) {
430                 tb.setUnreservedBandwidth(unreservedBandwidthList(la.getUnreservedBandwidth()));
431             }
432             if (la.getMaxLinkBandwidth() != null) {
433                 tb.setMaxLinkBandwidth(bandwidthToBigDecimal(la.getMaxLinkBandwidth()));
434             }
435             if (la.getMaxReservableBandwidth() != null) {
436                 tb.setMaxResvLinkBandwidth(bandwidthToBigDecimal(la.getMaxReservableBandwidth()));
437             }
438             if (la.getSharedRiskLinkGroups() != null) {
439                 final List<SrlgValues> srlgs = new ArrayList<>();
440                 for (final SrlgId id : la.getSharedRiskLinkGroups()) {
441                     srlgs.add(new SrlgValuesBuilder().setSrlgValue(id.getValue()).build());
442                 }
443                 tb.setSrlg(new SrlgBuilder().setSrlgValues(srlgs).build());
444             }
445         }
446
447         final OspfLinkAttributesBuilder ilab = new OspfLinkAttributesBuilder();
448         ilab.setTed(tb.build());
449         if (topologyIdentifier != null) {
450             ilab.setMultiTopologyId(topologyIdentifier.getValue().shortValue());
451         }
452
453         return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1Builder().setOspfLinkAttributes(
454             ilab.build()).build();
455     }
456
457     private NodeHolder getNode(final NodeId id) {
458         if (this.nodes.containsKey(id)) {
459             LOG.debug("Node {} is already present", id);
460             return this.nodes.get(id);
461         }
462
463         final NodeHolder ret = new NodeHolder(id);
464         this.nodes.put(id, ret);
465         return ret;
466     }
467
468     private void putNode(final WriteTransaction trans, final NodeHolder holder) {
469         if (holder.syncState(trans)) {
470             this.nodes.remove(holder.getNodeId());
471         }
472     }
473
474     private void augmentProtocolId(final LinkstateRoute value, final IgpLinkAttributesBuilder ilab, final LinkAttributes la, final LinkDescriptors ld) {
475         switch (value.getProtocolId()) {
476         case Direct:
477         case Static:
478         case Unknown:
479             break;
480         case IsisLevel1:
481         case IsisLevel2:
482             ilab.addAugmentation(
483                 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpLinkAttributes1.class,
484                 isisLinkAttributes(ld.getMultiTopologyId(), la));
485             break;
486         case Ospf:
487             ilab.addAugmentation(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1.class,
488                 ospfLinkAttributes(ld.getMultiTopologyId(), la));
489             break;
490         default:
491             break;
492         }
493     }
494
495     private void createLink(final WriteTransaction trans, final UriBuilder base,
496         final LinkstateRoute value, final LinkCase l, final Attributes attributes) {
497         // defensive lookup
498         final LinkAttributes la;
499         final Attributes1 attr = attributes.getAugmentation(Attributes1.class);
500         if (attr != null) {
501             final AttributeType attrType = attr.getAttributeType();
502             if (attrType != null) {
503                 la = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.linkstate.routes.linkstate.route.attributes.attribute.type.LinkCase)
504                     attrType).getLinkAttributes();
505             } else {
506                 LOG.debug("Missing attribute type in link {} route {}, skipping it", l, value);
507                 la = null;
508             }
509         } else {
510             LOG.debug("Missing attributes in link {} route {}, skipping it", l, value);
511             la = null;
512         }
513
514         final IgpLinkAttributesBuilder ilab = new IgpLinkAttributesBuilder();
515         if (la != null) {
516             if (la.getMetric() != null) {
517                 ilab.setMetric(la.getMetric().getValue());
518             }
519             ilab.setName(la.getLinkName());
520         }
521         augmentProtocolId(value, ilab, la, l.getLinkDescriptors());
522
523         final LinkBuilder lb = new LinkBuilder();
524         lb.setLinkId(buildLinkId(base, l));
525         lb.addAugmentation(Link1.class, new Link1Builder().setIgpLinkAttributes(ilab.build()).build());
526
527         final NodeId srcNode = buildNodeId(base, l.getLocalNodeDescriptors());
528         LOG.debug("Link {} implies source node {}", l, srcNode);
529
530         final NodeId dstNode = buildNodeId(base, l.getRemoteNodeDescriptors());
531         LOG.debug("Link {} implies destination node {}", l, dstNode);
532
533         final TerminationPoint srcTp = buildLocalTp(base, l.getLinkDescriptors());
534         LOG.debug("Link {} implies source TP {}", l, srcTp);
535
536         final TerminationPoint dstTp = buildRemoteTp(base, l.getLinkDescriptors());
537         LOG.debug("Link {} implies destination TP {}", l, dstTp);
538
539         lb.setSource(new SourceBuilder().setSourceNode(srcNode).setSourceTp(srcTp.getTpId()).build());
540         lb.setDestination(new DestinationBuilder().setDestNode(dstNode).setDestTp(dstTp.getTpId()).build());
541
542         final NodeHolder snh = getNode(srcNode);
543         snh.addTp(srcTp, lb.getLinkId(), false);
544         LOG.debug("Created TP {} as link source", srcTp);
545         putNode(trans, snh);
546
547         final NodeHolder dnh = getNode(dstNode);
548         dnh.addTp(dstTp, lb.getLinkId(), true);
549         LOG.debug("Created TP {} as link destination", dstTp);
550         putNode(trans, dnh);
551
552         final InstanceIdentifier<Link> lid = buildLinkIdentifier(lb.getLinkId());
553         final Link link = lb.build();
554
555         trans.put(LogicalDatastoreType.OPERATIONAL, lid, link);
556         LOG.debug("Created link {} at {} for {}", link, lid, l);
557     }
558
559     private void removeTp(final WriteTransaction trans, final NodeId node, final TpId tp,
560         final LinkId link, final boolean isRemote) {
561         final NodeHolder nh = this.nodes.get(node);
562         if (nh != null) {
563             nh.removeTp(tp, link, isRemote);
564             putNode(trans, nh);
565         } else {
566             LOG.warn("Removed non-existent node {}", node);
567         }
568     }
569
570     private void removeLink(final WriteTransaction trans, final UriBuilder base, final LinkCase l) {
571         final LinkId id = buildLinkId(base, l);
572         final InstanceIdentifier<?> lid = buildLinkIdentifier(id);
573         trans.delete(LogicalDatastoreType.OPERATIONAL, lid);
574         LOG.debug("Removed link {}", lid);
575
576         removeTp(trans, buildNodeId(base, l.getLocalNodeDescriptors()), buildLocalTpId(base, l.getLinkDescriptors()), id, false);
577         removeTp(trans, buildNodeId(base, l.getRemoteNodeDescriptors()), buildRemoteTpId(base, l.getLinkDescriptors()), id, true);
578     }
579
580     private List<Short> nodeMultiTopology(final List<TopologyIdentifier> list) {
581         final List<Short> ret = new ArrayList<>(list.size());
582         for (final TopologyIdentifier id : list) {
583             ret.add(id.getValue().shortValue());
584         }
585         return ret;
586     }
587
588     private org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1 isisNodeAttributes(
589         final NodeIdentifier node, final NodeAttributes na) {
590         final org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.node.attributes.isis.node.attributes.TedBuilder tb = new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.node.attributes.isis.node.attributes.TedBuilder();
591         final IsisNodeAttributesBuilder ab = new IsisNodeAttributesBuilder();
592
593         if (na != null) {
594             if (na.getIpv4RouterId() != null) {
595                 tb.setTeRouterIdIpv4(na.getIpv4RouterId());
596             }
597             if (na.getIpv6RouterId() != null) {
598                 tb.setTeRouterIdIpv6(na.getIpv6RouterId());
599             }
600             if (na.getTopologyIdentifier() != null) {
601                 ab.setMultiTopologyId(nodeMultiTopology(na.getTopologyIdentifier()));
602             }
603         }
604
605         final CRouterIdentifier ri = node.getCRouterIdentifier();
606         if (ri instanceof IsisPseudonodeCase) {
607             final IsisPseudonode pn = ((IsisPseudonodeCase) ri).getIsisPseudonode();
608             final IsoBuilder b = new IsoBuilder();
609             b.setIsoSystemId(new IsoSystemId(UriBuilder.isoId(pn.getIsIsRouterIdentifier().getIsoSystemId())));
610             b.setIsoPseudonodeId(new IsoPseudonodeId(Hex.encodeHexString(new byte[] {pn.getPsn().byteValue()})));
611             ab.setIso(b.build());
612         } else if (ri instanceof IsisNodeCase) {
613             final IsisNode in = ((IsisNodeCase) ri).getIsisNode();
614             ab.setIso(new IsoBuilder().setIsoSystemId(new IsoSystemId(UriBuilder.isoId(in.getIsoSystemId()))).build());
615         }
616
617         ab.setTed(tb.build());
618
619         return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1Builder().setIsisNodeAttributes(
620             ab.build()).build();
621     }
622
623     private org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1 ospfNodeAttributes(
624         final NodeIdentifier node, final NodeAttributes na) {
625         final org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.ospf.node.attributes.TedBuilder tb = new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.ospf.node.attributes.TedBuilder();
626         final OspfNodeAttributesBuilder ab = new OspfNodeAttributesBuilder();
627         if (na != null) {
628             if (na.getIpv4RouterId() != null) {
629                 tb.setTeRouterIdIpv4(na.getIpv4RouterId());
630             }
631             if (na.getIpv6RouterId() != null) {
632                 tb.setTeRouterIdIpv6(na.getIpv6RouterId());
633             }
634             if (na.getTopologyIdentifier() != null) {
635                 ab.setMultiTopologyId(nodeMultiTopology(na.getTopologyIdentifier()));
636             }
637             final CRouterIdentifier ri = node.getCRouterIdentifier();
638             if (ri instanceof OspfPseudonodeCase) {
639                 final OspfPseudonode pn = ((OspfPseudonodeCase) ri).getOspfPseudonode();
640
641                 ab.setRouterType(new PseudonodeBuilder().setPseudonode(Boolean.TRUE).build());
642                 ab.setDrInterfaceId(pn.getLanInterface().getValue());
643             } else if (ri instanceof OspfNodeCase && na.getNodeFlags() != null) {
644                 // TODO: what should we do with in.getOspfRouterId()?
645
646                 final NodeFlagBits nf = na.getNodeFlags();
647                 if (nf.isAbr() != null) {
648                     ab.setRouterType(new AbrBuilder().setAbr(nf.isAbr()).build());
649                 } else if (nf.isExternal() != null) {
650                     ab.setRouterType(new InternalBuilder().setInternal(!nf.isExternal()).build());
651                 }
652             }
653         }
654         ab.setTed(tb.build());
655         return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1Builder().setOspfNodeAttributes(
656             ab.build()).build();
657     }
658
659     private void augmentProtocolId(final LinkstateRoute value, final IgpNodeAttributesBuilder inab, final NodeAttributes na, final NodeIdentifier nd) {
660         switch (value.getProtocolId()) {
661         case Direct:
662         case Static:
663         case Unknown:
664             break;
665         case IsisLevel1:
666         case IsisLevel2:
667             inab.addAugmentation(
668                 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1.class,
669                 isisNodeAttributes(nd, na));
670             break;
671         case Ospf:
672             inab.addAugmentation(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1.class,
673                 ospfNodeAttributes(nd, na));
674             break;
675         default:
676             break;
677         }
678     }
679
680     private void createNode(final WriteTransaction trans, final UriBuilder base,
681         final LinkstateRoute value, final NodeCase n, final Attributes attributes) {
682         final NodeAttributes na;
683         //defensive lookup
684         final Attributes1 attr = attributes.getAugmentation(Attributes1.class);
685         if (attr != null) {
686             final AttributeType attrType = attr.getAttributeType();
687             if (attrType != null) {
688                 na = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.linkstate.routes.linkstate.route.attributes.attribute.type.NodeCase)
689                     attrType).getNodeAttributes();
690             } else {
691                 LOG.debug("Missing attribute type in node {} route {}, skipping it", n, value);
692                 na = null;
693             }
694         } else {
695             LOG.debug("Missing attributes in node {} route {}, skipping it", n, value);
696             na = null;
697         }
698         final IgpNodeAttributesBuilder inab = new IgpNodeAttributesBuilder();
699         final List<IpAddress> ids = new ArrayList<>();
700         if (na != null) {
701             if (na.getIpv4RouterId() != null) {
702                 ids.add(new IpAddress(na.getIpv4RouterId()));
703             }
704             if (na.getIpv6RouterId() != null) {
705                 ids.add(new IpAddress(na.getIpv6RouterId()));
706             }
707             if (na.getDynamicHostname() != null) {
708                 inab.setName(new DomainName(na.getDynamicHostname()));
709             }
710         }
711         if (!ids.isEmpty()) {
712             inab.setRouterId(ids);
713         }
714         augmentProtocolId(value, inab, na, n.getNodeDescriptors());
715
716         final NodeId nid = buildNodeId(base, n.getNodeDescriptors());
717         final NodeHolder nh = getNode(nid);
718         /*
719          *  Eventhough the the holder creates a dummy structure, we need to duplicate it here,
720          *  as that is the API requirement. The reason for it is the possible presence of supporting
721          *  node -- something which the holder does not track.
722          */
723         final NodeBuilder nb = new NodeBuilder();
724         nb.setNodeId(nid);
725         nb.setKey(new NodeKey(nb.getNodeId()));
726
727         nh.advertized(nb, inab);
728         putNode(trans, nh);
729     }
730
731     private void removeNode(final WriteTransaction trans, final UriBuilder base, final NodeCase n) {
732         final NodeId id = buildNodeId(base, n.getNodeDescriptors());
733         final NodeHolder nh = this.nodes.get(id);
734         if (nh != null) {
735             nh.unadvertized();
736             putNode(trans, nh);
737         } else {
738             LOG.warn("Node {} does not have a holder", id);
739         }
740     }
741
742     private void augmentProtocolId(final LinkstateRoute value, final PrefixAttributes pa, final PrefixBuilder pb) {
743         switch (value.getProtocolId()) {
744         case Direct:
745         case IsisLevel1:
746         case IsisLevel2:
747         case Static:
748         case Unknown:
749             break;
750         case Ospf:
751             if (pa != null && pa.getOspfForwardingAddress() != null) {
752                 pb.addAugmentation(
753                     Prefix1.class,
754                     new Prefix1Builder().setOspfPrefixAttributes(
755                         new OspfPrefixAttributesBuilder().setForwardingAddress(pa.getOspfForwardingAddress().getIpv4Address()).build()).build());
756             }
757             break;
758         default:
759             break;
760         }
761     }
762
763     private void createPrefix(final WriteTransaction trans, final UriBuilder base,
764         final LinkstateRoute value, final PrefixCase p, final Attributes attributes) {
765         final IpPrefix ippfx = p.getPrefixDescriptors().getIpReachabilityInformation();
766         if (ippfx == null) {
767             LOG.warn("IP reachability not present in prefix {} route {}, skipping it", p, value);
768             return;
769         }
770         final PrefixBuilder pb = new PrefixBuilder();
771         pb.setKey(new PrefixKey(ippfx));
772         pb.setPrefix(ippfx);
773
774         final PrefixAttributes pa;
775         // Very defensive lookup
776         final Attributes1 attr = attributes.getAugmentation(Attributes1.class);
777         if (attr != null) {
778             final AttributeType attrType = attr.getAttributeType();
779             if (attrType != null) {
780                 pa = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.linkstate.routes.linkstate.route.attributes.attribute.type.PrefixCase)
781                     attrType).getPrefixAttributes();
782             } else {
783                 LOG.debug("Missing attribute type in IP {} prefix {} route {}, skipping it", ippfx, p, value);
784                 pa = null;
785             }
786         } else {
787             LOG.debug("Missing attributes in IP {} prefix {} route {}, skipping it", ippfx, p, value);
788             pa = null;
789         }
790         if (pa != null) {
791             pb.setMetric(pa.getPrefixMetric().getValue());
792         }
793         augmentProtocolId(value, pa, pb);
794
795         final Prefix pfx = pb.build();
796
797         /*
798          * All set, but... the hosting node may not exist, we may need to fake it.
799          */
800         final NodeId node = buildNodeId(base, p.getAdvertisingNodeDescriptors());
801         final NodeHolder nh = getNode(node);
802         nh.addPrefix(pfx);
803         LOG.debug("Created prefix {} for {}", pfx, p);
804         putNode(trans, nh);
805     }
806
807     private void removePrefix(final WriteTransaction trans, final UriBuilder base, final PrefixCase p) {
808         final NodeId node = buildNodeId(base, p.getAdvertisingNodeDescriptors());
809         final NodeHolder nh = this.nodes.get(node);
810         if (nh != null) {
811             nh.removePrefix(p);
812             LOG.debug("Removed prefix {}", p);
813             putNode(trans, nh);
814         } else {
815             LOG.warn("Removing prefix from non-existing node {}", node);
816         }
817     }
818
819     @Override
820     protected void createObject(final ReadWriteTransaction trans,
821         final InstanceIdentifier<LinkstateRoute> id, final LinkstateRoute value) {
822         final UriBuilder base = new UriBuilder(value);
823
824         final ObjectType t = value.getObjectType();
825         Preconditions.checkArgument(t != null, "Route %s value %s has null object type", id, value);
826
827         if (t instanceof LinkCase) {
828             createLink(trans, base, value, (LinkCase) t, value.getAttributes());
829         } else if (t instanceof NodeCase) {
830             createNode(trans, base, value, (NodeCase) t, value.getAttributes());
831         } else if (t instanceof PrefixCase) {
832             createPrefix(trans, base, value, (PrefixCase) t, value.getAttributes());
833         } else {
834             throw new IllegalStateException("Unhandled object class " + t.getImplementedInterface());
835         }
836     }
837
838     @Override
839     protected void removeObject(final ReadWriteTransaction trans,
840         final InstanceIdentifier<LinkstateRoute> id, final LinkstateRoute value) {
841         final UriBuilder base = new UriBuilder(value);
842
843         final ObjectType t = value.getObjectType();
844         if (t instanceof LinkCase) {
845             removeLink(trans, base, (LinkCase) t);
846         } else if (t instanceof NodeCase) {
847             removeNode(trans, base, (NodeCase) t);
848         } else if (t instanceof PrefixCase) {
849             removePrefix(trans, base, (PrefixCase) t);
850         } else {
851             throw new IllegalStateException("Unhandled object class " + t.getImplementedInterface());
852         }
853     }
854 }