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