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.apache.commons.codec.binary.Hex;
23 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
24 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
25 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
26 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
27 import org.opendaylight.protocol.bgp.rib.RibReference;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.DomainName;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.Ipv4InterfaceIdentifier;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.Ipv6InterfaceIdentifier;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NodeFlagBits;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NodeIdentifier;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.TopologyIdentifier;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.ObjectType;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.LinkCase;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.NodeCase;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.PrefixCase;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.link._case.LinkDescriptors;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.linkstate.routes.LinkstateRoute;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.linkstate.routes.linkstate.route.Attributes1;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.linkstate.routes.linkstate.route.attributes.AttributeType;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.linkstate.routes.linkstate.route.attributes.attribute.type.link._case.LinkAttributes;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.linkstate.routes.linkstate.route.attributes.attribute.type.node._case.NodeAttributes;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.linkstate.routes.linkstate.route.attributes.attribute.type.prefix._case.PrefixAttributes;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.CRouterIdentifier;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.IsisNodeCase;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.IsisPseudonodeCase;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.OspfNodeCase;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.OspfPseudonodeCase;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.isis.node._case.IsisNode;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.isis.pseudonode._case.IsisPseudonode;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.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.opendaylight.params.xml.ns.yang.rsvp.rev130820.SrlgId;
58 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IsoPseudonodeId;
59 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IsoSystemId;
60 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.link.attributes.IsisLinkAttributesBuilder;
61 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.node.attributes.IsisNodeAttributesBuilder;
62 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.node.attributes.isis.node.attributes.IsoBuilder;
63 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.srlg.attributes.SrlgValues;
64 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.srlg.attributes.SrlgValuesBuilder;
65 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.SrlgBuilder;
66 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.UnreservedBandwidth;
67 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.UnreservedBandwidthBuilder;
68 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.UnreservedBandwidthKey;
69 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.LinkId;
70 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
71 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
72 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
73 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.DestinationBuilder;
74 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.SourceBuilder;
75 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
76 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkBuilder;
77 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkKey;
78 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
79 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
80 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
81 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.TopologyTypesBuilder;
82 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
83 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
84 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
85 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Link1;
86 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Link1Builder;
87 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1;
88 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1Builder;
89 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TerminationPoint1;
90 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TerminationPoint1Builder;
91 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TopologyTypes1;
92 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TopologyTypes1Builder;
93 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.link.attributes.IgpLinkAttributesBuilder;
94 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.IgpNodeAttributesBuilder;
95 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.igp.node.attributes.Prefix;
96 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.igp.node.attributes.PrefixBuilder;
97 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.igp.node.attributes.PrefixKey;
98 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.termination.point.attributes.IgpTerminationPointAttributesBuilder;
99 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.termination.point.attributes.igp.termination.point.attributes.TerminationPointType;
100 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.termination.point.attributes.igp.termination.point.attributes.termination.point.type.IpBuilder;
101 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.termination.point.attributes.igp.termination.point.attributes.termination.point.type.UnnumberedBuilder;
102 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.Prefix1;
103 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.Prefix1Builder;
104 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.link.attributes.OspfLinkAttributesBuilder;
105 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.OspfNodeAttributesBuilder;
106 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.ospf.node.attributes.router.type.AbrBuilder;
107 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.ospf.node.attributes.router.type.InternalBuilder;
108 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.ospf.node.attributes.router.type.PseudonodeBuilder;
109 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.prefix.attributes.OspfPrefixAttributesBuilder;
110 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
111 import org.slf4j.Logger;
112 import org.slf4j.LoggerFactory;
114 public final class LinkstateTopologyBuilder extends AbstractTopologyBuilder<LinkstateRoute> {
115 private static final class TpHolder {
116 private final Set<LinkId> local = new HashSet<>();
117 private final Set<LinkId> remote = new HashSet<>();
119 private final TerminationPoint tp;
121 private TpHolder(final TerminationPoint tp) {
122 this.tp = Preconditions.checkNotNull(tp);
125 private synchronized void addLink(final LinkId id, final boolean isRemote) {
133 private synchronized boolean removeLink(final LinkId id, final boolean isRemote) {
134 final boolean removed;
136 removed = this.remote.remove(id);
138 removed = this.local.remove(id);
141 LOG.warn("Removed non-reference link {} from TP {} isRemote {}", this.tp.getTpId(), id, isRemote);
144 return this.local.isEmpty() && this.remote.isEmpty();
147 private TerminationPoint getTp() {
152 private final class NodeHolder {
153 private final Map<PrefixKey, Prefix> prefixes = new HashMap<>();
154 private final Map<TpId, TpHolder> tps = new HashMap<>();
155 private boolean advertized = false;
156 private IgpNodeAttributesBuilder inab;
157 private NodeBuilder nb;
159 private NodeHolder(final NodeId id) {
160 this.inab = new IgpNodeAttributesBuilder();
161 this.nb = new NodeBuilder().setKey(new NodeKey(id)).setNodeId(id);
165 * Synchronized in-core state of a node into the backing store using the transaction
167 * @param trans data modification transaction which to use
168 * @return True if the node has been purged, false otherwise.
170 private boolean syncState(final WriteTransaction trans) {
171 final InstanceIdentifier<Node> nid = getInstanceIdentifier().child(
172 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node.class,
176 * Transaction's putOperationalData() does a merge. Force it onto a replace
177 * by removing the data. If we decide to remove the node -- we just skip the put.
179 trans.delete(LogicalDatastoreType.OPERATIONAL, nid);
181 if (!this.advertized) {
182 if (this.tps.isEmpty() && this.prefixes.isEmpty()) {
183 LOG.debug("Removing unadvertized unused node {}", this.nb.getNodeId());
187 LOG.debug("Node {} is still implied by {} TPs and {} prefixes", this.nb.getNodeId(), this.tps.size(), this.prefixes.size());
190 // Re-generate termination points
191 this.nb.setTerminationPoint(Lists.newArrayList(Collections2.transform(this.tps.values(), new Function<TpHolder, TerminationPoint>() {
193 public TerminationPoint apply(final TpHolder input) {
194 return input.getTp();
198 // Re-generate prefixes
199 this.inab.setPrefix(Lists.newArrayList(this.prefixes.values()));
201 // Write the node out
202 final Node n = this.nb.addAugmentation(Node1.class, new Node1Builder().setIgpNodeAttributes(this.inab.build()).build()).build();
203 trans.put(LogicalDatastoreType.OPERATIONAL, nid, n);
204 LOG.debug("Created node {} at {}", n, nid);
208 private synchronized void removeTp(final TpId tp, final LinkId link, final boolean isRemote) {
209 final TpHolder h = this.tps.get(tp);
211 if (h.removeLink(link, isRemote)) {
213 LOG.debug("Removed TP {}", tp);
216 LOG.warn("Removed non-present TP {} by link {}", tp, link);
220 private void addTp(final TerminationPoint tp, final LinkId link, final boolean isRemote) {
221 TpHolder h = this.tps.get(tp.getTpId());
223 h = new TpHolder(tp);
224 this.tps.put(tp.getTpId(), h);
227 h.addLink(link, isRemote);
230 private void addPrefix(final Prefix pfx) {
231 this.prefixes.put(pfx.getKey(), pfx);
234 private void removePrefix(final PrefixCase p) {
235 this.prefixes.remove(new PrefixKey(p.getPrefixDescriptors().getIpReachabilityInformation()));
238 private void unadvertized() {
239 this.inab = new IgpNodeAttributesBuilder();
240 this.nb = new NodeBuilder().setKey(this.nb.getKey()).setNodeId(this.nb.getNodeId());
241 this.advertized = false;
242 LOG.debug("Node {} is unadvertized", this.nb.getNodeId());
245 private void advertized(final NodeBuilder nb, final IgpNodeAttributesBuilder inab) {
246 this.nb = Preconditions.checkNotNull(nb);
247 this.inab = Preconditions.checkNotNull(inab);
248 this.advertized = true;
249 LOG.debug("Node {} is advertized", nb.getNodeId());
252 private Object getNodeId() {
253 return this.nb.getNodeId();
257 private static final Logger LOG = LoggerFactory.getLogger(LinkstateTopologyBuilder.class);
258 private final Map<NodeId, NodeHolder> nodes = new HashMap<>();
260 public LinkstateTopologyBuilder(final DataBroker dataProvider, final RibReference locRibReference, final TopologyId topologyId) {
261 super(dataProvider, locRibReference, topologyId, new TopologyTypesBuilder().addAugmentation(TopologyTypes1.class,
262 new TopologyTypes1Builder().build()).build(), LinkstateRoute.class);
265 private LinkId buildLinkId(final UriBuilder base, final LinkCase link) {
266 return new LinkId(new UriBuilder(base, "link").add(link).toString());
269 private NodeId buildNodeId(final UriBuilder base, final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NodeIdentifier node) {
270 return new NodeId(new UriBuilder(base, "node").add("", node).toString());
273 private TpId buildTpId(final UriBuilder base, final TopologyIdentifier topologyIdentifier,
274 final Ipv4InterfaceIdentifier ipv4InterfaceIdentifier, final Ipv6InterfaceIdentifier ipv6InterfaceIdentifier, final Long id) {
275 final UriBuilder b = new UriBuilder(base, "tp");
276 if (topologyIdentifier != null) {
277 b.add("mt", topologyIdentifier.getValue());
279 if (ipv4InterfaceIdentifier != null) {
280 b.add("ipv4", ipv4InterfaceIdentifier.getValue());
282 if (ipv6InterfaceIdentifier != null) {
283 b.add("ipv6", ipv6InterfaceIdentifier.getValue());
286 return new TpId(b.add("id", id).toString());
289 private TpId buildLocalTpId(final UriBuilder base, final LinkDescriptors linkDescriptors) {
290 return buildTpId(base, linkDescriptors.getMultiTopologyId(), linkDescriptors.getIpv4InterfaceAddress(),
291 linkDescriptors.getIpv6InterfaceAddress(), linkDescriptors.getLinkLocalIdentifier());
294 private TerminationPoint buildTp(final TpId id, final TerminationPointType type) {
295 final TerminationPointBuilder stpb = new TerminationPointBuilder();
296 stpb.setKey(new TerminationPointKey(id));
300 stpb.addAugmentation(TerminationPoint1.class, 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<Link> buildLinkIdentifier(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.rev150210.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.rev150210.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()));
399 if (la.getSharedRiskLinkGroups() != null) {
400 final List<SrlgValues> srlgs = new ArrayList<>();
401 for (final SrlgId id : la.getSharedRiskLinkGroups()) {
402 srlgs.add(new SrlgValuesBuilder().setSrlgValue(id.getValue()).build());
404 tb.setSrlg(new SrlgBuilder().setSrlgValues(srlgs).build());
408 final IsisLinkAttributesBuilder ilab = new IsisLinkAttributesBuilder();
409 ilab.setTed(tb.build());
410 if (topologyIdentifier != null) {
411 ilab.setMultiTopologyId(topologyIdentifier.getValue().shortValue());
414 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpLinkAttributes1Builder().setIsisLinkAttributes(
415 ilab.build()).build();
418 private org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1 ospfLinkAttributes(
419 final TopologyIdentifier topologyIdentifier, final LinkAttributes la) {
420 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();
423 if (la.getAdminGroup() != null) {
424 tb.setColor(la.getAdminGroup().getValue());
426 if (la.getTeMetric() != null) {
427 tb.setTeDefaultMetric(la.getTeMetric().getValue());
429 if (la.getUnreservedBandwidth() != null) {
430 tb.setUnreservedBandwidth(unreservedBandwidthList(la.getUnreservedBandwidth()));
432 if (la.getMaxLinkBandwidth() != null) {
433 tb.setMaxLinkBandwidth(bandwidthToBigDecimal(la.getMaxLinkBandwidth()));
435 if (la.getMaxReservableBandwidth() != null) {
436 tb.setMaxResvLinkBandwidth(bandwidthToBigDecimal(la.getMaxReservableBandwidth()));
438 if (la.getSharedRiskLinkGroups() != null) {
439 final List<SrlgValues> srlgs = new ArrayList<>();
440 for (final SrlgId id : la.getSharedRiskLinkGroups()) {
441 srlgs.add(new SrlgValuesBuilder().setSrlgValue(id.getValue()).build());
443 tb.setSrlg(new SrlgBuilder().setSrlgValues(srlgs).build());
447 final OspfLinkAttributesBuilder ilab = new OspfLinkAttributesBuilder();
448 ilab.setTed(tb.build());
449 if (topologyIdentifier != null) {
450 ilab.setMultiTopologyId(topologyIdentifier.getValue().shortValue());
453 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1Builder().setOspfLinkAttributes(
454 ilab.build()).build();
457 private NodeHolder getNode(final NodeId id) {
458 if (this.nodes.containsKey(id)) {
459 LOG.debug("Node {} is already present", id);
460 return this.nodes.get(id);
463 final NodeHolder ret = new NodeHolder(id);
464 this.nodes.put(id, ret);
468 private void putNode(final WriteTransaction trans, final NodeHolder holder) {
469 if (holder.syncState(trans)) {
470 this.nodes.remove(holder.getNodeId());
474 private void createLink(final WriteTransaction trans, final UriBuilder base,
475 final LinkstateRoute value, final LinkCase l, final Attributes attributes) {
477 final LinkAttributes la;
478 final Attributes1 attr = attributes.getAugmentation(Attributes1.class);
480 final AttributeType attrType = attr.getAttributeType();
481 if (attrType != null) {
482 la = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.linkstate.routes.linkstate.route.attributes.attribute.type.LinkCase)
483 attrType).getLinkAttributes();
485 LOG.debug("Missing attribute type in link {} route {}, skipping it", l, value);
489 LOG.debug("Missing attributes in link {} route {}, skipping it", l, value);
493 final IgpLinkAttributesBuilder ilab = new IgpLinkAttributesBuilder();
495 if (la.getMetric() != null) {
496 ilab.setMetric(la.getMetric().getValue());
498 ilab.setName(la.getLinkName());
501 switch (value.getProtocolId()) {
508 ilab.addAugmentation(
509 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpLinkAttributes1.class,
510 isisLinkAttributes(l.getLinkDescriptors().getMultiTopologyId(), la));
513 ilab.addAugmentation(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1.class,
514 ospfLinkAttributes(l.getLinkDescriptors().getMultiTopologyId(), la));
520 final LinkBuilder lb = new LinkBuilder();
521 lb.setLinkId(buildLinkId(base, l));
522 lb.addAugmentation(Link1.class, new Link1Builder().setIgpLinkAttributes(ilab.build()).build());
524 final NodeId srcNode = buildNodeId(base, l.getLocalNodeDescriptors());
525 LOG.debug("Link {} implies source node {}", l, srcNode);
527 final NodeId dstNode = buildNodeId(base, l.getRemoteNodeDescriptors());
528 LOG.debug("Link {} implies destination node {}", l, dstNode);
530 final TerminationPoint srcTp = buildLocalTp(base, l.getLinkDescriptors());
531 LOG.debug("Link {} implies source TP {}", l, srcTp);
533 final TerminationPoint dstTp = buildRemoteTp(base, l.getLinkDescriptors());
534 LOG.debug("Link {} implies destination TP {}", l, dstTp);
536 lb.setSource(new SourceBuilder().setSourceNode(srcNode).setSourceTp(srcTp.getTpId()).build());
537 lb.setDestination(new DestinationBuilder().setDestNode(dstNode).setDestTp(dstTp.getTpId()).build());
539 final NodeHolder snh = getNode(srcNode);
540 snh.addTp(srcTp, lb.getLinkId(), false);
541 LOG.debug("Created TP {} as link source", srcTp);
544 final NodeHolder dnh = getNode(dstNode);
545 dnh.addTp(dstTp, lb.getLinkId(), true);
546 LOG.debug("Created TP {} as link destination", dstTp);
549 final InstanceIdentifier<Link> lid = buildLinkIdentifier(lb.getLinkId());
550 final Link link = lb.build();
552 trans.put(LogicalDatastoreType.OPERATIONAL, lid, link);
553 LOG.debug("Created link {} at {} for {}", link, lid, l);
556 private void removeTp(final WriteTransaction trans, final NodeId node, final TpId tp,
557 final LinkId link, final boolean isRemote) {
558 final NodeHolder nh = this.nodes.get(node);
560 nh.removeTp(tp, link, isRemote);
563 LOG.warn("Removed non-existent node {}", node);
567 private void removeLink(final WriteTransaction trans, final UriBuilder base, final LinkCase l) {
568 final LinkId id = buildLinkId(base, l);
569 final InstanceIdentifier<?> lid = buildLinkIdentifier(id);
570 trans.delete(LogicalDatastoreType.OPERATIONAL, lid);
571 LOG.debug("Removed link {}", lid);
573 removeTp(trans, buildNodeId(base, l.getLocalNodeDescriptors()), buildLocalTpId(base, l.getLinkDescriptors()), id, false);
574 removeTp(trans, buildNodeId(base, l.getRemoteNodeDescriptors()), buildRemoteTpId(base, l.getLinkDescriptors()), id, true);
577 private List<Short> nodeMultiTopology(final List<TopologyIdentifier> list) {
578 final List<Short> ret = new ArrayList<>(list.size());
579 for (final TopologyIdentifier id : list) {
580 ret.add(id.getValue().shortValue());
585 private org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1 isisNodeAttributes(
586 final NodeIdentifier node, final NodeAttributes na) {
587 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();
588 final IsisNodeAttributesBuilder ab = new IsisNodeAttributesBuilder();
591 if (na.getIpv4RouterId() != null) {
592 tb.setTeRouterIdIpv4(na.getIpv4RouterId());
594 if (na.getIpv6RouterId() != null) {
595 tb.setTeRouterIdIpv6(na.getIpv6RouterId());
597 if (na.getTopologyIdentifier() != null) {
598 ab.setMultiTopologyId(nodeMultiTopology(na.getTopologyIdentifier()));
602 final CRouterIdentifier ri = node.getCRouterIdentifier();
603 if (ri instanceof IsisPseudonodeCase) {
604 final IsisPseudonode pn = ((IsisPseudonodeCase) ri).getIsisPseudonode();
605 final IsoBuilder b = new IsoBuilder();
606 b.setIsoSystemId(new IsoSystemId(UriBuilder.isoId(pn.getIsIsRouterIdentifier().getIsoSystemId())));
607 b.setIsoPseudonodeId(new IsoPseudonodeId(Hex.encodeHexString(new byte[] {pn.getPsn().byteValue()})));
608 ab.setIso(b.build());
609 } else if (ri instanceof IsisNodeCase) {
610 final IsisNode in = ((IsisNodeCase) ri).getIsisNode();
611 ab.setIso(new IsoBuilder().setIsoSystemId(new IsoSystemId(UriBuilder.isoId(in.getIsoSystemId()))).build());
614 ab.setTed(tb.build());
616 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1Builder().setIsisNodeAttributes(
620 private org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1 ospfNodeAttributes(
621 final NodeIdentifier node, final NodeAttributes na) {
622 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();
623 final OspfNodeAttributesBuilder ab = new OspfNodeAttributesBuilder();
625 if (na.getIpv4RouterId() != null) {
626 tb.setTeRouterIdIpv4(na.getIpv4RouterId());
628 if (na.getIpv6RouterId() != null) {
629 tb.setTeRouterIdIpv6(na.getIpv6RouterId());
631 if (na.getTopologyIdentifier() != null) {
632 ab.setMultiTopologyId(nodeMultiTopology(na.getTopologyIdentifier()));
634 final CRouterIdentifier ri = node.getCRouterIdentifier();
635 if (ri instanceof OspfPseudonodeCase) {
636 final OspfPseudonode pn = ((OspfPseudonodeCase) ri).getOspfPseudonode();
638 ab.setRouterType(new PseudonodeBuilder().setPseudonode(Boolean.TRUE).build());
639 ab.setDrInterfaceId(pn.getLanInterface().getValue());
640 } else if (ri instanceof OspfNodeCase && na.getNodeFlags() != null) {
641 // TODO: what should we do with in.getOspfRouterId()?
643 final NodeFlagBits nf = na.getNodeFlags();
644 if (nf.isAbr() != null) {
645 ab.setRouterType(new AbrBuilder().setAbr(nf.isAbr()).build());
646 } else if (nf.isExternal() != null) {
647 ab.setRouterType(new InternalBuilder().setInternal(!nf.isExternal()).build());
651 ab.setTed(tb.build());
652 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1Builder().setOspfNodeAttributes(
656 private void augmentProtocolId(final LinkstateRoute value, final IgpNodeAttributesBuilder inab, final NodeAttributes na, final NodeIdentifier nd) {
657 switch (value.getProtocolId()) {
664 inab.addAugmentation(
665 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1.class,
666 isisNodeAttributes(nd, na));
669 inab.addAugmentation(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1.class,
670 ospfNodeAttributes(nd, na));
677 private void createNode(final WriteTransaction trans, final UriBuilder base,
678 final LinkstateRoute value, final NodeCase n, final Attributes attributes) {
679 final NodeAttributes na;
681 final Attributes1 attr = attributes.getAugmentation(Attributes1.class);
683 final AttributeType attrType = attr.getAttributeType();
684 if (attrType != null) {
685 na = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.linkstate.routes.linkstate.route.attributes.attribute.type.NodeCase)
686 attrType).getNodeAttributes();
688 LOG.debug("Missing attribute type in node {} route {}, skipping it", n, value);
692 LOG.debug("Missing attributes in node {} route {}, skipping it", n, value);
695 final IgpNodeAttributesBuilder inab = new IgpNodeAttributesBuilder();
696 final List<IpAddress> ids = new ArrayList<>();
698 if (na.getIpv4RouterId() != null) {
699 ids.add(new IpAddress(na.getIpv4RouterId()));
701 if (na.getIpv6RouterId() != null) {
702 ids.add(new IpAddress(na.getIpv6RouterId()));
704 if (na.getDynamicHostname() != null) {
705 inab.setName(new DomainName(na.getDynamicHostname()));
708 if (!ids.isEmpty()) {
709 inab.setRouterId(ids);
711 augmentProtocolId(value, inab, na, n.getNodeDescriptors());
713 final NodeId nid = buildNodeId(base, n.getNodeDescriptors());
714 final NodeHolder nh = getNode(nid);
716 * Eventhough the the holder creates a dummy structure, we need to duplicate it here,
717 * as that is the API requirement. The reason for it is the possible presence of supporting
718 * node -- something which the holder does not track.
720 final NodeBuilder nb = new NodeBuilder();
722 nb.setKey(new NodeKey(nb.getNodeId()));
724 nh.advertized(nb, inab);
728 private void removeNode(final WriteTransaction trans, final UriBuilder base, final NodeCase n) {
729 final NodeId id = buildNodeId(base, n.getNodeDescriptors());
730 final NodeHolder nh = this.nodes.get(id);
735 LOG.warn("Node {} does not have a holder", id);
739 private void augmentProtocolId(final LinkstateRoute value, final PrefixAttributes pa, final PrefixBuilder pb) {
740 switch (value.getProtocolId()) {
748 if (pa != null && pa.getOspfForwardingAddress() != null) {
751 new Prefix1Builder().setOspfPrefixAttributes(
752 new OspfPrefixAttributesBuilder().setForwardingAddress(pa.getOspfForwardingAddress().getIpv4Address()).build()).build());
760 private void createPrefix(final WriteTransaction trans, final UriBuilder base,
761 final LinkstateRoute value, final PrefixCase p, final Attributes attributes) {
762 final IpPrefix ippfx = p.getPrefixDescriptors().getIpReachabilityInformation();
764 LOG.warn("IP reachability not present in prefix {} route {}, skipping it", p, value);
767 final PrefixBuilder pb = new PrefixBuilder();
768 pb.setKey(new PrefixKey(ippfx));
771 final PrefixAttributes pa;
772 // Very defensive lookup
773 final Attributes1 attr = attributes.getAugmentation(Attributes1.class);
775 final AttributeType attrType = attr.getAttributeType();
776 if (attrType != null) {
777 pa = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.linkstate.routes.linkstate.route.attributes.attribute.type.PrefixCase)
778 attrType).getPrefixAttributes();
780 LOG.debug("Missing attribute type in IP {} prefix {} route {}, skipping it", ippfx, p, value);
784 LOG.debug("Missing attributes in IP {} prefix {} route {}, skipping it", ippfx, p, value);
788 pb.setMetric(pa.getPrefixMetric().getValue());
790 augmentProtocolId(value, pa, pb);
792 final Prefix pfx = pb.build();
795 * All set, but... the hosting node may not exist, we may need to fake it.
797 final NodeId node = buildNodeId(base, p.getAdvertisingNodeDescriptors());
798 final NodeHolder nh = getNode(node);
800 LOG.debug("Created prefix {} for {}", pfx, p);
804 private void removePrefix(final WriteTransaction trans, final UriBuilder base, final PrefixCase p) {
805 final NodeId node = buildNodeId(base, p.getAdvertisingNodeDescriptors());
806 final NodeHolder nh = this.nodes.get(node);
809 LOG.debug("Removed prefix {}", p);
812 LOG.warn("Removing prefix from non-existing node {}", node);
817 protected void createObject(final ReadWriteTransaction trans,
818 final InstanceIdentifier<LinkstateRoute> id, final LinkstateRoute value) {
819 final UriBuilder base = new UriBuilder(value);
821 final ObjectType t = value.getObjectType();
822 Preconditions.checkArgument(t != null, "Route %s value %s has null object type", id, value);
824 if (t instanceof LinkCase) {
825 createLink(trans, base, value, (LinkCase) t, value.getAttributes());
826 } else if (t instanceof NodeCase) {
827 createNode(trans, base, value, (NodeCase) t, value.getAttributes());
828 } else if (t instanceof PrefixCase) {
829 createPrefix(trans, base, value, (PrefixCase) t, value.getAttributes());
831 throw new IllegalStateException("Unhandled object class " + t.getImplementedInterface());
836 protected void removeObject(final ReadWriteTransaction trans,
837 final InstanceIdentifier<LinkstateRoute> id, final LinkstateRoute value) {
838 final UriBuilder base = new UriBuilder(value);
840 final ObjectType t = value.getObjectType();
841 if (t instanceof LinkCase) {
842 removeLink(trans, base, (LinkCase) t);
843 } else if (t instanceof NodeCase) {
844 removeNode(trans, base, (NodeCase) t);
845 } else if (t instanceof PrefixCase) {
846 removePrefix(trans, base, (PrefixCase) t);
848 throw new IllegalStateException("Unhandled object class " + t.getImplementedInterface());