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.pseudonode._case.OspfPseudonode;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.route.Attributes;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth;
51 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IsoPseudonodeId;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IsoSystemId;
53 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.link.attributes.IsisLinkAttributesBuilder;
54 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.node.attributes.IsisNodeAttributesBuilder;
55 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.node.attributes.isis.node.attributes.IsoBuilder;
56 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.UnreservedBandwidth;
57 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.UnreservedBandwidthBuilder;
58 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.UnreservedBandwidthKey;
59 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.LinkId;
60 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
61 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
62 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
63 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.DestinationBuilder;
64 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.SourceBuilder;
65 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
66 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkBuilder;
67 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkKey;
68 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
69 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
70 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
71 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.TopologyTypesBuilder;
72 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
73 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
74 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
75 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Link1;
76 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Link1Builder;
77 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1;
78 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1Builder;
79 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TerminationPoint1;
80 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TerminationPoint1Builder;
81 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TopologyTypes1;
82 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TopologyTypes1Builder;
83 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.link.attributes.IgpLinkAttributesBuilder;
84 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.IgpNodeAttributesBuilder;
85 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;
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.PrefixBuilder;
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.PrefixKey;
88 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.termination.point.attributes.IgpTerminationPointAttributesBuilder;
89 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;
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.termination.point.type.IpBuilder;
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.UnnumberedBuilder;
92 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.Prefix1;
93 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.Prefix1Builder;
94 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.link.attributes.OspfLinkAttributesBuilder;
95 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.OspfNodeAttributesBuilder;
96 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.ospf.node.attributes.router.type.AbrBuilder;
97 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.ospf.node.attributes.router.type.InternalBuilder;
98 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.ospf.node.attributes.router.type.PseudonodeBuilder;
99 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.prefix.attributes.OspfPrefixAttributesBuilder;
100 import org.opendaylight.yangtools.yang.binding.DataObject;
101 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
102 import org.slf4j.Logger;
103 import org.slf4j.LoggerFactory;
105 import com.google.common.base.Function;
106 import com.google.common.base.Preconditions;
107 import com.google.common.collect.Collections2;
108 import com.google.common.collect.Lists;
110 public final class LinkstateTopologyBuilder extends AbstractTopologyBuilder<LinkstateRoute> {
111 private static final class TpHolder {
112 private final Set<LinkId> local = new HashSet<>();
113 private final Set<LinkId> remote = new HashSet<>();
115 private final TerminationPoint tp;
117 private TpHolder(final TerminationPoint tp) {
118 this.tp = Preconditions.checkNotNull(tp);
121 private synchronized void addLink(final LinkId id, final boolean isRemote) {
129 private synchronized boolean removeLink(final LinkId id, final boolean isRemote) {
130 final boolean removed;
132 removed = this.remote.remove(id);
134 removed = this.local.remove(id);
137 LOG.warn("Removed non-reference link {} from TP {} isRemote {}", this.tp.getTpId(), id, isRemote);
140 return this.local.isEmpty() && this.remote.isEmpty();
143 private TerminationPoint getTp() {
148 private final class NodeHolder {
149 private final Map<PrefixKey, Prefix> prefixes = new HashMap<>();
150 private final Map<TpId, TpHolder> tps = new HashMap<>();
151 private boolean advertized = false;
152 private IgpNodeAttributesBuilder inab;
153 private NodeBuilder nb;
155 private NodeHolder(final NodeId id) {
156 this.inab = new IgpNodeAttributesBuilder();
157 this.nb = new NodeBuilder().setKey(new NodeKey(id)).setNodeId(id);
161 * Synchronized in-core state of a node into the backing store using the transaction
163 * @param trans data modification transaction which to use
164 * @return True if the node has been purged, false otherwise.
166 private boolean syncState(final DataModification<InstanceIdentifier<?>, DataObject> trans) {
167 final InstanceIdentifier<Node> nid = getInstanceIdentifier().child(
168 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node.class,
172 * Transaction's putOperationalData() does a merge. Force it onto a replace
173 * by removing the data. If we decide to remove the node -- we just skip the put.
175 trans.removeOperationalData(nid);
177 if (!this.advertized) {
178 if (this.tps.isEmpty() && this.prefixes.isEmpty()) {
179 LOG.debug("Removing unadvertized unused node {}", this.nb.getNodeId());
183 LOG.debug("Node {} is still implied by {} TPs and {} prefixes", this.nb.getNodeId(), this.tps.size(), this.prefixes.size());
186 // Re-generate termination points
187 this.nb.setTerminationPoint(Lists.newArrayList(Collections2.transform(this.tps.values(),
188 new Function<TpHolder, TerminationPoint>() {
190 public TerminationPoint apply(final TpHolder input) {
191 return input.getTp();
195 // Re-generate prefixes
196 this.inab.setPrefix(Lists.newArrayList(this.prefixes.values()));
198 // Write the node out
199 final Node n = this.nb.addAugmentation(Node1.class, new Node1Builder().setIgpNodeAttributes(this.inab.build()).build()).build();
200 trans.putOperationalData(nid, n);
201 LOG.debug("Created node {} at {}", n, nid);
205 private synchronized void removeTp(final TpId tp, final LinkId link, final boolean isRemote) {
206 final TpHolder h = this.tps.get(tp);
208 if (h.removeLink(link, isRemote)) {
210 LOG.debug("Removed TP {}", tp);
213 LOG.warn("Removed non-present TP {} by link {}", tp, link);
217 private void addTp(final TerminationPoint tp, final LinkId link, final boolean isRemote) {
218 TpHolder h = this.tps.get(tp.getTpId());
220 h = new TpHolder(tp);
221 this.tps.put(tp.getTpId(), h);
224 h.addLink(link, isRemote);
227 private void addPrefix(final Prefix pfx) {
228 this.prefixes.put(pfx.getKey(), pfx);
231 private void removePrefix(final PrefixCase p) {
232 this.prefixes.remove(new PrefixKey(p.getIpReachabilityInformation()));
235 private void unadvertized() {
236 this.inab = new IgpNodeAttributesBuilder();
237 this.nb = new NodeBuilder().setKey(this.nb.getKey()).setNodeId(this.nb.getNodeId());
238 this.advertized = false;
239 LOG.debug("Node {} is unadvertized", this.nb.getNodeId());
242 private void advertized(final NodeBuilder nb, final IgpNodeAttributesBuilder inab) {
243 this.nb = Preconditions.checkNotNull(nb);
244 this.inab = Preconditions.checkNotNull(inab);
245 this.advertized = true;
246 LOG.debug("Node {} is advertized", nb.getNodeId());
249 private Object getNodeId() {
250 return this.nb.getNodeId();
254 private static final Logger LOG = LoggerFactory.getLogger(LinkstateTopologyBuilder.class);
255 private final Map<NodeId, NodeHolder> nodes = new HashMap<>();
257 public LinkstateTopologyBuilder(final DataProviderService dataProvider, final RibReference locRibReference, final TopologyId topologyId) {
258 super(dataProvider, locRibReference, topologyId, new TopologyTypesBuilder().addAugmentation(TopologyTypes1.class,
259 new TopologyTypes1Builder().build()).build(), LinkstateRoute.class);
262 private LinkId buildLinkId(final UriBuilder base, final LinkCase link) {
263 return new LinkId(new UriBuilder(base, "link").add(link).toString());
266 private NodeId buildNodeId(final UriBuilder base, final NodeIdentifier node) {
267 return new NodeId(new UriBuilder(base, "node").add("", node).toString());
270 private TpId buildTpId(final UriBuilder base, final TopologyIdentifier topologyIdentifier,
271 final Ipv4InterfaceIdentifier ipv4InterfaceIdentifier, final Ipv6InterfaceIdentifier ipv6InterfaceIdentifier, final Long id) {
272 final UriBuilder b = new UriBuilder(base, "tp");
273 if (topologyIdentifier != null) {
274 b.add("mt", topologyIdentifier.getValue());
276 if (ipv4InterfaceIdentifier != null) {
277 b.add("ipv4", ipv4InterfaceIdentifier.getValue());
279 if (ipv6InterfaceIdentifier != null) {
280 b.add("ipv6", ipv6InterfaceIdentifier.getValue());
283 return new TpId(b.add("id", id).toString());
286 private TpId buildLocalTpId(final UriBuilder base, final LinkDescriptors linkDescriptors) {
287 return buildTpId(base, linkDescriptors.getMultiTopologyId(), linkDescriptors.getIpv4InterfaceAddress(),
288 linkDescriptors.getIpv6InterfaceAddress(), linkDescriptors.getLinkLocalIdentifier());
291 private TerminationPoint buildTp(final TpId id, final TerminationPointType type) {
292 final TerminationPointBuilder stpb = new TerminationPointBuilder();
293 stpb.setKey(new TerminationPointKey(id));
297 stpb.addAugmentation(
298 TerminationPoint1.class,
299 new TerminationPoint1Builder().setIgpTerminationPointAttributes(
300 new IgpTerminationPointAttributesBuilder().setTerminationPointType(null).build()).build());
306 private TerminationPointType getTpType(final Ipv4InterfaceIdentifier ipv4InterfaceIdentifier,
307 final Ipv6InterfaceIdentifier ipv6InterfaceIdentifier, final Long id) {
308 // Order of preference: Unnumbered first, then IP
310 LOG.debug("Unnumbered termination point type: {}", id);
311 return new UnnumberedBuilder().setUnnumberedId(id).build();
315 if (ipv6InterfaceIdentifier != null) {
316 ip = new IpAddress(ipv6InterfaceIdentifier);
317 } else if (ipv4InterfaceIdentifier != null) {
318 ip = new IpAddress(ipv4InterfaceIdentifier);
324 LOG.debug("IP termination point type: {}", ip);
325 return new IpBuilder().setIpAddress(Lists.newArrayList(ip)).build();
331 private TerminationPoint buildLocalTp(final UriBuilder base, final LinkDescriptors linkDescriptors) {
332 final TpId id = buildLocalTpId(base, linkDescriptors);
333 final TerminationPointType t = getTpType(linkDescriptors.getIpv4InterfaceAddress(), linkDescriptors.getIpv6InterfaceAddress(),
334 linkDescriptors.getLinkLocalIdentifier());
336 return buildTp(id, t);
339 private TpId buildRemoteTpId(final UriBuilder base, final LinkDescriptors linkDescriptors) {
340 return buildTpId(base, linkDescriptors.getMultiTopologyId(), linkDescriptors.getIpv4NeighborAddress(),
341 linkDescriptors.getIpv6NeighborAddress(), linkDescriptors.getLinkRemoteIdentifier());
344 private TerminationPoint buildRemoteTp(final UriBuilder base, final LinkDescriptors linkDescriptors) {
345 final TpId id = buildRemoteTpId(base, linkDescriptors);
346 final TerminationPointType t = getTpType(linkDescriptors.getIpv4NeighborAddress(), linkDescriptors.getIpv6NeighborAddress(),
347 linkDescriptors.getLinkRemoteIdentifier());
349 return buildTp(id, t);
352 private InstanceIdentifier<?> buildLinkIdentifier(final UriBuilder base, final LinkId id) {
353 return getInstanceIdentifier().child(
354 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link.class,
358 private static Float bandwidthToFloat(final Bandwidth bandwidth) {
359 return ByteBuffer.wrap(bandwidth.getValue()).getFloat();
362 private static BigDecimal bandwidthToBigDecimal(final Bandwidth bandwidth) {
363 return BigDecimal.valueOf(bandwidthToFloat(bandwidth));
366 private static List<UnreservedBandwidth> unreservedBandwidthList(
367 final List<? extends org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.UnreservedBandwidth> input) {
368 final List<UnreservedBandwidth> ret = new ArrayList<>(input.size());
370 for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.UnreservedBandwidth i : input) {
371 ret.add(new UnreservedBandwidthBuilder().setBandwidth(bandwidthToBigDecimal(i.getBandwidth())).setKey(
372 new UnreservedBandwidthKey(i.getPriority())).build());
378 private org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpLinkAttributes1 isisLinkAttributes(
379 final TopologyIdentifier topologyIdentifier, final LinkAttributes la) {
380 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();
383 if (la.getAdminGroup() != null) {
384 tb.setColor(la.getAdminGroup().getValue());
386 if (la.getTeMetric() != null) {
387 tb.setTeDefaultMetric(la.getTeMetric().getValue());
389 if (la.getUnreservedBandwidth() != null) {
390 tb.setUnreservedBandwidth(unreservedBandwidthList(la.getUnreservedBandwidth()));
392 if (la.getMaxLinkBandwidth() != null) {
393 tb.setMaxLinkBandwidth(bandwidthToBigDecimal(la.getMaxLinkBandwidth()));
395 if (la.getMaxReservableBandwidth() != null) {
396 tb.setMaxResvLinkBandwidth(bandwidthToBigDecimal(la.getMaxReservableBandwidth()));
400 final IsisLinkAttributesBuilder ilab = new IsisLinkAttributesBuilder();
401 ilab.setTed(tb.build());
402 if (topologyIdentifier != null) {
403 ilab.setMultiTopologyId(topologyIdentifier.getValue().shortValue());
406 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpLinkAttributes1Builder().setIsisLinkAttributes(
407 ilab.build()).build();
410 private org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1 ospfLinkAttributes(
411 final TopologyIdentifier topologyIdentifier, final LinkAttributes la) {
412 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();
415 if (la.getAdminGroup() != null) {
416 tb.setColor(la.getAdminGroup().getValue());
418 if (la.getTeMetric() != null) {
419 tb.setTeDefaultMetric(la.getTeMetric().getValue());
421 if (la.getUnreservedBandwidth() != null) {
422 tb.setUnreservedBandwidth(unreservedBandwidthList(la.getUnreservedBandwidth()));
424 if (la.getMaxLinkBandwidth() != null) {
425 tb.setMaxLinkBandwidth(bandwidthToBigDecimal(la.getMaxLinkBandwidth()));
427 if (la.getMaxReservableBandwidth() != null) {
428 tb.setMaxResvLinkBandwidth(bandwidthToBigDecimal(la.getMaxReservableBandwidth()));
432 final OspfLinkAttributesBuilder ilab = new OspfLinkAttributesBuilder();
433 ilab.setTed(tb.build());
434 if (topologyIdentifier != null) {
435 ilab.setMultiTopologyId(topologyIdentifier.getValue().shortValue());
438 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1Builder().setOspfLinkAttributes(
439 ilab.build()).build();
442 private NodeHolder getNode(final NodeId id) {
443 if (this.nodes.containsKey(id)) {
444 LOG.debug("Node {} is already present", id);
445 return this.nodes.get(id);
448 final NodeHolder ret = new NodeHolder(id);
449 this.nodes.put(id, ret);
453 private void putNode(final DataModification<InstanceIdentifier<?>, DataObject> trans, final NodeHolder holder) {
454 if (holder.syncState(trans)) {
455 this.nodes.remove(holder.getNodeId());
459 private void createLink(final DataModification<InstanceIdentifier<?>, DataObject> trans, final UriBuilder base,
460 final LinkstateRoute value, final LinkCase l, final Attributes attributes) {
461 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(
462 Attributes1.class).getAttributeType()).getLinkAttributes();
464 final IgpLinkAttributesBuilder ilab = new IgpLinkAttributesBuilder();
466 if (la.getMetric() != null) {
467 ilab.setMetric(la.getMetric().getValue());
469 ilab.setName(la.getLinkName());
472 switch (value.getProtocolId()) {
479 ilab.addAugmentation(
480 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpLinkAttributes1.class,
481 isisLinkAttributes(l.getLinkDescriptors().getMultiTopologyId(), la));
484 ilab.addAugmentation(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1.class,
485 ospfLinkAttributes(l.getLinkDescriptors().getMultiTopologyId(), la));
489 final LinkBuilder lb = new LinkBuilder();
490 lb.setLinkId(buildLinkId(base, l));
491 lb.addAugmentation(Link1.class, new Link1Builder().setIgpLinkAttributes(ilab.build()).build());
493 final NodeId srcNode = buildNodeId(base, l.getLocalNodeDescriptors());
494 LOG.debug("Link {} implies source node {}", l, srcNode);
496 final NodeId dstNode = buildNodeId(base, l.getRemoteNodeDescriptors());
497 LOG.debug("Link {} implies destination node {}", l, dstNode);
499 final TerminationPoint srcTp = buildLocalTp(base, l.getLinkDescriptors());
500 LOG.debug("Link {} implies source TP {}", l, srcTp);
502 final TerminationPoint dstTp = buildRemoteTp(base, l.getLinkDescriptors());
503 LOG.debug("Link {} implies destination TP {}", l, dstTp);
505 lb.setSource(new SourceBuilder().setSourceNode(srcNode).setSourceTp(srcTp.getTpId()).build());
506 lb.setDestination(new DestinationBuilder().setDestNode(dstNode).setDestTp(dstTp.getTpId()).build());
508 final NodeHolder snh = getNode(srcNode);
509 snh.addTp(srcTp, lb.getLinkId(), false);
510 LOG.debug("Created TP {} as link source", srcTp);
513 final NodeHolder dnh = getNode(dstNode);
514 dnh.addTp(dstTp, lb.getLinkId(), true);
515 LOG.debug("Created TP {} as link destination", dstTp);
518 final InstanceIdentifier<?> lid = buildLinkIdentifier(base, lb.getLinkId());
519 final Link link = lb.build();
522 * Transaction's putOperationalData() does a merge. Force it onto a replace
523 * by removing the data.
525 trans.removeOperationalData(lid);
526 trans.putOperationalData(lid, link);
527 LOG.debug("Created link {} at {} for {}", link, lid, l);
530 private void removeTp(final DataModification<InstanceIdentifier<?>, DataObject> trans, final NodeId node, final TpId tp,
531 final LinkId link, final boolean isRemote) {
532 final NodeHolder nh = this.nodes.get(node);
534 nh.removeTp(tp, link, isRemote);
537 LOG.warn("Removed non-existent node {}", node);
541 private void removeLink(final DataModification<InstanceIdentifier<?>, DataObject> trans, final UriBuilder base, final LinkCase l) {
542 final LinkId id = buildLinkId(base, l);
543 final InstanceIdentifier<?> lid = buildLinkIdentifier(base, id);
544 trans.removeOperationalData(lid);
545 LOG.debug("Removed link {}", lid);
547 removeTp(trans, buildNodeId(base, l.getLocalNodeDescriptors()), buildLocalTpId(base, l.getLinkDescriptors()), id, false);
548 removeTp(trans, buildNodeId(base, l.getRemoteNodeDescriptors()), buildRemoteTpId(base, l.getLinkDescriptors()), id, true);
551 private List<Short> nodeMultiTopology(final List<TopologyIdentifier> list) {
552 final List<Short> ret = new ArrayList<>(list.size());
553 for (final TopologyIdentifier id : list) {
554 ret.add(id.getValue().shortValue());
559 private org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1 isisNodeAttributes(
560 final NodeIdentifier node, final NodeAttributes na) {
561 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();
562 final IsisNodeAttributesBuilder ab = new IsisNodeAttributesBuilder();
565 if (na.getIpv4RouterId() != null) {
566 tb.setTeRouterIdIpv4(na.getIpv4RouterId());
568 if (na.getIpv6RouterId() != null) {
569 tb.setTeRouterIdIpv6(na.getIpv6RouterId());
571 if (na.getTopologyIdentifier() != null) {
572 ab.setMultiTopologyId(nodeMultiTopology(na.getTopologyIdentifier()));
576 final CRouterIdentifier ri = node.getCRouterIdentifier();
577 if (ri instanceof IsisPseudonodeCase) {
578 final IsisPseudonode pn = ((IsisPseudonodeCase) ri).getIsisPseudonode();
579 ab.setIso(new IsoBuilder().setIsoPseudonodeId(new IsoPseudonodeId(pn.toString())).build());
580 } else if (ri instanceof IsisNodeCase) {
581 final IsisNode in = ((IsisNodeCase) ri).getIsisNode();
582 ab.setIso(new IsoBuilder().setIsoSystemId(new IsoSystemId(in.getIsoSystemId().toString())).build());
585 ab.setTed(tb.build());
587 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1Builder().setIsisNodeAttributes(
591 private org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1 ospfNodeAttributes(
592 final NodeDescriptors node, final NodeAttributes na) {
593 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();
594 final OspfNodeAttributesBuilder ab = new OspfNodeAttributesBuilder();
597 if (na.getIpv4RouterId() != null) {
598 tb.setTeRouterIdIpv4(na.getIpv4RouterId());
600 if (na.getIpv6RouterId() != null) {
601 tb.setTeRouterIdIpv6(na.getIpv6RouterId());
603 if (na.getTopologyIdentifier() != null) {
604 ab.setMultiTopologyId(nodeMultiTopology(na.getTopologyIdentifier()));
608 final CRouterIdentifier ri = node.getCRouterIdentifier();
609 if (ri instanceof OspfPseudonodeCase) {
610 final OspfPseudonode pn = ((OspfPseudonodeCase) ri).getOspfPseudonode();
612 ab.setRouterType(new PseudonodeBuilder().setPseudonode(Boolean.TRUE).build());
613 ab.setDrInterfaceId(pn.getLanInterface().getValue());
614 } else if (ri instanceof OspfNodeCase) {
615 // TODO: what should we do with in.getOspfRouterId()?
616 // final OspfNode in = ((OspfNodeCase) ri).getOspfNode();
619 final NodeFlagBits nf = na.getNodeFlags();
622 ab.setRouterType(new AbrBuilder().setAbr(Boolean.TRUE).build());
623 } else if (!nf.isExternal()) {
624 ab.setRouterType(new InternalBuilder().setInternal(Boolean.TRUE).build());
630 ab.setTed(tb.build());
632 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1Builder().setOspfNodeAttributes(
636 private void createNode(final DataModification<InstanceIdentifier<?>, DataObject> trans, final UriBuilder base,
637 final LinkstateRoute value, final NodeCase n, final Attributes attributes) {
638 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(
639 Attributes1.class).getAttributeType()).getNodeAttributes();
640 final IgpNodeAttributesBuilder inab = new IgpNodeAttributesBuilder();
642 final List<IpAddress> ids = new ArrayList<>();
644 if (na.getIpv4RouterId() != null) {
645 ids.add(new IpAddress(na.getIpv4RouterId()));
647 if (na.getIpv6RouterId() != null) {
648 ids.add(new IpAddress(na.getIpv6RouterId()));
650 if (na.getDynamicHostname() != null) {
651 inab.setName(new DomainName(na.getDynamicHostname()));
655 if (!ids.isEmpty()) {
656 inab.setRouterId(ids);
659 switch (value.getProtocolId()) {
666 inab.addAugmentation(
667 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1.class,
668 isisNodeAttributes(n.getNodeDescriptors(), na));
671 inab.addAugmentation(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1.class,
672 ospfNodeAttributes(n.getNodeDescriptors(), na));
676 final NodeId nid = buildNodeId(base, n.getNodeDescriptors());
677 final NodeHolder nh = getNode(nid);
680 * Eventhough the the holder creates a dummy structure, we need to duplicate it here,
681 * as that is the API requirement. The reason for it is the possible presence of supporting
682 * node -- something which the holder does not track.
684 final NodeBuilder nb = new NodeBuilder();
686 nb.setKey(new NodeKey(nb.getNodeId()));
688 nh.advertized(nb, inab);
692 private void removeNode(final DataModification<InstanceIdentifier<?>, DataObject> trans, final UriBuilder base, final NodeCase n) {
693 final NodeId id = buildNodeId(base, n.getNodeDescriptors());
694 final NodeHolder nh = this.nodes.get(id);
699 LOG.warn("Node {} does not have a holder", id);
703 private void createPrefix(final DataModification<InstanceIdentifier<?>, DataObject> trans, final UriBuilder base,
704 final LinkstateRoute value, final PrefixCase p, final Attributes attributes) {
705 final IpPrefix ippfx = p.getIpReachabilityInformation();
707 LOG.warn("IP reachability not present in prefix {} route {}, skipping it", p, value);
711 final PrefixBuilder pb = new PrefixBuilder();
712 pb.setKey(new PrefixKey(ippfx));
715 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(
716 Attributes1.class).getAttributeType()).getPrefixAttributes();
718 pb.setMetric(pa.getPrefixMetric().getValue());
721 switch (value.getProtocolId()) {
732 new Prefix1Builder().setOspfPrefixAttributes(
733 new OspfPrefixAttributesBuilder().setForwardingAddress(pa.getOspfForwardingAddress().getIpv4Address()).build()).build());
738 final Prefix pfx = pb.build();
741 * All set, but... the hosting node may not exist, we may need to fake it.
743 final NodeId node = buildNodeId(base, p.getAdvertisingNodeDescriptors());
744 final NodeHolder nh = getNode(node);
746 LOG.debug("Created prefix {} for {}", pfx, p);
750 private void removePrefix(final DataModification<InstanceIdentifier<?>, DataObject> trans, final UriBuilder base, final PrefixCase p) {
751 final NodeId node = buildNodeId(base, p.getAdvertisingNodeDescriptors());
752 final NodeHolder nh = this.nodes.get(node);
755 LOG.debug("Removed prefix {}", p);
758 LOG.warn("Removing prefix from non-existing node {}", node);
763 protected void createObject(final DataModification<InstanceIdentifier<?>, DataObject> trans,
764 final InstanceIdentifier<LinkstateRoute> id, final LinkstateRoute value) {
765 final UriBuilder base = new UriBuilder(value);
767 final ObjectType t = value.getObjectType();
768 if (t instanceof LinkCase) {
769 createLink(trans, base, value, (LinkCase) t, value.getAttributes());
770 } else if (t instanceof NodeCase) {
771 createNode(trans, base, value, (NodeCase) t, value.getAttributes());
772 } else if (t instanceof PrefixCase) {
773 createPrefix(trans, base, value, (PrefixCase) t, value.getAttributes());
775 throw new IllegalStateException("Unhandled object class " + t.getImplementedInterface());
780 protected void removeObject(final DataModification<InstanceIdentifier<?>, DataObject> trans,
781 final InstanceIdentifier<LinkstateRoute> id, final LinkstateRoute value) {
782 final UriBuilder base = new UriBuilder(value);
784 final ObjectType t = value.getObjectType();
785 if (t instanceof LinkCase) {
786 removeLink(trans, base, (LinkCase) t);
787 } else if (t instanceof NodeCase) {
788 removeNode(trans, base, (NodeCase) t);
789 } else if (t instanceof PrefixCase) {
790 removePrefix(trans, base, (PrefixCase) t);
792 throw new IllegalStateException("Unhandled object class " + t.getImplementedInterface());