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 java.math.BigDecimal;
11 import java.nio.ByteBuffer;
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.HashSet;
15 import java.util.List;
19 import org.opendaylight.controller.md.sal.common.api.data.DataModification;
20 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
21 import org.opendaylight.protocol.bgp.rib.RibReference;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.DomainName;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.Ipv4InterfaceIdentifier;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.Ipv6InterfaceIdentifier;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.NodeFlagBits;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.NodeIdentifier;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.TopologyIdentifier;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.LinkstateRoute;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.Attributes1;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.ObjectType;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.attributes.attribute.type.link._case.LinkAttributes;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.attributes.attribute.type.node._case.NodeAttributes;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.attributes.attribute.type.prefix._case.PrefixAttributes;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.object.type.LinkCase;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.object.type.NodeCase;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.object.type.PrefixCase;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.object.type.link._case.LinkDescriptors;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.object.type.node._case.NodeDescriptors;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.CRouterIdentifier;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.IsisNodeCase;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.IsisPseudonodeCase;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.OspfNodeCase;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.OspfPseudonodeCase;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.isis.node._case.IsisNode;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.isis.pseudonode._case.IsisPseudonode;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.ospf.node._case.OspfNode;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.node.identifier.c.router.identifier.ospf.pseudonode._case.OspfPseudonode;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.route.Attributes;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IsoPseudonodeId;
53 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IsoSystemId;
54 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.link.attributes.IsisLinkAttributesBuilder;
55 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.node.attributes.IsisNodeAttributesBuilder;
56 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.node.attributes.isis.node.attributes.IsoBuilder;
57 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.UnreservedBandwidth;
58 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.UnreservedBandwidthBuilder;
59 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.UnreservedBandwidthKey;
60 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.LinkId;
61 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
62 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
63 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
64 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.DestinationBuilder;
65 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.SourceBuilder;
66 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
67 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkBuilder;
68 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkKey;
69 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
70 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
71 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
72 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.TopologyTypesBuilder;
73 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
74 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
75 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
76 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Link1;
77 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Link1Builder;
78 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1;
79 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1Builder;
80 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TerminationPoint1;
81 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TerminationPoint1Builder;
82 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TopologyTypes1;
83 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TopologyTypes1Builder;
84 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.link.attributes.IgpLinkAttributesBuilder;
85 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.IgpNodeAttributesBuilder;
86 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;
87 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;
88 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;
89 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.termination.point.attributes.IgpTerminationPointAttributesBuilder;
90 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;
91 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;
92 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;
93 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.Prefix1;
94 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.Prefix1Builder;
95 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.link.attributes.OspfLinkAttributesBuilder;
96 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.OspfNodeAttributesBuilder;
97 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.ospf.node.attributes.router.type.AbrBuilder;
98 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.ospf.node.attributes.router.type.InternalBuilder;
99 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.ospf.node.attributes.router.type.PseudonodeBuilder;
100 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.prefix.attributes.OspfPrefixAttributesBuilder;
101 import org.opendaylight.yangtools.yang.binding.DataObject;
102 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
103 import org.slf4j.Logger;
104 import org.slf4j.LoggerFactory;
106 import com.google.common.base.Function;
107 import com.google.common.base.Preconditions;
108 import com.google.common.collect.Collections2;
109 import com.google.common.collect.Lists;
111 public final class LinkstateTopologyBuilder extends AbstractTopologyBuilder<LinkstateRoute> {
112 private static final class TpHolder {
113 private final Set<LinkId> local = new HashSet<>();
114 private final Set<LinkId> remote = new HashSet<>();
116 private final TerminationPoint tp;
118 private TpHolder(final TerminationPoint tp) {
119 this.tp = Preconditions.checkNotNull(tp);
122 private synchronized void addLink(final LinkId id, final boolean isRemote) {
130 private synchronized boolean removeLink(final LinkId id, final boolean isRemote) {
131 final boolean removed;
133 removed = remote.remove(id);
135 removed = local.remove(id);
138 LOG.warn("Removed non-reference link {} from TP {} isRemote {}", tp.getTpId(), id, isRemote);
141 return local.isEmpty() && remote.isEmpty();
144 private TerminationPoint getTp() {
149 private final class NodeHolder {
150 final Map<PrefixKey, Prefix> prefixes = new HashMap<>();
151 final Map<TpId, TpHolder> tps = new HashMap<>();
152 boolean advertized = false;
153 IgpNodeAttributesBuilder inab;
156 private NodeHolder(final NodeId id) {
157 inab = new IgpNodeAttributesBuilder();
158 nb = new NodeBuilder().setKey(new NodeKey(id)).setNodeId(id);
162 * Synchronized in-core state of a node into the backing store using the transaction
164 * @param trans data modification transaction which to use
165 * @return True if the node has been purged, false otherwise.
167 private boolean syncState(final DataModification<InstanceIdentifier<?>, DataObject> trans) {
168 final InstanceIdentifier<Node> nid = InstanceIdentifier.builder(getInstanceIdentifier()).child(
169 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node.class,
170 nb.getKey()).build();
172 if (tps.isEmpty() && prefixes.isEmpty()) {
173 LOG.debug("Removing unadvertized unused node {}", nb.getNodeId());
174 trans.removeOperationalData(nid);
178 LOG.debug("Node {} is still implied by {} TPs and {} prefixes", tps.size(), prefixes.size());
181 // Re-generate termination points
182 nb.setTerminationPoint(Lists.newArrayList(Collections2.transform(tps.values(), new Function<TpHolder, TerminationPoint>() {
184 public TerminationPoint apply(final TpHolder input) {
185 return input.getTp();
189 // Re-generate prefixes
190 inab.setPrefix(Lists.newArrayList(prefixes.values()));
192 // Write the node out
193 final Node n = nb.addAugmentation(Node1.class, new Node1Builder().setIgpNodeAttributes(inab.build()).build()).build();
194 trans.putOperationalData(nid, n);
195 LOG.debug("Created node {} at {}", n, nid);
199 private synchronized void removeTp(final TpId tp, final LinkId link, final boolean isRemote) {
200 final TpHolder h = tps.get(tp);
202 if (h.removeLink(link, isRemote)) {
204 LOG.debug("Removed TP {}", tp);
207 LOG.warn("Removed non-present TP {} by link {}", tp, link);
211 private void addTp(final TerminationPoint tp, final LinkId link, final boolean isRemote) {
212 TpHolder h = tps.get(tp.getTpId());
214 h = new TpHolder(tp);
215 tps.put(tp.getTpId(), h);
218 h.addLink(link, isRemote);
221 private void addPrefix(final Prefix pfx) {
222 prefixes.put(pfx.getKey(), pfx);
225 private void removePrefix(final PrefixCase p) {
226 prefixes.remove(new PrefixKey(p.getIpReachabilityInformation()));
229 private void unadvertized() {
230 inab = new IgpNodeAttributesBuilder();
231 nb = new NodeBuilder().setKey(nb.getKey()).setNodeId(nb.getNodeId());
233 LOG.debug("Node {} is unadvertized", nb.getNodeId());
236 private void advertized(final NodeBuilder nb, final IgpNodeAttributesBuilder inab) {
237 this.nb = Preconditions.checkNotNull(nb);
238 this.inab = Preconditions.checkNotNull(inab);
240 LOG.debug("Node {} is advertized", nb.getNodeId());
243 private Object getNodeId() {
244 return nb.getNodeId();
248 private static final Logger LOG = LoggerFactory.getLogger(LinkstateTopologyBuilder.class);
249 private final Map<NodeId, NodeHolder> nodes = new HashMap<>();
251 public LinkstateTopologyBuilder(final DataProviderService dataProvider, final RibReference locRibReference, final TopologyId topologyId) {
252 super(dataProvider, locRibReference, topologyId, new TopologyTypesBuilder().addAugmentation(TopologyTypes1.class,
253 new TopologyTypes1Builder().build()).build(), LinkstateRoute.class);
256 private LinkId buildLinkId(final UriBuilder base, final LinkCase link) {
257 return new LinkId(new UriBuilder(base, "link").add(link).toString());
260 private NodeId buildNodeId(final UriBuilder base, final NodeIdentifier node) {
261 return new NodeId(new UriBuilder(base, "node").add("", node).toString());
264 private TpId buildTpId(final UriBuilder base, final TopologyIdentifier topologyIdentifier,
265 final Ipv4InterfaceIdentifier ipv4InterfaceIdentifier, final Ipv6InterfaceIdentifier ipv6InterfaceIdentifier, final Long id) {
266 final UriBuilder b = new UriBuilder(base, "tp");
267 if (topologyIdentifier != null) {
268 b.add("mt", topologyIdentifier.getValue());
270 if (ipv4InterfaceIdentifier != null) {
271 b.add("ipv4", ipv4InterfaceIdentifier.getValue());
273 if (ipv6InterfaceIdentifier != null) {
274 b.add("ipv6", ipv6InterfaceIdentifier.getValue());
277 return new TpId(b.add("id", id).toString());
280 private TpId buildLocalTpId(final UriBuilder base, final LinkDescriptors linkDescriptors) {
281 return buildTpId(base, linkDescriptors.getMultiTopologyId(), linkDescriptors.getIpv4InterfaceAddress(),
282 linkDescriptors.getIpv6InterfaceAddress(), linkDescriptors.getLinkLocalIdentifier());
285 private TerminationPoint buildTp(final TpId id, final TerminationPointType type) {
286 final TerminationPointBuilder stpb = new TerminationPointBuilder();
287 stpb.setKey(new TerminationPointKey(id));
291 stpb.addAugmentation(
292 TerminationPoint1.class,
293 new TerminationPoint1Builder().setIgpTerminationPointAttributes(
294 new IgpTerminationPointAttributesBuilder().setTerminationPointType(null).build()).build());
300 private TerminationPointType getTpType(final Ipv4InterfaceIdentifier ipv4InterfaceIdentifier,
301 final Ipv6InterfaceIdentifier ipv6InterfaceIdentifier, final Long id) {
302 // Order of preference: Unnumbered first, then IP
304 LOG.debug("Unnumbered termination point type: {}", id);
305 return new UnnumberedBuilder().setUnnumberedId(id).build();
309 if (ipv6InterfaceIdentifier != null) {
310 ip = new IpAddress(ipv6InterfaceIdentifier);
311 } else if (ipv4InterfaceIdentifier != null) {
312 ip = new IpAddress(ipv4InterfaceIdentifier);
318 LOG.debug("IP termination point type: {}", ip);
319 return new IpBuilder().setIpAddress(Lists.newArrayList(ip)).build();
325 private TerminationPoint buildLocalTp(final UriBuilder base, final LinkDescriptors linkDescriptors) {
326 final TpId id = buildLocalTpId(base, linkDescriptors);
327 final TerminationPointType t = getTpType(linkDescriptors.getIpv4InterfaceAddress(), linkDescriptors.getIpv6InterfaceAddress(),
328 linkDescriptors.getLinkLocalIdentifier());
330 return buildTp(id, t);
333 private TpId buildRemoteTpId(final UriBuilder base, final LinkDescriptors linkDescriptors) {
334 return buildTpId(base, linkDescriptors.getMultiTopologyId(), linkDescriptors.getIpv4NeighborAddress(),
335 linkDescriptors.getIpv6NeighborAddress(), linkDescriptors.getLinkRemoteIdentifier());
338 private TerminationPoint buildRemoteTp(final UriBuilder base, final LinkDescriptors linkDescriptors) {
339 final TpId id = buildRemoteTpId(base, linkDescriptors);
340 final TerminationPointType t = getTpType(linkDescriptors.getIpv4NeighborAddress(), linkDescriptors.getIpv6NeighborAddress(),
341 linkDescriptors.getLinkRemoteIdentifier());
343 return buildTp(id, t);
346 private InstanceIdentifier<?> buildLinkIdentifier(final UriBuilder base, final LinkId id) {
347 return InstanceIdentifier.builder(getInstanceIdentifier()).child(
348 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link.class,
349 new LinkKey(id)).toInstance();
352 private static Float bandwidthToFloat(final Bandwidth bandwidth) {
353 return ByteBuffer.wrap(bandwidth.getValue()).getFloat();
356 private static BigDecimal bandwidthToBigDecimal(final Bandwidth bandwidth) {
357 return BigDecimal.valueOf(bandwidthToFloat(bandwidth));
360 private static List<UnreservedBandwidth> unreservedBandwidthList(
361 final List<? extends org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.UnreservedBandwidth> input) {
362 final List<UnreservedBandwidth> ret = new ArrayList<>(input.size());
364 for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.UnreservedBandwidth i : input) {
365 ret.add(new UnreservedBandwidthBuilder().setBandwidth(bandwidthToBigDecimal(i.getBandwidth())).setKey(
366 new UnreservedBandwidthKey(i.getPriority())).build());
372 private org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpLinkAttributes1 isisLinkAttributes(
373 final TopologyIdentifier topologyIdentifier, final LinkAttributes la) {
374 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();
377 if (la.getAdminGroup() != null) {
378 tb.setColor(la.getAdminGroup().getValue());
380 if (la.getTeMetric() != null) {
381 tb.setTeDefaultMetric(la.getTeMetric().getValue());
383 if (la.getUnreservedBandwidth() != null) {
384 tb.setUnreservedBandwidth(unreservedBandwidthList(la.getUnreservedBandwidth()));
386 if (la.getMaxLinkBandwidth() != null) {
387 tb.setMaxLinkBandwidth(bandwidthToBigDecimal(la.getMaxLinkBandwidth()));
389 if (la.getMaxReservableBandwidth() != null) {
390 tb.setMaxResvLinkBandwidth(bandwidthToBigDecimal(la.getMaxReservableBandwidth()));
394 final IsisLinkAttributesBuilder ilab = new IsisLinkAttributesBuilder();
395 ilab.setTed(tb.build());
396 if (topologyIdentifier != null) {
397 ilab.setMultiTopologyId(topologyIdentifier.getValue().shortValue());
400 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpLinkAttributes1Builder().setIsisLinkAttributes(
401 ilab.build()).build();
404 private org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1 ospfLinkAttributes(
405 final TopologyIdentifier topologyIdentifier, final LinkAttributes la) {
406 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();
409 if (la.getAdminGroup() != null) {
410 tb.setColor(la.getAdminGroup().getValue());
412 if (la.getTeMetric() != null) {
413 tb.setTeDefaultMetric(la.getTeMetric().getValue());
415 if (la.getUnreservedBandwidth() != null) {
416 tb.setUnreservedBandwidth(unreservedBandwidthList(la.getUnreservedBandwidth()));
418 if (la.getMaxLinkBandwidth() != null) {
419 tb.setMaxLinkBandwidth(bandwidthToBigDecimal(la.getMaxLinkBandwidth()));
421 if (la.getMaxReservableBandwidth() != null) {
422 tb.setMaxResvLinkBandwidth(bandwidthToBigDecimal(la.getMaxReservableBandwidth()));
426 final OspfLinkAttributesBuilder ilab = new OspfLinkAttributesBuilder();
427 ilab.setTed(tb.build());
428 if (topologyIdentifier != null) {
429 ilab.setMultiTopologyId(topologyIdentifier.getValue().shortValue());
432 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1Builder().setOspfLinkAttributes(
433 ilab.build()).build();
436 private NodeHolder getNode(final NodeId id) {
437 if (nodes.containsKey(id)) {
438 LOG.debug("Node {} is already present", id);
439 return nodes.get(id);
442 final NodeHolder ret = new NodeHolder(id);
447 private void putNode(final DataModification<InstanceIdentifier<?>, DataObject> trans, final NodeHolder holder) {
448 if (holder.syncState(trans)) {
449 nodes.remove(holder.getNodeId());
453 private void createLink(final DataModification<InstanceIdentifier<?>, DataObject> trans, final UriBuilder base,
454 final LinkstateRoute value, final LinkCase l, final Attributes attributes) {
455 final LinkAttributes la = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.attributes.attribute.type.LinkCase) attributes.getAugmentation(
456 Attributes1.class).getAttributeType()).getLinkAttributes();
458 final IgpLinkAttributesBuilder ilab = new IgpLinkAttributesBuilder();
460 ilab.setMetric(la.getMetric().getValue());
461 ilab.setName(la.getLinkName());
464 switch (value.getProtocolId()) {
471 ilab.addAugmentation(
472 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpLinkAttributes1.class,
473 isisLinkAttributes(l.getLinkDescriptors().getMultiTopologyId(), la));
476 ilab.addAugmentation(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1.class,
477 ospfLinkAttributes(l.getLinkDescriptors().getMultiTopologyId(), la));
481 final LinkBuilder lb = new LinkBuilder();
482 lb.setLinkId(buildLinkId(base, l));
483 lb.addAugmentation(Link1.class, new Link1Builder().setIgpLinkAttributes(ilab.build()).build());
485 final NodeId srcNode = buildNodeId(base, l.getLocalNodeDescriptors());
486 LOG.debug("Link {} implies source node {}", l, srcNode);
488 final NodeId dstNode = buildNodeId(base, l.getRemoteNodeDescriptors());
489 LOG.debug("Link {} implies destination node {}", l, dstNode);
491 final TerminationPoint srcTp = buildLocalTp(base, l.getLinkDescriptors());
492 LOG.debug("Link {} implies source TP {}", l, srcTp);
494 final TerminationPoint dstTp = buildRemoteTp(base, l.getLinkDescriptors());
495 LOG.debug("Link {} implies destination TP {}", l, dstTp);
497 lb.setSource(new SourceBuilder().setSourceNode(srcNode).setSourceTp(srcTp.getTpId()).build());
498 lb.setDestination(new DestinationBuilder().setDestNode(dstNode).setDestTp(dstTp.getTpId()).build());
500 final NodeHolder snh = getNode(srcNode);
501 snh.addTp(srcTp, lb.getLinkId(), false);
503 LOG.debug("Created TP {} as link source", srcTp);
505 final NodeHolder dnh = getNode(dstNode);
506 dnh.addTp(dstTp, lb.getLinkId(), true);
508 LOG.debug("Created TP {} as link destination", dstTp);
510 final InstanceIdentifier<?> lid = buildLinkIdentifier(base, lb.getLinkId());
511 final Link link = lb.build();
513 trans.putOperationalData(lid, link);
514 LOG.debug("Created link {} at {} for {}", link, lid, l);
517 private void removeTp(final DataModification<InstanceIdentifier<?>, DataObject> trans,
518 final NodeId node, final TpId tp, final LinkId link, final boolean isRemote) {
519 final NodeHolder nh = nodes.get(node);
521 nh.removeTp(tp, link, isRemote);
524 LOG.warn("Removed non-existent node {}", node);
528 private void removeLink(final DataModification<InstanceIdentifier<?>, DataObject> trans, final UriBuilder base, final LinkCase l) {
529 final LinkId id = buildLinkId(base, l);
530 final InstanceIdentifier<?> lid = buildLinkIdentifier(base, id);
531 trans.removeOperationalData(lid);
532 LOG.debug("Removed link {}", lid);
534 removeTp(trans, buildNodeId(base, l.getLocalNodeDescriptors()), buildLocalTpId(base, l.getLinkDescriptors()), id, false);
535 removeTp(trans, buildNodeId(base, l.getRemoteNodeDescriptors()), buildRemoteTpId(base, l.getLinkDescriptors()), id, true);
538 private List<Short> nodeMultiTopology(final List<TopologyIdentifier> list) {
539 final List<Short> ret = new ArrayList<>(list.size());
540 for (final TopologyIdentifier id : list) {
541 ret.add(id.getValue().shortValue());
546 private org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1 isisNodeAttributes(
547 final NodeIdentifier node, final NodeAttributes na) {
548 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();
549 final IsisNodeAttributesBuilder ab = new IsisNodeAttributesBuilder();
552 if (na.getIpv4RouterId() != null) {
553 tb.setTeRouterIdIpv4(na.getIpv4RouterId());
555 if (na.getIpv6RouterId() != null) {
556 tb.setTeRouterIdIpv6(na.getIpv6RouterId());
558 if (na.getTopologyIdentifier() != null) {
559 ab.setMultiTopologyId(nodeMultiTopology(na.getTopologyIdentifier()));
563 final CRouterIdentifier ri = node.getCRouterIdentifier();
564 if (ri instanceof IsisPseudonodeCase) {
565 final IsisPseudonode pn = ((IsisPseudonodeCase) ri).getIsisPseudonode();
566 ab.setIso(new IsoBuilder().setIsoPseudonodeId(new IsoPseudonodeId(pn.toString())).build());
567 } else if (ri instanceof IsisNodeCase) {
568 final IsisNode in = ((IsisNodeCase) ri).getIsisNode();
569 ab.setIso(new IsoBuilder().setIsoSystemId(new IsoSystemId(in.getIsoSystemId().toString())).build());
572 ab.setTed(tb.build());
574 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1Builder().setIsisNodeAttributes(
578 private org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1 ospfNodeAttributes(
579 final NodeDescriptors node, final NodeAttributes na) {
580 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();
581 final OspfNodeAttributesBuilder ab = new OspfNodeAttributesBuilder();
584 if (na.getIpv4RouterId() != null) {
585 tb.setTeRouterIdIpv4(na.getIpv4RouterId());
587 if (na.getIpv6RouterId() != null) {
588 tb.setTeRouterIdIpv6(na.getIpv6RouterId());
590 if (na.getTopologyIdentifier() != null) {
591 ab.setMultiTopologyId(nodeMultiTopology(na.getTopologyIdentifier()));
595 final CRouterIdentifier ri = node.getCRouterIdentifier();
596 if (ri instanceof OspfPseudonodeCase) {
597 final OspfPseudonode pn = ((OspfPseudonodeCase) ri).getOspfPseudonode();
599 ab.setRouterType(new PseudonodeBuilder().setPseudonode(Boolean.TRUE).build());
600 ab.setDrInterfaceId(pn.getLanInterface().getValue());
601 } else if (ri instanceof OspfNodeCase) {
602 final OspfNode in = ((OspfNodeCase) ri).getOspfNode();
604 // TODO: what should we do with in.getOspfRouterId()?
607 final NodeFlagBits nf = na.getNodeFlags();
610 ab.setRouterType(new AbrBuilder().setAbr(Boolean.TRUE).build());
611 } else if (!nf.isExternal()) {
612 ab.setRouterType(new InternalBuilder().setInternal(Boolean.TRUE).build());
618 ab.setTed(tb.build());
620 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1Builder().setOspfNodeAttributes(
624 private void createNode(final DataModification<InstanceIdentifier<?>, DataObject> trans, final UriBuilder base,
625 final LinkstateRoute value, final NodeCase n, final Attributes attributes) {
626 final NodeAttributes na = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.attributes.attribute.type.NodeCase) attributes.getAugmentation(
627 Attributes1.class).getAttributeType()).getNodeAttributes();
628 final IgpNodeAttributesBuilder inab = new IgpNodeAttributesBuilder();
630 final List<IpAddress> ids = new ArrayList<>();
632 if (na.getIpv4RouterId() != null) {
633 ids.add(new IpAddress(na.getIpv4RouterId()));
635 if (na.getIpv6RouterId() != null) {
636 ids.add(new IpAddress(na.getIpv6RouterId()));
638 if (na.getDynamicHostname() != null) {
639 inab.setName(new DomainName(na.getDynamicHostname()));
643 if (!ids.isEmpty()) {
644 inab.setRouterId(ids);
647 switch (value.getProtocolId()) {
654 inab.addAugmentation(
655 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1.class,
656 isisNodeAttributes(n.getNodeDescriptors(), na));
659 inab.addAugmentation(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1.class,
660 ospfNodeAttributes(n.getNodeDescriptors(), na));
664 final NodeId nid = buildNodeId(base, n.getNodeDescriptors());
665 final NodeHolder nh = getNode(nid);
668 * Eventhough the the holder creates a dummy structure, we need to duplicate it here,
669 * as that is the API requirement. The reason for it is the possible presence of supporting
670 * node -- something which the holder does not track.
672 final NodeBuilder nb = new NodeBuilder();
674 nb.setKey(new NodeKey(nb.getNodeId()));
676 nh.advertized(nb, inab);
680 private void removeNode(final DataModification<InstanceIdentifier<?>, DataObject> trans, final UriBuilder base, final NodeCase n) {
681 final NodeId id = buildNodeId(base, n.getNodeDescriptors());
682 final NodeHolder nh = nodes.get(id);
687 LOG.warn("Node {} does not have a holder", id);
691 private void createPrefix(final DataModification<InstanceIdentifier<?>, DataObject> trans, final UriBuilder base,
692 final LinkstateRoute value, final PrefixCase p, final Attributes attributes) {
693 final IpPrefix ippfx = p.getIpReachabilityInformation();
695 LOG.warn("IP reachability not present in prefix {} route {}, skipping it", p, value);
699 final PrefixBuilder pb = new PrefixBuilder();
700 pb.setKey(new PrefixKey(ippfx));
703 final PrefixAttributes pa = ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev131125.bgp.rib.rib.loc.rib.tables.routes.linkstate.routes._case.linkstate.routes.linkstate.route.attributes.attribute.type.PrefixCase) attributes.getAugmentation(
704 Attributes1.class).getAttributeType()).getPrefixAttributes();
706 pb.setMetric(pa.getPrefixMetric().getValue());
709 switch (value.getProtocolId()) {
720 new Prefix1Builder().setOspfPrefixAttributes(
721 new OspfPrefixAttributesBuilder().setForwardingAddress(pa.getOspfForwardingAddress()).build()).build());
726 final Prefix pfx = pb.build();
729 * All set, but... the hosting node may not exist, we may need to fake it.
731 final NodeId node = buildNodeId(base, p.getAdvertisingNodeDescriptors());
732 final NodeHolder nh = getNode(node);
734 LOG.debug("Created prefix {} for {}", pfx, p);
738 private void removePrefix(final DataModification<InstanceIdentifier<?>, DataObject> trans, final UriBuilder base, final PrefixCase p) {
739 final NodeId node = buildNodeId(base, p.getAdvertisingNodeDescriptors());
740 final NodeHolder nh = nodes.get(node);
743 LOG.debug("Removed prefix {}", p);
746 LOG.warn("Removing prefix from non-existing node {}", node);
751 protected void createObject(final DataModification<InstanceIdentifier<?>, DataObject> trans,
752 final InstanceIdentifier<LinkstateRoute> id, final LinkstateRoute value) {
753 final UriBuilder base = new UriBuilder(value);
755 final ObjectType t = value.getObjectType();
756 if (t instanceof LinkCase) {
757 createLink(trans, base, value, (LinkCase) t, value.getAttributes());
758 } else if (t instanceof NodeCase) {
759 createNode(trans, base, value, (NodeCase) t, value.getAttributes());
760 } else if (t instanceof PrefixCase) {
761 createPrefix(trans, base, value, (PrefixCase) t, value.getAttributes());
763 throw new IllegalStateException("Unhandled object class " + t.getImplementedInterface());
768 protected void removeObject(final DataModification<InstanceIdentifier<?>, DataObject> trans,
769 final InstanceIdentifier<LinkstateRoute> id, final LinkstateRoute value) {
770 final UriBuilder base = new UriBuilder(value);
772 final ObjectType t = value.getObjectType();
773 if (t instanceof LinkCase) {
774 removeLink(trans, base, (LinkCase) t);
775 } else if (t instanceof NodeCase) {
776 removeNode(trans, base, (NodeCase) t);
777 } else if (t instanceof PrefixCase) {
778 removePrefix(trans, base, (PrefixCase) t);
780 throw new IllegalStateException("Unhandled object class " + t.getImplementedInterface());