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