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