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