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 java.math.BigDecimal;
11 import java.nio.ByteBuffer;
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.HashSet;
15 import java.util.List;
19 import org.opendaylight.controller.md.sal.common.api.data.DataModification;
20 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
21 import org.opendaylight.protocol.bgp.rib.RibReference;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.DomainName;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.Ipv4InterfaceIdentifier;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.Ipv6InterfaceIdentifier;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.NodeFlagBits;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.NodeIdentifier;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.TopologyIdentifier;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.LinkstateRoute;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.Attributes1;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.ObjectType;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.attributes.attribute.type.link._case.LinkAttributes;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.attributes.attribute.type.node._case.NodeAttributes;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.attributes.attribute.type.prefix._case.PrefixAttributes;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.object.type.LinkCase;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.object.type.NodeCase;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.object.type.PrefixCase;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.object.type.link._case.LinkDescriptors;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.object.type.node._case.NodeDescriptors;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.CRouterIdentifier;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.IsisNodeCase;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.IsisPseudonodeCase;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.OspfNodeCase;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.OspfPseudonodeCase;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.isis.node._case.IsisNode;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.isis.pseudonode._case.IsisPseudonode;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.ospf.node._case.OspfNode;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.ospf.pseudonode._case.OspfPseudonode;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.route.Attributes;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IsoPseudonodeId;
53 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IsoSystemId;
54 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.link.attributes.IsisLinkAttributesBuilder;
55 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.node.attributes.IsisNodeAttributesBuilder;
56 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.node.attributes.isis.node.attributes.IsoBuilder;
57 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.UnreservedBandwidth;
58 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.UnreservedBandwidthBuilder;
59 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.UnreservedBandwidthKey;
60 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.LinkId;
61 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
62 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
63 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
64 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.DestinationBuilder;
65 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.SourceBuilder;
66 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
67 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkBuilder;
68 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkKey;
69 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
70 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
71 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
72 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.TopologyTypesBuilder;
73 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
74 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
75 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
76 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Link1;
77 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Link1Builder;
78 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1;
79 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1Builder;
80 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TerminationPoint1;
81 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TerminationPoint1Builder;
82 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TopologyTypes1;
83 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TopologyTypes1Builder;
84 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.link.attributes.IgpLinkAttributesBuilder;
85 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.IgpNodeAttributesBuilder;
86 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;
87 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;
88 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;
89 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.termination.point.attributes.IgpTerminationPointAttributesBuilder;
90 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;
91 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;
92 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;
93 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.Prefix1;
94 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.Prefix1Builder;
95 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.link.attributes.OspfLinkAttributesBuilder;
96 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.OspfNodeAttributesBuilder;
97 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.ospf.node.attributes.router.type.AbrBuilder;
98 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.ospf.node.attributes.router.type.InternalBuilder;
99 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.ospf.node.attributes.router.type.PseudonodeBuilder;
100 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.prefix.attributes.OspfPrefixAttributesBuilder;
101 import org.opendaylight.yangtools.yang.binding.DataObject;
102 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
103 import org.slf4j.Logger;
104 import org.slf4j.LoggerFactory;
106 import com.google.common.base.Function;
107 import com.google.common.base.Preconditions;
108 import com.google.common.collect.Collections2;
109 import com.google.common.collect.Lists;
111 public final class LinkstateTopologyBuilder extends AbstractTopologyBuilder<LinkstateRoute> {
112 private static final class TpHolder {
113 private final Set<LinkId> local = new HashSet<>();
114 private final Set<LinkId> remote = new HashSet<>();
116 private final TerminationPoint tp;
118 private TpHolder(final TerminationPoint tp) {
119 this.tp = Preconditions.checkNotNull(tp);
122 private synchronized void addLink(final LinkId id, final boolean isRemote) {
130 private synchronized boolean removeLink(final LinkId id, final boolean isRemote) {
131 final boolean removed;
133 removed = this.remote.remove(id);
135 removed = this.local.remove(id);
138 LOG.warn("Removed non-reference link {} from TP {} isRemote {}", this.tp.getTpId(), id, isRemote);
141 return this.local.isEmpty() && this.remote.isEmpty();
144 private TerminationPoint getTp() {
149 private final class NodeHolder {
150 private final Map<PrefixKey, Prefix> prefixes = new HashMap<>();
151 private final Map<TpId, TpHolder> tps = new HashMap<>();
152 private boolean advertized = false;
153 private IgpNodeAttributesBuilder inab;
154 private NodeBuilder nb;
156 private NodeHolder(final NodeId id) {
157 this.inab = new IgpNodeAttributesBuilder();
158 this.nb = new NodeBuilder().setKey(new NodeKey(id)).setNodeId(id);
162 * Synchronized in-core state of a node into the backing store using the transaction
164 * @param trans data modification transaction which to use
165 * @return True if the node has been purged, false otherwise.
167 private boolean syncState(final DataModification<InstanceIdentifier<?>, DataObject> trans) {
168 final InstanceIdentifier<Node> nid = getInstanceIdentifier().child(
169 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node.class,
173 * Transaction's putOperationalData() does a merge. Force it onto a replace
174 * by removing the data. If we decide to remove the node -- we just skip the put.
176 trans.removeOperationalData(nid);
178 if (!this.advertized) {
179 if (this.tps.isEmpty() && this.prefixes.isEmpty()) {
180 LOG.debug("Removing unadvertized unused node {}", this.nb.getNodeId());
184 LOG.debug("Node {} is still implied by {} TPs and {} prefixes", this.nb.getNodeId(), this.tps.size(), this.prefixes.size());
187 // Re-generate termination points
188 this.nb.setTerminationPoint(Lists.newArrayList(Collections2.transform(this.tps.values(),
189 new Function<TpHolder, TerminationPoint>() {
191 public TerminationPoint apply(final TpHolder input) {
192 return input.getTp();
196 // Re-generate prefixes
197 this.inab.setPrefix(Lists.newArrayList(this.prefixes.values()));
199 // Write the node out
200 final Node n = this.nb.addAugmentation(Node1.class, new Node1Builder().setIgpNodeAttributes(this.inab.build()).build()).build();
201 trans.putOperationalData(nid, n);
202 LOG.debug("Created node {} at {}", n, nid);
206 private synchronized void removeTp(final TpId tp, final LinkId link, final boolean isRemote) {
207 final TpHolder h = this.tps.get(tp);
209 if (h.removeLink(link, isRemote)) {
211 LOG.debug("Removed TP {}", tp);
214 LOG.warn("Removed non-present TP {} by link {}", tp, link);
218 private void addTp(final TerminationPoint tp, final LinkId link, final boolean isRemote) {
219 TpHolder h = this.tps.get(tp.getTpId());
221 h = new TpHolder(tp);
222 this.tps.put(tp.getTpId(), h);
225 h.addLink(link, isRemote);
228 private void addPrefix(final Prefix pfx) {
229 this.prefixes.put(pfx.getKey(), pfx);
232 private void removePrefix(final PrefixCase p) {
233 this.prefixes.remove(new PrefixKey(p.getIpReachabilityInformation()));
236 private void unadvertized() {
237 this.inab = new IgpNodeAttributesBuilder();
238 this.nb = new NodeBuilder().setKey(this.nb.getKey()).setNodeId(this.nb.getNodeId());
239 this.advertized = false;
240 LOG.debug("Node {} is unadvertized", this.nb.getNodeId());
243 private void advertized(final NodeBuilder nb, final IgpNodeAttributesBuilder inab) {
244 this.nb = Preconditions.checkNotNull(nb);
245 this.inab = Preconditions.checkNotNull(inab);
246 this.advertized = true;
247 LOG.debug("Node {} is advertized", nb.getNodeId());
250 private Object getNodeId() {
251 return this.nb.getNodeId();
255 private static final Logger LOG = LoggerFactory.getLogger(LinkstateTopologyBuilder.class);
256 private final Map<NodeId, NodeHolder> nodes = new HashMap<>();
258 public LinkstateTopologyBuilder(final DataProviderService dataProvider, final RibReference locRibReference, final TopologyId topologyId) {
259 super(dataProvider, locRibReference, topologyId, new TopologyTypesBuilder().addAugmentation(TopologyTypes1.class,
260 new TopologyTypes1Builder().build()).build(), LinkstateRoute.class);
263 private LinkId buildLinkId(final UriBuilder base, final LinkCase link) {
264 return new LinkId(new UriBuilder(base, "link").add(link).toString());
267 private NodeId buildNodeId(final UriBuilder base, final NodeIdentifier node) {
268 return new NodeId(new UriBuilder(base, "node").add("", node).toString());
271 private TpId buildTpId(final UriBuilder base, final TopologyIdentifier topologyIdentifier,
272 final Ipv4InterfaceIdentifier ipv4InterfaceIdentifier, final Ipv6InterfaceIdentifier ipv6InterfaceIdentifier, final Long id) {
273 final UriBuilder b = new UriBuilder(base, "tp");
274 if (topologyIdentifier != null) {
275 b.add("mt", topologyIdentifier.getValue());
277 if (ipv4InterfaceIdentifier != null) {
278 b.add("ipv4", ipv4InterfaceIdentifier.getValue());
280 if (ipv6InterfaceIdentifier != null) {
281 b.add("ipv6", ipv6InterfaceIdentifier.getValue());
284 return new TpId(b.add("id", id).toString());
287 private TpId buildLocalTpId(final UriBuilder base, final LinkDescriptors linkDescriptors) {
288 return buildTpId(base, linkDescriptors.getMultiTopologyId(), linkDescriptors.getIpv4InterfaceAddress(),
289 linkDescriptors.getIpv6InterfaceAddress(), linkDescriptors.getLinkLocalIdentifier());
292 private TerminationPoint buildTp(final TpId id, final TerminationPointType type) {
293 final TerminationPointBuilder stpb = new TerminationPointBuilder();
294 stpb.setKey(new TerminationPointKey(id));
298 stpb.addAugmentation(
299 TerminationPoint1.class,
300 new TerminationPoint1Builder().setIgpTerminationPointAttributes(
301 new IgpTerminationPointAttributesBuilder().setTerminationPointType(null).build()).build());
307 private TerminationPointType getTpType(final Ipv4InterfaceIdentifier ipv4InterfaceIdentifier,
308 final Ipv6InterfaceIdentifier ipv6InterfaceIdentifier, final Long id) {
309 // Order of preference: Unnumbered first, then IP
311 LOG.debug("Unnumbered termination point type: {}", id);
312 return new UnnumberedBuilder().setUnnumberedId(id).build();
316 if (ipv6InterfaceIdentifier != null) {
317 ip = new IpAddress(ipv6InterfaceIdentifier);
318 } else if (ipv4InterfaceIdentifier != null) {
319 ip = new IpAddress(ipv4InterfaceIdentifier);
325 LOG.debug("IP termination point type: {}", ip);
326 return new IpBuilder().setIpAddress(Lists.newArrayList(ip)).build();
332 private TerminationPoint buildLocalTp(final UriBuilder base, final LinkDescriptors linkDescriptors) {
333 final TpId id = buildLocalTpId(base, linkDescriptors);
334 final TerminationPointType t = getTpType(linkDescriptors.getIpv4InterfaceAddress(), linkDescriptors.getIpv6InterfaceAddress(),
335 linkDescriptors.getLinkLocalIdentifier());
337 return buildTp(id, t);
340 private TpId buildRemoteTpId(final UriBuilder base, final LinkDescriptors linkDescriptors) {
341 return buildTpId(base, linkDescriptors.getMultiTopologyId(), linkDescriptors.getIpv4NeighborAddress(),
342 linkDescriptors.getIpv6NeighborAddress(), linkDescriptors.getLinkRemoteIdentifier());
345 private TerminationPoint buildRemoteTp(final UriBuilder base, final LinkDescriptors linkDescriptors) {
346 final TpId id = buildRemoteTpId(base, linkDescriptors);
347 final TerminationPointType t = getTpType(linkDescriptors.getIpv4NeighborAddress(), linkDescriptors.getIpv6NeighborAddress(),
348 linkDescriptors.getLinkRemoteIdentifier());
350 return buildTp(id, t);
353 private InstanceIdentifier<?> buildLinkIdentifier(final UriBuilder base, final LinkId id) {
354 return getInstanceIdentifier().child(
355 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link.class,
359 private static Float bandwidthToFloat(final Bandwidth bandwidth) {
360 return ByteBuffer.wrap(bandwidth.getValue()).getFloat();
363 private static BigDecimal bandwidthToBigDecimal(final Bandwidth bandwidth) {
364 return BigDecimal.valueOf(bandwidthToFloat(bandwidth));
367 private static List<UnreservedBandwidth> unreservedBandwidthList(
368 final List<? extends org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.UnreservedBandwidth> input) {
369 final List<UnreservedBandwidth> ret = new ArrayList<>(input.size());
371 for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.UnreservedBandwidth i : input) {
372 ret.add(new UnreservedBandwidthBuilder().setBandwidth(bandwidthToBigDecimal(i.getBandwidth())).setKey(
373 new UnreservedBandwidthKey(i.getPriority())).build());
379 private org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpLinkAttributes1 isisLinkAttributes(
380 final TopologyIdentifier topologyIdentifier, final LinkAttributes la) {
381 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();
384 if (la.getAdminGroup() != null) {
385 tb.setColor(la.getAdminGroup().getValue());
387 if (la.getTeMetric() != null) {
388 tb.setTeDefaultMetric(la.getTeMetric().getValue());
390 if (la.getUnreservedBandwidth() != null) {
391 tb.setUnreservedBandwidth(unreservedBandwidthList(la.getUnreservedBandwidth()));
393 if (la.getMaxLinkBandwidth() != null) {
394 tb.setMaxLinkBandwidth(bandwidthToBigDecimal(la.getMaxLinkBandwidth()));
396 if (la.getMaxReservableBandwidth() != null) {
397 tb.setMaxResvLinkBandwidth(bandwidthToBigDecimal(la.getMaxReservableBandwidth()));
401 final IsisLinkAttributesBuilder ilab = new IsisLinkAttributesBuilder();
402 ilab.setTed(tb.build());
403 if (topologyIdentifier != null) {
404 ilab.setMultiTopologyId(topologyIdentifier.getValue().shortValue());
407 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpLinkAttributes1Builder().setIsisLinkAttributes(
408 ilab.build()).build();
411 private org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1 ospfLinkAttributes(
412 final TopologyIdentifier topologyIdentifier, final LinkAttributes la) {
413 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();
416 if (la.getAdminGroup() != null) {
417 tb.setColor(la.getAdminGroup().getValue());
419 if (la.getTeMetric() != null) {
420 tb.setTeDefaultMetric(la.getTeMetric().getValue());
422 if (la.getUnreservedBandwidth() != null) {
423 tb.setUnreservedBandwidth(unreservedBandwidthList(la.getUnreservedBandwidth()));
425 if (la.getMaxLinkBandwidth() != null) {
426 tb.setMaxLinkBandwidth(bandwidthToBigDecimal(la.getMaxLinkBandwidth()));
428 if (la.getMaxReservableBandwidth() != null) {
429 tb.setMaxResvLinkBandwidth(bandwidthToBigDecimal(la.getMaxReservableBandwidth()));
433 final OspfLinkAttributesBuilder ilab = new OspfLinkAttributesBuilder();
434 ilab.setTed(tb.build());
435 if (topologyIdentifier != null) {
436 ilab.setMultiTopologyId(topologyIdentifier.getValue().shortValue());
439 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1Builder().setOspfLinkAttributes(
440 ilab.build()).build();
443 private NodeHolder getNode(final NodeId id) {
444 if (this.nodes.containsKey(id)) {
445 LOG.debug("Node {} is already present", id);
446 return this.nodes.get(id);
449 final NodeHolder ret = new NodeHolder(id);
450 this.nodes.put(id, ret);
454 private void putNode(final DataModification<InstanceIdentifier<?>, DataObject> trans, final NodeHolder holder) {
455 if (holder.syncState(trans)) {
456 this.nodes.remove(holder.getNodeId());
460 private void createLink(final DataModification<InstanceIdentifier<?>, DataObject> trans, final UriBuilder base,
461 final LinkstateRoute value, final LinkCase l, final Attributes attributes) {
462 final LinkAttributes la = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.attributes.attribute.type.LinkCase) attributes.getAugmentation(
463 Attributes1.class).getAttributeType()).getLinkAttributes();
465 final IgpLinkAttributesBuilder ilab = new IgpLinkAttributesBuilder();
467 if (la.getMetric() != null) {
468 ilab.setMetric(la.getMetric().getValue());
470 ilab.setName(la.getLinkName());
473 switch (value.getProtocolId()) {
480 ilab.addAugmentation(
481 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpLinkAttributes1.class,
482 isisLinkAttributes(l.getLinkDescriptors().getMultiTopologyId(), la));
485 ilab.addAugmentation(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1.class,
486 ospfLinkAttributes(l.getLinkDescriptors().getMultiTopologyId(), la));
490 final LinkBuilder lb = new LinkBuilder();
491 lb.setLinkId(buildLinkId(base, l));
492 lb.addAugmentation(Link1.class, new Link1Builder().setIgpLinkAttributes(ilab.build()).build());
494 final NodeId srcNode = buildNodeId(base, l.getLocalNodeDescriptors());
495 LOG.debug("Link {} implies source node {}", l, srcNode);
497 final NodeId dstNode = buildNodeId(base, l.getRemoteNodeDescriptors());
498 LOG.debug("Link {} implies destination node {}", l, dstNode);
500 final TerminationPoint srcTp = buildLocalTp(base, l.getLinkDescriptors());
501 LOG.debug("Link {} implies source TP {}", l, srcTp);
503 final TerminationPoint dstTp = buildRemoteTp(base, l.getLinkDescriptors());
504 LOG.debug("Link {} implies destination TP {}", l, dstTp);
506 lb.setSource(new SourceBuilder().setSourceNode(srcNode).setSourceTp(srcTp.getTpId()).build());
507 lb.setDestination(new DestinationBuilder().setDestNode(dstNode).setDestTp(dstTp.getTpId()).build());
509 final NodeHolder snh = getNode(srcNode);
510 snh.addTp(srcTp, lb.getLinkId(), false);
511 LOG.debug("Created TP {} as link source", srcTp);
514 final NodeHolder dnh = getNode(dstNode);
515 dnh.addTp(dstTp, lb.getLinkId(), true);
516 LOG.debug("Created TP {} as link destination", dstTp);
519 final InstanceIdentifier<?> lid = buildLinkIdentifier(base, lb.getLinkId());
520 final Link link = lb.build();
523 * Transaction's putOperationalData() does a merge. Force it onto a replace
524 * by removing the data.
526 trans.removeOperationalData(lid);
527 trans.putOperationalData(lid, link);
528 LOG.debug("Created link {} at {} for {}", link, lid, l);
531 private void removeTp(final DataModification<InstanceIdentifier<?>, DataObject> trans, final NodeId node, final TpId tp,
532 final LinkId link, final boolean isRemote) {
533 final NodeHolder nh = this.nodes.get(node);
535 nh.removeTp(tp, link, isRemote);
538 LOG.warn("Removed non-existent node {}", node);
542 private void removeLink(final DataModification<InstanceIdentifier<?>, DataObject> trans, final UriBuilder base, final LinkCase l) {
543 final LinkId id = buildLinkId(base, l);
544 final InstanceIdentifier<?> lid = buildLinkIdentifier(base, id);
545 trans.removeOperationalData(lid);
546 LOG.debug("Removed link {}", lid);
548 removeTp(trans, buildNodeId(base, l.getLocalNodeDescriptors()), buildLocalTpId(base, l.getLinkDescriptors()), id, false);
549 removeTp(trans, buildNodeId(base, l.getRemoteNodeDescriptors()), buildRemoteTpId(base, l.getLinkDescriptors()), id, true);
552 private List<Short> nodeMultiTopology(final List<TopologyIdentifier> list) {
553 final List<Short> ret = new ArrayList<>(list.size());
554 for (final TopologyIdentifier id : list) {
555 ret.add(id.getValue().shortValue());
560 private org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1 isisNodeAttributes(
561 final NodeIdentifier node, final NodeAttributes na) {
562 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();
563 final IsisNodeAttributesBuilder ab = new IsisNodeAttributesBuilder();
566 if (na.getIpv4RouterId() != null) {
567 tb.setTeRouterIdIpv4(na.getIpv4RouterId());
569 if (na.getIpv6RouterId() != null) {
570 tb.setTeRouterIdIpv6(na.getIpv6RouterId());
572 if (na.getTopologyIdentifier() != null) {
573 ab.setMultiTopologyId(nodeMultiTopology(na.getTopologyIdentifier()));
577 final CRouterIdentifier ri = node.getCRouterIdentifier();
578 if (ri instanceof IsisPseudonodeCase) {
579 final IsisPseudonode pn = ((IsisPseudonodeCase) ri).getIsisPseudonode();
580 ab.setIso(new IsoBuilder().setIsoPseudonodeId(new IsoPseudonodeId(pn.toString())).build());
581 } else if (ri instanceof IsisNodeCase) {
582 final IsisNode in = ((IsisNodeCase) ri).getIsisNode();
583 ab.setIso(new IsoBuilder().setIsoSystemId(new IsoSystemId(in.getIsoSystemId().toString())).build());
586 ab.setTed(tb.build());
588 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1Builder().setIsisNodeAttributes(
592 private org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1 ospfNodeAttributes(
593 final NodeDescriptors node, final NodeAttributes na) {
594 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();
595 final OspfNodeAttributesBuilder ab = new OspfNodeAttributesBuilder();
598 if (na.getIpv4RouterId() != null) {
599 tb.setTeRouterIdIpv4(na.getIpv4RouterId());
601 if (na.getIpv6RouterId() != null) {
602 tb.setTeRouterIdIpv6(na.getIpv6RouterId());
604 if (na.getTopologyIdentifier() != null) {
605 ab.setMultiTopologyId(nodeMultiTopology(na.getTopologyIdentifier()));
609 final CRouterIdentifier ri = node.getCRouterIdentifier();
610 if (ri instanceof OspfPseudonodeCase) {
611 final OspfPseudonode pn = ((OspfPseudonodeCase) ri).getOspfPseudonode();
613 ab.setRouterType(new PseudonodeBuilder().setPseudonode(Boolean.TRUE).build());
614 ab.setDrInterfaceId(pn.getLanInterface().getValue());
615 } else if (ri instanceof OspfNodeCase) {
616 final OspfNode in = ((OspfNodeCase) ri).getOspfNode();
618 // TODO: what should we do with in.getOspfRouterId()?
621 final NodeFlagBits nf = na.getNodeFlags();
624 ab.setRouterType(new AbrBuilder().setAbr(Boolean.TRUE).build());
625 } else if (!nf.isExternal()) {
626 ab.setRouterType(new InternalBuilder().setInternal(Boolean.TRUE).build());
632 ab.setTed(tb.build());
634 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1Builder().setOspfNodeAttributes(
638 private void createNode(final DataModification<InstanceIdentifier<?>, DataObject> trans, final UriBuilder base,
639 final LinkstateRoute value, final NodeCase n, final Attributes attributes) {
640 final NodeAttributes na = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.attributes.attribute.type.NodeCase) attributes.getAugmentation(
641 Attributes1.class).getAttributeType()).getNodeAttributes();
642 final IgpNodeAttributesBuilder inab = new IgpNodeAttributesBuilder();
644 final List<IpAddress> ids = new ArrayList<>();
646 if (na.getIpv4RouterId() != null) {
647 ids.add(new IpAddress(na.getIpv4RouterId()));
649 if (na.getIpv6RouterId() != null) {
650 ids.add(new IpAddress(na.getIpv6RouterId()));
652 if (na.getDynamicHostname() != null) {
653 inab.setName(new DomainName(na.getDynamicHostname()));
657 if (!ids.isEmpty()) {
658 inab.setRouterId(ids);
661 switch (value.getProtocolId()) {
668 inab.addAugmentation(
669 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1.class,
670 isisNodeAttributes(n.getNodeDescriptors(), na));
673 inab.addAugmentation(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1.class,
674 ospfNodeAttributes(n.getNodeDescriptors(), na));
678 final NodeId nid = buildNodeId(base, n.getNodeDescriptors());
679 final NodeHolder nh = getNode(nid);
682 * Eventhough the the holder creates a dummy structure, we need to duplicate it here,
683 * as that is the API requirement. The reason for it is the possible presence of supporting
684 * node -- something which the holder does not track.
686 final NodeBuilder nb = new NodeBuilder();
688 nb.setKey(new NodeKey(nb.getNodeId()));
690 nh.advertized(nb, inab);
694 private void removeNode(final DataModification<InstanceIdentifier<?>, DataObject> trans, final UriBuilder base, final NodeCase n) {
695 final NodeId id = buildNodeId(base, n.getNodeDescriptors());
696 final NodeHolder nh = this.nodes.get(id);
701 LOG.warn("Node {} does not have a holder", id);
705 private void createPrefix(final DataModification<InstanceIdentifier<?>, DataObject> trans, final UriBuilder base,
706 final LinkstateRoute value, final PrefixCase p, final Attributes attributes) {
707 final IpPrefix ippfx = p.getIpReachabilityInformation();
709 LOG.warn("IP reachability not present in prefix {} route {}, skipping it", p, value);
713 final PrefixBuilder pb = new PrefixBuilder();
714 pb.setKey(new PrefixKey(ippfx));
717 final PrefixAttributes pa = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.attributes.attribute.type.PrefixCase) attributes.getAugmentation(
718 Attributes1.class).getAttributeType()).getPrefixAttributes();
720 pb.setMetric(pa.getPrefixMetric().getValue());
723 switch (value.getProtocolId()) {
734 new Prefix1Builder().setOspfPrefixAttributes(
735 new OspfPrefixAttributesBuilder().setForwardingAddress(pa.getOspfForwardingAddress().getIpv4Address()).build()).build());
740 final Prefix pfx = pb.build();
743 * All set, but... the hosting node may not exist, we may need to fake it.
745 final NodeId node = buildNodeId(base, p.getAdvertisingNodeDescriptors());
746 final NodeHolder nh = getNode(node);
748 LOG.debug("Created prefix {} for {}", pfx, p);
752 private void removePrefix(final DataModification<InstanceIdentifier<?>, DataObject> trans, final UriBuilder base, final PrefixCase p) {
753 final NodeId node = buildNodeId(base, p.getAdvertisingNodeDescriptors());
754 final NodeHolder nh = this.nodes.get(node);
757 LOG.debug("Removed prefix {}", p);
760 LOG.warn("Removing prefix from non-existing node {}", node);
765 protected void createObject(final DataModification<InstanceIdentifier<?>, DataObject> trans,
766 final InstanceIdentifier<LinkstateRoute> id, final LinkstateRoute value) {
767 final UriBuilder base = new UriBuilder(value);
769 final ObjectType t = value.getObjectType();
770 if (t instanceof LinkCase) {
771 createLink(trans, base, value, (LinkCase) t, value.getAttributes());
772 } else if (t instanceof NodeCase) {
773 createNode(trans, base, value, (NodeCase) t, value.getAttributes());
774 } else if (t instanceof PrefixCase) {
775 createPrefix(trans, base, value, (PrefixCase) t, value.getAttributes());
777 throw new IllegalStateException("Unhandled object class " + t.getImplementedInterface());
782 protected void removeObject(final DataModification<InstanceIdentifier<?>, DataObject> trans,
783 final InstanceIdentifier<LinkstateRoute> id, final LinkstateRoute value) {
784 final UriBuilder base = new UriBuilder(value);
786 final ObjectType t = value.getObjectType();
787 if (t instanceof LinkCase) {
788 removeLink(trans, base, (LinkCase) t);
789 } else if (t instanceof NodeCase) {
790 removeNode(trans, base, (NodeCase) t);
791 } else if (t instanceof PrefixCase) {
792 removePrefix(trans, base, (PrefixCase) t);
794 throw new IllegalStateException("Unhandled object class " + t.getImplementedInterface());