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 augmentProtocolId(final LinkstateRoute value, final IgpLinkAttributesBuilder ilab, final LinkAttributes la, final LinkDescriptors ld) {
475 switch (value.getProtocolId()) {
482 ilab.addAugmentation(
483 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpLinkAttributes1.class,
484 isisLinkAttributes(ld.getMultiTopologyId(), la));
487 ilab.addAugmentation(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1.class,
488 ospfLinkAttributes(ld.getMultiTopologyId(), la));
495 private void createLink(final WriteTransaction trans, final UriBuilder base,
496 final LinkstateRoute value, final LinkCase l, final Attributes attributes) {
498 final LinkAttributes la;
499 final Attributes1 attr = attributes.getAugmentation(Attributes1.class);
501 final AttributeType attrType = attr.getAttributeType();
502 if (attrType != null) {
503 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)
504 attrType).getLinkAttributes();
506 LOG.debug("Missing attribute type in link {} route {}, skipping it", l, value);
510 LOG.debug("Missing attributes in link {} route {}, skipping it", l, value);
514 final IgpLinkAttributesBuilder ilab = new IgpLinkAttributesBuilder();
516 if (la.getMetric() != null) {
517 ilab.setMetric(la.getMetric().getValue());
519 ilab.setName(la.getLinkName());
521 augmentProtocolId(value, ilab, la, l.getLinkDescriptors());
523 final LinkBuilder lb = new LinkBuilder();
524 lb.setLinkId(buildLinkId(base, l));
525 lb.addAugmentation(Link1.class, new Link1Builder().setIgpLinkAttributes(ilab.build()).build());
527 final NodeId srcNode = buildNodeId(base, l.getLocalNodeDescriptors());
528 LOG.debug("Link {} implies source node {}", l, srcNode);
530 final NodeId dstNode = buildNodeId(base, l.getRemoteNodeDescriptors());
531 LOG.debug("Link {} implies destination node {}", l, dstNode);
533 final TerminationPoint srcTp = buildLocalTp(base, l.getLinkDescriptors());
534 LOG.debug("Link {} implies source TP {}", l, srcTp);
536 final TerminationPoint dstTp = buildRemoteTp(base, l.getLinkDescriptors());
537 LOG.debug("Link {} implies destination TP {}", l, dstTp);
539 lb.setSource(new SourceBuilder().setSourceNode(srcNode).setSourceTp(srcTp.getTpId()).build());
540 lb.setDestination(new DestinationBuilder().setDestNode(dstNode).setDestTp(dstTp.getTpId()).build());
542 final NodeHolder snh = getNode(srcNode);
543 snh.addTp(srcTp, lb.getLinkId(), false);
544 LOG.debug("Created TP {} as link source", srcTp);
547 final NodeHolder dnh = getNode(dstNode);
548 dnh.addTp(dstTp, lb.getLinkId(), true);
549 LOG.debug("Created TP {} as link destination", dstTp);
552 final InstanceIdentifier<Link> lid = buildLinkIdentifier(lb.getLinkId());
553 final Link link = lb.build();
555 trans.put(LogicalDatastoreType.OPERATIONAL, lid, link);
556 LOG.debug("Created link {} at {} for {}", link, lid, l);
559 private void removeTp(final WriteTransaction trans, final NodeId node, final TpId tp,
560 final LinkId link, final boolean isRemote) {
561 final NodeHolder nh = this.nodes.get(node);
563 nh.removeTp(tp, link, isRemote);
566 LOG.warn("Removed non-existent node {}", node);
570 private void removeLink(final WriteTransaction trans, final UriBuilder base, final LinkCase l) {
571 final LinkId id = buildLinkId(base, l);
572 final InstanceIdentifier<?> lid = buildLinkIdentifier(id);
573 trans.delete(LogicalDatastoreType.OPERATIONAL, lid);
574 LOG.debug("Removed link {}", lid);
576 removeTp(trans, buildNodeId(base, l.getLocalNodeDescriptors()), buildLocalTpId(base, l.getLinkDescriptors()), id, false);
577 removeTp(trans, buildNodeId(base, l.getRemoteNodeDescriptors()), buildRemoteTpId(base, l.getLinkDescriptors()), id, true);
580 private List<Short> nodeMultiTopology(final List<TopologyIdentifier> list) {
581 final List<Short> ret = new ArrayList<>(list.size());
582 for (final TopologyIdentifier id : list) {
583 ret.add(id.getValue().shortValue());
588 private org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1 isisNodeAttributes(
589 final NodeIdentifier node, final NodeAttributes na) {
590 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();
591 final IsisNodeAttributesBuilder ab = new IsisNodeAttributesBuilder();
594 if (na.getIpv4RouterId() != null) {
595 tb.setTeRouterIdIpv4(na.getIpv4RouterId());
597 if (na.getIpv6RouterId() != null) {
598 tb.setTeRouterIdIpv6(na.getIpv6RouterId());
600 if (na.getTopologyIdentifier() != null) {
601 ab.setMultiTopologyId(nodeMultiTopology(na.getTopologyIdentifier()));
605 final CRouterIdentifier ri = node.getCRouterIdentifier();
606 if (ri instanceof IsisPseudonodeCase) {
607 final IsisPseudonode pn = ((IsisPseudonodeCase) ri).getIsisPseudonode();
608 final IsoBuilder b = new IsoBuilder();
609 b.setIsoSystemId(new IsoSystemId(UriBuilder.isoId(pn.getIsIsRouterIdentifier().getIsoSystemId())));
610 b.setIsoPseudonodeId(new IsoPseudonodeId(Hex.encodeHexString(new byte[] {pn.getPsn().byteValue()})));
611 ab.setIso(b.build());
612 } else if (ri instanceof IsisNodeCase) {
613 final IsisNode in = ((IsisNodeCase) ri).getIsisNode();
614 ab.setIso(new IsoBuilder().setIsoSystemId(new IsoSystemId(UriBuilder.isoId(in.getIsoSystemId()))).build());
617 ab.setTed(tb.build());
619 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1Builder().setIsisNodeAttributes(
623 private org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1 ospfNodeAttributes(
624 final NodeIdentifier node, final NodeAttributes na) {
625 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();
626 final OspfNodeAttributesBuilder ab = new OspfNodeAttributesBuilder();
628 if (na.getIpv4RouterId() != null) {
629 tb.setTeRouterIdIpv4(na.getIpv4RouterId());
631 if (na.getIpv6RouterId() != null) {
632 tb.setTeRouterIdIpv6(na.getIpv6RouterId());
634 if (na.getTopologyIdentifier() != null) {
635 ab.setMultiTopologyId(nodeMultiTopology(na.getTopologyIdentifier()));
637 final CRouterIdentifier ri = node.getCRouterIdentifier();
638 if (ri instanceof OspfPseudonodeCase) {
639 final OspfPseudonode pn = ((OspfPseudonodeCase) ri).getOspfPseudonode();
641 ab.setRouterType(new PseudonodeBuilder().setPseudonode(Boolean.TRUE).build());
642 ab.setDrInterfaceId(pn.getLanInterface().getValue());
643 } else if (ri instanceof OspfNodeCase && na.getNodeFlags() != null) {
644 // TODO: what should we do with in.getOspfRouterId()?
646 final NodeFlagBits nf = na.getNodeFlags();
647 if (nf.isAbr() != null) {
648 ab.setRouterType(new AbrBuilder().setAbr(nf.isAbr()).build());
649 } else if (nf.isExternal() != null) {
650 ab.setRouterType(new InternalBuilder().setInternal(!nf.isExternal()).build());
654 ab.setTed(tb.build());
655 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1Builder().setOspfNodeAttributes(
659 private void augmentProtocolId(final LinkstateRoute value, final IgpNodeAttributesBuilder inab, final NodeAttributes na, final NodeIdentifier nd) {
660 switch (value.getProtocolId()) {
667 inab.addAugmentation(
668 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1.class,
669 isisNodeAttributes(nd, na));
672 inab.addAugmentation(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1.class,
673 ospfNodeAttributes(nd, na));
680 private void createNode(final WriteTransaction trans, final UriBuilder base,
681 final LinkstateRoute value, final NodeCase n, final Attributes attributes) {
682 final NodeAttributes na;
684 final Attributes1 attr = attributes.getAugmentation(Attributes1.class);
686 final AttributeType attrType = attr.getAttributeType();
687 if (attrType != null) {
688 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)
689 attrType).getNodeAttributes();
691 LOG.debug("Missing attribute type in node {} route {}, skipping it", n, value);
695 LOG.debug("Missing attributes in node {} route {}, skipping it", n, value);
698 final IgpNodeAttributesBuilder inab = new IgpNodeAttributesBuilder();
699 final List<IpAddress> ids = new ArrayList<>();
701 if (na.getIpv4RouterId() != null) {
702 ids.add(new IpAddress(na.getIpv4RouterId()));
704 if (na.getIpv6RouterId() != null) {
705 ids.add(new IpAddress(na.getIpv6RouterId()));
707 if (na.getDynamicHostname() != null) {
708 inab.setName(new DomainName(na.getDynamicHostname()));
711 if (!ids.isEmpty()) {
712 inab.setRouterId(ids);
714 augmentProtocolId(value, inab, na, n.getNodeDescriptors());
716 final NodeId nid = buildNodeId(base, n.getNodeDescriptors());
717 final NodeHolder nh = getNode(nid);
719 * Eventhough the the holder creates a dummy structure, we need to duplicate it here,
720 * as that is the API requirement. The reason for it is the possible presence of supporting
721 * node -- something which the holder does not track.
723 final NodeBuilder nb = new NodeBuilder();
725 nb.setKey(new NodeKey(nb.getNodeId()));
727 nh.advertized(nb, inab);
731 private void removeNode(final WriteTransaction trans, final UriBuilder base, final NodeCase n) {
732 final NodeId id = buildNodeId(base, n.getNodeDescriptors());
733 final NodeHolder nh = this.nodes.get(id);
738 LOG.warn("Node {} does not have a holder", id);
742 private void augmentProtocolId(final LinkstateRoute value, final PrefixAttributes pa, final PrefixBuilder pb) {
743 switch (value.getProtocolId()) {
751 if (pa != null && pa.getOspfForwardingAddress() != null) {
754 new Prefix1Builder().setOspfPrefixAttributes(
755 new OspfPrefixAttributesBuilder().setForwardingAddress(pa.getOspfForwardingAddress().getIpv4Address()).build()).build());
763 private void createPrefix(final WriteTransaction trans, final UriBuilder base,
764 final LinkstateRoute value, final PrefixCase p, final Attributes attributes) {
765 final IpPrefix ippfx = p.getPrefixDescriptors().getIpReachabilityInformation();
767 LOG.warn("IP reachability not present in prefix {} route {}, skipping it", p, value);
770 final PrefixBuilder pb = new PrefixBuilder();
771 pb.setKey(new PrefixKey(ippfx));
774 final PrefixAttributes pa;
775 // Very defensive lookup
776 final Attributes1 attr = attributes.getAugmentation(Attributes1.class);
778 final AttributeType attrType = attr.getAttributeType();
779 if (attrType != null) {
780 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)
781 attrType).getPrefixAttributes();
783 LOG.debug("Missing attribute type in IP {} prefix {} route {}, skipping it", ippfx, p, value);
787 LOG.debug("Missing attributes in IP {} prefix {} route {}, skipping it", ippfx, p, value);
791 pb.setMetric(pa.getPrefixMetric().getValue());
793 augmentProtocolId(value, pa, pb);
795 final Prefix pfx = pb.build();
798 * All set, but... the hosting node may not exist, we may need to fake it.
800 final NodeId node = buildNodeId(base, p.getAdvertisingNodeDescriptors());
801 final NodeHolder nh = getNode(node);
803 LOG.debug("Created prefix {} for {}", pfx, p);
807 private void removePrefix(final WriteTransaction trans, final UriBuilder base, final PrefixCase p) {
808 final NodeId node = buildNodeId(base, p.getAdvertisingNodeDescriptors());
809 final NodeHolder nh = this.nodes.get(node);
812 LOG.debug("Removed prefix {}", p);
815 LOG.warn("Removing prefix from non-existing node {}", node);
820 protected void createObject(final ReadWriteTransaction trans,
821 final InstanceIdentifier<LinkstateRoute> id, final LinkstateRoute value) {
822 final UriBuilder base = new UriBuilder(value);
824 final ObjectType t = value.getObjectType();
825 Preconditions.checkArgument(t != null, "Route %s value %s has null object type", id, value);
827 if (t instanceof LinkCase) {
828 createLink(trans, base, value, (LinkCase) t, value.getAttributes());
829 } else if (t instanceof NodeCase) {
830 createNode(trans, base, value, (NodeCase) t, value.getAttributes());
831 } else if (t instanceof PrefixCase) {
832 createPrefix(trans, base, value, (PrefixCase) t, value.getAttributes());
834 throw new IllegalStateException("Unhandled object class " + t.getImplementedInterface());
839 protected void removeObject(final ReadWriteTransaction trans,
840 final InstanceIdentifier<LinkstateRoute> id, final LinkstateRoute value) {
841 final UriBuilder base = new UriBuilder(value);
843 final ObjectType t = value.getObjectType();
844 if (t instanceof LinkCase) {
845 removeLink(trans, base, (LinkCase) t);
846 } else if (t instanceof NodeCase) {
847 removeNode(trans, base, (NodeCase) t);
848 } else if (t instanceof PrefixCase) {
849 removePrefix(trans, base, (PrefixCase) t);
851 throw new IllegalStateException("Unhandled object class " + t.getImplementedInterface());