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 com.google.common.io.BaseEncoding;
15 import java.math.BigDecimal;
16 import java.nio.ByteBuffer;
17 import java.util.ArrayList;
18 import java.util.HashMap;
19 import java.util.HashSet;
20 import java.util.List;
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.IsisAreaIdentifier;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NodeFlagBits;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NodeIdentifier;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.TopologyIdentifier;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.ObjectType;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.LinkCase;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.NodeCase;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.PrefixCase;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.link._case.LinkDescriptors;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.LinkStateAttribute;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.LinkAttributesCase;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.NodeAttributesCase;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.PrefixAttributesCase;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.link.attributes._case.LinkAttributes;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.node.attributes._case.NodeAttributes;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.prefix.attributes._case.PrefixAttributes;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.LinkstateRoutes;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.linkstate.routes.LinkstateRoute;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.linkstate.routes.linkstate.route.Attributes1;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.CRouterIdentifier;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.IsisNodeCase;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.IsisPseudonodeCase;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.OspfNodeCase;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.OspfPseudonodeCase;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.isis.node._case.IsisNode;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.isis.pseudonode._case.IsisPseudonode;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.ospf.pseudonode._case.OspfPseudonode;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.Tables;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp.topology.types.rev160524.TopologyTypes1;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp.topology.types.rev160524.TopologyTypes1Builder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp.topology.types.rev160524.bgp.linkstate.topology.type.BgpLinkstateTopologyBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.SrlgId;
67 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IsoNetId;
68 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IsoPseudonodeId;
69 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IsoSystemId;
70 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.link.attributes.IsisLinkAttributesBuilder;
71 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.node.attributes.IsisNodeAttributesBuilder;
72 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.node.attributes.isis.node.attributes.IsoBuilder;
73 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.srlg.attributes.SrlgValues;
74 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.srlg.attributes.SrlgValuesBuilder;
75 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.SrlgBuilder;
76 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.UnreservedBandwidth;
77 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.UnreservedBandwidthBuilder;
78 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.UnreservedBandwidthKey;
79 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.LinkId;
80 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
81 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
82 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
83 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.DestinationBuilder;
84 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.SourceBuilder;
85 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
86 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkBuilder;
87 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkKey;
88 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
89 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
90 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
91 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.TopologyTypes;
92 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.TopologyTypesBuilder;
93 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
94 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
95 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
96 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Link1;
97 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Link1Builder;
98 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1;
99 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1Builder;
100 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TerminationPoint1;
101 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TerminationPoint1Builder;
102 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.link.attributes.IgpLinkAttributesBuilder;
103 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.IgpNodeAttributesBuilder;
104 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;
105 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;
106 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;
107 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.termination.point.attributes.IgpTerminationPointAttributesBuilder;
108 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;
109 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;
110 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;
111 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.Prefix1;
112 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.Prefix1Builder;
113 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.link.attributes.OspfLinkAttributesBuilder;
114 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.OspfNodeAttributesBuilder;
115 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.ospf.node.attributes.router.type.AbrBuilder;
116 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.ospf.node.attributes.router.type.InternalBuilder;
117 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.ospf.node.attributes.router.type.PseudonodeBuilder;
118 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.prefix.attributes.OspfPrefixAttributesBuilder;
119 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
120 import org.slf4j.Logger;
121 import org.slf4j.LoggerFactory;
123 public final class LinkstateTopologyBuilder extends AbstractTopologyBuilder<LinkstateRoute> {
124 private static final TopologyTypes LINKSTATE_TOPOLOGY_TYPE = new TopologyTypesBuilder()
125 .addAugmentation(TopologyTypes1.class,
126 new TopologyTypes1Builder()
127 .setBgpLinkstateTopology(new BgpLinkstateTopologyBuilder().build()).build())
130 private static final String UNHANDLED_OBJECT_CLASS = "Unhandled object class {}";
132 private static final class TpHolder {
133 private final Set<LinkId> local = new HashSet<>();
134 private final Set<LinkId> remote = new HashSet<>();
136 private final TerminationPoint tp;
138 private TpHolder(final TerminationPoint tp) {
139 this.tp = Preconditions.checkNotNull(tp);
142 private synchronized void addLink(final LinkId id, final boolean isRemote) {
150 private synchronized boolean removeLink(final LinkId id, final boolean isRemote) {
151 final boolean removed;
153 removed = this.remote.remove(id);
155 removed = this.local.remove(id);
158 LOG.warn("Removed non-reference link {} from TP {} isRemote {}", this.tp.getTpId(), id, isRemote);
161 return this.local.isEmpty() && this.remote.isEmpty();
164 private TerminationPoint getTp() {
169 private final class NodeHolder {
170 private final Map<PrefixKey, Prefix> prefixes = new HashMap<>();
171 private final Map<TpId, TpHolder> tps = new HashMap<>();
172 private boolean advertized = false;
173 private IgpNodeAttributesBuilder inab;
174 private NodeBuilder nb;
176 private NodeHolder(final NodeId id) {
177 this.inab = new IgpNodeAttributesBuilder();
178 this.nb = new NodeBuilder().setKey(new NodeKey(id)).setNodeId(id);
182 * Synchronized in-core state of a node into the backing store using the transaction
184 * @param trans data modification transaction which to use
185 * @return True if the node has been purged, false otherwise.
187 private boolean syncState(final WriteTransaction trans) {
188 final InstanceIdentifier<Node> nid = getInstanceIdentifier().child(
189 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node.class,
193 * Transaction's putOperationalData() does a merge. Force it onto a replace
194 * by removing the data. If we decide to remove the node -- we just skip the put.
196 trans.delete(LogicalDatastoreType.OPERATIONAL, nid);
198 if (!this.advertized) {
199 if (this.tps.isEmpty() && this.prefixes.isEmpty()) {
200 LOG.debug("Removing unadvertized unused node {}", this.nb.getNodeId());
204 LOG.debug("Node {} is still implied by {} TPs and {} prefixes", this.nb.getNodeId(), this.tps.size(), this.prefixes.size());
207 // Re-generate termination points
208 this.nb.setTerminationPoint(Lists.newArrayList(Collections2.transform(this.tps.values(), new Function<TpHolder, TerminationPoint>() {
210 public TerminationPoint apply(final TpHolder input) {
211 return input.getTp();
215 // Re-generate prefixes
216 this.inab.setPrefix(Lists.newArrayList(this.prefixes.values()));
218 // Write the node out
219 final Node n = this.nb.addAugmentation(Node1.class, new Node1Builder().setIgpNodeAttributes(this.inab.build()).build()).build();
220 trans.put(LogicalDatastoreType.OPERATIONAL, nid, n);
221 LOG.debug("Created node {} at {}", n, nid);
225 private synchronized void removeTp(final TpId tp, final LinkId link, final boolean isRemote) {
226 final TpHolder h = this.tps.get(tp);
228 if (h.removeLink(link, isRemote)) {
230 LOG.debug("Removed TP {}", tp);
233 LOG.warn("Removed non-present TP {} by link {}", tp, link);
237 private void addTp(final TerminationPoint tp, final LinkId link, final boolean isRemote) {
238 TpHolder h = this.tps.get(tp.getTpId());
240 h = new TpHolder(tp);
241 this.tps.put(tp.getTpId(), h);
244 h.addLink(link, isRemote);
247 private void addPrefix(final Prefix pfx) {
248 this.prefixes.put(pfx.getKey(), pfx);
251 private void removePrefix(final PrefixCase p) {
252 this.prefixes.remove(new PrefixKey(p.getPrefixDescriptors().getIpReachabilityInformation()));
255 private void unadvertized() {
256 this.inab = new IgpNodeAttributesBuilder();
257 this.nb = new NodeBuilder().setKey(this.nb.getKey()).setNodeId(this.nb.getNodeId());
258 this.advertized = false;
259 LOG.debug("Node {} is unadvertized", this.nb.getNodeId());
262 private void advertized(final NodeBuilder nb, final IgpNodeAttributesBuilder inab) {
263 this.nb = Preconditions.checkNotNull(nb);
264 this.inab = Preconditions.checkNotNull(inab);
265 this.advertized = true;
266 LOG.debug("Node {} is advertized", nb.getNodeId());
269 private Object getNodeId() {
270 return this.nb.getNodeId();
274 private static final Logger LOG = LoggerFactory.getLogger(LinkstateTopologyBuilder.class);
275 private final Map<NodeId, NodeHolder> nodes = new HashMap<>();
277 public LinkstateTopologyBuilder(final DataBroker dataProvider, final RibReference locRibReference, final TopologyId topologyId) {
278 super(dataProvider, locRibReference, topologyId, LINKSTATE_TOPOLOGY_TYPE);
281 private static LinkId buildLinkId(final UriBuilder base, final LinkCase link) {
282 return new LinkId(new UriBuilder(base, "link").add(link).toString());
285 private static NodeId buildNodeId(final UriBuilder base,
286 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NodeIdentifier node) {
287 return new NodeId(new UriBuilder(base, "node").add("", node).toString());
290 private static TpId buildTpId(final UriBuilder base, final TopologyIdentifier topologyIdentifier,
291 final Ipv4InterfaceIdentifier ipv4InterfaceIdentifier, final Ipv6InterfaceIdentifier ipv6InterfaceIdentifier, final Long id) {
292 final UriBuilder b = new UriBuilder(base, "tp");
293 if (topologyIdentifier != null) {
294 b.add("mt", topologyIdentifier.getValue());
296 if (ipv4InterfaceIdentifier != null) {
297 b.add("ipv4", ipv4InterfaceIdentifier.getValue());
299 if (ipv6InterfaceIdentifier != null) {
300 b.add("ipv6", ipv6InterfaceIdentifier.getValue());
303 return new TpId(b.add("id", id).toString());
306 private static TpId buildLocalTpId(final UriBuilder base, final LinkDescriptors linkDescriptors) {
307 return buildTpId(base, linkDescriptors.getMultiTopologyId(), linkDescriptors.getIpv4InterfaceAddress(),
308 linkDescriptors.getIpv6InterfaceAddress(), linkDescriptors.getLinkLocalIdentifier());
311 private static TerminationPoint buildTp(final TpId id, final TerminationPointType type) {
312 final TerminationPointBuilder stpb = new TerminationPointBuilder();
313 stpb.setKey(new TerminationPointKey(id));
317 stpb.addAugmentation(TerminationPoint1.class, new TerminationPoint1Builder().setIgpTerminationPointAttributes(
318 new IgpTerminationPointAttributesBuilder().setTerminationPointType(null).build()).build());
324 private static TerminationPointType getTpType(final Ipv4InterfaceIdentifier ipv4InterfaceIdentifier,
325 final Ipv6InterfaceIdentifier ipv6InterfaceIdentifier, final Long id) {
326 // Order of preference: Unnumbered first, then IP
328 LOG.debug("Unnumbered termination point type: {}", id);
329 return new UnnumberedBuilder().setUnnumberedId(id).build();
333 if (ipv6InterfaceIdentifier != null) {
334 ip = new IpAddress(ipv6InterfaceIdentifier);
335 } else if (ipv4InterfaceIdentifier != null) {
336 ip = new IpAddress(ipv4InterfaceIdentifier);
342 LOG.debug("IP termination point type: {}", ip);
343 return new IpBuilder().setIpAddress(Lists.newArrayList(ip)).build();
349 private static TerminationPoint buildLocalTp(final UriBuilder base, final LinkDescriptors linkDescriptors) {
350 final TpId id = buildLocalTpId(base, linkDescriptors);
351 final TerminationPointType t = getTpType(linkDescriptors.getIpv4InterfaceAddress(), linkDescriptors.getIpv6InterfaceAddress(),
352 linkDescriptors.getLinkLocalIdentifier());
354 return buildTp(id, t);
357 private static TpId buildRemoteTpId(final UriBuilder base, final LinkDescriptors linkDescriptors) {
358 return buildTpId(base, linkDescriptors.getMultiTopologyId(), linkDescriptors.getIpv4NeighborAddress(),
359 linkDescriptors.getIpv6NeighborAddress(), linkDescriptors.getLinkRemoteIdentifier());
362 private static TerminationPoint buildRemoteTp(final UriBuilder base, final LinkDescriptors linkDescriptors) {
363 final TpId id = buildRemoteTpId(base, linkDescriptors);
364 final TerminationPointType t = getTpType(linkDescriptors.getIpv4NeighborAddress(), linkDescriptors.getIpv6NeighborAddress(),
365 linkDescriptors.getLinkRemoteIdentifier());
367 return buildTp(id, t);
370 private InstanceIdentifier<Link> buildLinkIdentifier(final LinkId id) {
371 return getInstanceIdentifier().child(
372 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link.class,
376 private static Float bandwidthToFloat(final Bandwidth bandwidth) {
377 return ByteBuffer.wrap(bandwidth.getValue()).getFloat();
380 private static BigDecimal bandwidthToBigDecimal(final Bandwidth bandwidth) {
381 return BigDecimal.valueOf(bandwidthToFloat(bandwidth));
384 private static List<UnreservedBandwidth> unreservedBandwidthList(
385 final List<? extends org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.UnreservedBandwidth> input) {
386 final List<UnreservedBandwidth> ret = new ArrayList<>(input.size());
388 for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.UnreservedBandwidth i : input) {
389 ret.add(new UnreservedBandwidthBuilder().setBandwidth(bandwidthToBigDecimal(i.getBandwidth())).setKey(
390 new UnreservedBandwidthKey(i.getPriority())).build());
396 private static org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpLinkAttributes1 isisLinkAttributes(
397 final TopologyIdentifier topologyIdentifier, final LinkAttributes la) {
398 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();
401 if (la.getAdminGroup() != null) {
402 tb.setColor(la.getAdminGroup().getValue());
404 if (la.getTeMetric() != null) {
405 tb.setTeDefaultMetric(la.getTeMetric().getValue());
407 if (la.getUnreservedBandwidth() != null) {
408 tb.setUnreservedBandwidth(unreservedBandwidthList(la.getUnreservedBandwidth()));
410 if (la.getMaxLinkBandwidth() != null) {
411 tb.setMaxLinkBandwidth(bandwidthToBigDecimal(la.getMaxLinkBandwidth()));
413 if (la.getMaxReservableBandwidth() != null) {
414 tb.setMaxResvLinkBandwidth(bandwidthToBigDecimal(la.getMaxReservableBandwidth()));
416 if (la.getSharedRiskLinkGroups() != null) {
417 final List<SrlgValues> srlgs = new ArrayList<>();
418 for (final SrlgId id : la.getSharedRiskLinkGroups()) {
419 srlgs.add(new SrlgValuesBuilder().setSrlgValue(id.getValue()).build());
421 tb.setSrlg(new SrlgBuilder().setSrlgValues(srlgs).build());
425 final IsisLinkAttributesBuilder ilab = new IsisLinkAttributesBuilder();
426 ilab.setTed(tb.build());
427 if (topologyIdentifier != null) {
428 ilab.setMultiTopologyId(topologyIdentifier.getValue().shortValue());
431 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpLinkAttributes1Builder().setIsisLinkAttributes(
432 ilab.build()).build();
435 private static org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1 ospfLinkAttributes(
436 final TopologyIdentifier topologyIdentifier, final LinkAttributes la) {
437 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();
440 if (la.getAdminGroup() != null) {
441 tb.setColor(la.getAdminGroup().getValue());
443 if (la.getTeMetric() != null) {
444 tb.setTeDefaultMetric(la.getTeMetric().getValue());
446 if (la.getUnreservedBandwidth() != null) {
447 tb.setUnreservedBandwidth(unreservedBandwidthList(la.getUnreservedBandwidth()));
449 if (la.getMaxLinkBandwidth() != null) {
450 tb.setMaxLinkBandwidth(bandwidthToBigDecimal(la.getMaxLinkBandwidth()));
452 if (la.getMaxReservableBandwidth() != null) {
453 tb.setMaxResvLinkBandwidth(bandwidthToBigDecimal(la.getMaxReservableBandwidth()));
455 if (la.getSharedRiskLinkGroups() != null) {
456 final List<SrlgValues> srlgs = new ArrayList<>();
457 for (final SrlgId id : la.getSharedRiskLinkGroups()) {
458 srlgs.add(new SrlgValuesBuilder().setSrlgValue(id.getValue()).build());
460 tb.setSrlg(new SrlgBuilder().setSrlgValues(srlgs).build());
464 final OspfLinkAttributesBuilder ilab = new OspfLinkAttributesBuilder();
465 ilab.setTed(tb.build());
466 if (topologyIdentifier != null) {
467 ilab.setMultiTopologyId(topologyIdentifier.getValue().shortValue());
470 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1Builder().setOspfLinkAttributes(
471 ilab.build()).build();
474 private NodeHolder getNode(final NodeId id) {
475 if (this.nodes.containsKey(id)) {
476 LOG.debug("Node {} is already present", id);
477 return this.nodes.get(id);
480 final NodeHolder ret = new NodeHolder(id);
481 this.nodes.put(id, ret);
485 private void putNode(final WriteTransaction trans, final NodeHolder holder) {
486 if (holder.syncState(trans)) {
487 this.nodes.remove(holder.getNodeId());
491 private static void augmentProtocolId(final LinkstateRoute value, final IgpLinkAttributesBuilder ilab,
492 final LinkAttributes la, final LinkDescriptors ld) {
493 switch (value.getProtocolId()) {
498 ilab.addAugmentation(
499 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpLinkAttributes1.class,
500 isisLinkAttributes(ld.getMultiTopologyId(), la));
504 ilab.addAugmentation(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1.class,
505 ospfLinkAttributes(ld.getMultiTopologyId(), la));
512 private void createLink(final WriteTransaction trans, final UriBuilder base,
513 final LinkstateRoute value, final LinkCase l, final Attributes attributes) {
515 final LinkAttributes la;
516 final Attributes1 attr = attributes.getAugmentation(Attributes1.class);
518 final LinkStateAttribute attrType = attr.getLinkStateAttribute();
519 if (attrType != null) {
520 la = ((LinkAttributesCase)attrType).getLinkAttributes();
522 LOG.debug("Missing attribute type in link {} route {}, skipping it", l, value);
526 LOG.debug("Missing attributes in link {} route {}, skipping it", l, value);
530 final IgpLinkAttributesBuilder ilab = new IgpLinkAttributesBuilder();
532 if (la.getMetric() != null) {
533 ilab.setMetric(la.getMetric().getValue());
535 ilab.setName(la.getLinkName());
537 augmentProtocolId(value, ilab, la, l.getLinkDescriptors());
539 final LinkBuilder lb = new LinkBuilder();
540 lb.setLinkId(buildLinkId(base, l));
541 lb.addAugmentation(Link1.class, new Link1Builder().setIgpLinkAttributes(ilab.build()).build());
543 final NodeId srcNode = buildNodeId(base, l.getLocalNodeDescriptors());
544 LOG.debug("Link {} implies source node {}", l, srcNode);
546 final NodeId dstNode = buildNodeId(base, l.getRemoteNodeDescriptors());
547 LOG.debug("Link {} implies destination node {}", l, dstNode);
549 final TerminationPoint srcTp = buildLocalTp(base, l.getLinkDescriptors());
550 LOG.debug("Link {} implies source TP {}", l, srcTp);
552 final TerminationPoint dstTp = buildRemoteTp(base, l.getLinkDescriptors());
553 LOG.debug("Link {} implies destination TP {}", l, dstTp);
555 lb.setSource(new SourceBuilder().setSourceNode(srcNode).setSourceTp(srcTp.getTpId()).build());
556 lb.setDestination(new DestinationBuilder().setDestNode(dstNode).setDestTp(dstTp.getTpId()).build());
558 final NodeHolder snh = getNode(srcNode);
559 snh.addTp(srcTp, lb.getLinkId(), false);
560 LOG.debug("Created TP {} as link source", srcTp);
563 final NodeHolder dnh = getNode(dstNode);
564 dnh.addTp(dstTp, lb.getLinkId(), true);
565 LOG.debug("Created TP {} as link destination", dstTp);
568 final InstanceIdentifier<Link> lid = buildLinkIdentifier(lb.getLinkId());
569 final Link link = lb.build();
571 trans.put(LogicalDatastoreType.OPERATIONAL, lid, link);
572 LOG.debug("Created link {} at {} for {}", link, lid, l);
575 private void removeTp(final WriteTransaction trans, final NodeId node, final TpId tp,
576 final LinkId link, final boolean isRemote) {
577 final NodeHolder nh = this.nodes.get(node);
579 nh.removeTp(tp, link, isRemote);
582 LOG.warn("Removed non-existent node {}", node);
586 private void removeLink(final WriteTransaction trans, final UriBuilder base, final LinkCase l) {
587 final LinkId id = buildLinkId(base, l);
588 final InstanceIdentifier<?> lid = buildLinkIdentifier(id);
589 trans.delete(LogicalDatastoreType.OPERATIONAL, lid);
590 LOG.debug("Removed link {}", lid);
592 removeTp(trans, buildNodeId(base, l.getLocalNodeDescriptors()), buildLocalTpId(base, l.getLinkDescriptors()), id, false);
593 removeTp(trans, buildNodeId(base, l.getRemoteNodeDescriptors()), buildRemoteTpId(base, l.getLinkDescriptors()), id, true);
596 private static List<Short> nodeMultiTopology(final List<TopologyIdentifier> list) {
597 final List<Short> ret = new ArrayList<>(list.size());
598 for (final TopologyIdentifier id : list) {
599 ret.add(id.getValue().shortValue());
604 private static org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1 isisNodeAttributes(
605 final NodeIdentifier node, final NodeAttributes na) {
606 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();
607 final IsisNodeAttributesBuilder ab = new IsisNodeAttributesBuilder();
610 if (na.getIpv4RouterId() != null) {
611 tb.setTeRouterIdIpv4(na.getIpv4RouterId());
613 if (na.getIpv6RouterId() != null) {
614 tb.setTeRouterIdIpv6(na.getIpv6RouterId());
616 if (na.getTopologyIdentifier() != null) {
617 ab.setMultiTopologyId(nodeMultiTopology(na.getTopologyIdentifier()));
621 final CRouterIdentifier ri = node.getCRouterIdentifier();
622 if (ri instanceof IsisPseudonodeCase) {
623 final IsisPseudonode pn = ((IsisPseudonodeCase) ri).getIsisPseudonode();
624 final IsoBuilder b = new IsoBuilder();
625 final String systemId = UriBuilder.isoId(pn.getIsIsRouterIdentifier().getIsoSystemId());
626 b.setIsoSystemId(new IsoSystemId(systemId));
627 b.setIsoPseudonodeId(new IsoPseudonodeId(BaseEncoding.base16().encode(new byte[] {pn.getPsn().byteValue()})));
628 ab.setIso(b.build());
630 ab.setNet(toIsoNetIds(na.getIsisAreaId(), systemId));
632 } else if (ri instanceof IsisNodeCase) {
633 final IsisNode in = ((IsisNodeCase) ri).getIsisNode();
634 final String systemId = UriBuilder.isoId(in.getIsoSystemId());
635 ab.setIso(new IsoBuilder().setIsoSystemId(new IsoSystemId(systemId)).build());
637 ab.setNet(toIsoNetIds(na.getIsisAreaId(), systemId));
641 ab.setTed(tb.build());
643 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1Builder().setIsisNodeAttributes(
647 private static List<IsoNetId> toIsoNetIds(final List<IsisAreaIdentifier> areaIds, final String systemId) {
648 return Lists.transform(areaIds, new Function<IsisAreaIdentifier, IsoNetId>() {
650 public IsoNetId apply(final IsisAreaIdentifier input) {
651 return new IsoNetId(UriBuilder.toIsoNetId(input, systemId));
656 private static org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1 ospfNodeAttributes(
657 final NodeIdentifier node, final NodeAttributes na) {
658 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();
659 final OspfNodeAttributesBuilder ab = new OspfNodeAttributesBuilder();
661 if (na.getIpv4RouterId() != null) {
662 tb.setTeRouterIdIpv4(na.getIpv4RouterId());
664 if (na.getIpv6RouterId() != null) {
665 tb.setTeRouterIdIpv6(na.getIpv6RouterId());
667 if (na.getTopologyIdentifier() != null) {
668 ab.setMultiTopologyId(nodeMultiTopology(na.getTopologyIdentifier()));
670 final CRouterIdentifier ri = node.getCRouterIdentifier();
671 if (ri instanceof OspfPseudonodeCase) {
672 final OspfPseudonode pn = ((OspfPseudonodeCase) ri).getOspfPseudonode();
674 ab.setRouterType(new PseudonodeBuilder().setPseudonode(Boolean.TRUE).build());
675 ab.setDrInterfaceId(pn.getLanInterface().getValue());
676 } else if (ri instanceof OspfNodeCase && na.getNodeFlags() != null) {
677 // TODO: what should we do with in.getOspfRouterId()?
679 final NodeFlagBits nf = na.getNodeFlags();
680 if (nf.isAbr() != null) {
681 ab.setRouterType(new AbrBuilder().setAbr(nf.isAbr()).build());
682 } else if (nf.isExternal() != null) {
683 ab.setRouterType(new InternalBuilder().setInternal(!nf.isExternal()).build());
687 ab.setTed(tb.build());
688 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1Builder().setOspfNodeAttributes(
692 private static void augmentProtocolId(final LinkstateRoute value, final IgpNodeAttributesBuilder inab,
693 final NodeAttributes na, final NodeIdentifier nd) {
694 switch (value.getProtocolId()) {
699 inab.addAugmentation(
700 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1.class,
701 isisNodeAttributes(nd, na));
704 inab.addAugmentation(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1.class,
705 ospfNodeAttributes(nd, na));
712 private void createNode(final WriteTransaction trans, final UriBuilder base,
713 final LinkstateRoute value, final NodeCase n, final Attributes attributes) {
714 final NodeAttributes na;
716 final Attributes1 attr = attributes.getAugmentation(Attributes1.class);
718 final LinkStateAttribute attrType = attr.getLinkStateAttribute();
719 if (attrType != null) {
720 na = ((NodeAttributesCase)attrType).getNodeAttributes();
722 LOG.debug("Missing attribute type in node {} route {}, skipping it", n, value);
726 LOG.debug("Missing attributes in node {} route {}, skipping it", n, value);
729 final IgpNodeAttributesBuilder inab = new IgpNodeAttributesBuilder();
730 final List<IpAddress> ids = new ArrayList<>();
732 if (na.getIpv4RouterId() != null) {
733 ids.add(new IpAddress(na.getIpv4RouterId()));
735 if (na.getIpv6RouterId() != null) {
736 ids.add(new IpAddress(na.getIpv6RouterId()));
738 if (na.getDynamicHostname() != null) {
739 inab.setName(new DomainName(na.getDynamicHostname()));
742 if (!ids.isEmpty()) {
743 inab.setRouterId(ids);
745 augmentProtocolId(value, inab, na, n.getNodeDescriptors());
747 final NodeId nid = buildNodeId(base, n.getNodeDescriptors());
748 final NodeHolder nh = getNode(nid);
750 * Eventhough the the holder creates a dummy structure, we need to duplicate it here,
751 * as that is the API requirement. The reason for it is the possible presence of supporting
752 * node -- something which the holder does not track.
754 final NodeBuilder nb = new NodeBuilder();
756 nb.setKey(new NodeKey(nb.getNodeId()));
758 nh.advertized(nb, inab);
762 private void removeNode(final WriteTransaction trans, final UriBuilder base, final NodeCase n) {
763 final NodeId id = buildNodeId(base, n.getNodeDescriptors());
764 final NodeHolder nh = this.nodes.get(id);
769 LOG.warn("Node {} does not have a holder", id);
773 private static void augmentProtocolId(final LinkstateRoute value, final PrefixAttributes pa, final PrefixBuilder pb) {
774 switch (value.getProtocolId()) {
780 if (pa != null && pa.getOspfForwardingAddress() != null) {
783 new Prefix1Builder().setOspfPrefixAttributes(
784 new OspfPrefixAttributesBuilder().setForwardingAddress(pa.getOspfForwardingAddress().getIpv4Address()).build()).build());
792 private void createPrefix(final WriteTransaction trans, final UriBuilder base,
793 final LinkstateRoute value, final PrefixCase p, final Attributes attributes) {
794 final IpPrefix ippfx = p.getPrefixDescriptors().getIpReachabilityInformation();
796 LOG.warn("IP reachability not present in prefix {} route {}, skipping it", p, value);
799 final PrefixBuilder pb = new PrefixBuilder();
800 pb.setKey(new PrefixKey(ippfx));
803 final PrefixAttributes pa;
804 // Very defensive lookup
805 final Attributes1 attr = attributes.getAugmentation(Attributes1.class);
807 final LinkStateAttribute attrType = attr.getLinkStateAttribute();
808 if (attrType != null) {
809 pa = ((PrefixAttributesCase)attrType).getPrefixAttributes();
811 LOG.debug("Missing attribute type in IP {} prefix {} route {}, skipping it", ippfx, p, value);
815 LOG.debug("Missing attributes in IP {} prefix {} route {}, skipping it", ippfx, p, value);
819 pb.setMetric(pa.getPrefixMetric().getValue());
821 augmentProtocolId(value, pa, pb);
823 final Prefix pfx = pb.build();
826 * All set, but... the hosting node may not exist, we may need to fake it.
828 final NodeId node = buildNodeId(base, p.getAdvertisingNodeDescriptors());
829 final NodeHolder nh = getNode(node);
831 LOG.debug("Created prefix {} for {}", pfx, p);
835 private void removePrefix(final WriteTransaction trans, final UriBuilder base, final PrefixCase p) {
836 final NodeId node = buildNodeId(base, p.getAdvertisingNodeDescriptors());
837 final NodeHolder nh = this.nodes.get(node);
840 LOG.debug("Removed prefix {}", p);
843 LOG.warn("Removing prefix from non-existing node {}", node);
848 protected void createObject(final ReadWriteTransaction trans,
849 final InstanceIdentifier<LinkstateRoute> id, final LinkstateRoute value) {
850 final UriBuilder base = new UriBuilder(value);
852 final ObjectType t = value.getObjectType();
853 Preconditions.checkArgument(t != null, "Route %s value %s has null object type", id, value);
855 if (t instanceof LinkCase) {
856 createLink(trans, base, value, (LinkCase) t, value.getAttributes());
857 } else if (t instanceof NodeCase) {
858 createNode(trans, base, value, (NodeCase) t, value.getAttributes());
859 } else if (t instanceof PrefixCase) {
860 createPrefix(trans, base, value, (PrefixCase) t, value.getAttributes());
862 LOG.debug(UNHANDLED_OBJECT_CLASS, t.getImplementedInterface());
867 protected void removeObject(final ReadWriteTransaction trans,
868 final InstanceIdentifier<LinkstateRoute> id, final LinkstateRoute value) {
869 final UriBuilder base = new UriBuilder(value);
871 final ObjectType t = value.getObjectType();
872 if (t instanceof LinkCase) {
873 removeLink(trans, base, (LinkCase) t);
874 } else if (t instanceof NodeCase) {
875 removeNode(trans, base, (NodeCase) t);
876 } else if (t instanceof PrefixCase) {
877 removePrefix(trans, base, (PrefixCase) t);
879 LOG.debug(UNHANDLED_OBJECT_CLASS, t.getImplementedInterface());
883 @SuppressWarnings({ "rawtypes", "unchecked" })
885 protected InstanceIdentifier<LinkstateRoute> getRouteWildcard(final InstanceIdentifier<Tables> tablesId) {
886 return tablesId.child((Class)LinkstateRoutes.class).child(LinkstateRoute.class);