Update MRI projects for Aluminium
[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.annotations.VisibleForTesting;
13 import com.google.common.base.Preconditions;
14 import com.google.common.collect.Collections2;
15 import com.google.common.collect.Lists;
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.mdsal.binding.api.DataBroker;
23 import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
24 import org.opendaylight.mdsal.binding.api.WriteTransaction;
25 import org.opendaylight.mdsal.common.api.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.rev200120.Ipv4InterfaceIdentifier;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.Ipv6InterfaceIdentifier;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.LinkstateAddressFamily;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.LinkstateSubsequentAddressFamily;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.TopologyIdentifier;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.bgp.rib.rib.loc.rib.tables.routes.LinkstateRoutesCase;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.ObjectType;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.attribute.SrAdjIds;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.object.type.LinkCase;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.object.type.NodeCase;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.object.type.PrefixCase;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.object.type.link._case.LinkDescriptors;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.path.attribute.LinkStateAttribute;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.path.attribute.link.state.attribute.LinkAttributesCase;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.path.attribute.link.state.attribute.NodeAttributesCase;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.path.attribute.link.state.attribute.PrefixAttributesCase;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.path.attribute.link.state.attribute.link.attributes._case.LinkAttributes;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.path.attribute.link.state.attribute.node.attributes._case.NodeAttributes;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.path.attribute.link.state.attribute.prefix.attributes._case.PrefixAttributes;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.routes.LinkstateRoutes;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.routes.linkstate.routes.LinkstateRoute;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.routes.linkstate.routes.linkstate.route.Attributes1;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.prefix.state.SrPrefix;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.Attributes;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.Tables;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.ext.rev200120.prefix.sid.tlv.Flags;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.ext.rev200120.prefix.sid.tlv.flags.IsisPrefixFlagsCase;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.ext.rev200120.sid.label.index.SidLabelIndex;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.ext.rev200120.sid.label.index.sid.label.index.LocalLabelCase;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.segment.routing.ext.rev200120.sid.label.index.sid.label.index.SidCase;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp.topology.types.rev160524.TopologyTypes1Builder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp.topology.types.rev160524.bgp.linkstate.topology.type.BgpLinkstateTopologyBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.sr.rev130819.SegmentId;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.sr.rev130819.sr.node.attributes.Segments;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.sr.rev130819.sr.node.attributes.SegmentsBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.sr.rev130819.sr.node.attributes.SegmentsKey;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.sr.rev130819.sr.node.attributes.segments.segment.specification.AdjacencyCaseBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.sr.rev130819.sr.node.attributes.segments.segment.specification.PrefixCaseBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.sr.rev130819.sr.node.attributes.segments.segment.specification.adjacency._case.AdjacencyBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.sr.rev130819.topology.sr.type.TopologySrBuilder;
70 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.LinkId;
71 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
72 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
73 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
74 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.DestinationBuilder;
75 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.SourceBuilder;
76 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
77 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
78 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkBuilder;
79 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkKey;
80 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
81 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
82 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
83 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.TopologyTypes;
84 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.TopologyTypesBuilder;
85 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
86 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
87 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
88 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Link1Builder;
89 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1;
90 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1Builder;
91 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TerminationPoint1Builder;
92 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.link.attributes.IgpLinkAttributesBuilder;
93 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.IgpNodeAttributes;
94 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.IgpNodeAttributesBuilder;
95 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;
96 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;
97 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;
98 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.termination.point.attributes.IgpTerminationPointAttributesBuilder;
99 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;
100 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;
101 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;
102 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
103 import org.opendaylight.yangtools.yang.common.Uint32;
104 import org.slf4j.Logger;
105 import org.slf4j.LoggerFactory;
106
107 public class LinkstateTopologyBuilder extends AbstractTopologyBuilder<LinkstateRoute> {
108     @VisibleForTesting
109     static final TopologyTypes LINKSTATE_TOPOLOGY_TYPE = new TopologyTypesBuilder()
110             .addAugmentation(new TopologyTypes1Builder()
111                     .setBgpLinkstateTopology(new BgpLinkstateTopologyBuilder().build()).build()).build();
112     @VisibleForTesting
113     static final TopologyTypes SR_AWARE_LINKSTATE_TOPOLOGY_TYPE = new TopologyTypesBuilder(LINKSTATE_TOPOLOGY_TYPE)
114             .addAugmentation(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.sr.rev130819
115                 .TopologyTypes1Builder().setTopologySr(new TopologySrBuilder().build()).build())
116             .build();
117
118     private static final String UNHANDLED_OBJECT_CLASS = "Unhandled object class {}";
119
120     private static final class TpHolder {
121         private final Set<LinkId> local = new HashSet<>();
122         private final Set<LinkId> remote = new HashSet<>();
123
124         private final TerminationPoint tp;
125
126         TpHolder(final TerminationPoint tp) {
127             this.tp = requireNonNull(tp);
128         }
129
130         synchronized void addLink(final LinkId id, final boolean isRemote) {
131             if (isRemote) {
132                 this.remote.add(id);
133             } else {
134                 this.local.add(id);
135             }
136         }
137
138         synchronized boolean removeLink(final LinkId id, final boolean isRemote) {
139             final boolean removed;
140             if (isRemote) {
141                 removed = this.remote.remove(id);
142             } else {
143                 removed = this.local.remove(id);
144             }
145             if (!removed) {
146                 LOG.warn("Removed non-reference link {} from TP {} isRemote {}", this.tp.getTpId().getValue(),
147                         id.getValue(), isRemote);
148             }
149
150             return this.local.isEmpty() && this.remote.isEmpty();
151         }
152
153         TerminationPoint getTp() {
154             return this.tp;
155         }
156     }
157
158     private final class NodeHolder {
159         private final Map<PrefixKey, Prefix> prefixes = new HashMap<>();
160         private final Map<TpId, TpHolder> tps = new HashMap<>();
161         private boolean advertized = false;
162         private IgpNodeAttributesBuilder inab;
163         private NodeBuilder nb;
164         private NodeSrHolder sr;
165
166         NodeHolder(final NodeId id) {
167             this.inab = new IgpNodeAttributesBuilder();
168             this.nb = new NodeBuilder().withKey(new NodeKey(id)).setNodeId(id);
169         }
170
171         /**
172          * Synchronized in-core state of a node into the backing store using the transaction.
173          *
174          * @param trans data modification transaction which to use
175          * @return True if the node has been purged, false otherwise.
176          */
177         boolean syncState(final WriteTransaction trans) {
178             final InstanceIdentifier<Node> nid = getNodeInstanceIdentifier(this.nb.key());
179
180             /*
181              * Transaction's putOperationalData() does a merge. Force it onto a replace
182              * by removing the data. If we decide to remove the node -- we just skip the put.
183              */
184             trans.delete(LogicalDatastoreType.OPERATIONAL, nid);
185
186             if (!this.advertized) {
187                 if (this.tps.isEmpty() && this.prefixes.isEmpty()) {
188                     LOG.trace("Removing unadvertized unused node {}", this.nb.getNodeId().getValue());
189                     return true;
190                 }
191
192                 LOG.trace("Node {} is still implied by {} TPs and {} prefixes", this.nb.getNodeId().getValue(),
193                         this.tps.size(), this.prefixes.size());
194             }
195
196             // Re-generate termination points
197             this.nb.setTerminationPoint(Lists.newArrayList(Collections2.transform(this.tps.values(), TpHolder::getTp)));
198
199             // Re-generate prefixes
200             this.inab.setPrefix(Lists.newArrayList(this.prefixes.values()));
201
202             // Write the node out
203             if (this.sr != null && this.sr.getSegmentCount() > 0) {
204                 this.nb.addAugmentation(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.sr
205                     .rev130819.Node1Builder().setSegments(this.sr.getSegments()).build());
206             }
207             final Node n = this.nb
208                     .addAugmentation(new Node1Builder().setIgpNodeAttributes(this.inab.build()).build())
209                     .build();
210             trans.put(LogicalDatastoreType.OPERATIONAL, nid, n);
211             LOG.trace("Created node {} at {}", n, nid);
212             return false;
213         }
214
215         boolean checkForRemoval(final WriteTransaction trans) {
216             final InstanceIdentifier<Node> nid = getNodeInstanceIdentifier(this.nb.key());
217
218             if (!this.advertized) {
219                 if (this.tps.isEmpty() && this.prefixes.isEmpty()) {
220                     trans.delete(LogicalDatastoreType.OPERATIONAL, nid);
221                     LOG.trace("Removing unadvertized unused node {}", this.nb.getNodeId().getValue());
222                     return true;
223                 }
224
225                 LOG.trace("Node {} is still implied by {} TPs and {} prefixes", this.nb.getNodeId().getValue(),
226                         this.tps.size(), this.prefixes.size());
227             }
228             return false;
229         }
230
231         synchronized void removeTp(final TpId tp, final LinkId link, final boolean isRemote) {
232             final TpHolder h = this.tps.get(tp);
233             if (h != null) {
234                 if (h.removeLink(link, isRemote)) {
235                     this.tps.remove(tp);
236                     LOG.trace("Removed TP {}", tp.getValue());
237                 }
238             } else {
239                 LOG.warn("Removed non-present TP {} by link {}", tp.getValue(), link.getValue());
240             }
241         }
242
243         void addTp(final TerminationPoint tp, final LinkId link, final boolean isRemote) {
244             final TpHolder h = this.tps.computeIfAbsent(tp.getTpId(), k -> new TpHolder(tp));
245             h.addLink(link, isRemote);
246         }
247
248         void addPrefix(final Prefix pfx) {
249             this.prefixes.put(pfx.key(), pfx);
250         }
251
252         void removePrefix(final PrefixCase prefixCase) {
253             this.prefixes.remove(new PrefixKey(prefixCase.getPrefixDescriptors().getIpReachabilityInformation()));
254         }
255
256         void unadvertized() {
257             this.inab = new IgpNodeAttributesBuilder();
258             this.nb = new NodeBuilder().withKey(this.nb.key()).setNodeId(this.nb.getNodeId());
259             this.advertized = false;
260             LOG.debug("Node {} is unadvertized", this.nb.getNodeId().getValue());
261         }
262
263         void advertized(final NodeBuilder nodeBuilder, final IgpNodeAttributesBuilder igpNodeAttBuilder) {
264             this.nb = requireNonNull(nodeBuilder);
265             this.inab = requireNonNull(igpNodeAttBuilder);
266             this.advertized = true;
267             LOG.debug("Node {} is advertized", nodeBuilder.getNodeId().getValue());
268         }
269
270         NodeId getNodeId() {
271             return this.nb.getNodeId();
272         }
273
274         NodeSrHolder getSrHolder() {
275             return this.sr;
276         }
277
278         NodeSrHolder createSrHolderIfRequired() {
279             if (this.sr == null) {
280                 this.sr = new NodeSrHolder(this.nb.getNodeId());
281             }
282             return this.sr;
283         }
284     }
285
286     private final class NodeSrHolder {
287         private final NodeId nodeId;
288         private Long srgbFirstValue = null;
289         private Integer srgbRangeSize = null;
290         private final List<Segments> segments = new ArrayList<>();
291         private final Map<IpPrefix, SrPrefix> srPrefixes = new HashMap<>();
292         private final Map<IpPrefix, Segments> prefixSegments = new HashMap<>();
293         private final Map<LinkId, Segments> adjSegments = new HashMap<>();
294
295         NodeSrHolder(final NodeId nodeId) {
296             this.nodeId = nodeId;
297         }
298
299         void addSrgb(final WriteTransaction trans, final boolean updateNode, final Long srgbFirstVal,
300                 final Integer srgbRangeSz) {
301             this.srgbFirstValue = srgbFirstVal;
302             this.srgbRangeSize = srgbRangeSz;
303             this.srPrefixes.entrySet().forEach(entry -> {
304                 final IpPrefix ippfx = entry.getKey();
305                 final SrPrefix srPrefix = entry.getValue();
306                 final SidLabelIndex sidLabelIndex = srPrefix.getSidLabelIndex();
307                 if (sidLabelIndex instanceof SidCase) {
308                     final Long sidIndex = ((SidCase) sidLabelIndex).getSid().longValue();
309                     if (sidIndex >= this.srgbRangeSize) {
310                         LOG.warn("Prefix SID index {} is outside the SRGB range of {} for node {}", sidIndex,
311                                 this.srgbRangeSize, this.nodeId.getValue());
312                         return;
313                     }
314                     final Long prefixSid = this.srgbFirstValue + sidIndex;
315                     final boolean isNodeSid = isAssociatedWithNodeSid(ippfx, srPrefix);
316                     addPrefixSid(trans, updateNode, ippfx, prefixSid, isNodeSid);
317                 }
318             });
319         }
320
321         void removeSrgb(final WriteTransaction trans) {
322             this.srgbFirstValue = null;
323             this.srgbRangeSize = null;
324             this.srPrefixes.entrySet().forEach(entry -> {
325                 final IpPrefix ippfx = entry.getKey();
326                 final SrPrefix srPrefix = entry.getValue();
327                 final SidLabelIndex sidLabelIndex = srPrefix.getSidLabelIndex();
328                 if (sidLabelIndex instanceof SidCase) {
329                     removePrefixSid(trans, false, ippfx);
330                 }
331             });
332         }
333
334         void addSrPrefix(final WriteTransaction trans, final boolean updateNode, final IpPrefix ippfx,
335                 final SrPrefix srPrefix) {
336             this.srPrefixes.put(ippfx, srPrefix);
337             final SidLabelIndex sidLabelIndex = srPrefix.getSidLabelIndex();
338             Long prefixSid = null;
339             if (sidLabelIndex instanceof LocalLabelCase) {
340                 prefixSid = ((LocalLabelCase) sidLabelIndex).getLocalLabel().getValue().longValue();
341             } else if (sidLabelIndex instanceof SidCase) {
342                 if (this.srgbFirstValue != null && this.srgbRangeSize != null) {
343                     final Long sidIndex = ((SidCase) sidLabelIndex).getSid().longValue();
344                     if (sidIndex >= this.srgbRangeSize) {
345                         LOG.warn("Prefix SID index {} is outside the SRGB range of {} for node {}", sidIndex,
346                                 this.srgbRangeSize, this.nodeId.getValue());
347                         return;
348                     }
349                     prefixSid = this.srgbFirstValue + sidIndex;
350                 }
351             }
352             if (prefixSid != null) {
353                 final boolean isNodeSid = isAssociatedWithNodeSid(ippfx, srPrefix);
354                 addPrefixSid(trans, updateNode, ippfx, prefixSid, isNodeSid);
355             }
356         }
357
358         void removeSrPrefix(final WriteTransaction trans, final IpPrefix ippfx) {
359             if (!this.srPrefixes.containsKey(ippfx)) {
360                 return;
361             }
362             removePrefixSid(trans, true, ippfx);
363             this.srPrefixes.remove(ippfx);
364         }
365
366         void addPrefixSid(final WriteTransaction trans, final boolean updateNode, final IpPrefix ippfx,
367                 final Long prefixSid, final boolean isNodeSid) {
368             LOG.trace("Adding prefix SID {} for prefix {} on node {}", prefixSid, ippfx.stringValue(),
369                     this.nodeId.getValue());
370             final SegmentId segmentId = new SegmentId(Uint32.valueOf(prefixSid));
371             final Segments prefixSegment = new SegmentsBuilder()
372                     .setSegmentId(segmentId)
373                     .withKey(new SegmentsKey(segmentId))
374                     .setSegmentSpecification(new PrefixCaseBuilder()
375                         .setPrefix(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.sr
376                             .rev130819.sr.node.attributes.segments.segment.specification.prefix._case.PrefixBuilder()
377                                 .setPrefix(ippfx).setNodeSid(isNodeSid ? isNodeSid : null).build())
378                         .build())
379                     .build();
380             this.prefixSegments.put(ippfx, prefixSegment);
381             this.segments.add(prefixSegment);
382             addSegment(trans, updateNode, prefixSegment);
383         }
384
385         void removePrefixSid(final WriteTransaction trans, final boolean updateNode, final IpPrefix ippfx) {
386             if (!this.prefixSegments.containsKey(ippfx)) {
387                 return;
388             }
389             LOG.trace("Removing prefix SID for prefix {} on node {}", ippfx.stringValue(),
390                     this.nodeId.getValue());
391             final Segments prefixSegment = this.prefixSegments.remove(ippfx);
392             this.segments.remove(prefixSegment);
393             removeSegment(trans, updateNode, prefixSegment);
394         }
395
396         void addAdjacencySid(final WriteTransaction trans, final boolean updateNode, final LinkId linkId,
397                 final Long adjSid) {
398             LOG.trace("Adding adjacency SID {} for link {} on node {}", adjSid, linkId.getValue(),
399                     this.nodeId.getValue());
400             final SegmentId segmentId = new SegmentId(Uint32.valueOf(adjSid));
401             final SegmentsBuilder sb = new SegmentsBuilder();
402             sb.setSegmentId(segmentId);
403             sb.withKey(new SegmentsKey(segmentId));
404             sb.setSegmentSpecification(new AdjacencyCaseBuilder()
405                     .setAdjacency(new AdjacencyBuilder().setAdjacency(linkId).build()).build());
406             final Segments adjSegment = sb.build();
407             this.adjSegments.put(linkId, adjSegment);
408             this.segments.add(adjSegment);
409             addSegment(trans, updateNode, adjSegment);
410         }
411
412         void removeAdjacencySid(final WriteTransaction trans, final LinkId linkId) {
413             if (!this.adjSegments.containsKey(linkId)) {
414                 return;
415             }
416             LOG.trace("Removing adjacency SID for link {} on node {}", linkId.getValue(),
417                     this.nodeId.getValue());
418             final Segments adjSegment = this.adjSegments.remove(linkId);
419             this.segments.remove(adjSegment);
420             removeSegment(trans, true, adjSegment);
421         }
422
423         void addSegment(final WriteTransaction trans, final boolean updateNode, final Segments segment) {
424             if (updateNode) {
425                 final InstanceIdentifier<Node> nodeIId = getNodeInstanceIdentifier(new NodeKey(this.nodeId));
426                 final InstanceIdentifier<Segments> segmentIId = nodeIId.builder()
427                         .augmentation(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.sr
428                             .rev130819.Node1.class)
429                         .child(Segments.class, segment.key()).build();
430                 trans.put(LogicalDatastoreType.OPERATIONAL, segmentIId, segment);
431             }
432             addSrAwareTopologyType(trans);
433         }
434
435         void removeSegment(final WriteTransaction trans, final boolean updateNode, final Segments segment) {
436             if (updateNode) {
437                 final InstanceIdentifier<Node> nodeIId = getNodeInstanceIdentifier(new NodeKey(this.nodeId));
438                 final InstanceIdentifier<Segments> segmentIId = nodeIId.builder()
439                         .augmentation(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.sr
440                             .rev130819.Node1.class)
441                         .child(Segments.class, segment.key()).build();
442                 trans.delete(LogicalDatastoreType.OPERATIONAL, segmentIId);
443             }
444             removeSrAwareTopologyTypeIfRequired(trans);
445         }
446
447         boolean isAssociatedWithNodeSid(final IpPrefix ippfx, final SrPrefix srPrefix) {
448             if (ippfx.getIpv4Prefix() != null && !ippfx.stringValue().endsWith("/32")
449                     || ippfx.getIpv6Prefix() != null && !ippfx.stringValue().endsWith("/128")) {
450                 return false;
451             }
452             final Flags prefixFlags = srPrefix.getFlags();
453             if (prefixFlags instanceof IsisPrefixFlagsCase) {
454                 return !Boolean.FALSE.equals(((IsisPrefixFlagsCase) prefixFlags).getIsisPrefixFlags().isNodeSid());
455             }
456             return true;
457         }
458
459         List<Segments> getSegments() {
460             return this.segments;
461         }
462
463         int getSegmentCount() {
464             return this.segments.size();
465         }
466     }
467
468     private static final Logger LOG = LoggerFactory.getLogger(LinkstateTopologyBuilder.class);
469     private final Map<NodeId, NodeHolder> nodes = new HashMap<>();
470     private boolean srAwareTopologyTypeAdded;
471
472     public LinkstateTopologyBuilder(final DataBroker dataProvider, final RibReference locRibReference,
473             final TopologyId topologyId) {
474         super(dataProvider, locRibReference, topologyId, LINKSTATE_TOPOLOGY_TYPE, LinkstateAddressFamily.class,
475                 LinkstateSubsequentAddressFamily.class);
476     }
477
478     @VisibleForTesting
479     LinkstateTopologyBuilder(final DataBroker dataProvider, final RibReference locRibReference,
480             final TopologyId topologyId, final long listenerResetLimitInMillsec,
481             final int listenerResetEnforceCounter) {
482         super(dataProvider, locRibReference, topologyId, LINKSTATE_TOPOLOGY_TYPE, LinkstateAddressFamily.class,
483                 LinkstateSubsequentAddressFamily.class,
484                 listenerResetLimitInMillsec, listenerResetEnforceCounter);
485     }
486
487     private static LinkId buildLinkId(final UriBuilder base, final LinkCase link) {
488         return new LinkId(new UriBuilder(base, "link").add(link).toString());
489     }
490
491     private static NodeId buildNodeId(final UriBuilder base, final org.opendaylight.yang.gen.v1.urn.opendaylight
492             .params.xml.ns.yang.bgp.linkstate.rev200120.NodeIdentifier node) {
493         return new NodeId(new UriBuilder(base, "node").addPrefix("", node).toString());
494     }
495
496     private static TpId buildTpId(final UriBuilder base, final TopologyIdentifier topologyIdentifier,
497             final Ipv4InterfaceIdentifier ipv4InterfaceIdentifier,
498             final Ipv6InterfaceIdentifier ipv6InterfaceIdentifier, final Uint32 id) {
499         final UriBuilder b = new UriBuilder(base, "tp");
500         if (topologyIdentifier != null) {
501             b.add("mt", topologyIdentifier.getValue());
502         }
503         if (ipv4InterfaceIdentifier != null) {
504             b.add("ipv4", ipv4InterfaceIdentifier.getValue());
505         }
506         if (ipv6InterfaceIdentifier != null) {
507             b.add("ipv6", ipv6InterfaceIdentifier.getValue());
508         }
509
510         return new TpId(b.add("id", id).toString());
511     }
512
513     private static TpId buildLocalTpId(final UriBuilder base, final LinkDescriptors linkDescriptors) {
514         return buildTpId(base, linkDescriptors.getMultiTopologyId(), linkDescriptors.getIpv4InterfaceAddress(),
515                 linkDescriptors.getIpv6InterfaceAddress(), linkDescriptors.getLinkLocalIdentifier());
516     }
517
518     private static TerminationPoint buildTp(final TpId id, final TerminationPointType type) {
519         final TerminationPointBuilder stpb = new TerminationPointBuilder();
520         stpb.withKey(new TerminationPointKey(id));
521         stpb.setTpId(id);
522
523         if (type != null) {
524             stpb.addAugmentation(new TerminationPoint1Builder()
525                     .setIgpTerminationPointAttributes(new IgpTerminationPointAttributesBuilder()
526                         .setTerminationPointType(type)
527                         .build())
528                     .build());
529         }
530
531         return stpb.build();
532     }
533
534     private static TerminationPointType getTpType(final Ipv4InterfaceIdentifier ipv4InterfaceIdentifier,
535             final Ipv6InterfaceIdentifier ipv6InterfaceIdentifier, final Uint32 id) {
536         // Order of preference: Unnumbered first, then IP
537         if (id != null) {
538             LOG.debug("Unnumbered termination point type: {}", id);
539             return new UnnumberedBuilder().setUnnumberedId(id).build();
540         }
541
542         final IpAddress ip;
543         if (ipv6InterfaceIdentifier != null) {
544             ip = new IpAddress(ipv6InterfaceIdentifier);
545         } else if (ipv4InterfaceIdentifier != null) {
546             ip = new IpAddress(ipv4InterfaceIdentifier);
547         } else {
548             ip = null;
549         }
550
551         if (ip != null) {
552             LOG.debug("IP termination point type: {}", ip);
553             return new IpBuilder().setIpAddress(Lists.newArrayList(ip)).build();
554         }
555
556         return null;
557     }
558
559     private static TerminationPoint buildLocalTp(final UriBuilder base, final LinkDescriptors linkDescriptors) {
560         final TpId id = buildLocalTpId(base, linkDescriptors);
561         final TerminationPointType t = getTpType(linkDescriptors.getIpv4InterfaceAddress(),
562                 linkDescriptors.getIpv6InterfaceAddress(),
563                 linkDescriptors.getLinkLocalIdentifier());
564
565         return buildTp(id, t);
566     }
567
568     private static TpId buildRemoteTpId(final UriBuilder base, final LinkDescriptors linkDescriptors) {
569         return buildTpId(base, linkDescriptors.getMultiTopologyId(), linkDescriptors.getIpv4NeighborAddress(),
570                 linkDescriptors.getIpv6NeighborAddress(), linkDescriptors.getLinkRemoteIdentifier());
571     }
572
573     private static TerminationPoint buildRemoteTp(final UriBuilder base, final LinkDescriptors linkDescriptors) {
574         final TpId id = buildRemoteTpId(base, linkDescriptors);
575         final TerminationPointType t = getTpType(linkDescriptors.getIpv4NeighborAddress(),
576                 linkDescriptors.getIpv6NeighborAddress(),
577                 linkDescriptors.getLinkRemoteIdentifier());
578
579         return buildTp(id, t);
580     }
581
582     private InstanceIdentifier<Link> buildLinkIdentifier(final LinkId id) {
583         return getInstanceIdentifier().child(
584                 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology
585                         .topology.Link.class, new LinkKey(id));
586     }
587
588     private NodeHolder getNode(final NodeId id) {
589         return this.nodes.computeIfAbsent(id, NodeHolder::new);
590     }
591
592     private void putNode(final WriteTransaction trans, final NodeHolder holder) {
593         if (holder.syncState(trans)) {
594             this.nodes.remove(holder.getNodeId());
595         }
596     }
597
598     private void checkNodeForRemoval(final WriteTransaction trans, final NodeHolder holder) {
599         if (holder.checkForRemoval(trans)) {
600             this.nodes.remove(holder.getNodeId());
601         }
602     }
603
604     private void createLink(final WriteTransaction trans, final UriBuilder base,
605             final LinkstateRoute value, final LinkCase linkCase, final Attributes attributes) {
606         // defensive lookup
607         final LinkAttributes la;
608         final Attributes1 attr = attributes.augmentation(Attributes1.class);
609         if (attr != null) {
610             final LinkStateAttribute attrType = attr.getLinkStateAttribute();
611             if (attrType != null) {
612                 la = ((LinkAttributesCase)attrType).getLinkAttributes();
613             } else {
614                 LOG.debug("Missing attribute type in link {} route {}, skipping it", linkCase, value);
615                 la = null;
616             }
617         } else {
618             LOG.debug("Missing attributes in link {} route {}, skipping it", linkCase, value);
619             la = null;
620         }
621
622         final IgpLinkAttributesBuilder ilab = new IgpLinkAttributesBuilder();
623         Long adjSid = null;
624         if (la != null) {
625             if (la.getMetric() != null) {
626                 ilab.setMetric(la.getMetric().getValue());
627             }
628             ilab.setName(la.getLinkName());
629             if (la.getSrAdjIds() != null && !la.getSrAdjIds().isEmpty()) {
630                 final SrAdjIds srAdjIds = la.getSrAdjIds().get(0);
631                 if (srAdjIds != null) {
632                     final SidLabelIndex sidLabelIndex = srAdjIds.getSidLabelIndex();
633                     if (sidLabelIndex instanceof LocalLabelCase) {
634                         adjSid = ((LocalLabelCase) sidLabelIndex).getLocalLabel().getValue().longValue();
635                     }
636                 }
637             }
638         }
639         ProtocolUtil.augmentProtocolId(value, ilab, la, linkCase.getLinkDescriptors());
640
641         final LinkBuilder lb = new LinkBuilder()
642                 .setLinkId(buildLinkId(base, linkCase))
643                 .addAugmentation(new Link1Builder().setIgpLinkAttributes(ilab.build()).build());
644
645         final NodeId srcNode = buildNodeId(base, linkCase.getLocalNodeDescriptors());
646         LOG.trace("Link {} implies source node {}", linkCase, srcNode);
647
648         final NodeId dstNode = buildNodeId(base, linkCase.getRemoteNodeDescriptors());
649         LOG.trace("Link {} implies destination node {}", linkCase, dstNode);
650
651         final TerminationPoint srcTp = buildLocalTp(base, linkCase.getLinkDescriptors());
652         LOG.trace("Link {} implies source TP {}", linkCase, srcTp);
653
654         final TerminationPoint dstTp = buildRemoteTp(base, linkCase.getLinkDescriptors());
655         LOG.trace("Link {} implies destination TP {}", linkCase, dstTp);
656
657         lb.setSource(new SourceBuilder().setSourceNode(srcNode).setSourceTp(srcTp.getTpId()).build());
658         lb.setDestination(new DestinationBuilder().setDestNode(dstNode).setDestTp(dstTp.getTpId()).build());
659
660         LOG.trace("Created TP {} as link source", srcTp);
661         NodeHolder snh = this.nodes.get(srcNode);
662         if (snh == null) {
663             snh = getNode(srcNode);
664             snh.addTp(srcTp, lb.getLinkId(), false);
665             if (adjSid != null) {
666                 snh.createSrHolderIfRequired().addAdjacencySid(trans, false, lb.getLinkId(), adjSid);
667             }
668             putNode(trans, snh);
669         } else {
670             snh.addTp(srcTp, lb.getLinkId(), false);
671             if (adjSid != null) {
672                 snh.createSrHolderIfRequired().addAdjacencySid(trans, true, lb.getLinkId(), adjSid);
673             }
674             final InstanceIdentifier<Node> nid = getNodeInstanceIdentifier(new NodeKey(snh.getNodeId()));
675             trans.put(LogicalDatastoreType.OPERATIONAL, nid.child(TerminationPoint.class, srcTp.key()), srcTp);
676         }
677         if (adjSid != null) {
678             lb.addAugmentation(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.sr
679                 .rev130819.Link1Builder().setSegment(new SegmentId(Uint32.valueOf(adjSid))).build());
680         }
681
682         LOG.debug("Created TP {} as link destination", dstTp);
683         NodeHolder dnh = this.nodes.get(dstNode);
684         if (dnh == null) {
685             dnh = getNode(dstNode);
686             dnh.addTp(dstTp, lb.getLinkId(), true);
687             putNode(trans, dnh);
688         } else {
689             dnh.addTp(dstTp, lb.getLinkId(), true);
690             final InstanceIdentifier<Node> nid = getInstanceIdentifier().child(
691                     org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network
692                             .topology.topology.Node.class, new NodeKey(dnh.getNodeId()));
693             trans.put(LogicalDatastoreType.OPERATIONAL, nid.child(TerminationPoint.class, dstTp.key()), dstTp);
694         }
695
696         final InstanceIdentifier<Link> lid = buildLinkIdentifier(lb.getLinkId());
697         final Link link = lb.build();
698
699         trans.put(LogicalDatastoreType.OPERATIONAL, lid, link);
700         LOG.debug("Created link {} at {} for {}", link, lid, linkCase);
701     }
702
703     private void removeTp(final WriteTransaction trans, final NodeId node, final TpId tp,
704             final LinkId link, final boolean isRemote) {
705         final NodeHolder nh = this.nodes.get(node);
706         if (nh != null) {
707             final InstanceIdentifier<Node> nid = getNodeInstanceIdentifier(new NodeKey(nh.getNodeId()));
708             trans.delete(LogicalDatastoreType.OPERATIONAL, nid.child(TerminationPoint.class,
709                     new TerminationPointKey(tp)));
710             nh.removeTp(tp, link, isRemote);
711             if (!isRemote) {
712                 nh.createSrHolderIfRequired().removeAdjacencySid(trans, link);
713             }
714             checkNodeForRemoval(trans, nh);
715         } else {
716             LOG.warn("Removed non-existent node {}", node.getValue());
717         }
718     }
719
720     private void removeLink(final WriteTransaction trans, final UriBuilder base, final LinkCase linkCase) {
721         final LinkId id = buildLinkId(base, linkCase);
722         final InstanceIdentifier<?> lid = buildLinkIdentifier(id);
723         trans.delete(LogicalDatastoreType.OPERATIONAL, lid);
724         LOG.debug("Removed link {}", lid);
725
726         removeTp(trans, buildNodeId(base, linkCase.getLocalNodeDescriptors()),
727                 buildLocalTpId(base, linkCase.getLinkDescriptors()), id, false);
728         removeTp(trans, buildNodeId(base, linkCase.getRemoteNodeDescriptors()),
729                 buildRemoteTpId(base, linkCase.getLinkDescriptors()), id, true);
730     }
731
732     private void createNode(final WriteTransaction trans, final UriBuilder base,
733             final LinkstateRoute value, final NodeCase nodeCase, final Attributes attributes) {
734         final NodeAttributes na;
735         //defensive lookup
736         final Attributes1 attr = attributes.augmentation(Attributes1.class);
737         if (attr != null) {
738             final LinkStateAttribute attrType = attr.getLinkStateAttribute();
739             if (attrType != null) {
740                 na = ((NodeAttributesCase)attrType).getNodeAttributes();
741             } else {
742                 LOG.debug("Missing attribute type in node {} route {}, skipping it", nodeCase, value);
743                 na = null;
744             }
745         } else {
746             LOG.debug("Missing attributes in node {} route {}, skipping it", nodeCase, value);
747             na = null;
748         }
749         final IgpNodeAttributesBuilder inab = new IgpNodeAttributesBuilder();
750         final List<IpAddress> ids = new ArrayList<>();
751         Long srgbFirstValue = null;
752         Integer srgbRangeSize = null;
753         if (na != null) {
754             if (na.getIpv4RouterId() != null) {
755                 ids.add(new IpAddress(na.getIpv4RouterId()));
756             }
757             if (na.getIpv6RouterId() != null) {
758                 ids.add(new IpAddress(na.getIpv6RouterId()));
759             }
760             if (na.getDynamicHostname() != null) {
761                 inab.setName(new DomainName(na.getDynamicHostname()));
762             }
763             if (na.getSrCapabilities() != null) {
764                 final SidLabelIndex sidLabelIndex = na.getSrCapabilities().getSidLabelIndex();
765                 if (sidLabelIndex instanceof LocalLabelCase) {
766                     srgbFirstValue = ((LocalLabelCase) sidLabelIndex).getLocalLabel().getValue().longValue();
767                 }
768                 srgbRangeSize = na.getSrCapabilities().getRangeSize() != null
769                         ? na.getSrCapabilities().getRangeSize().getValue().intValue()
770                         : null;
771             }
772         }
773         if (!ids.isEmpty()) {
774             inab.setRouterId(ids);
775         }
776         ProtocolUtil.augmentProtocolId(value, inab, na, nodeCase.getNodeDescriptors());
777
778         final NodeId nid = buildNodeId(base, nodeCase.getNodeDescriptors());
779         final NodeHolder nh = getNode(nid);
780         /*
781          *  Eventhough the the holder creates a dummy structure, we need to duplicate it here,
782          *  as that is the API requirement. The reason for it is the possible presence of supporting
783          *  node -- something which the holder does not track.
784          */
785         final NodeBuilder nb = new NodeBuilder();
786         nb.setNodeId(nid);
787         nb.withKey(new NodeKey(nb.getNodeId()));
788
789         nh.advertized(nb, inab);
790         if (srgbFirstValue != null && srgbRangeSize != null) {
791             nh.createSrHolderIfRequired().addSrgb(trans, false, srgbFirstValue, srgbRangeSize);
792         }
793         putNode(trans, nh);
794     }
795
796     private void removeNode(final WriteTransaction trans, final UriBuilder base, final NodeCase nodeCase) {
797         final NodeId id = buildNodeId(base, nodeCase.getNodeDescriptors());
798         final NodeHolder nh = this.nodes.get(id);
799         if (nh != null) {
800             nh.unadvertized();
801             nh.createSrHolderIfRequired().removeSrgb(trans);
802             putNode(trans, nh);
803         } else {
804             LOG.warn("Node {} does not have a holder", id.getValue());
805         }
806     }
807
808     private void createPrefix(final WriteTransaction trans, final UriBuilder base,
809             final LinkstateRoute value, final PrefixCase prefixCase, final Attributes attributes) {
810         final IpPrefix ippfx = prefixCase.getPrefixDescriptors().getIpReachabilityInformation();
811         if (ippfx == null) {
812             LOG.warn("IP reachability not present in prefix {} route {}, skipping it", prefixCase, value);
813             return;
814         }
815         final PrefixBuilder pb = new PrefixBuilder();
816         final PrefixKey pk = new PrefixKey(ippfx);
817         pb.withKey(pk);
818         pb.setPrefix(ippfx);
819
820         final PrefixAttributes pa;
821         // Very defensive lookup
822         final Attributes1 attr = attributes.augmentation(Attributes1.class);
823         if (attr != null) {
824             final LinkStateAttribute attrType = attr.getLinkStateAttribute();
825             if (attrType != null) {
826                 pa = ((PrefixAttributesCase)attrType).getPrefixAttributes();
827             } else {
828                 LOG.debug("Missing attribute type in IP {} prefix {} route {}, skipping it", ippfx, prefixCase, value);
829                 pa = null;
830             }
831         } else {
832             LOG.debug("Missing attributes in IP {} prefix {} route {}, skipping it", ippfx, prefixCase, value);
833             pa = null;
834         }
835         SrPrefix srPrefix = null;
836         if (pa != null) {
837             if (pa.getPrefixMetric() != null) {
838                 pb.setMetric(pa.getPrefixMetric().getValue());
839             }
840             if (pa.getSrPrefix() != null) {
841                 srPrefix = pa.getSrPrefix();
842             }
843         }
844         ProtocolUtil.augmentProtocolId(value, pa, pb);
845
846         final Prefix pfx = pb.build();
847         LOG.debug("Created prefix {} for {}", pfx, prefixCase);
848
849         /*
850          * All set, but... the hosting node may not exist, we may need to fake it.
851          */
852         final NodeId node = buildNodeId(base, prefixCase.getAdvertisingNodeDescriptors());
853         NodeHolder nh = this.nodes.get(node);
854         if (nh == null) {
855             nh = getNode(node);
856             nh.addPrefix(pfx);
857             if (srPrefix != null) {
858                 nh.createSrHolderIfRequired().addSrPrefix(trans, false, ippfx, srPrefix);
859             }
860             putNode(trans, nh);
861         } else {
862             nh.addPrefix(pfx);
863             if (srPrefix != null) {
864                 nh.createSrHolderIfRequired().addSrPrefix(trans, true, ippfx, srPrefix);
865             }
866             final InstanceIdentifier<Node> nid = getNodeInstanceIdentifier(new NodeKey(nh.getNodeId()));
867             final InstanceIdentifier<IgpNodeAttributes> inaId = nid.builder().augmentation(Node1.class)
868                     .child(IgpNodeAttributes.class).build();
869             trans.put(LogicalDatastoreType.OPERATIONAL, inaId.child(Prefix.class, pk), pfx);
870         }
871     }
872
873     private void removePrefix(final WriteTransaction trans, final UriBuilder base, final PrefixCase prefixCase) {
874         final NodeId node = buildNodeId(base, prefixCase.getAdvertisingNodeDescriptors());
875         final NodeHolder nh = this.nodes.get(node);
876         if (nh != null) {
877             LOG.debug("Removed prefix {}", prefixCase);
878             final InstanceIdentifier<Node> nid = getNodeInstanceIdentifier(new NodeKey(nh.getNodeId()));
879             final InstanceIdentifier<IgpNodeAttributes> inaId = nid.builder().augmentation(Node1.class)
880                     .child(IgpNodeAttributes.class).build();
881             final IpPrefix ippfx = prefixCase.getPrefixDescriptors().getIpReachabilityInformation();
882             if (ippfx == null) {
883                 LOG.warn("IP reachability not present in prefix {}, skipping it", prefixCase);
884                 return;
885             }
886             final PrefixKey pk = new PrefixKey(ippfx);
887             trans.delete(LogicalDatastoreType.OPERATIONAL, inaId.child(Prefix.class, pk));
888             nh.removePrefix(prefixCase);
889             nh.createSrHolderIfRequired().removeSrPrefix(trans, ippfx);
890             checkNodeForRemoval(trans, nh);
891         } else {
892             LOG.warn("Removing prefix from non-existing node {}", node.getValue());
893         }
894     }
895
896     private InstanceIdentifier<Node> getNodeInstanceIdentifier(final NodeKey nodeKey) {
897         return getInstanceIdentifier().child(
898                 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network
899                         .topology.topology.Node.class, nodeKey);
900     }
901
902     protected void addSrAwareTopologyType(final WriteTransaction trans) {
903         if (this.srAwareTopologyTypeAdded) {
904             return;
905         }
906         LOG.debug("Adding SR-aware topology-type for topology {}",
907                 getInstanceIdentifier().firstKeyOf(Topology.class).getTopologyId().getValue());
908         trans.put(LogicalDatastoreType.OPERATIONAL, getInstanceIdentifier().child(TopologyTypes.class),
909                 SR_AWARE_LINKSTATE_TOPOLOGY_TYPE);
910         this.srAwareTopologyTypeAdded = true;
911     }
912
913     protected void removeSrAwareTopologyTypeIfRequired(final WriteTransaction trans) {
914         if (!this.srAwareTopologyTypeAdded) {
915             return;
916         }
917         final boolean isSidPresent = this.nodes.values().stream().filter(nh -> nh.getSrHolder() != null)
918                 .map(nh -> nh.getSrHolder().getSegmentCount()).anyMatch(cnt -> cnt != 0);
919         if (isSidPresent) {
920             return;
921         }
922         LOG.debug("Removing SR-aware topology-type from topology {}",
923                 getInstanceIdentifier().firstKeyOf(Topology.class).getTopologyId().getValue());
924         trans.put(LogicalDatastoreType.OPERATIONAL, getInstanceIdentifier().child(TopologyTypes.class),
925                 LINKSTATE_TOPOLOGY_TYPE);
926         this.srAwareTopologyTypeAdded = false;
927     }
928
929     @Override
930     protected void createObject(final ReadWriteTransaction trans,
931             final InstanceIdentifier<LinkstateRoute> id, final LinkstateRoute value) {
932         final UriBuilder base = new UriBuilder(value);
933
934         final ObjectType t = value.getObjectType();
935         Preconditions.checkArgument(t != null, "Route %s value %s has null object type", id, value);
936
937         if (t instanceof LinkCase) {
938             createLink(trans, base, value, (LinkCase) t, value.getAttributes());
939         } else if (t instanceof NodeCase) {
940             createNode(trans, base, value, (NodeCase) t, value.getAttributes());
941         } else if (t instanceof PrefixCase) {
942             createPrefix(trans, base, value, (PrefixCase) t, value.getAttributes());
943         } else {
944             LOG.debug(UNHANDLED_OBJECT_CLASS, t.implementedInterface());
945         }
946     }
947
948     @Override
949     protected void removeObject(final ReadWriteTransaction trans,
950             final InstanceIdentifier<LinkstateRoute> id, final LinkstateRoute value) {
951         if (value == null) {
952             LOG.error("Empty before-data received in delete data change notification for instance id {}", id);
953             return;
954         }
955
956         final UriBuilder base = new UriBuilder(value);
957
958         final ObjectType t = value.getObjectType();
959         if (t instanceof LinkCase) {
960             removeLink(trans, base, (LinkCase) t);
961         } else if (t instanceof NodeCase) {
962             removeNode(trans, base, (NodeCase) t);
963         } else if (t instanceof PrefixCase) {
964             removePrefix(trans, base, (PrefixCase) t);
965         } else {
966             LOG.debug(UNHANDLED_OBJECT_CLASS, t.implementedInterface());
967         }
968     }
969
970     @Override
971     protected InstanceIdentifier<LinkstateRoute> getRouteWildcard(final InstanceIdentifier<Tables> tablesId) {
972         return tablesId.child(LinkstateRoutesCase.class, LinkstateRoutes.class).child(LinkstateRoute.class);
973     }
974
975     @Override
976     protected void clearTopology() {
977         this.nodes.clear();
978         this.srAwareTopologyTypeAdded = false;
979     }
980 }