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.rsvp.rev150820.SrlgId;
64 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IsoNetId;
65 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IsoPseudonodeId;
66 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IsoSystemId;
67 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.link.attributes.IsisLinkAttributesBuilder;
68 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.node.attributes.IsisNodeAttributesBuilder;
69 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.node.attributes.isis.node.attributes.IsoBuilder;
70 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.srlg.attributes.SrlgValues;
71 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.srlg.attributes.SrlgValuesBuilder;
72 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.SrlgBuilder;
73 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.UnreservedBandwidth;
74 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.UnreservedBandwidthBuilder;
75 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.UnreservedBandwidthKey;
76 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.LinkId;
77 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
78 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
79 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
80 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.DestinationBuilder;
81 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.SourceBuilder;
82 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
83 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkBuilder;
84 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkKey;
85 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
86 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
87 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
88 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.TopologyTypesBuilder;
89 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
90 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
91 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
92 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Link1;
93 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Link1Builder;
94 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1;
95 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1Builder;
96 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TerminationPoint1;
97 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TerminationPoint1Builder;
98 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TopologyTypes1;
99 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TopologyTypes1Builder;
100 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.link.attributes.IgpLinkAttributesBuilder;
101 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.IgpNodeAttributesBuilder;
102 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;
103 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;
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.PrefixKey;
105 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.termination.point.attributes.IgpTerminationPointAttributesBuilder;
106 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;
107 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;
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.termination.point.type.UnnumberedBuilder;
109 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.Prefix1;
110 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.Prefix1Builder;
111 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.link.attributes.OspfLinkAttributesBuilder;
112 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.OspfNodeAttributesBuilder;
113 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.ospf.node.attributes.router.type.AbrBuilder;
114 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.ospf.node.attributes.router.type.InternalBuilder;
115 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.ospf.node.attributes.router.type.PseudonodeBuilder;
116 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.prefix.attributes.OspfPrefixAttributesBuilder;
117 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
118 import org.slf4j.Logger;
119 import org.slf4j.LoggerFactory;
121 public final class LinkstateTopologyBuilder extends AbstractTopologyBuilder<LinkstateRoute> {
123 private static final String UNHANDLED_OBJECT_CLASS = "Unhandled object class {}";
125 private static final class TpHolder {
126 private final Set<LinkId> local = new HashSet<>();
127 private final Set<LinkId> remote = new HashSet<>();
129 private final TerminationPoint tp;
131 private TpHolder(final TerminationPoint tp) {
132 this.tp = Preconditions.checkNotNull(tp);
135 private synchronized void addLink(final LinkId id, final boolean isRemote) {
143 private synchronized boolean removeLink(final LinkId id, final boolean isRemote) {
144 final boolean removed;
146 removed = this.remote.remove(id);
148 removed = this.local.remove(id);
151 LOG.warn("Removed non-reference link {} from TP {} isRemote {}", this.tp.getTpId(), id, isRemote);
154 return this.local.isEmpty() && this.remote.isEmpty();
157 private TerminationPoint getTp() {
162 private final class NodeHolder {
163 private final Map<PrefixKey, Prefix> prefixes = new HashMap<>();
164 private final Map<TpId, TpHolder> tps = new HashMap<>();
165 private boolean advertized = false;
166 private IgpNodeAttributesBuilder inab;
167 private NodeBuilder nb;
169 private NodeHolder(final NodeId id) {
170 this.inab = new IgpNodeAttributesBuilder();
171 this.nb = new NodeBuilder().setKey(new NodeKey(id)).setNodeId(id);
175 * Synchronized in-core state of a node into the backing store using the transaction
177 * @param trans data modification transaction which to use
178 * @return True if the node has been purged, false otherwise.
180 private boolean syncState(final WriteTransaction trans) {
181 final InstanceIdentifier<Node> nid = getInstanceIdentifier().child(
182 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node.class,
186 * Transaction's putOperationalData() does a merge. Force it onto a replace
187 * by removing the data. If we decide to remove the node -- we just skip the put.
189 trans.delete(LogicalDatastoreType.OPERATIONAL, nid);
191 if (!this.advertized) {
192 if (this.tps.isEmpty() && this.prefixes.isEmpty()) {
193 LOG.debug("Removing unadvertized unused node {}", this.nb.getNodeId());
197 LOG.debug("Node {} is still implied by {} TPs and {} prefixes", this.nb.getNodeId(), this.tps.size(), this.prefixes.size());
200 // Re-generate termination points
201 this.nb.setTerminationPoint(Lists.newArrayList(Collections2.transform(this.tps.values(), new Function<TpHolder, TerminationPoint>() {
203 public TerminationPoint apply(final TpHolder input) {
204 return input.getTp();
208 // Re-generate prefixes
209 this.inab.setPrefix(Lists.newArrayList(this.prefixes.values()));
211 // Write the node out
212 final Node n = this.nb.addAugmentation(Node1.class, new Node1Builder().setIgpNodeAttributes(this.inab.build()).build()).build();
213 trans.put(LogicalDatastoreType.OPERATIONAL, nid, n);
214 LOG.debug("Created node {} at {}", n, nid);
218 private synchronized void removeTp(final TpId tp, final LinkId link, final boolean isRemote) {
219 final TpHolder h = this.tps.get(tp);
221 if (h.removeLink(link, isRemote)) {
223 LOG.debug("Removed TP {}", tp);
226 LOG.warn("Removed non-present TP {} by link {}", tp, link);
230 private void addTp(final TerminationPoint tp, final LinkId link, final boolean isRemote) {
231 TpHolder h = this.tps.get(tp.getTpId());
233 h = new TpHolder(tp);
234 this.tps.put(tp.getTpId(), h);
237 h.addLink(link, isRemote);
240 private void addPrefix(final Prefix pfx) {
241 this.prefixes.put(pfx.getKey(), pfx);
244 private void removePrefix(final PrefixCase p) {
245 this.prefixes.remove(new PrefixKey(p.getPrefixDescriptors().getIpReachabilityInformation()));
248 private void unadvertized() {
249 this.inab = new IgpNodeAttributesBuilder();
250 this.nb = new NodeBuilder().setKey(this.nb.getKey()).setNodeId(this.nb.getNodeId());
251 this.advertized = false;
252 LOG.debug("Node {} is unadvertized", this.nb.getNodeId());
255 private void advertized(final NodeBuilder nb, final IgpNodeAttributesBuilder inab) {
256 this.nb = Preconditions.checkNotNull(nb);
257 this.inab = Preconditions.checkNotNull(inab);
258 this.advertized = true;
259 LOG.debug("Node {} is advertized", nb.getNodeId());
262 private Object getNodeId() {
263 return this.nb.getNodeId();
267 private static final Logger LOG = LoggerFactory.getLogger(LinkstateTopologyBuilder.class);
268 private final Map<NodeId, NodeHolder> nodes = new HashMap<>();
270 public LinkstateTopologyBuilder(final DataBroker dataProvider, final RibReference locRibReference, final TopologyId topologyId) {
271 super(dataProvider, locRibReference, topologyId, new TopologyTypesBuilder().addAugmentation(TopologyTypes1.class,
272 new TopologyTypes1Builder().build()).build());
275 private static LinkId buildLinkId(final UriBuilder base, final LinkCase link) {
276 return new LinkId(new UriBuilder(base, "link").add(link).toString());
279 private static NodeId buildNodeId(final UriBuilder base,
280 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NodeIdentifier node) {
281 return new NodeId(new UriBuilder(base, "node").add("", node).toString());
284 private static TpId buildTpId(final UriBuilder base, final TopologyIdentifier topologyIdentifier,
285 final Ipv4InterfaceIdentifier ipv4InterfaceIdentifier, final Ipv6InterfaceIdentifier ipv6InterfaceIdentifier, final Long id) {
286 final UriBuilder b = new UriBuilder(base, "tp");
287 if (topologyIdentifier != null) {
288 b.add("mt", topologyIdentifier.getValue());
290 if (ipv4InterfaceIdentifier != null) {
291 b.add("ipv4", ipv4InterfaceIdentifier.getValue());
293 if (ipv6InterfaceIdentifier != null) {
294 b.add("ipv6", ipv6InterfaceIdentifier.getValue());
297 return new TpId(b.add("id", id).toString());
300 private static TpId buildLocalTpId(final UriBuilder base, final LinkDescriptors linkDescriptors) {
301 return buildTpId(base, linkDescriptors.getMultiTopologyId(), linkDescriptors.getIpv4InterfaceAddress(),
302 linkDescriptors.getIpv6InterfaceAddress(), linkDescriptors.getLinkLocalIdentifier());
305 private static TerminationPoint buildTp(final TpId id, final TerminationPointType type) {
306 final TerminationPointBuilder stpb = new TerminationPointBuilder();
307 stpb.setKey(new TerminationPointKey(id));
311 stpb.addAugmentation(TerminationPoint1.class, new TerminationPoint1Builder().setIgpTerminationPointAttributes(
312 new IgpTerminationPointAttributesBuilder().setTerminationPointType(null).build()).build());
318 private static TerminationPointType getTpType(final Ipv4InterfaceIdentifier ipv4InterfaceIdentifier,
319 final Ipv6InterfaceIdentifier ipv6InterfaceIdentifier, final Long id) {
320 // Order of preference: Unnumbered first, then IP
322 LOG.debug("Unnumbered termination point type: {}", id);
323 return new UnnumberedBuilder().setUnnumberedId(id).build();
327 if (ipv6InterfaceIdentifier != null) {
328 ip = new IpAddress(ipv6InterfaceIdentifier);
329 } else if (ipv4InterfaceIdentifier != null) {
330 ip = new IpAddress(ipv4InterfaceIdentifier);
336 LOG.debug("IP termination point type: {}", ip);
337 return new IpBuilder().setIpAddress(Lists.newArrayList(ip)).build();
343 private static TerminationPoint buildLocalTp(final UriBuilder base, final LinkDescriptors linkDescriptors) {
344 final TpId id = buildLocalTpId(base, linkDescriptors);
345 final TerminationPointType t = getTpType(linkDescriptors.getIpv4InterfaceAddress(), linkDescriptors.getIpv6InterfaceAddress(),
346 linkDescriptors.getLinkLocalIdentifier());
348 return buildTp(id, t);
351 private static TpId buildRemoteTpId(final UriBuilder base, final LinkDescriptors linkDescriptors) {
352 return buildTpId(base, linkDescriptors.getMultiTopologyId(), linkDescriptors.getIpv4NeighborAddress(),
353 linkDescriptors.getIpv6NeighborAddress(), linkDescriptors.getLinkRemoteIdentifier());
356 private static TerminationPoint buildRemoteTp(final UriBuilder base, final LinkDescriptors linkDescriptors) {
357 final TpId id = buildRemoteTpId(base, linkDescriptors);
358 final TerminationPointType t = getTpType(linkDescriptors.getIpv4NeighborAddress(), linkDescriptors.getIpv6NeighborAddress(),
359 linkDescriptors.getLinkRemoteIdentifier());
361 return buildTp(id, t);
364 private InstanceIdentifier<Link> buildLinkIdentifier(final LinkId id) {
365 return getInstanceIdentifier().child(
366 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link.class,
370 private static Float bandwidthToFloat(final Bandwidth bandwidth) {
371 return ByteBuffer.wrap(bandwidth.getValue()).getFloat();
374 private static BigDecimal bandwidthToBigDecimal(final Bandwidth bandwidth) {
375 return BigDecimal.valueOf(bandwidthToFloat(bandwidth));
378 private static List<UnreservedBandwidth> unreservedBandwidthList(
379 final List<? extends org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.UnreservedBandwidth> input) {
380 final List<UnreservedBandwidth> ret = new ArrayList<>(input.size());
382 for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.UnreservedBandwidth i : input) {
383 ret.add(new UnreservedBandwidthBuilder().setBandwidth(bandwidthToBigDecimal(i.getBandwidth())).setKey(
384 new UnreservedBandwidthKey(i.getPriority())).build());
390 private static org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpLinkAttributes1 isisLinkAttributes(
391 final TopologyIdentifier topologyIdentifier, final LinkAttributes la) {
392 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();
395 if (la.getAdminGroup() != null) {
396 tb.setColor(la.getAdminGroup().getValue());
398 if (la.getTeMetric() != null) {
399 tb.setTeDefaultMetric(la.getTeMetric().getValue());
401 if (la.getUnreservedBandwidth() != null) {
402 tb.setUnreservedBandwidth(unreservedBandwidthList(la.getUnreservedBandwidth()));
404 if (la.getMaxLinkBandwidth() != null) {
405 tb.setMaxLinkBandwidth(bandwidthToBigDecimal(la.getMaxLinkBandwidth()));
407 if (la.getMaxReservableBandwidth() != null) {
408 tb.setMaxResvLinkBandwidth(bandwidthToBigDecimal(la.getMaxReservableBandwidth()));
410 if (la.getSharedRiskLinkGroups() != null) {
411 final List<SrlgValues> srlgs = new ArrayList<>();
412 for (final SrlgId id : la.getSharedRiskLinkGroups()) {
413 srlgs.add(new SrlgValuesBuilder().setSrlgValue(id.getValue()).build());
415 tb.setSrlg(new SrlgBuilder().setSrlgValues(srlgs).build());
419 final IsisLinkAttributesBuilder ilab = new IsisLinkAttributesBuilder();
420 ilab.setTed(tb.build());
421 if (topologyIdentifier != null) {
422 ilab.setMultiTopologyId(topologyIdentifier.getValue().shortValue());
425 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpLinkAttributes1Builder().setIsisLinkAttributes(
426 ilab.build()).build();
429 private static org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1 ospfLinkAttributes(
430 final TopologyIdentifier topologyIdentifier, final LinkAttributes la) {
431 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();
434 if (la.getAdminGroup() != null) {
435 tb.setColor(la.getAdminGroup().getValue());
437 if (la.getTeMetric() != null) {
438 tb.setTeDefaultMetric(la.getTeMetric().getValue());
440 if (la.getUnreservedBandwidth() != null) {
441 tb.setUnreservedBandwidth(unreservedBandwidthList(la.getUnreservedBandwidth()));
443 if (la.getMaxLinkBandwidth() != null) {
444 tb.setMaxLinkBandwidth(bandwidthToBigDecimal(la.getMaxLinkBandwidth()));
446 if (la.getMaxReservableBandwidth() != null) {
447 tb.setMaxResvLinkBandwidth(bandwidthToBigDecimal(la.getMaxReservableBandwidth()));
449 if (la.getSharedRiskLinkGroups() != null) {
450 final List<SrlgValues> srlgs = new ArrayList<>();
451 for (final SrlgId id : la.getSharedRiskLinkGroups()) {
452 srlgs.add(new SrlgValuesBuilder().setSrlgValue(id.getValue()).build());
454 tb.setSrlg(new SrlgBuilder().setSrlgValues(srlgs).build());
458 final OspfLinkAttributesBuilder ilab = new OspfLinkAttributesBuilder();
459 ilab.setTed(tb.build());
460 if (topologyIdentifier != null) {
461 ilab.setMultiTopologyId(topologyIdentifier.getValue().shortValue());
464 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1Builder().setOspfLinkAttributes(
465 ilab.build()).build();
468 private NodeHolder getNode(final NodeId id) {
469 if (this.nodes.containsKey(id)) {
470 LOG.debug("Node {} is already present", id);
471 return this.nodes.get(id);
474 final NodeHolder ret = new NodeHolder(id);
475 this.nodes.put(id, ret);
479 private void putNode(final WriteTransaction trans, final NodeHolder holder) {
480 if (holder.syncState(trans)) {
481 this.nodes.remove(holder.getNodeId());
485 private static void augmentProtocolId(final LinkstateRoute value, final IgpLinkAttributesBuilder ilab,
486 final LinkAttributes la, final LinkDescriptors ld) {
487 switch (value.getProtocolId()) {
494 ilab.addAugmentation(
495 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpLinkAttributes1.class,
496 isisLinkAttributes(ld.getMultiTopologyId(), la));
499 ilab.addAugmentation(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1.class,
500 ospfLinkAttributes(ld.getMultiTopologyId(), la));
507 private void createLink(final WriteTransaction trans, final UriBuilder base,
508 final LinkstateRoute value, final LinkCase l, final Attributes attributes) {
510 final LinkAttributes la;
511 final Attributes1 attr = attributes.getAugmentation(Attributes1.class);
513 final LinkStateAttribute attrType = attr.getLinkStateAttribute();
514 if (attrType != null) {
515 la = ((LinkAttributesCase)attrType).getLinkAttributes();
517 LOG.debug("Missing attribute type in link {} route {}, skipping it", l, value);
521 LOG.debug("Missing attributes in link {} route {}, skipping it", l, value);
525 final IgpLinkAttributesBuilder ilab = new IgpLinkAttributesBuilder();
527 if (la.getMetric() != null) {
528 ilab.setMetric(la.getMetric().getValue());
530 ilab.setName(la.getLinkName());
532 augmentProtocolId(value, ilab, la, l.getLinkDescriptors());
534 final LinkBuilder lb = new LinkBuilder();
535 lb.setLinkId(buildLinkId(base, l));
536 lb.addAugmentation(Link1.class, new Link1Builder().setIgpLinkAttributes(ilab.build()).build());
538 final NodeId srcNode = buildNodeId(base, l.getLocalNodeDescriptors());
539 LOG.debug("Link {} implies source node {}", l, srcNode);
541 final NodeId dstNode = buildNodeId(base, l.getRemoteNodeDescriptors());
542 LOG.debug("Link {} implies destination node {}", l, dstNode);
544 final TerminationPoint srcTp = buildLocalTp(base, l.getLinkDescriptors());
545 LOG.debug("Link {} implies source TP {}", l, srcTp);
547 final TerminationPoint dstTp = buildRemoteTp(base, l.getLinkDescriptors());
548 LOG.debug("Link {} implies destination TP {}", l, dstTp);
550 lb.setSource(new SourceBuilder().setSourceNode(srcNode).setSourceTp(srcTp.getTpId()).build());
551 lb.setDestination(new DestinationBuilder().setDestNode(dstNode).setDestTp(dstTp.getTpId()).build());
553 final NodeHolder snh = getNode(srcNode);
554 snh.addTp(srcTp, lb.getLinkId(), false);
555 LOG.debug("Created TP {} as link source", srcTp);
558 final NodeHolder dnh = getNode(dstNode);
559 dnh.addTp(dstTp, lb.getLinkId(), true);
560 LOG.debug("Created TP {} as link destination", dstTp);
563 final InstanceIdentifier<Link> lid = buildLinkIdentifier(lb.getLinkId());
564 final Link link = lb.build();
566 trans.put(LogicalDatastoreType.OPERATIONAL, lid, link);
567 LOG.debug("Created link {} at {} for {}", link, lid, l);
570 private void removeTp(final WriteTransaction trans, final NodeId node, final TpId tp,
571 final LinkId link, final boolean isRemote) {
572 final NodeHolder nh = this.nodes.get(node);
574 nh.removeTp(tp, link, isRemote);
577 LOG.warn("Removed non-existent node {}", node);
581 private void removeLink(final WriteTransaction trans, final UriBuilder base, final LinkCase l) {
582 final LinkId id = buildLinkId(base, l);
583 final InstanceIdentifier<?> lid = buildLinkIdentifier(id);
584 trans.delete(LogicalDatastoreType.OPERATIONAL, lid);
585 LOG.debug("Removed link {}", lid);
587 removeTp(trans, buildNodeId(base, l.getLocalNodeDescriptors()), buildLocalTpId(base, l.getLinkDescriptors()), id, false);
588 removeTp(trans, buildNodeId(base, l.getRemoteNodeDescriptors()), buildRemoteTpId(base, l.getLinkDescriptors()), id, true);
591 private static List<Short> nodeMultiTopology(final List<TopologyIdentifier> list) {
592 final List<Short> ret = new ArrayList<>(list.size());
593 for (final TopologyIdentifier id : list) {
594 ret.add(id.getValue().shortValue());
599 private static org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1 isisNodeAttributes(
600 final NodeIdentifier node, final NodeAttributes na) {
601 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();
602 final IsisNodeAttributesBuilder ab = new IsisNodeAttributesBuilder();
605 if (na.getIpv4RouterId() != null) {
606 tb.setTeRouterIdIpv4(na.getIpv4RouterId());
608 if (na.getIpv6RouterId() != null) {
609 tb.setTeRouterIdIpv6(na.getIpv6RouterId());
611 if (na.getTopologyIdentifier() != null) {
612 ab.setMultiTopologyId(nodeMultiTopology(na.getTopologyIdentifier()));
616 final CRouterIdentifier ri = node.getCRouterIdentifier();
617 if (ri instanceof IsisPseudonodeCase) {
618 final IsisPseudonode pn = ((IsisPseudonodeCase) ri).getIsisPseudonode();
619 final IsoBuilder b = new IsoBuilder();
620 final String systemId = UriBuilder.isoId(pn.getIsIsRouterIdentifier().getIsoSystemId());
621 b.setIsoSystemId(new IsoSystemId(systemId));
622 b.setIsoPseudonodeId(new IsoPseudonodeId(BaseEncoding.base16().encode(new byte[] {pn.getPsn().byteValue()})));
623 ab.setIso(b.build());
625 ab.setNet(toIsoNetIds(na.getIsisAreaId(), systemId));
627 } else if (ri instanceof IsisNodeCase) {
628 final IsisNode in = ((IsisNodeCase) ri).getIsisNode();
629 final String systemId = UriBuilder.isoId(in.getIsoSystemId());
630 ab.setIso(new IsoBuilder().setIsoSystemId(new IsoSystemId(systemId)).build());
632 ab.setNet(toIsoNetIds(na.getIsisAreaId(), systemId));
636 ab.setTed(tb.build());
638 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1Builder().setIsisNodeAttributes(
642 private static List<IsoNetId> toIsoNetIds(final List<IsisAreaIdentifier> areaIds, final String systemId) {
643 return Lists.transform(areaIds, new Function<IsisAreaIdentifier, IsoNetId>() {
645 public IsoNetId apply(final IsisAreaIdentifier input) {
646 return new IsoNetId(UriBuilder.toIsoNetId(input, systemId));
651 private static org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1 ospfNodeAttributes(
652 final NodeIdentifier node, final NodeAttributes na) {
653 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();
654 final OspfNodeAttributesBuilder ab = new OspfNodeAttributesBuilder();
656 if (na.getIpv4RouterId() != null) {
657 tb.setTeRouterIdIpv4(na.getIpv4RouterId());
659 if (na.getIpv6RouterId() != null) {
660 tb.setTeRouterIdIpv6(na.getIpv6RouterId());
662 if (na.getTopologyIdentifier() != null) {
663 ab.setMultiTopologyId(nodeMultiTopology(na.getTopologyIdentifier()));
665 final CRouterIdentifier ri = node.getCRouterIdentifier();
666 if (ri instanceof OspfPseudonodeCase) {
667 final OspfPseudonode pn = ((OspfPseudonodeCase) ri).getOspfPseudonode();
669 ab.setRouterType(new PseudonodeBuilder().setPseudonode(Boolean.TRUE).build());
670 ab.setDrInterfaceId(pn.getLanInterface().getValue());
671 } else if (ri instanceof OspfNodeCase && na.getNodeFlags() != null) {
672 // TODO: what should we do with in.getOspfRouterId()?
674 final NodeFlagBits nf = na.getNodeFlags();
675 if (nf.isAbr() != null) {
676 ab.setRouterType(new AbrBuilder().setAbr(nf.isAbr()).build());
677 } else if (nf.isExternal() != null) {
678 ab.setRouterType(new InternalBuilder().setInternal(!nf.isExternal()).build());
682 ab.setTed(tb.build());
683 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1Builder().setOspfNodeAttributes(
687 private static void augmentProtocolId(final LinkstateRoute value, final IgpNodeAttributesBuilder inab,
688 final NodeAttributes na, final NodeIdentifier nd) {
689 switch (value.getProtocolId()) {
696 inab.addAugmentation(
697 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1.class,
698 isisNodeAttributes(nd, na));
701 inab.addAugmentation(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1.class,
702 ospfNodeAttributes(nd, na));
709 private void createNode(final WriteTransaction trans, final UriBuilder base,
710 final LinkstateRoute value, final NodeCase n, final Attributes attributes) {
711 final NodeAttributes na;
713 final Attributes1 attr = attributes.getAugmentation(Attributes1.class);
715 final LinkStateAttribute attrType = attr.getLinkStateAttribute();
716 if (attrType != null) {
717 na = ((NodeAttributesCase)attrType).getNodeAttributes();
719 LOG.debug("Missing attribute type in node {} route {}, skipping it", n, value);
723 LOG.debug("Missing attributes in node {} route {}, skipping it", n, value);
726 final IgpNodeAttributesBuilder inab = new IgpNodeAttributesBuilder();
727 final List<IpAddress> ids = new ArrayList<>();
729 if (na.getIpv4RouterId() != null) {
730 ids.add(new IpAddress(na.getIpv4RouterId()));
732 if (na.getIpv6RouterId() != null) {
733 ids.add(new IpAddress(na.getIpv6RouterId()));
735 if (na.getDynamicHostname() != null) {
736 inab.setName(new DomainName(na.getDynamicHostname()));
739 if (!ids.isEmpty()) {
740 inab.setRouterId(ids);
742 augmentProtocolId(value, inab, na, n.getNodeDescriptors());
744 final NodeId nid = buildNodeId(base, n.getNodeDescriptors());
745 final NodeHolder nh = getNode(nid);
747 * Eventhough the the holder creates a dummy structure, we need to duplicate it here,
748 * as that is the API requirement. The reason for it is the possible presence of supporting
749 * node -- something which the holder does not track.
751 final NodeBuilder nb = new NodeBuilder();
753 nb.setKey(new NodeKey(nb.getNodeId()));
755 nh.advertized(nb, inab);
759 private void removeNode(final WriteTransaction trans, final UriBuilder base, final NodeCase n) {
760 final NodeId id = buildNodeId(base, n.getNodeDescriptors());
761 final NodeHolder nh = this.nodes.get(id);
766 LOG.warn("Node {} does not have a holder", id);
770 private static void augmentProtocolId(final LinkstateRoute value, final PrefixAttributes pa, final PrefixBuilder pb) {
771 switch (value.getProtocolId()) {
779 if (pa != null && pa.getOspfForwardingAddress() != null) {
782 new Prefix1Builder().setOspfPrefixAttributes(
783 new OspfPrefixAttributesBuilder().setForwardingAddress(pa.getOspfForwardingAddress().getIpv4Address()).build()).build());
791 private void createPrefix(final WriteTransaction trans, final UriBuilder base,
792 final LinkstateRoute value, final PrefixCase p, final Attributes attributes) {
793 final IpPrefix ippfx = p.getPrefixDescriptors().getIpReachabilityInformation();
795 LOG.warn("IP reachability not present in prefix {} route {}, skipping it", p, value);
798 final PrefixBuilder pb = new PrefixBuilder();
799 pb.setKey(new PrefixKey(ippfx));
802 final PrefixAttributes pa;
803 // Very defensive lookup
804 final Attributes1 attr = attributes.getAugmentation(Attributes1.class);
806 final LinkStateAttribute attrType = attr.getLinkStateAttribute();
807 if (attrType != null) {
808 pa = ((PrefixAttributesCase)attrType).getPrefixAttributes();
810 LOG.debug("Missing attribute type in IP {} prefix {} route {}, skipping it", ippfx, p, value);
814 LOG.debug("Missing attributes in IP {} prefix {} route {}, skipping it", ippfx, p, value);
818 pb.setMetric(pa.getPrefixMetric().getValue());
820 augmentProtocolId(value, pa, pb);
822 final Prefix pfx = pb.build();
825 * All set, but... the hosting node may not exist, we may need to fake it.
827 final NodeId node = buildNodeId(base, p.getAdvertisingNodeDescriptors());
828 final NodeHolder nh = getNode(node);
830 LOG.debug("Created prefix {} for {}", pfx, p);
834 private void removePrefix(final WriteTransaction trans, final UriBuilder base, final PrefixCase p) {
835 final NodeId node = buildNodeId(base, p.getAdvertisingNodeDescriptors());
836 final NodeHolder nh = this.nodes.get(node);
839 LOG.debug("Removed prefix {}", p);
842 LOG.warn("Removing prefix from non-existing node {}", node);
847 protected void createObject(final ReadWriteTransaction trans,
848 final InstanceIdentifier<LinkstateRoute> id, final LinkstateRoute value) {
849 final UriBuilder base = new UriBuilder(value);
851 final ObjectType t = value.getObjectType();
852 Preconditions.checkArgument(t != null, "Route %s value %s has null object type", id, value);
854 if (t instanceof LinkCase) {
855 createLink(trans, base, value, (LinkCase) t, value.getAttributes());
856 } else if (t instanceof NodeCase) {
857 createNode(trans, base, value, (NodeCase) t, value.getAttributes());
858 } else if (t instanceof PrefixCase) {
859 createPrefix(trans, base, value, (PrefixCase) t, value.getAttributes());
861 LOG.debug(UNHANDLED_OBJECT_CLASS, t.getImplementedInterface());
866 protected void removeObject(final ReadWriteTransaction trans,
867 final InstanceIdentifier<LinkstateRoute> id, final LinkstateRoute value) {
868 final UriBuilder base = new UriBuilder(value);
870 final ObjectType t = value.getObjectType();
871 if (t instanceof LinkCase) {
872 removeLink(trans, base, (LinkCase) t);
873 } else if (t instanceof NodeCase) {
874 removeNode(trans, base, (NodeCase) t);
875 } else if (t instanceof PrefixCase) {
876 removePrefix(trans, base, (PrefixCase) t);
878 LOG.debug(UNHANDLED_OBJECT_CLASS, t.getImplementedInterface());
882 @SuppressWarnings({ "rawtypes", "unchecked" })
884 protected InstanceIdentifier<LinkstateRoute> getRouteWildcard(final InstanceIdentifier<Tables> tablesId) {
885 return tablesId.child((Class)LinkstateRoutes.class).child(LinkstateRoute.class);