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