2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.bgpcep.bgp.topology.provider;
10 import static java.util.Objects.requireNonNull;
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;
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.rev180329.Ipv4InterfaceIdentifier;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.Ipv6InterfaceIdentifier;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.LinkstateAddressFamily;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.LinkstateSubsequentAddressFamily;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.TopologyIdentifier;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.ObjectType;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.object.type.LinkCase;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.object.type.NodeCase;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.object.type.PrefixCase;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.object.type.link._case.LinkDescriptors;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.LinkStateAttribute;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.link.state.attribute.LinkAttributesCase;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.link.state.attribute.NodeAttributesCase;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.link.state.attribute.PrefixAttributesCase;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.link.state.attribute.link.attributes._case.LinkAttributes;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.link.state.attribute.node.attributes._case.NodeAttributes;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.path.attribute.link.state.attribute.prefix.attributes._case.PrefixAttributes;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.routes.LinkstateRoutes;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.routes.linkstate.routes.LinkstateRoute;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.routes.linkstate.routes.linkstate.route.Attributes1;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.Tables;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp.topology.types.rev160524.TopologyTypes1;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp.topology.types.rev160524.TopologyTypes1Builder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp.topology.types.rev160524.bgp.linkstate.topology.type.BgpLinkstateTopologyBuilder;
55 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.LinkId;
56 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
57 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
58 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
59 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.DestinationBuilder;
60 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.SourceBuilder;
61 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
62 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkBuilder;
63 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkKey;
64 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
65 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
66 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
67 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.TopologyTypes;
68 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.TopologyTypesBuilder;
69 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
70 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
71 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
72 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Link1;
73 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Link1Builder;
74 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1;
75 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1Builder;
76 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TerminationPoint1;
77 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TerminationPoint1Builder;
78 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.link.attributes.IgpLinkAttributesBuilder;
79 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.IgpNodeAttributes;
80 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.IgpNodeAttributesBuilder;
81 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;
82 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;
83 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;
84 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.termination.point.attributes.IgpTerminationPointAttributesBuilder;
85 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;
86 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;
87 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;
88 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
89 import org.slf4j.Logger;
90 import org.slf4j.LoggerFactory;
92 public class LinkstateTopologyBuilder extends AbstractTopologyBuilder<LinkstateRoute> {
93 private static final TopologyTypes LINKSTATE_TOPOLOGY_TYPE = new TopologyTypesBuilder()
94 .addAugmentation(TopologyTypes1.class, new TopologyTypes1Builder()
95 .setBgpLinkstateTopology(new BgpLinkstateTopologyBuilder().build()).build()).build();
97 private static final String UNHANDLED_OBJECT_CLASS = "Unhandled object class {}";
99 private static final class TpHolder {
100 private final Set<LinkId> local = new HashSet<>();
101 private final Set<LinkId> remote = new HashSet<>();
103 private final TerminationPoint tp;
105 private TpHolder(final TerminationPoint tp) {
106 this.tp = requireNonNull(tp);
109 private synchronized void addLink(final LinkId id, final boolean isRemote) {
117 private synchronized boolean removeLink(final LinkId id, final boolean isRemote) {
118 final boolean removed;
120 removed = this.remote.remove(id);
122 removed = this.local.remove(id);
125 LOG.warn("Removed non-reference link {} from TP {} isRemote {}", this.tp.getTpId(), id, isRemote);
128 return this.local.isEmpty() && this.remote.isEmpty();
131 private TerminationPoint getTp() {
136 private final class NodeHolder {
137 private final Map<PrefixKey, Prefix> prefixes = new HashMap<>();
138 private final Map<TpId, TpHolder> tps = new HashMap<>();
139 private boolean advertized = false;
140 private IgpNodeAttributesBuilder inab;
141 private NodeBuilder nb;
143 private NodeHolder(final NodeId id) {
144 this.inab = new IgpNodeAttributesBuilder();
145 this.nb = new NodeBuilder().setKey(new NodeKey(id)).setNodeId(id);
149 * Synchronized in-core state of a node into the backing store using the transaction.
151 * @param trans data modification transaction which to use
152 * @return True if the node has been purged, false otherwise.
154 private boolean syncState(final WriteTransaction trans) {
155 final InstanceIdentifier<Node> nid = getNodeInstanceIdentifier(this.nb.getKey());
158 * Transaction's putOperationalData() does a merge. Force it onto a replace
159 * by removing the data. If we decide to remove the node -- we just skip the put.
161 trans.delete(LogicalDatastoreType.OPERATIONAL, nid);
163 if (!this.advertized) {
164 if (this.tps.isEmpty() && this.prefixes.isEmpty()) {
165 LOG.trace("Removing unadvertized unused node {}", this.nb.getNodeId());
169 LOG.trace("Node {} is still implied by {} TPs and {} prefixes", this.nb.getNodeId(), this.tps.size(),
170 this.prefixes.size());
173 // Re-generate termination points
174 this.nb.setTerminationPoint(Lists.newArrayList(Collections2.transform(this.tps.values(), TpHolder::getTp)));
176 // Re-generate prefixes
177 this.inab.setPrefix(Lists.newArrayList(this.prefixes.values()));
179 // Write the node out
180 final Node n = this.nb.addAugmentation(Node1.class, new Node1Builder()
181 .setIgpNodeAttributes(this.inab.build()).build()).build();
182 trans.put(LogicalDatastoreType.OPERATIONAL, nid, n);
183 LOG.trace("Created node {} at {}", n, nid);
187 private boolean checkForRemoval(final WriteTransaction trans) {
188 final InstanceIdentifier<Node> nid = getNodeInstanceIdentifier(this.nb.getKey());
190 if (!this.advertized) {
191 if (this.tps.isEmpty() && this.prefixes.isEmpty()) {
192 trans.delete(LogicalDatastoreType.OPERATIONAL, nid);
193 LOG.trace("Removing unadvertized unused node {}", this.nb.getNodeId());
197 LOG.trace("Node {} is still implied by {} TPs and {} prefixes", this.nb.getNodeId(),
198 this.tps.size(), this.prefixes.size());
203 private synchronized void removeTp(final TpId tp, final LinkId link, final boolean isRemote) {
204 final TpHolder h = this.tps.get(tp);
206 if (h.removeLink(link, isRemote)) {
208 LOG.trace("Removed TP {}", tp);
211 LOG.warn("Removed non-present TP {} by link {}", tp, link);
215 private void addTp(final TerminationPoint tp, final LinkId link, final boolean isRemote) {
216 final TpHolder h = this.tps.computeIfAbsent(tp.getTpId(), k -> new TpHolder(tp));
217 h.addLink(link, isRemote);
220 private void addPrefix(final Prefix pfx) {
221 this.prefixes.put(pfx.getKey(), pfx);
224 private void removePrefix(final PrefixCase prefixCase) {
225 this.prefixes.remove(new PrefixKey(prefixCase.getPrefixDescriptors().getIpReachabilityInformation()));
228 private void unadvertized() {
229 this.inab = new IgpNodeAttributesBuilder();
230 this.nb = new NodeBuilder().setKey(this.nb.getKey()).setNodeId(this.nb.getNodeId());
231 this.advertized = false;
232 LOG.debug("Node {} is unadvertized", this.nb.getNodeId());
235 private void advertized(final NodeBuilder nodeBuilder, final IgpNodeAttributesBuilder igpNodeAttBuilder) {
236 this.nb = requireNonNull(nodeBuilder);
237 this.inab = requireNonNull(igpNodeAttBuilder);
238 this.advertized = true;
239 LOG.debug("Node {} is advertized", nodeBuilder.getNodeId());
242 private NodeId getNodeId() {
243 return this.nb.getNodeId();
247 private static final Logger LOG = LoggerFactory.getLogger(LinkstateTopologyBuilder.class);
248 private final Map<NodeId, NodeHolder> nodes = new HashMap<>();
250 public LinkstateTopologyBuilder(final DataBroker dataProvider, final RibReference locRibReference,
251 final TopologyId topologyId) {
252 super(dataProvider, locRibReference, topologyId, LINKSTATE_TOPOLOGY_TYPE, LinkstateAddressFamily.class,
253 LinkstateSubsequentAddressFamily.class);
257 LinkstateTopologyBuilder(final DataBroker dataProvider, final RibReference locRibReference,
258 final TopologyId topologyId, final long listenerResetLimitInMillsec,
259 final int listenerResetEnforceCounter) {
260 super(dataProvider, locRibReference, topologyId, LINKSTATE_TOPOLOGY_TYPE, LinkstateAddressFamily.class,
261 LinkstateSubsequentAddressFamily.class,
262 listenerResetLimitInMillsec, listenerResetEnforceCounter);
265 private static LinkId buildLinkId(final UriBuilder base, final LinkCase link) {
266 return new LinkId(new UriBuilder(base, "link").add(link).toString());
269 private static NodeId buildNodeId(final UriBuilder base, final org.opendaylight.yang.gen.v1.urn.opendaylight
270 .params.xml.ns.yang.bgp.linkstate.rev180329.NodeIdentifier node) {
271 return new NodeId(new UriBuilder(base, "node").addPrefix("", node).toString());
274 private static TpId buildTpId(final UriBuilder base, final TopologyIdentifier topologyIdentifier,
275 final Ipv4InterfaceIdentifier ipv4InterfaceIdentifier,
276 final Ipv6InterfaceIdentifier ipv6InterfaceIdentifier, final Long id) {
277 final UriBuilder b = new UriBuilder(base, "tp");
278 if (topologyIdentifier != null) {
279 b.add("mt", topologyIdentifier.getValue());
281 if (ipv4InterfaceIdentifier != null) {
282 b.add("ipv4", ipv4InterfaceIdentifier.getValue());
284 if (ipv6InterfaceIdentifier != null) {
285 b.add("ipv6", ipv6InterfaceIdentifier.getValue());
288 return new TpId(b.add("id", id).toString());
291 private static TpId buildLocalTpId(final UriBuilder base, final LinkDescriptors linkDescriptors) {
292 return buildTpId(base, linkDescriptors.getMultiTopologyId(), linkDescriptors.getIpv4InterfaceAddress(),
293 linkDescriptors.getIpv6InterfaceAddress(), linkDescriptors.getLinkLocalIdentifier());
296 private static TerminationPoint buildTp(final TpId id, final TerminationPointType type) {
297 final TerminationPointBuilder stpb = new TerminationPointBuilder();
298 stpb.setKey(new TerminationPointKey(id));
302 stpb.addAugmentation(TerminationPoint1.class, new TerminationPoint1Builder()
303 .setIgpTerminationPointAttributes(
304 new IgpTerminationPointAttributesBuilder().setTerminationPointType(type).build()).build());
310 private static TerminationPointType getTpType(final Ipv4InterfaceIdentifier ipv4InterfaceIdentifier,
311 final Ipv6InterfaceIdentifier ipv6InterfaceIdentifier, final Long id) {
312 // Order of preference: Unnumbered first, then IP
314 LOG.debug("Unnumbered termination point type: {}", id);
315 return new UnnumberedBuilder().setUnnumberedId(id).build();
319 if (ipv6InterfaceIdentifier != null) {
320 ip = new IpAddress(ipv6InterfaceIdentifier);
321 } else if (ipv4InterfaceIdentifier != null) {
322 ip = new IpAddress(ipv4InterfaceIdentifier);
328 LOG.debug("IP termination point type: {}", ip);
329 return new IpBuilder().setIpAddress(Lists.newArrayList(ip)).build();
335 private static TerminationPoint buildLocalTp(final UriBuilder base, final LinkDescriptors linkDescriptors) {
336 final TpId id = buildLocalTpId(base, linkDescriptors);
337 final TerminationPointType t = getTpType(linkDescriptors.getIpv4InterfaceAddress(),
338 linkDescriptors.getIpv6InterfaceAddress(),
339 linkDescriptors.getLinkLocalIdentifier());
341 return buildTp(id, t);
344 private static TpId buildRemoteTpId(final UriBuilder base, final LinkDescriptors linkDescriptors) {
345 return buildTpId(base, linkDescriptors.getMultiTopologyId(), linkDescriptors.getIpv4NeighborAddress(),
346 linkDescriptors.getIpv6NeighborAddress(), linkDescriptors.getLinkRemoteIdentifier());
349 private static TerminationPoint buildRemoteTp(final UriBuilder base, final LinkDescriptors linkDescriptors) {
350 final TpId id = buildRemoteTpId(base, linkDescriptors);
351 final TerminationPointType t = getTpType(linkDescriptors.getIpv4NeighborAddress(),
352 linkDescriptors.getIpv6NeighborAddress(),
353 linkDescriptors.getLinkRemoteIdentifier());
355 return buildTp(id, t);
358 private InstanceIdentifier<Link> buildLinkIdentifier(final LinkId id) {
359 return getInstanceIdentifier().child(
360 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology
361 .topology.Link.class, new LinkKey(id));
364 private NodeHolder getNode(final NodeId id) {
365 if (this.nodes.containsKey(id)) {
366 LOG.debug("Node {} is already present", id);
367 return this.nodes.get(id);
370 final NodeHolder ret = new NodeHolder(id);
371 this.nodes.put(id, ret);
375 private void putNode(final WriteTransaction trans, final NodeHolder holder) {
376 if (holder.syncState(trans)) {
377 this.nodes.remove(holder.getNodeId());
381 private void checkNodeForRemoval(final WriteTransaction trans, final NodeHolder holder) {
382 if (holder.checkForRemoval(trans)) {
383 this.nodes.remove(holder.getNodeId());
387 private void createLink(final WriteTransaction trans, final UriBuilder base,
388 final LinkstateRoute value, final LinkCase linkCase, final Attributes attributes) {
390 final LinkAttributes la;
391 final Attributes1 attr = attributes.getAugmentation(Attributes1.class);
393 final LinkStateAttribute attrType = attr.getLinkStateAttribute();
394 if (attrType != null) {
395 la = ((LinkAttributesCase)attrType).getLinkAttributes();
397 LOG.debug("Missing attribute type in link {} route {}, skipping it", linkCase, value);
401 LOG.debug("Missing attributes in link {} route {}, skipping it", linkCase, value);
405 final IgpLinkAttributesBuilder ilab = new IgpLinkAttributesBuilder();
407 if (la.getMetric() != null) {
408 ilab.setMetric(la.getMetric().getValue());
410 ilab.setName(la.getLinkName());
412 ProtocolUtil.augmentProtocolId(value, ilab, la, linkCase.getLinkDescriptors());
414 final LinkBuilder lb = new LinkBuilder();
415 lb.setLinkId(buildLinkId(base, linkCase));
416 lb.addAugmentation(Link1.class, new Link1Builder().setIgpLinkAttributes(ilab.build()).build());
418 final NodeId srcNode = buildNodeId(base, linkCase.getLocalNodeDescriptors());
419 LOG.trace("Link {} implies source node {}", linkCase, srcNode);
421 final NodeId dstNode = buildNodeId(base, linkCase.getRemoteNodeDescriptors());
422 LOG.trace("Link {} implies destination node {}", linkCase, dstNode);
424 final TerminationPoint srcTp = buildLocalTp(base, linkCase.getLinkDescriptors());
425 LOG.trace("Link {} implies source TP {}", linkCase, srcTp);
427 final TerminationPoint dstTp = buildRemoteTp(base, linkCase.getLinkDescriptors());
428 LOG.trace("Link {} implies destination TP {}", linkCase, dstTp);
430 lb.setSource(new SourceBuilder().setSourceNode(srcNode).setSourceTp(srcTp.getTpId()).build());
431 lb.setDestination(new DestinationBuilder().setDestNode(dstNode).setDestTp(dstTp.getTpId()).build());
433 LOG.trace("Created TP {} as link source", srcTp);
434 NodeHolder snh = this.nodes.get(srcNode);
436 snh = getNode(srcNode);
437 snh.addTp(srcTp, lb.getLinkId(), false);
440 snh.addTp(srcTp, lb.getLinkId(), false);
441 final InstanceIdentifier<Node> nid = getNodeInstanceIdentifier(new NodeKey(snh.getNodeId()));
442 trans.put(LogicalDatastoreType.OPERATIONAL, nid.child(TerminationPoint.class, srcTp.getKey()), srcTp);
445 LOG.debug("Created TP {} as link destination", dstTp);
446 NodeHolder dnh = this.nodes.get(dstNode);
448 dnh = getNode(dstNode);
449 dnh.addTp(dstTp, lb.getLinkId(), true);
452 dnh.addTp(dstTp, lb.getLinkId(), true);
453 final InstanceIdentifier<Node> nid = getInstanceIdentifier().child(
454 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network
455 .topology.topology.Node.class, new NodeKey(dnh.getNodeId()));
456 trans.put(LogicalDatastoreType.OPERATIONAL, nid.child(TerminationPoint.class, dstTp.getKey()), dstTp);
459 final InstanceIdentifier<Link> lid = buildLinkIdentifier(lb.getLinkId());
460 final Link link = lb.build();
462 trans.put(LogicalDatastoreType.OPERATIONAL, lid, link);
463 LOG.debug("Created link {} at {} for {}", link, lid, linkCase);
466 private void removeTp(final WriteTransaction trans, final NodeId node, final TpId tp,
467 final LinkId link, final boolean isRemote) {
468 final NodeHolder nh = this.nodes.get(node);
470 final InstanceIdentifier<Node> nid = getNodeInstanceIdentifier(new NodeKey(nh.getNodeId()));
471 trans.delete(LogicalDatastoreType.OPERATIONAL, nid.child(TerminationPoint.class,
472 new TerminationPointKey(tp)));
473 nh.removeTp(tp, link, isRemote);
474 checkNodeForRemoval(trans, nh);
476 LOG.warn("Removed non-existent node {}", node);
480 private void removeLink(final WriteTransaction trans, final UriBuilder base, final LinkCase linkCase) {
481 final LinkId id = buildLinkId(base, linkCase);
482 final InstanceIdentifier<?> lid = buildLinkIdentifier(id);
483 trans.delete(LogicalDatastoreType.OPERATIONAL, lid);
484 LOG.debug("Removed link {}", lid);
486 removeTp(trans, buildNodeId(base, linkCase.getLocalNodeDescriptors()),
487 buildLocalTpId(base, linkCase.getLinkDescriptors()), id, false);
488 removeTp(trans, buildNodeId(base, linkCase.getRemoteNodeDescriptors()),
489 buildRemoteTpId(base, linkCase.getLinkDescriptors()), id, true);
492 private void createNode(final WriteTransaction trans, final UriBuilder base,
493 final LinkstateRoute value, final NodeCase nodeCase, final Attributes attributes) {
494 final NodeAttributes na;
496 final Attributes1 attr = attributes.getAugmentation(Attributes1.class);
498 final LinkStateAttribute attrType = attr.getLinkStateAttribute();
499 if (attrType != null) {
500 na = ((NodeAttributesCase)attrType).getNodeAttributes();
502 LOG.debug("Missing attribute type in node {} route {}, skipping it", nodeCase, value);
506 LOG.debug("Missing attributes in node {} route {}, skipping it", nodeCase, value);
509 final IgpNodeAttributesBuilder inab = new IgpNodeAttributesBuilder();
510 final List<IpAddress> ids = new ArrayList<>();
512 if (na.getIpv4RouterId() != null) {
513 ids.add(new IpAddress(na.getIpv4RouterId()));
515 if (na.getIpv6RouterId() != null) {
516 ids.add(new IpAddress(na.getIpv6RouterId()));
518 if (na.getDynamicHostname() != null) {
519 inab.setName(new DomainName(na.getDynamicHostname()));
522 if (!ids.isEmpty()) {
523 inab.setRouterId(ids);
525 ProtocolUtil.augmentProtocolId(value, inab, na, nodeCase.getNodeDescriptors());
527 final NodeId nid = buildNodeId(base, nodeCase.getNodeDescriptors());
528 final NodeHolder nh = getNode(nid);
530 * Eventhough the the holder creates a dummy structure, we need to duplicate it here,
531 * as that is the API requirement. The reason for it is the possible presence of supporting
532 * node -- something which the holder does not track.
534 final NodeBuilder nb = new NodeBuilder();
536 nb.setKey(new NodeKey(nb.getNodeId()));
538 nh.advertized(nb, inab);
542 private void removeNode(final WriteTransaction trans, final UriBuilder base, final NodeCase nodeCase) {
543 final NodeId id = buildNodeId(base, nodeCase.getNodeDescriptors());
544 final NodeHolder nh = this.nodes.get(id);
549 LOG.warn("Node {} does not have a holder", id);
553 private void createPrefix(final WriteTransaction trans, final UriBuilder base,
554 final LinkstateRoute value, final PrefixCase prefixCase, final Attributes attributes) {
555 final IpPrefix ippfx = prefixCase.getPrefixDescriptors().getIpReachabilityInformation();
557 LOG.warn("IP reachability not present in prefix {} route {}, skipping it", prefixCase, value);
560 final PrefixBuilder pb = new PrefixBuilder();
561 final PrefixKey pk = new PrefixKey(ippfx);
565 final PrefixAttributes pa;
566 // Very defensive lookup
567 final Attributes1 attr = attributes.getAugmentation(Attributes1.class);
569 final LinkStateAttribute attrType = attr.getLinkStateAttribute();
570 if (attrType != null) {
571 pa = ((PrefixAttributesCase)attrType).getPrefixAttributes();
573 LOG.debug("Missing attribute type in IP {} prefix {} route {}, skipping it", ippfx, prefixCase, value);
577 LOG.debug("Missing attributes in IP {} prefix {} route {}, skipping it", ippfx, prefixCase, value);
581 pb.setMetric(pa.getPrefixMetric().getValue());
583 ProtocolUtil.augmentProtocolId(value, pa, pb);
585 final Prefix pfx = pb.build();
586 LOG.debug("Created prefix {} for {}", pfx, prefixCase);
589 * All set, but... the hosting node may not exist, we may need to fake it.
591 final NodeId node = buildNodeId(base, prefixCase.getAdvertisingNodeDescriptors());
592 NodeHolder nh = this.nodes.get(node);
599 final InstanceIdentifier<Node> nid = getNodeInstanceIdentifier(new NodeKey(nh.getNodeId()));
600 final InstanceIdentifier<IgpNodeAttributes> inaId = nid.builder().augmentation(Node1.class)
601 .child(IgpNodeAttributes.class).build();
602 trans.put(LogicalDatastoreType.OPERATIONAL, inaId.child(Prefix.class, pk), pfx);
606 private void removePrefix(final WriteTransaction trans, final UriBuilder base, final PrefixCase prefixCase) {
607 final NodeId node = buildNodeId(base, prefixCase.getAdvertisingNodeDescriptors());
608 final NodeHolder nh = this.nodes.get(node);
610 LOG.debug("Removed prefix {}", prefixCase);
611 final InstanceIdentifier<Node> nid = getNodeInstanceIdentifier(new NodeKey(nh.getNodeId()));
612 final InstanceIdentifier<IgpNodeAttributes> inaId = nid.builder().augmentation(Node1.class)
613 .child(IgpNodeAttributes.class).build();
614 final IpPrefix ippfx = prefixCase.getPrefixDescriptors().getIpReachabilityInformation();
616 LOG.warn("IP reachability not present in prefix {}, skipping it", prefixCase);
619 final PrefixKey pk = new PrefixKey(ippfx);
620 trans.delete(LogicalDatastoreType.OPERATIONAL, inaId.child(Prefix.class, pk));
621 nh.removePrefix(prefixCase);
622 checkNodeForRemoval(trans, nh);
624 LOG.warn("Removing prefix from non-existing node {}", node);
628 private InstanceIdentifier<Node> getNodeInstanceIdentifier(final NodeKey nodeKey) {
629 return getInstanceIdentifier().child(
630 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network
631 .topology.topology.Node.class, nodeKey);
635 protected void createObject(final ReadWriteTransaction trans,
636 final InstanceIdentifier<LinkstateRoute> id, final LinkstateRoute value) {
637 final UriBuilder base = new UriBuilder(value);
639 final ObjectType t = value.getObjectType();
640 Preconditions.checkArgument(t != null, "Route %s value %s has null object type", id, value);
642 if (t instanceof LinkCase) {
643 createLink(trans, base, value, (LinkCase) t, value.getAttributes());
644 } else if (t instanceof NodeCase) {
645 createNode(trans, base, value, (NodeCase) t, value.getAttributes());
646 } else if (t instanceof PrefixCase) {
647 createPrefix(trans, base, value, (PrefixCase) t, value.getAttributes());
649 LOG.debug(UNHANDLED_OBJECT_CLASS, t.getImplementedInterface());
654 protected void removeObject(final ReadWriteTransaction trans,
655 final InstanceIdentifier<LinkstateRoute> id, final LinkstateRoute value) {
657 LOG.error("Empty before-data received in delete data change notification for instance id {}", id);
661 final UriBuilder base = new UriBuilder(value);
663 final ObjectType t = value.getObjectType();
664 if (t instanceof LinkCase) {
665 removeLink(trans, base, (LinkCase) t);
666 } else if (t instanceof NodeCase) {
667 removeNode(trans, base, (NodeCase) t);
668 } else if (t instanceof PrefixCase) {
669 removePrefix(trans, base, (PrefixCase) t);
671 LOG.debug(UNHANDLED_OBJECT_CLASS, t.getImplementedInterface());
675 @SuppressWarnings({ "rawtypes", "unchecked" })
677 protected InstanceIdentifier<LinkstateRoute> getRouteWildcard(final InstanceIdentifier<Tables> tablesId) {
678 return tablesId.child((Class)LinkstateRoutes.class).child(LinkstateRoute.class);
682 protected void clearTopology() {