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.rev131125.Ipv4InterfaceIdentifier;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.Ipv6InterfaceIdentifier;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.NodeFlagBits;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.NodeIdentifier;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.TopologyIdentifier;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.routes.linkstate.routes.LinkstateRoute;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.routes.linkstate.routes.linkstate.route.Attributes1;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.routes.linkstate.routes.linkstate.route.ObjectType;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.routes.linkstate.routes.linkstate.route.attributes.AttributeType;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.routes.linkstate.routes.linkstate.route.attributes.attribute.type.link._case.LinkAttributes;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.routes.linkstate.routes.linkstate.route.attributes.attribute.type.node._case.NodeAttributes;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.routes.linkstate.routes.linkstate.route.attributes.attribute.type.prefix._case.PrefixAttributes;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.routes.linkstate.routes.linkstate.route.object.type.LinkCase;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.routes.linkstate.routes.linkstate.route.object.type.NodeCase;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.routes.linkstate.routes.linkstate.route.object.type.PrefixCase;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.routes.linkstate.routes.linkstate.route.object.type.link._case.LinkDescriptors;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.routes.linkstate.routes.linkstate.route.object.type.node._case.NodeDescriptors;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.CRouterIdentifier;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.IsisNodeCase;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.IsisPseudonodeCase;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.OspfNodeCase;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.OspfPseudonodeCase;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.isis.node._case.IsisNode;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.isis.pseudonode._case.IsisPseudonode;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.ospf.pseudonode._case.OspfPseudonode;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.route.Attributes;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.SrlgId;
59 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IsoPseudonodeId;
60 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IsoSystemId;
61 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.link.attributes.IsisLinkAttributesBuilder;
62 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.node.attributes.IsisNodeAttributesBuilder;
63 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.node.attributes.isis.node.attributes.IsoBuilder;
64 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.srlg.attributes.SrlgValues;
65 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.srlg.attributes.SrlgValuesBuilder;
66 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.SrlgBuilder;
67 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.UnreservedBandwidth;
68 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.UnreservedBandwidthBuilder;
69 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.UnreservedBandwidthKey;
70 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.LinkId;
71 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
72 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
73 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
74 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.DestinationBuilder;
75 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.SourceBuilder;
76 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
77 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkBuilder;
78 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkKey;
79 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
80 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
81 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
82 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.TopologyTypesBuilder;
83 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
84 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
85 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
86 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Link1;
87 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Link1Builder;
88 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1;
89 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1Builder;
90 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TerminationPoint1;
91 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TerminationPoint1Builder;
92 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TopologyTypes1;
93 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TopologyTypes1Builder;
94 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.link.attributes.IgpLinkAttributesBuilder;
95 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.IgpNodeAttributesBuilder;
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.Prefix;
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.PrefixBuilder;
98 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;
99 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.termination.point.attributes.IgpTerminationPointAttributesBuilder;
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.TerminationPointType;
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.IpBuilder;
102 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;
103 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.Prefix1;
104 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.Prefix1Builder;
105 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.link.attributes.OspfLinkAttributesBuilder;
106 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.OspfNodeAttributesBuilder;
107 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.ospf.node.attributes.router.type.AbrBuilder;
108 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.ospf.node.attributes.router.type.InternalBuilder;
109 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.ospf.node.attributes.router.type.PseudonodeBuilder;
110 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.prefix.attributes.OspfPrefixAttributesBuilder;
111 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
112 import org.slf4j.Logger;
113 import org.slf4j.LoggerFactory;
115 public final class LinkstateTopologyBuilder extends AbstractTopologyBuilder<LinkstateRoute> {
116 private static final class TpHolder {
117 private final Set<LinkId> local = new HashSet<>();
118 private final Set<LinkId> remote = new HashSet<>();
120 private final TerminationPoint tp;
122 private TpHolder(final TerminationPoint tp) {
123 this.tp = Preconditions.checkNotNull(tp);
126 private synchronized void addLink(final LinkId id, final boolean isRemote) {
134 private synchronized boolean removeLink(final LinkId id, final boolean isRemote) {
135 final boolean removed;
137 removed = this.remote.remove(id);
139 removed = this.local.remove(id);
142 LOG.warn("Removed non-reference link {} from TP {} isRemote {}", this.tp.getTpId(), id, isRemote);
145 return this.local.isEmpty() && this.remote.isEmpty();
148 private TerminationPoint getTp() {
153 private final class NodeHolder {
154 private final Map<PrefixKey, Prefix> prefixes = new HashMap<>();
155 private final Map<TpId, TpHolder> tps = new HashMap<>();
156 private boolean advertized = false;
157 private IgpNodeAttributesBuilder inab;
158 private NodeBuilder nb;
160 private NodeHolder(final NodeId id) {
161 this.inab = new IgpNodeAttributesBuilder();
162 this.nb = new NodeBuilder().setKey(new NodeKey(id)).setNodeId(id);
166 * Synchronized in-core state of a node into the backing store using the transaction
168 * @param trans data modification transaction which to use
169 * @return True if the node has been purged, false otherwise.
171 private boolean syncState(final WriteTransaction trans) {
172 final InstanceIdentifier<Node> nid = getInstanceIdentifier().child(
173 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node.class,
177 * Transaction's putOperationalData() does a merge. Force it onto a replace
178 * by removing the data. If we decide to remove the node -- we just skip the put.
180 trans.delete(LogicalDatastoreType.OPERATIONAL, nid);
182 if (!this.advertized) {
183 if (this.tps.isEmpty() && this.prefixes.isEmpty()) {
184 LOG.debug("Removing unadvertized unused node {}", this.nb.getNodeId());
188 LOG.debug("Node {} is still implied by {} TPs and {} prefixes", this.nb.getNodeId(), this.tps.size(), this.prefixes.size());
191 // Re-generate termination points
192 this.nb.setTerminationPoint(Lists.newArrayList(Collections2.transform(this.tps.values(), new Function<TpHolder, TerminationPoint>() {
194 public TerminationPoint apply(final TpHolder input) {
195 return input.getTp();
199 // Re-generate prefixes
200 this.inab.setPrefix(Lists.newArrayList(this.prefixes.values()));
202 // Write the node out
203 final Node n = this.nb.addAugmentation(Node1.class, new Node1Builder().setIgpNodeAttributes(this.inab.build()).build()).build();
204 trans.put(LogicalDatastoreType.OPERATIONAL, nid, n);
205 LOG.debug("Created node {} at {}", n, nid);
209 private synchronized void removeTp(final TpId tp, final LinkId link, final boolean isRemote) {
210 final TpHolder h = this.tps.get(tp);
212 if (h.removeLink(link, isRemote)) {
214 LOG.debug("Removed TP {}", tp);
217 LOG.warn("Removed non-present TP {} by link {}", tp, link);
221 private void addTp(final TerminationPoint tp, final LinkId link, final boolean isRemote) {
222 TpHolder h = this.tps.get(tp.getTpId());
224 h = new TpHolder(tp);
225 this.tps.put(tp.getTpId(), h);
228 h.addLink(link, isRemote);
231 private void addPrefix(final Prefix pfx) {
232 this.prefixes.put(pfx.getKey(), pfx);
235 private void removePrefix(final PrefixCase p) {
236 this.prefixes.remove(new PrefixKey(p.getIpReachabilityInformation()));
239 private void unadvertized() {
240 this.inab = new IgpNodeAttributesBuilder();
241 this.nb = new NodeBuilder().setKey(this.nb.getKey()).setNodeId(this.nb.getNodeId());
242 this.advertized = false;
243 LOG.debug("Node {} is unadvertized", this.nb.getNodeId());
246 private void advertized(final NodeBuilder nb, final IgpNodeAttributesBuilder inab) {
247 this.nb = Preconditions.checkNotNull(nb);
248 this.inab = Preconditions.checkNotNull(inab);
249 this.advertized = true;
250 LOG.debug("Node {} is advertized", nb.getNodeId());
253 private Object getNodeId() {
254 return this.nb.getNodeId();
258 private static final Logger LOG = LoggerFactory.getLogger(LinkstateTopologyBuilder.class);
259 private final Map<NodeId, NodeHolder> nodes = new HashMap<>();
261 public LinkstateTopologyBuilder(final DataBroker dataProvider, final RibReference locRibReference, final TopologyId topologyId) {
262 super(dataProvider, locRibReference, topologyId, new TopologyTypesBuilder().addAugmentation(TopologyTypes1.class,
263 new TopologyTypes1Builder().build()).build(), LinkstateRoute.class);
266 private LinkId buildLinkId(final UriBuilder base, final LinkCase link) {
267 return new LinkId(new UriBuilder(base, "link").add(link).toString());
270 private NodeId buildNodeId(final UriBuilder base, final NodeIdentifier node) {
271 return new NodeId(new UriBuilder(base, "node").add("", node).toString());
274 private TpId buildTpId(final UriBuilder base, final TopologyIdentifier topologyIdentifier,
275 final Ipv4InterfaceIdentifier ipv4InterfaceIdentifier, final Ipv6InterfaceIdentifier ipv6InterfaceIdentifier, final Long id) {
276 final UriBuilder b = new UriBuilder(base, "tp");
277 if (topologyIdentifier != null) {
278 b.add("mt", topologyIdentifier.getValue());
280 if (ipv4InterfaceIdentifier != null) {
281 b.add("ipv4", ipv4InterfaceIdentifier.getValue());
283 if (ipv6InterfaceIdentifier != null) {
284 b.add("ipv6", ipv6InterfaceIdentifier.getValue());
287 return new TpId(b.add("id", id).toString());
290 private TpId buildLocalTpId(final UriBuilder base, final LinkDescriptors linkDescriptors) {
291 return buildTpId(base, linkDescriptors.getMultiTopologyId(), linkDescriptors.getIpv4InterfaceAddress(),
292 linkDescriptors.getIpv6InterfaceAddress(), linkDescriptors.getLinkLocalIdentifier());
295 private TerminationPoint buildTp(final TpId id, final TerminationPointType type) {
296 final TerminationPointBuilder stpb = new TerminationPointBuilder();
297 stpb.setKey(new TerminationPointKey(id));
301 stpb.addAugmentation(TerminationPoint1.class, new TerminationPoint1Builder().setIgpTerminationPointAttributes(
302 new IgpTerminationPointAttributesBuilder().setTerminationPointType(null).build()).build());
308 private TerminationPointType getTpType(final Ipv4InterfaceIdentifier ipv4InterfaceIdentifier,
309 final Ipv6InterfaceIdentifier ipv6InterfaceIdentifier, final Long id) {
310 // Order of preference: Unnumbered first, then IP
312 LOG.debug("Unnumbered termination point type: {}", id);
313 return new UnnumberedBuilder().setUnnumberedId(id).build();
317 if (ipv6InterfaceIdentifier != null) {
318 ip = new IpAddress(ipv6InterfaceIdentifier);
319 } else if (ipv4InterfaceIdentifier != null) {
320 ip = new IpAddress(ipv4InterfaceIdentifier);
326 LOG.debug("IP termination point type: {}", ip);
327 return new IpBuilder().setIpAddress(Lists.newArrayList(ip)).build();
333 private TerminationPoint buildLocalTp(final UriBuilder base, final LinkDescriptors linkDescriptors) {
334 final TpId id = buildLocalTpId(base, linkDescriptors);
335 final TerminationPointType t = getTpType(linkDescriptors.getIpv4InterfaceAddress(), linkDescriptors.getIpv6InterfaceAddress(),
336 linkDescriptors.getLinkLocalIdentifier());
338 return buildTp(id, t);
341 private TpId buildRemoteTpId(final UriBuilder base, final LinkDescriptors linkDescriptors) {
342 return buildTpId(base, linkDescriptors.getMultiTopologyId(), linkDescriptors.getIpv4NeighborAddress(),
343 linkDescriptors.getIpv6NeighborAddress(), linkDescriptors.getLinkRemoteIdentifier());
346 private TerminationPoint buildRemoteTp(final UriBuilder base, final LinkDescriptors linkDescriptors) {
347 final TpId id = buildRemoteTpId(base, linkDescriptors);
348 final TerminationPointType t = getTpType(linkDescriptors.getIpv4NeighborAddress(), linkDescriptors.getIpv6NeighborAddress(),
349 linkDescriptors.getLinkRemoteIdentifier());
351 return buildTp(id, t);
354 private InstanceIdentifier<Link> buildLinkIdentifier(final LinkId id) {
355 return getInstanceIdentifier().child(
356 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link.class,
360 private static Float bandwidthToFloat(final Bandwidth bandwidth) {
361 return ByteBuffer.wrap(bandwidth.getValue()).getFloat();
364 private static BigDecimal bandwidthToBigDecimal(final Bandwidth bandwidth) {
365 return BigDecimal.valueOf(bandwidthToFloat(bandwidth));
368 private static List<UnreservedBandwidth> unreservedBandwidthList(
369 final List<? extends org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.UnreservedBandwidth> input) {
370 final List<UnreservedBandwidth> ret = new ArrayList<>(input.size());
372 for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.UnreservedBandwidth i : input) {
373 ret.add(new UnreservedBandwidthBuilder().setBandwidth(bandwidthToBigDecimal(i.getBandwidth())).setKey(
374 new UnreservedBandwidthKey(i.getPriority())).build());
380 private org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpLinkAttributes1 isisLinkAttributes(
381 final TopologyIdentifier topologyIdentifier, final LinkAttributes la) {
382 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();
385 if (la.getAdminGroup() != null) {
386 tb.setColor(la.getAdminGroup().getValue());
388 if (la.getTeMetric() != null) {
389 tb.setTeDefaultMetric(la.getTeMetric().getValue());
391 if (la.getUnreservedBandwidth() != null) {
392 tb.setUnreservedBandwidth(unreservedBandwidthList(la.getUnreservedBandwidth()));
394 if (la.getMaxLinkBandwidth() != null) {
395 tb.setMaxLinkBandwidth(bandwidthToBigDecimal(la.getMaxLinkBandwidth()));
397 if (la.getMaxReservableBandwidth() != null) {
398 tb.setMaxResvLinkBandwidth(bandwidthToBigDecimal(la.getMaxReservableBandwidth()));
400 if (la.getSharedRiskLinkGroups() != null) {
401 final List<SrlgValues> srlgs = new ArrayList<>();
402 for (final SrlgId id : la.getSharedRiskLinkGroups()) {
403 srlgs.add(new SrlgValuesBuilder().setSrlgValue(id.getValue()).build());
405 tb.setSrlg(new SrlgBuilder().setSrlgValues(srlgs).build());
409 final IsisLinkAttributesBuilder ilab = new IsisLinkAttributesBuilder();
410 ilab.setTed(tb.build());
411 if (topologyIdentifier != null) {
412 ilab.setMultiTopologyId(topologyIdentifier.getValue().shortValue());
415 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpLinkAttributes1Builder().setIsisLinkAttributes(
416 ilab.build()).build();
419 private org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1 ospfLinkAttributes(
420 final TopologyIdentifier topologyIdentifier, final LinkAttributes la) {
421 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();
424 if (la.getAdminGroup() != null) {
425 tb.setColor(la.getAdminGroup().getValue());
427 if (la.getTeMetric() != null) {
428 tb.setTeDefaultMetric(la.getTeMetric().getValue());
430 if (la.getUnreservedBandwidth() != null) {
431 tb.setUnreservedBandwidth(unreservedBandwidthList(la.getUnreservedBandwidth()));
433 if (la.getMaxLinkBandwidth() != null) {
434 tb.setMaxLinkBandwidth(bandwidthToBigDecimal(la.getMaxLinkBandwidth()));
436 if (la.getMaxReservableBandwidth() != null) {
437 tb.setMaxResvLinkBandwidth(bandwidthToBigDecimal(la.getMaxReservableBandwidth()));
439 if (la.getSharedRiskLinkGroups() != null) {
440 final List<SrlgValues> srlgs = new ArrayList<>();
441 for (final SrlgId id : la.getSharedRiskLinkGroups()) {
442 srlgs.add(new SrlgValuesBuilder().setSrlgValue(id.getValue()).build());
444 tb.setSrlg(new SrlgBuilder().setSrlgValues(srlgs).build());
448 final OspfLinkAttributesBuilder ilab = new OspfLinkAttributesBuilder();
449 ilab.setTed(tb.build());
450 if (topologyIdentifier != null) {
451 ilab.setMultiTopologyId(topologyIdentifier.getValue().shortValue());
454 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1Builder().setOspfLinkAttributes(
455 ilab.build()).build();
458 private NodeHolder getNode(final NodeId id) {
459 if (this.nodes.containsKey(id)) {
460 LOG.debug("Node {} is already present", id);
461 return this.nodes.get(id);
464 final NodeHolder ret = new NodeHolder(id);
465 this.nodes.put(id, ret);
469 private void putNode(final WriteTransaction trans, final NodeHolder holder) {
470 if (holder.syncState(trans)) {
471 this.nodes.remove(holder.getNodeId());
475 private void createLink(final WriteTransaction trans, final UriBuilder base,
476 final LinkstateRoute value, final LinkCase l, final Attributes attributes) {
478 final LinkAttributes la;
479 final Attributes1 attr = attributes.getAugmentation(Attributes1.class);
481 final AttributeType attrType = attr.getAttributeType();
482 if (attrType != null) {
483 la = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.routes.linkstate.routes.linkstate.route.attributes.attribute.type.LinkCase)
484 attrType).getLinkAttributes();
486 LOG.debug("Missing attribute type in link {} route {}, skipping it", l, value);
490 LOG.debug("Missing attributes in link {} route {}, skipping it", l, value);
494 final IgpLinkAttributesBuilder ilab = new IgpLinkAttributesBuilder();
496 if (la.getMetric() != null) {
497 ilab.setMetric(la.getMetric().getValue());
499 ilab.setName(la.getLinkName());
502 switch (value.getProtocolId()) {
509 ilab.addAugmentation(
510 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpLinkAttributes1.class,
511 isisLinkAttributes(l.getLinkDescriptors().getMultiTopologyId(), la));
514 ilab.addAugmentation(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1.class,
515 ospfLinkAttributes(l.getLinkDescriptors().getMultiTopologyId(), la));
521 final LinkBuilder lb = new LinkBuilder();
522 lb.setLinkId(buildLinkId(base, l));
523 lb.addAugmentation(Link1.class, new Link1Builder().setIgpLinkAttributes(ilab.build()).build());
525 final NodeId srcNode = buildNodeId(base, l.getLocalNodeDescriptors());
526 LOG.debug("Link {} implies source node {}", l, srcNode);
528 final NodeId dstNode = buildNodeId(base, l.getRemoteNodeDescriptors());
529 LOG.debug("Link {} implies destination node {}", l, dstNode);
531 final TerminationPoint srcTp = buildLocalTp(base, l.getLinkDescriptors());
532 LOG.debug("Link {} implies source TP {}", l, srcTp);
534 final TerminationPoint dstTp = buildRemoteTp(base, l.getLinkDescriptors());
535 LOG.debug("Link {} implies destination TP {}", l, dstTp);
537 lb.setSource(new SourceBuilder().setSourceNode(srcNode).setSourceTp(srcTp.getTpId()).build());
538 lb.setDestination(new DestinationBuilder().setDestNode(dstNode).setDestTp(dstTp.getTpId()).build());
540 final NodeHolder snh = getNode(srcNode);
541 snh.addTp(srcTp, lb.getLinkId(), false);
542 LOG.debug("Created TP {} as link source", srcTp);
545 final NodeHolder dnh = getNode(dstNode);
546 dnh.addTp(dstTp, lb.getLinkId(), true);
547 LOG.debug("Created TP {} as link destination", dstTp);
550 final InstanceIdentifier<Link> lid = buildLinkIdentifier(lb.getLinkId());
551 final Link link = lb.build();
553 trans.put(LogicalDatastoreType.OPERATIONAL, lid, link);
554 LOG.debug("Created link {} at {} for {}", link, lid, l);
557 private void removeTp(final WriteTransaction trans, final NodeId node, final TpId tp,
558 final LinkId link, final boolean isRemote) {
559 final NodeHolder nh = this.nodes.get(node);
561 nh.removeTp(tp, link, isRemote);
564 LOG.warn("Removed non-existent node {}", node);
568 private void removeLink(final WriteTransaction trans, final UriBuilder base, final LinkCase l) {
569 final LinkId id = buildLinkId(base, l);
570 final InstanceIdentifier<?> lid = buildLinkIdentifier(id);
571 trans.delete(LogicalDatastoreType.OPERATIONAL, lid);
572 LOG.debug("Removed link {}", lid);
574 removeTp(trans, buildNodeId(base, l.getLocalNodeDescriptors()), buildLocalTpId(base, l.getLinkDescriptors()), id, false);
575 removeTp(trans, buildNodeId(base, l.getRemoteNodeDescriptors()), buildRemoteTpId(base, l.getLinkDescriptors()), id, true);
578 private List<Short> nodeMultiTopology(final List<TopologyIdentifier> list) {
579 final List<Short> ret = new ArrayList<>(list.size());
580 for (final TopologyIdentifier id : list) {
581 ret.add(id.getValue().shortValue());
586 private org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1 isisNodeAttributes(
587 final NodeIdentifier node, final NodeAttributes na) {
588 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();
589 final IsisNodeAttributesBuilder ab = new IsisNodeAttributesBuilder();
592 if (na.getIpv4RouterId() != null) {
593 tb.setTeRouterIdIpv4(na.getIpv4RouterId());
595 if (na.getIpv6RouterId() != null) {
596 tb.setTeRouterIdIpv6(na.getIpv6RouterId());
598 if (na.getTopologyIdentifier() != null) {
599 ab.setMultiTopologyId(nodeMultiTopology(na.getTopologyIdentifier()));
603 final CRouterIdentifier ri = node.getCRouterIdentifier();
604 if (ri instanceof IsisPseudonodeCase) {
605 final IsisPseudonode pn = ((IsisPseudonodeCase) ri).getIsisPseudonode();
606 final IsoBuilder b = new IsoBuilder();
607 b.setIsoSystemId(new IsoSystemId(UriBuilder.isoId(pn.getIsIsRouterIdentifier().getIsoSystemId())));
608 b.setIsoPseudonodeId(new IsoPseudonodeId(Hex.encodeHexString(new byte[] {pn.getPsn().byteValue()})));
609 ab.setIso(b.build());
610 } else if (ri instanceof IsisNodeCase) {
611 final IsisNode in = ((IsisNodeCase) ri).getIsisNode();
612 ab.setIso(new IsoBuilder().setIsoSystemId(new IsoSystemId(UriBuilder.isoId(in.getIsoSystemId()))).build());
615 ab.setTed(tb.build());
617 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1Builder().setIsisNodeAttributes(
621 private org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1 ospfNodeAttributes(
622 final NodeDescriptors node, final NodeAttributes na) {
623 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();
624 final OspfNodeAttributesBuilder ab = new OspfNodeAttributesBuilder();
626 if (na.getIpv4RouterId() != null) {
627 tb.setTeRouterIdIpv4(na.getIpv4RouterId());
629 if (na.getIpv6RouterId() != null) {
630 tb.setTeRouterIdIpv6(na.getIpv6RouterId());
632 if (na.getTopologyIdentifier() != null) {
633 ab.setMultiTopologyId(nodeMultiTopology(na.getTopologyIdentifier()));
635 final CRouterIdentifier ri = node.getCRouterIdentifier();
636 if (ri instanceof OspfPseudonodeCase) {
637 final OspfPseudonode pn = ((OspfPseudonodeCase) ri).getOspfPseudonode();
639 ab.setRouterType(new PseudonodeBuilder().setPseudonode(Boolean.TRUE).build());
640 ab.setDrInterfaceId(pn.getLanInterface().getValue());
641 } else if (ri instanceof OspfNodeCase && na.getNodeFlags() != null) {
642 // TODO: what should we do with in.getOspfRouterId()?
644 final NodeFlagBits nf = na.getNodeFlags();
645 if (nf.isAbr() != null) {
646 ab.setRouterType(new AbrBuilder().setAbr(nf.isAbr()).build());
647 } else if (nf.isExternal() != null) {
648 ab.setRouterType(new InternalBuilder().setInternal(!nf.isExternal()).build());
652 ab.setTed(tb.build());
653 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1Builder().setOspfNodeAttributes(
657 private void augmentProtocolId(final LinkstateRoute value, final IgpNodeAttributesBuilder inab, final NodeAttributes na, final NodeDescriptors nd) {
658 switch (value.getProtocolId()) {
665 inab.addAugmentation(
666 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1.class,
667 isisNodeAttributes(nd, na));
670 inab.addAugmentation(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1.class,
671 ospfNodeAttributes(nd, na));
678 private void createNode(final WriteTransaction trans, final UriBuilder base,
679 final LinkstateRoute value, final NodeCase n, final Attributes attributes) {
680 final NodeAttributes na;
682 final Attributes1 attr = attributes.getAugmentation(Attributes1.class);
684 final AttributeType attrType = attr.getAttributeType();
685 if (attrType != null) {
686 na = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.routes.linkstate.routes.linkstate.route.attributes.attribute.type.NodeCase)
687 attrType).getNodeAttributes();
689 LOG.debug("Missing attribute type in node {} route {}, skipping it", n, value);
693 LOG.debug("Missing attributes in node {} route {}, skipping it", n, value);
696 final IgpNodeAttributesBuilder inab = new IgpNodeAttributesBuilder();
697 final List<IpAddress> ids = new ArrayList<>();
699 if (na.getIpv4RouterId() != null) {
700 ids.add(new IpAddress(na.getIpv4RouterId()));
702 if (na.getIpv6RouterId() != null) {
703 ids.add(new IpAddress(na.getIpv6RouterId()));
705 if (na.getDynamicHostname() != null) {
706 inab.setName(new DomainName(na.getDynamicHostname()));
709 if (!ids.isEmpty()) {
710 inab.setRouterId(ids);
712 augmentProtocolId(value, inab, na, n.getNodeDescriptors());
714 final NodeId nid = buildNodeId(base, n.getNodeDescriptors());
715 final NodeHolder nh = getNode(nid);
717 * Eventhough the the holder creates a dummy structure, we need to duplicate it here,
718 * as that is the API requirement. The reason for it is the possible presence of supporting
719 * node -- something which the holder does not track.
721 final NodeBuilder nb = new NodeBuilder();
723 nb.setKey(new NodeKey(nb.getNodeId()));
725 nh.advertized(nb, inab);
729 private void removeNode(final WriteTransaction trans, final UriBuilder base, final NodeCase n) {
730 final NodeId id = buildNodeId(base, n.getNodeDescriptors());
731 final NodeHolder nh = this.nodes.get(id);
736 LOG.warn("Node {} does not have a holder", id);
740 private void augmentProtocolId(final LinkstateRoute value, final PrefixAttributes pa, final PrefixBuilder pb) {
741 switch (value.getProtocolId()) {
749 if (pa != null && pa.getOspfForwardingAddress() != null) {
752 new Prefix1Builder().setOspfPrefixAttributes(
753 new OspfPrefixAttributesBuilder().setForwardingAddress(pa.getOspfForwardingAddress().getIpv4Address()).build()).build());
761 private void createPrefix(final WriteTransaction trans, final UriBuilder base,
762 final LinkstateRoute value, final PrefixCase p, final Attributes attributes) {
763 final IpPrefix ippfx = p.getIpReachabilityInformation();
765 LOG.warn("IP reachability not present in prefix {} route {}, skipping it", p, value);
768 final PrefixBuilder pb = new PrefixBuilder();
769 pb.setKey(new PrefixKey(ippfx));
772 final PrefixAttributes pa;
773 // Very defensive lookup
774 final Attributes1 attr = attributes.getAugmentation(Attributes1.class);
776 final AttributeType attrType = attr.getAttributeType();
777 if (attrType != null) {
778 pa = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.linkstate.routes.linkstate.routes.linkstate.route.attributes.attribute.type.PrefixCase)
779 attrType).getPrefixAttributes();
781 LOG.debug("Missing attribute type in IP {} prefix {} route {}, skipping it", ippfx, p, value);
785 LOG.debug("Missing attributes in IP {} prefix {} route {}, skipping it", ippfx, p, value);
789 pb.setMetric(pa.getPrefixMetric().getValue());
791 augmentProtocolId(value, pa, pb);
793 final Prefix pfx = pb.build();
796 * All set, but... the hosting node may not exist, we may need to fake it.
798 final NodeId node = buildNodeId(base, p.getAdvertisingNodeDescriptors());
799 final NodeHolder nh = getNode(node);
801 LOG.debug("Created prefix {} for {}", pfx, p);
805 private void removePrefix(final WriteTransaction trans, final UriBuilder base, final PrefixCase p) {
806 final NodeId node = buildNodeId(base, p.getAdvertisingNodeDescriptors());
807 final NodeHolder nh = this.nodes.get(node);
810 LOG.debug("Removed prefix {}", p);
813 LOG.warn("Removing prefix from non-existing node {}", node);
818 protected void createObject(final ReadWriteTransaction trans,
819 final InstanceIdentifier<LinkstateRoute> id, final LinkstateRoute value) {
820 final UriBuilder base = new UriBuilder(value);
822 final ObjectType t = value.getObjectType();
823 Preconditions.checkArgument(t != null, "Route %s value %s has null object type", id, value);
825 if (t instanceof LinkCase) {
826 createLink(trans, base, value, (LinkCase) t, value.getAttributes());
827 } else if (t instanceof NodeCase) {
828 createNode(trans, base, value, (NodeCase) t, value.getAttributes());
829 } else if (t instanceof PrefixCase) {
830 createPrefix(trans, base, value, (PrefixCase) t, value.getAttributes());
832 throw new IllegalStateException("Unhandled object class " + t.getImplementedInterface());
837 protected void removeObject(final ReadWriteTransaction trans,
838 final InstanceIdentifier<LinkstateRoute> id, final LinkstateRoute value) {
839 final UriBuilder base = new UriBuilder(value);
841 final ObjectType t = value.getObjectType();
842 if (t instanceof LinkCase) {
843 removeLink(trans, base, (LinkCase) t);
844 } else if (t instanceof NodeCase) {
845 removeNode(trans, base, (NodeCase) t);
846 } else if (t instanceof PrefixCase) {
847 removePrefix(trans, base, (PrefixCase) t);
849 throw new IllegalStateException("Unhandled object class " + t.getImplementedInterface());