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