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