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 com.google.common.base.Function;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Collections2;
13 import com.google.common.collect.Lists;
14 import java.math.BigDecimal;
15 import java.nio.ByteBuffer;
16 import java.util.ArrayList;
17 import java.util.HashMap;
18 import java.util.HashSet;
19 import java.util.List;
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.rev100924.DomainName;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.Ipv4InterfaceIdentifier;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.Ipv6InterfaceIdentifier;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.NodeFlagBits;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.NodeIdentifier;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.TopologyIdentifier;
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.LinkstateRoute;
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.Attributes1;
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.ObjectType;
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.attributes.AttributeType;
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.attributes.attribute.type.link._case.LinkAttributes;
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.attributes.attribute.type.node._case.NodeAttributes;
41 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;
42 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;
43 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;
44 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;
45 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;
46 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;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.CRouterIdentifier;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.IsisNodeCase;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.IsisPseudonodeCase;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.OspfNodeCase;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.OspfPseudonodeCase;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.isis.node._case.IsisNode;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.isis.pseudonode._case.IsisPseudonode;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.ospf.pseudonode._case.OspfPseudonode;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.route.Attributes;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth;
57 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IsoPseudonodeId;
58 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IsoSystemId;
59 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.link.attributes.IsisLinkAttributesBuilder;
60 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.node.attributes.IsisNodeAttributesBuilder;
61 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.node.attributes.isis.node.attributes.IsoBuilder;
62 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.UnreservedBandwidth;
63 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.UnreservedBandwidthBuilder;
64 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.UnreservedBandwidthKey;
65 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.LinkId;
66 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
67 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
68 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
69 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.DestinationBuilder;
70 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.SourceBuilder;
71 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
72 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkBuilder;
73 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkKey;
74 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
75 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
76 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
77 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.TopologyTypesBuilder;
78 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
79 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
80 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
81 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Link1;
82 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Link1Builder;
83 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1;
84 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1Builder;
85 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TerminationPoint1;
86 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TerminationPoint1Builder;
87 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TopologyTypes1;
88 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TopologyTypes1Builder;
89 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.link.attributes.IgpLinkAttributesBuilder;
90 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.IgpNodeAttributesBuilder;
91 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;
92 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;
93 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;
94 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.termination.point.attributes.IgpTerminationPointAttributesBuilder;
95 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;
96 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;
97 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;
98 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.Prefix1;
99 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.Prefix1Builder;
100 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.link.attributes.OspfLinkAttributesBuilder;
101 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.OspfNodeAttributesBuilder;
102 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.ospf.node.attributes.router.type.AbrBuilder;
103 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.ospf.node.attributes.router.type.InternalBuilder;
104 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.ospf.node.attributes.router.type.PseudonodeBuilder;
105 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.prefix.attributes.OspfPrefixAttributesBuilder;
106 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
107 import org.slf4j.Logger;
108 import org.slf4j.LoggerFactory;
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 WriteTransaction 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.delete(LogicalDatastoreType.OPERATIONAL, 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(), new Function<TpHolder, TerminationPoint>() {
189 public TerminationPoint apply(final TpHolder input) {
190 return input.getTp();
194 // Re-generate prefixes
195 this.inab.setPrefix(Lists.newArrayList(this.prefixes.values()));
197 // Write the node out
198 final Node n = this.nb.addAugmentation(Node1.class, new Node1Builder().setIgpNodeAttributes(this.inab.build()).build()).build();
199 trans.put(LogicalDatastoreType.OPERATIONAL, nid, n);
200 LOG.debug("Created node {} at {}", n, nid);
204 private synchronized void removeTp(final TpId tp, final LinkId link, final boolean isRemote) {
205 final TpHolder h = this.tps.get(tp);
207 if (h.removeLink(link, isRemote)) {
209 LOG.debug("Removed TP {}", tp);
212 LOG.warn("Removed non-present TP {} by link {}", tp, link);
216 private void addTp(final TerminationPoint tp, final LinkId link, final boolean isRemote) {
217 TpHolder h = this.tps.get(tp.getTpId());
219 h = new TpHolder(tp);
220 this.tps.put(tp.getTpId(), h);
223 h.addLink(link, isRemote);
226 private void addPrefix(final Prefix pfx) {
227 this.prefixes.put(pfx.getKey(), pfx);
230 private void removePrefix(final PrefixCase p) {
231 this.prefixes.remove(new PrefixKey(p.getIpReachabilityInformation()));
234 private void unadvertized() {
235 this.inab = new IgpNodeAttributesBuilder();
236 this.nb = new NodeBuilder().setKey(this.nb.getKey()).setNodeId(this.nb.getNodeId());
237 this.advertized = false;
238 LOG.debug("Node {} is unadvertized", this.nb.getNodeId());
241 private void advertized(final NodeBuilder nb, final IgpNodeAttributesBuilder inab) {
242 this.nb = Preconditions.checkNotNull(nb);
243 this.inab = Preconditions.checkNotNull(inab);
244 this.advertized = true;
245 LOG.debug("Node {} is advertized", nb.getNodeId());
248 private Object getNodeId() {
249 return this.nb.getNodeId();
253 private static final Logger LOG = LoggerFactory.getLogger(LinkstateTopologyBuilder.class);
254 private final Map<NodeId, NodeHolder> nodes = new HashMap<>();
256 public LinkstateTopologyBuilder(final DataBroker dataProvider, final RibReference locRibReference, final TopologyId topologyId) {
257 super(dataProvider, locRibReference, topologyId, new TopologyTypesBuilder().addAugmentation(TopologyTypes1.class,
258 new TopologyTypes1Builder().build()).build(), LinkstateRoute.class);
261 private LinkId buildLinkId(final UriBuilder base, final LinkCase link) {
262 return new LinkId(new UriBuilder(base, "link").add(link).toString());
265 private NodeId buildNodeId(final UriBuilder base, final NodeIdentifier node) {
266 return new NodeId(new UriBuilder(base, "node").add("", node).toString());
269 private TpId buildTpId(final UriBuilder base, final TopologyIdentifier topologyIdentifier,
270 final Ipv4InterfaceIdentifier ipv4InterfaceIdentifier, final Ipv6InterfaceIdentifier ipv6InterfaceIdentifier, final Long id) {
271 final UriBuilder b = new UriBuilder(base, "tp");
272 if (topologyIdentifier != null) {
273 b.add("mt", topologyIdentifier.getValue());
275 if (ipv4InterfaceIdentifier != null) {
276 b.add("ipv4", ipv4InterfaceIdentifier.getValue());
278 if (ipv6InterfaceIdentifier != null) {
279 b.add("ipv6", ipv6InterfaceIdentifier.getValue());
282 return new TpId(b.add("id", id).toString());
285 private TpId buildLocalTpId(final UriBuilder base, final LinkDescriptors linkDescriptors) {
286 return buildTpId(base, linkDescriptors.getMultiTopologyId(), linkDescriptors.getIpv4InterfaceAddress(),
287 linkDescriptors.getIpv6InterfaceAddress(), linkDescriptors.getLinkLocalIdentifier());
290 private TerminationPoint buildTp(final TpId id, final TerminationPointType type) {
291 final TerminationPointBuilder stpb = new TerminationPointBuilder();
292 stpb.setKey(new TerminationPointKey(id));
296 stpb.addAugmentation(TerminationPoint1.class, new TerminationPoint1Builder().setIgpTerminationPointAttributes(
297 new IgpTerminationPointAttributesBuilder().setTerminationPointType(null).build()).build());
303 private TerminationPointType getTpType(final Ipv4InterfaceIdentifier ipv4InterfaceIdentifier,
304 final Ipv6InterfaceIdentifier ipv6InterfaceIdentifier, final Long id) {
305 // Order of preference: Unnumbered first, then IP
307 LOG.debug("Unnumbered termination point type: {}", id);
308 return new UnnumberedBuilder().setUnnumberedId(id).build();
312 if (ipv6InterfaceIdentifier != null) {
313 ip = new IpAddress(ipv6InterfaceIdentifier);
314 } else if (ipv4InterfaceIdentifier != null) {
315 ip = new IpAddress(ipv4InterfaceIdentifier);
321 LOG.debug("IP termination point type: {}", ip);
322 return new IpBuilder().setIpAddress(Lists.newArrayList(ip)).build();
328 private TerminationPoint buildLocalTp(final UriBuilder base, final LinkDescriptors linkDescriptors) {
329 final TpId id = buildLocalTpId(base, linkDescriptors);
330 final TerminationPointType t = getTpType(linkDescriptors.getIpv4InterfaceAddress(), linkDescriptors.getIpv6InterfaceAddress(),
331 linkDescriptors.getLinkLocalIdentifier());
333 return buildTp(id, t);
336 private TpId buildRemoteTpId(final UriBuilder base, final LinkDescriptors linkDescriptors) {
337 return buildTpId(base, linkDescriptors.getMultiTopologyId(), linkDescriptors.getIpv4NeighborAddress(),
338 linkDescriptors.getIpv6NeighborAddress(), linkDescriptors.getLinkRemoteIdentifier());
341 private TerminationPoint buildRemoteTp(final UriBuilder base, final LinkDescriptors linkDescriptors) {
342 final TpId id = buildRemoteTpId(base, linkDescriptors);
343 final TerminationPointType t = getTpType(linkDescriptors.getIpv4NeighborAddress(), linkDescriptors.getIpv6NeighborAddress(),
344 linkDescriptors.getLinkRemoteIdentifier());
346 return buildTp(id, t);
349 private InstanceIdentifier<Link> buildLinkIdentifier(final LinkId id) {
350 return getInstanceIdentifier().child(
351 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link.class,
355 private static Float bandwidthToFloat(final Bandwidth bandwidth) {
356 return ByteBuffer.wrap(bandwidth.getValue()).getFloat();
359 private static BigDecimal bandwidthToBigDecimal(final Bandwidth bandwidth) {
360 return BigDecimal.valueOf(bandwidthToFloat(bandwidth));
363 private static List<UnreservedBandwidth> unreservedBandwidthList(
364 final List<? extends org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.UnreservedBandwidth> input) {
365 final List<UnreservedBandwidth> ret = new ArrayList<>(input.size());
367 for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.UnreservedBandwidth i : input) {
368 ret.add(new UnreservedBandwidthBuilder().setBandwidth(bandwidthToBigDecimal(i.getBandwidth())).setKey(
369 new UnreservedBandwidthKey(i.getPriority())).build());
375 private org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpLinkAttributes1 isisLinkAttributes(
376 final TopologyIdentifier topologyIdentifier, final LinkAttributes la) {
377 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();
380 if (la.getAdminGroup() != null) {
381 tb.setColor(la.getAdminGroup().getValue());
383 if (la.getTeMetric() != null) {
384 tb.setTeDefaultMetric(la.getTeMetric().getValue());
386 if (la.getUnreservedBandwidth() != null) {
387 tb.setUnreservedBandwidth(unreservedBandwidthList(la.getUnreservedBandwidth()));
389 if (la.getMaxLinkBandwidth() != null) {
390 tb.setMaxLinkBandwidth(bandwidthToBigDecimal(la.getMaxLinkBandwidth()));
392 if (la.getMaxReservableBandwidth() != null) {
393 tb.setMaxResvLinkBandwidth(bandwidthToBigDecimal(la.getMaxReservableBandwidth()));
397 final IsisLinkAttributesBuilder ilab = new IsisLinkAttributesBuilder();
398 ilab.setTed(tb.build());
399 if (topologyIdentifier != null) {
400 ilab.setMultiTopologyId(topologyIdentifier.getValue().shortValue());
403 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpLinkAttributes1Builder().setIsisLinkAttributes(
404 ilab.build()).build();
407 private org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1 ospfLinkAttributes(
408 final TopologyIdentifier topologyIdentifier, final LinkAttributes la) {
409 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();
412 if (la.getAdminGroup() != null) {
413 tb.setColor(la.getAdminGroup().getValue());
415 if (la.getTeMetric() != null) {
416 tb.setTeDefaultMetric(la.getTeMetric().getValue());
418 if (la.getUnreservedBandwidth() != null) {
419 tb.setUnreservedBandwidth(unreservedBandwidthList(la.getUnreservedBandwidth()));
421 if (la.getMaxLinkBandwidth() != null) {
422 tb.setMaxLinkBandwidth(bandwidthToBigDecimal(la.getMaxLinkBandwidth()));
424 if (la.getMaxReservableBandwidth() != null) {
425 tb.setMaxResvLinkBandwidth(bandwidthToBigDecimal(la.getMaxReservableBandwidth()));
429 final OspfLinkAttributesBuilder ilab = new OspfLinkAttributesBuilder();
430 ilab.setTed(tb.build());
431 if (topologyIdentifier != null) {
432 ilab.setMultiTopologyId(topologyIdentifier.getValue().shortValue());
435 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1Builder().setOspfLinkAttributes(
436 ilab.build()).build();
439 private NodeHolder getNode(final NodeId id) {
440 if (this.nodes.containsKey(id)) {
441 LOG.debug("Node {} is already present", id);
442 return this.nodes.get(id);
445 final NodeHolder ret = new NodeHolder(id);
446 this.nodes.put(id, ret);
450 private void putNode(final WriteTransaction trans, final NodeHolder holder) {
451 if (holder.syncState(trans)) {
452 this.nodes.remove(holder.getNodeId());
456 private void createLink(final WriteTransaction trans, final UriBuilder base,
457 final LinkstateRoute value, final LinkCase l, final Attributes attributes) {
458 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(
459 Attributes1.class).getAttributeType()).getLinkAttributes();
461 final IgpLinkAttributesBuilder ilab = new IgpLinkAttributesBuilder();
463 if (la.getMetric() != null) {
464 ilab.setMetric(la.getMetric().getValue());
466 ilab.setName(la.getLinkName());
469 switch (value.getProtocolId()) {
476 ilab.addAugmentation(
477 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpLinkAttributes1.class,
478 isisLinkAttributes(l.getLinkDescriptors().getMultiTopologyId(), la));
481 ilab.addAugmentation(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1.class,
482 ospfLinkAttributes(l.getLinkDescriptors().getMultiTopologyId(), la));
486 final LinkBuilder lb = new LinkBuilder();
487 lb.setLinkId(buildLinkId(base, l));
488 lb.addAugmentation(Link1.class, new Link1Builder().setIgpLinkAttributes(ilab.build()).build());
490 final NodeId srcNode = buildNodeId(base, l.getLocalNodeDescriptors());
491 LOG.debug("Link {} implies source node {}", l, srcNode);
493 final NodeId dstNode = buildNodeId(base, l.getRemoteNodeDescriptors());
494 LOG.debug("Link {} implies destination node {}", l, dstNode);
496 final TerminationPoint srcTp = buildLocalTp(base, l.getLinkDescriptors());
497 LOG.debug("Link {} implies source TP {}", l, srcTp);
499 final TerminationPoint dstTp = buildRemoteTp(base, l.getLinkDescriptors());
500 LOG.debug("Link {} implies destination TP {}", l, dstTp);
502 lb.setSource(new SourceBuilder().setSourceNode(srcNode).setSourceTp(srcTp.getTpId()).build());
503 lb.setDestination(new DestinationBuilder().setDestNode(dstNode).setDestTp(dstTp.getTpId()).build());
505 final NodeHolder snh = getNode(srcNode);
506 snh.addTp(srcTp, lb.getLinkId(), false);
507 LOG.debug("Created TP {} as link source", srcTp);
510 final NodeHolder dnh = getNode(dstNode);
511 dnh.addTp(dstTp, lb.getLinkId(), true);
512 LOG.debug("Created TP {} as link destination", dstTp);
515 final InstanceIdentifier<Link> lid = buildLinkIdentifier(lb.getLinkId());
516 final Link link = lb.build();
518 trans.put(LogicalDatastoreType.OPERATIONAL, lid, link);
519 LOG.debug("Created link {} at {} for {}", link, lid, l);
522 private void removeTp(final WriteTransaction trans, final NodeId node, final TpId tp,
523 final LinkId link, final boolean isRemote) {
524 final NodeHolder nh = this.nodes.get(node);
526 nh.removeTp(tp, link, isRemote);
529 LOG.warn("Removed non-existent node {}", node);
533 private void removeLink(final WriteTransaction trans, final UriBuilder base, final LinkCase l) {
534 final LinkId id = buildLinkId(base, l);
535 final InstanceIdentifier<?> lid = buildLinkIdentifier(id);
536 trans.delete(LogicalDatastoreType.OPERATIONAL, lid);
537 LOG.debug("Removed link {}", lid);
539 removeTp(trans, buildNodeId(base, l.getLocalNodeDescriptors()), buildLocalTpId(base, l.getLinkDescriptors()), id, false);
540 removeTp(trans, buildNodeId(base, l.getRemoteNodeDescriptors()), buildRemoteTpId(base, l.getLinkDescriptors()), id, true);
543 private List<Short> nodeMultiTopology(final List<TopologyIdentifier> list) {
544 final List<Short> ret = new ArrayList<>(list.size());
545 for (final TopologyIdentifier id : list) {
546 ret.add(id.getValue().shortValue());
551 private org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1 isisNodeAttributes(
552 final NodeIdentifier node, final NodeAttributes na) {
553 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();
554 final IsisNodeAttributesBuilder ab = new IsisNodeAttributesBuilder();
557 if (na.getIpv4RouterId() != null) {
558 tb.setTeRouterIdIpv4(na.getIpv4RouterId());
560 if (na.getIpv6RouterId() != null) {
561 tb.setTeRouterIdIpv6(na.getIpv6RouterId());
563 if (na.getTopologyIdentifier() != null) {
564 ab.setMultiTopologyId(nodeMultiTopology(na.getTopologyIdentifier()));
568 final CRouterIdentifier ri = node.getCRouterIdentifier();
569 if (ri instanceof IsisPseudonodeCase) {
570 final IsisPseudonode pn = ((IsisPseudonodeCase) ri).getIsisPseudonode();
571 ab.setIso(new IsoBuilder().setIsoPseudonodeId(new IsoPseudonodeId(pn.toString())).build());
572 } else if (ri instanceof IsisNodeCase) {
573 final IsisNode in = ((IsisNodeCase) ri).getIsisNode();
574 ab.setIso(new IsoBuilder().setIsoSystemId(new IsoSystemId(in.getIsoSystemId().toString())).build());
577 ab.setTed(tb.build());
579 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1Builder().setIsisNodeAttributes(
583 private org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1 ospfNodeAttributes(
584 final NodeDescriptors node, final NodeAttributes na) {
585 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();
586 final OspfNodeAttributesBuilder ab = new OspfNodeAttributesBuilder();
589 if (na.getIpv4RouterId() != null) {
590 tb.setTeRouterIdIpv4(na.getIpv4RouterId());
592 if (na.getIpv6RouterId() != null) {
593 tb.setTeRouterIdIpv6(na.getIpv6RouterId());
595 if (na.getTopologyIdentifier() != null) {
596 ab.setMultiTopologyId(nodeMultiTopology(na.getTopologyIdentifier()));
600 final CRouterIdentifier ri = node.getCRouterIdentifier();
601 if (ri instanceof OspfPseudonodeCase) {
602 final OspfPseudonode pn = ((OspfPseudonodeCase) ri).getOspfPseudonode();
604 ab.setRouterType(new PseudonodeBuilder().setPseudonode(Boolean.TRUE).build());
605 ab.setDrInterfaceId(pn.getLanInterface().getValue());
606 } else if (ri instanceof OspfNodeCase) {
607 // TODO: what should we do with in.getOspfRouterId()?
608 // final OspfNode in = ((OspfNodeCase) ri).getOspfNode();
610 if (na != null && na.getNodeFlags() != null) {
611 final NodeFlagBits nf = na.getNodeFlags();
613 ab.setRouterType(new AbrBuilder().setAbr(Boolean.TRUE).build());
614 } else if (!nf.isExternal()) {
615 ab.setRouterType(new InternalBuilder().setInternal(Boolean.TRUE).build());
620 ab.setTed(tb.build());
622 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1Builder().setOspfNodeAttributes(
626 private void createNode(final WriteTransaction trans, final UriBuilder base,
627 final LinkstateRoute value, final NodeCase n, final Attributes attributes) {
628 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(
629 Attributes1.class).getAttributeType()).getNodeAttributes();
630 final IgpNodeAttributesBuilder inab = new IgpNodeAttributesBuilder();
632 final List<IpAddress> ids = new ArrayList<>();
634 if (na.getIpv4RouterId() != null) {
635 ids.add(new IpAddress(na.getIpv4RouterId()));
637 if (na.getIpv6RouterId() != null) {
638 ids.add(new IpAddress(na.getIpv6RouterId()));
640 if (na.getDynamicHostname() != null) {
641 inab.setName(new DomainName(na.getDynamicHostname()));
645 if (!ids.isEmpty()) {
646 inab.setRouterId(ids);
649 switch (value.getProtocolId()) {
656 inab.addAugmentation(
657 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1.class,
658 isisNodeAttributes(n.getNodeDescriptors(), na));
661 inab.addAugmentation(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1.class,
662 ospfNodeAttributes(n.getNodeDescriptors(), na));
666 final NodeId nid = buildNodeId(base, n.getNodeDescriptors());
667 final NodeHolder nh = getNode(nid);
670 * Eventhough the the holder creates a dummy structure, we need to duplicate it here,
671 * as that is the API requirement. The reason for it is the possible presence of supporting
672 * node -- something which the holder does not track.
674 final NodeBuilder nb = new NodeBuilder();
676 nb.setKey(new NodeKey(nb.getNodeId()));
678 nh.advertized(nb, inab);
682 private void removeNode(final WriteTransaction trans, final UriBuilder base, final NodeCase n) {
683 final NodeId id = buildNodeId(base, n.getNodeDescriptors());
684 final NodeHolder nh = this.nodes.get(id);
689 LOG.warn("Node {} does not have a holder", id);
693 private void createPrefix(final WriteTransaction trans, final UriBuilder base,
694 final LinkstateRoute value, final PrefixCase p, final Attributes attributes) {
695 final IpPrefix ippfx = p.getIpReachabilityInformation();
697 LOG.warn("IP reachability not present in prefix {} route {}, skipping it", p, value);
701 final PrefixBuilder pb = new PrefixBuilder();
702 pb.setKey(new PrefixKey(ippfx));
705 final PrefixAttributes pa;
707 // Very defensive lookup
708 final Attributes1 attr = attributes.getAugmentation(Attributes1.class);
710 final AttributeType attrType = attr.getAttributeType();
711 if (attrType != null) {
712 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)
713 attrType).getPrefixAttributes();
715 LOG.debug("Missing attribute type in IP {} prefix {} route {}, skipping it", ippfx, p, value);
719 LOG.debug("Missing attributes in IP {} prefix {} route {}, skipping it", ippfx, p, value);
724 pb.setMetric(pa.getPrefixMetric().getValue());
727 switch (value.getProtocolId()) {
738 new Prefix1Builder().setOspfPrefixAttributes(
739 new OspfPrefixAttributesBuilder().setForwardingAddress(pa.getOspfForwardingAddress().getIpv4Address()).build()).build());
744 final Prefix pfx = pb.build();
747 * All set, but... the hosting node may not exist, we may need to fake it.
749 final NodeId node = buildNodeId(base, p.getAdvertisingNodeDescriptors());
750 final NodeHolder nh = getNode(node);
752 LOG.debug("Created prefix {} for {}", pfx, p);
756 private void removePrefix(final WriteTransaction trans, final UriBuilder base, final PrefixCase p) {
757 final NodeId node = buildNodeId(base, p.getAdvertisingNodeDescriptors());
758 final NodeHolder nh = this.nodes.get(node);
761 LOG.debug("Removed prefix {}", p);
764 LOG.warn("Removing prefix from non-existing node {}", node);
769 protected void createObject(final ReadWriteTransaction trans,
770 final InstanceIdentifier<LinkstateRoute> id, final LinkstateRoute value) {
771 final UriBuilder base = new UriBuilder(value);
773 final ObjectType t = value.getObjectType();
774 Preconditions.checkArgument(t != null, "Route %s value %s has null object type", id, value);
776 if (t instanceof LinkCase) {
777 createLink(trans, base, value, (LinkCase) t, value.getAttributes());
778 } else if (t instanceof NodeCase) {
779 createNode(trans, base, value, (NodeCase) t, value.getAttributes());
780 } else if (t instanceof PrefixCase) {
781 createPrefix(trans, base, value, (PrefixCase) t, value.getAttributes());
783 throw new IllegalStateException("Unhandled object class " + t.getImplementedInterface());
788 protected void removeObject(final ReadWriteTransaction trans,
789 final InstanceIdentifier<LinkstateRoute> id, final LinkstateRoute value) {
790 final UriBuilder base = new UriBuilder(value);
792 final ObjectType t = value.getObjectType();
793 if (t instanceof LinkCase) {
794 removeLink(trans, base, (LinkCase) t);
795 } else if (t instanceof NodeCase) {
796 removeNode(trans, base, (NodeCase) t);
797 } else if (t instanceof PrefixCase) {
798 removePrefix(trans, base, (PrefixCase) t);
800 throw new IllegalStateException("Unhandled object class " + t.getImplementedInterface());