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