2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.bgpcep.bgp.topology.provider;
10 import com.google.common.base.Function;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Collections2;
13 import com.google.common.collect.Lists;
14 import java.math.BigDecimal;
15 import java.nio.ByteBuffer;
16 import java.util.ArrayList;
17 import java.util.HashMap;
18 import java.util.HashSet;
19 import java.util.List;
22 import org.apache.commons.codec.binary.Hex;
23 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
24 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
25 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
26 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
27 import org.opendaylight.protocol.bgp.rib.RibReference;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.DomainName;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.Ipv4InterfaceIdentifier;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.Ipv6InterfaceIdentifier;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NodeFlagBits;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NodeIdentifier;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.TopologyIdentifier;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.ObjectType;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.LinkCase;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.NodeCase;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.PrefixCase;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.object.type.link._case.LinkDescriptors;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.LinkStateAttribute;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.LinkAttributesCase;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.NodeAttributesCase;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.path.attribute.link.state.attribute.PrefixAttributesCase;
45 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;
46 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;
47 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;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.linkstate.routes.LinkstateRoute;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.linkstate.routes.linkstate.route.Attributes1;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.CRouterIdentifier;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.IsisNodeCase;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.IsisPseudonodeCase;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.OspfNodeCase;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.OspfPseudonodeCase;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.isis.node._case.IsisNode;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.isis.pseudonode._case.IsisPseudonode;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.node.identifier.c.router.identifier.ospf.pseudonode._case.OspfPseudonode;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.route.Attributes;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.Bandwidth;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.SrlgId;
61 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IsoPseudonodeId;
62 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IsoSystemId;
63 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.link.attributes.IsisLinkAttributesBuilder;
64 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.node.attributes.IsisNodeAttributesBuilder;
65 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.isis.node.attributes.isis.node.attributes.IsoBuilder;
66 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.srlg.attributes.SrlgValues;
67 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.srlg.attributes.SrlgValuesBuilder;
68 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.SrlgBuilder;
69 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.UnreservedBandwidth;
70 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.UnreservedBandwidthBuilder;
71 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.ted.rev131021.ted.link.attributes.UnreservedBandwidthKey;
72 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.LinkId;
73 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
74 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
75 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
76 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.DestinationBuilder;
77 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.SourceBuilder;
78 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
79 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkBuilder;
80 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkKey;
81 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
82 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
83 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
84 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.TopologyTypesBuilder;
85 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
86 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
87 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
88 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Link1;
89 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Link1Builder;
90 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1;
91 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.Node1Builder;
92 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TerminationPoint1;
93 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TerminationPoint1Builder;
94 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TopologyTypes1;
95 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TopologyTypes1Builder;
96 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.link.attributes.IgpLinkAttributesBuilder;
97 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.IgpNodeAttributesBuilder;
98 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.node.attributes.igp.node.attributes.Prefix;
99 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;
100 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;
101 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.termination.point.attributes.IgpTerminationPointAttributesBuilder;
102 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.termination.point.attributes.igp.termination.point.attributes.TerminationPointType;
103 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;
104 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;
105 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.Prefix1;
106 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.Prefix1Builder;
107 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.link.attributes.OspfLinkAttributesBuilder;
108 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.OspfNodeAttributesBuilder;
109 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.ospf.node.attributes.router.type.AbrBuilder;
110 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.ospf.node.attributes.router.type.InternalBuilder;
111 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.node.attributes.ospf.node.attributes.router.type.PseudonodeBuilder;
112 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.ospf.prefix.attributes.OspfPrefixAttributesBuilder;
113 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
114 import org.slf4j.Logger;
115 import org.slf4j.LoggerFactory;
117 public final class LinkstateTopologyBuilder extends AbstractTopologyBuilder<LinkstateRoute> {
118 private static final class TpHolder {
119 private final Set<LinkId> local = new HashSet<>();
120 private final Set<LinkId> remote = new HashSet<>();
122 private final TerminationPoint tp;
124 private TpHolder(final TerminationPoint tp) {
125 this.tp = Preconditions.checkNotNull(tp);
128 private synchronized void addLink(final LinkId id, final boolean isRemote) {
136 private synchronized boolean removeLink(final LinkId id, final boolean isRemote) {
137 final boolean removed;
139 removed = this.remote.remove(id);
141 removed = this.local.remove(id);
144 LOG.warn("Removed non-reference link {} from TP {} isRemote {}", this.tp.getTpId(), id, isRemote);
147 return this.local.isEmpty() && this.remote.isEmpty();
150 private TerminationPoint getTp() {
155 private final class NodeHolder {
156 private final Map<PrefixKey, Prefix> prefixes = new HashMap<>();
157 private final Map<TpId, TpHolder> tps = new HashMap<>();
158 private boolean advertized = false;
159 private IgpNodeAttributesBuilder inab;
160 private NodeBuilder nb;
162 private NodeHolder(final NodeId id) {
163 this.inab = new IgpNodeAttributesBuilder();
164 this.nb = new NodeBuilder().setKey(new NodeKey(id)).setNodeId(id);
168 * Synchronized in-core state of a node into the backing store using the transaction
170 * @param trans data modification transaction which to use
171 * @return True if the node has been purged, false otherwise.
173 private boolean syncState(final WriteTransaction trans) {
174 final InstanceIdentifier<Node> nid = getInstanceIdentifier().child(
175 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node.class,
179 * Transaction's putOperationalData() does a merge. Force it onto a replace
180 * by removing the data. If we decide to remove the node -- we just skip the put.
182 trans.delete(LogicalDatastoreType.OPERATIONAL, nid);
184 if (!this.advertized) {
185 if (this.tps.isEmpty() && this.prefixes.isEmpty()) {
186 LOG.debug("Removing unadvertized unused node {}", this.nb.getNodeId());
190 LOG.debug("Node {} is still implied by {} TPs and {} prefixes", this.nb.getNodeId(), this.tps.size(), this.prefixes.size());
193 // Re-generate termination points
194 this.nb.setTerminationPoint(Lists.newArrayList(Collections2.transform(this.tps.values(), new Function<TpHolder, TerminationPoint>() {
196 public TerminationPoint apply(final TpHolder input) {
197 return input.getTp();
201 // Re-generate prefixes
202 this.inab.setPrefix(Lists.newArrayList(this.prefixes.values()));
204 // Write the node out
205 final Node n = this.nb.addAugmentation(Node1.class, new Node1Builder().setIgpNodeAttributes(this.inab.build()).build()).build();
206 trans.put(LogicalDatastoreType.OPERATIONAL, nid, n);
207 LOG.debug("Created node {} at {}", n, nid);
211 private synchronized void removeTp(final TpId tp, final LinkId link, final boolean isRemote) {
212 final TpHolder h = this.tps.get(tp);
214 if (h.removeLink(link, isRemote)) {
216 LOG.debug("Removed TP {}", tp);
219 LOG.warn("Removed non-present TP {} by link {}", tp, link);
223 private void addTp(final TerminationPoint tp, final LinkId link, final boolean isRemote) {
224 TpHolder h = this.tps.get(tp.getTpId());
226 h = new TpHolder(tp);
227 this.tps.put(tp.getTpId(), h);
230 h.addLink(link, isRemote);
233 private void addPrefix(final Prefix pfx) {
234 this.prefixes.put(pfx.getKey(), pfx);
237 private void removePrefix(final PrefixCase p) {
238 this.prefixes.remove(new PrefixKey(p.getPrefixDescriptors().getIpReachabilityInformation()));
241 private void unadvertized() {
242 this.inab = new IgpNodeAttributesBuilder();
243 this.nb = new NodeBuilder().setKey(this.nb.getKey()).setNodeId(this.nb.getNodeId());
244 this.advertized = false;
245 LOG.debug("Node {} is unadvertized", this.nb.getNodeId());
248 private void advertized(final NodeBuilder nb, final IgpNodeAttributesBuilder inab) {
249 this.nb = Preconditions.checkNotNull(nb);
250 this.inab = Preconditions.checkNotNull(inab);
251 this.advertized = true;
252 LOG.debug("Node {} is advertized", nb.getNodeId());
255 private Object getNodeId() {
256 return this.nb.getNodeId();
260 private static final Logger LOG = LoggerFactory.getLogger(LinkstateTopologyBuilder.class);
261 private final Map<NodeId, NodeHolder> nodes = new HashMap<>();
263 public LinkstateTopologyBuilder(final DataBroker dataProvider, final RibReference locRibReference, final TopologyId topologyId) {
264 super(dataProvider, locRibReference, topologyId, new TopologyTypesBuilder().addAugmentation(TopologyTypes1.class,
265 new TopologyTypes1Builder().build()).build(), LinkstateRoute.class);
268 private LinkId buildLinkId(final UriBuilder base, final LinkCase link) {
269 return new LinkId(new UriBuilder(base, "link").add(link).toString());
272 private NodeId buildNodeId(final UriBuilder base, final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.NodeIdentifier node) {
273 return new NodeId(new UriBuilder(base, "node").add("", node).toString());
276 private TpId buildTpId(final UriBuilder base, final TopologyIdentifier topologyIdentifier,
277 final Ipv4InterfaceIdentifier ipv4InterfaceIdentifier, final Ipv6InterfaceIdentifier ipv6InterfaceIdentifier, final Long id) {
278 final UriBuilder b = new UriBuilder(base, "tp");
279 if (topologyIdentifier != null) {
280 b.add("mt", topologyIdentifier.getValue());
282 if (ipv4InterfaceIdentifier != null) {
283 b.add("ipv4", ipv4InterfaceIdentifier.getValue());
285 if (ipv6InterfaceIdentifier != null) {
286 b.add("ipv6", ipv6InterfaceIdentifier.getValue());
289 return new TpId(b.add("id", id).toString());
292 private TpId buildLocalTpId(final UriBuilder base, final LinkDescriptors linkDescriptors) {
293 return buildTpId(base, linkDescriptors.getMultiTopologyId(), linkDescriptors.getIpv4InterfaceAddress(),
294 linkDescriptors.getIpv6InterfaceAddress(), linkDescriptors.getLinkLocalIdentifier());
297 private TerminationPoint buildTp(final TpId id, final TerminationPointType type) {
298 final TerminationPointBuilder stpb = new TerminationPointBuilder();
299 stpb.setKey(new TerminationPointKey(id));
303 stpb.addAugmentation(TerminationPoint1.class, new TerminationPoint1Builder().setIgpTerminationPointAttributes(
304 new IgpTerminationPointAttributesBuilder().setTerminationPointType(null).build()).build());
310 private TerminationPointType getTpType(final Ipv4InterfaceIdentifier ipv4InterfaceIdentifier,
311 final Ipv6InterfaceIdentifier ipv6InterfaceIdentifier, final Long id) {
312 // Order of preference: Unnumbered first, then IP
314 LOG.debug("Unnumbered termination point type: {}", id);
315 return new UnnumberedBuilder().setUnnumberedId(id).build();
319 if (ipv6InterfaceIdentifier != null) {
320 ip = new IpAddress(ipv6InterfaceIdentifier);
321 } else if (ipv4InterfaceIdentifier != null) {
322 ip = new IpAddress(ipv4InterfaceIdentifier);
328 LOG.debug("IP termination point type: {}", ip);
329 return new IpBuilder().setIpAddress(Lists.newArrayList(ip)).build();
335 private TerminationPoint buildLocalTp(final UriBuilder base, final LinkDescriptors linkDescriptors) {
336 final TpId id = buildLocalTpId(base, linkDescriptors);
337 final TerminationPointType t = getTpType(linkDescriptors.getIpv4InterfaceAddress(), linkDescriptors.getIpv6InterfaceAddress(),
338 linkDescriptors.getLinkLocalIdentifier());
340 return buildTp(id, t);
343 private TpId buildRemoteTpId(final UriBuilder base, final LinkDescriptors linkDescriptors) {
344 return buildTpId(base, linkDescriptors.getMultiTopologyId(), linkDescriptors.getIpv4NeighborAddress(),
345 linkDescriptors.getIpv6NeighborAddress(), linkDescriptors.getLinkRemoteIdentifier());
348 private TerminationPoint buildRemoteTp(final UriBuilder base, final LinkDescriptors linkDescriptors) {
349 final TpId id = buildRemoteTpId(base, linkDescriptors);
350 final TerminationPointType t = getTpType(linkDescriptors.getIpv4NeighborAddress(), linkDescriptors.getIpv6NeighborAddress(),
351 linkDescriptors.getLinkRemoteIdentifier());
353 return buildTp(id, t);
356 private InstanceIdentifier<Link> buildLinkIdentifier(final LinkId id) {
357 return getInstanceIdentifier().child(
358 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link.class,
362 private static Float bandwidthToFloat(final Bandwidth bandwidth) {
363 return ByteBuffer.wrap(bandwidth.getValue()).getFloat();
366 private static BigDecimal bandwidthToBigDecimal(final Bandwidth bandwidth) {
367 return BigDecimal.valueOf(bandwidthToFloat(bandwidth));
370 private static List<UnreservedBandwidth> unreservedBandwidthList(
371 final List<? extends org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.UnreservedBandwidth> input) {
372 final List<UnreservedBandwidth> ret = new ArrayList<>(input.size());
374 for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.UnreservedBandwidth i : input) {
375 ret.add(new UnreservedBandwidthBuilder().setBandwidth(bandwidthToBigDecimal(i.getBandwidth())).setKey(
376 new UnreservedBandwidthKey(i.getPriority())).build());
382 private org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpLinkAttributes1 isisLinkAttributes(
383 final TopologyIdentifier topologyIdentifier, final LinkAttributes la) {
384 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();
387 if (la.getAdminGroup() != null) {
388 tb.setColor(la.getAdminGroup().getValue());
390 if (la.getTeMetric() != null) {
391 tb.setTeDefaultMetric(la.getTeMetric().getValue());
393 if (la.getUnreservedBandwidth() != null) {
394 tb.setUnreservedBandwidth(unreservedBandwidthList(la.getUnreservedBandwidth()));
396 if (la.getMaxLinkBandwidth() != null) {
397 tb.setMaxLinkBandwidth(bandwidthToBigDecimal(la.getMaxLinkBandwidth()));
399 if (la.getMaxReservableBandwidth() != null) {
400 tb.setMaxResvLinkBandwidth(bandwidthToBigDecimal(la.getMaxReservableBandwidth()));
402 if (la.getSharedRiskLinkGroups() != null) {
403 final List<SrlgValues> srlgs = new ArrayList<>();
404 for (final SrlgId id : la.getSharedRiskLinkGroups()) {
405 srlgs.add(new SrlgValuesBuilder().setSrlgValue(id.getValue()).build());
407 tb.setSrlg(new SrlgBuilder().setSrlgValues(srlgs).build());
411 final IsisLinkAttributesBuilder ilab = new IsisLinkAttributesBuilder();
412 ilab.setTed(tb.build());
413 if (topologyIdentifier != null) {
414 ilab.setMultiTopologyId(topologyIdentifier.getValue().shortValue());
417 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpLinkAttributes1Builder().setIsisLinkAttributes(
418 ilab.build()).build();
421 private org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1 ospfLinkAttributes(
422 final TopologyIdentifier topologyIdentifier, final LinkAttributes la) {
423 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();
426 if (la.getAdminGroup() != null) {
427 tb.setColor(la.getAdminGroup().getValue());
429 if (la.getTeMetric() != null) {
430 tb.setTeDefaultMetric(la.getTeMetric().getValue());
432 if (la.getUnreservedBandwidth() != null) {
433 tb.setUnreservedBandwidth(unreservedBandwidthList(la.getUnreservedBandwidth()));
435 if (la.getMaxLinkBandwidth() != null) {
436 tb.setMaxLinkBandwidth(bandwidthToBigDecimal(la.getMaxLinkBandwidth()));
438 if (la.getMaxReservableBandwidth() != null) {
439 tb.setMaxResvLinkBandwidth(bandwidthToBigDecimal(la.getMaxReservableBandwidth()));
441 if (la.getSharedRiskLinkGroups() != null) {
442 final List<SrlgValues> srlgs = new ArrayList<>();
443 for (final SrlgId id : la.getSharedRiskLinkGroups()) {
444 srlgs.add(new SrlgValuesBuilder().setSrlgValue(id.getValue()).build());
446 tb.setSrlg(new SrlgBuilder().setSrlgValues(srlgs).build());
450 final OspfLinkAttributesBuilder ilab = new OspfLinkAttributesBuilder();
451 ilab.setTed(tb.build());
452 if (topologyIdentifier != null) {
453 ilab.setMultiTopologyId(topologyIdentifier.getValue().shortValue());
456 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1Builder().setOspfLinkAttributes(
457 ilab.build()).build();
460 private NodeHolder getNode(final NodeId id) {
461 if (this.nodes.containsKey(id)) {
462 LOG.debug("Node {} is already present", id);
463 return this.nodes.get(id);
466 final NodeHolder ret = new NodeHolder(id);
467 this.nodes.put(id, ret);
471 private void putNode(final WriteTransaction trans, final NodeHolder holder) {
472 if (holder.syncState(trans)) {
473 this.nodes.remove(holder.getNodeId());
477 private void augmentProtocolId(final LinkstateRoute value, final IgpLinkAttributesBuilder ilab, final LinkAttributes la, final LinkDescriptors ld) {
478 switch (value.getProtocolId()) {
485 ilab.addAugmentation(
486 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpLinkAttributes1.class,
487 isisLinkAttributes(ld.getMultiTopologyId(), la));
490 ilab.addAugmentation(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpLinkAttributes1.class,
491 ospfLinkAttributes(ld.getMultiTopologyId(), la));
498 private void createLink(final WriteTransaction trans, final UriBuilder base,
499 final LinkstateRoute value, final LinkCase l, final Attributes attributes) {
501 final LinkAttributes la;
502 final Attributes1 attr = attributes.getAugmentation(Attributes1.class);
504 final LinkStateAttribute attrType = attr.getLinkStateAttribute();
505 if (attrType != null) {
506 la = ((LinkAttributesCase)attrType).getLinkAttributes();
508 LOG.debug("Missing attribute type in link {} route {}, skipping it", l, value);
512 LOG.debug("Missing attributes in link {} route {}, skipping it", l, value);
516 final IgpLinkAttributesBuilder ilab = new IgpLinkAttributesBuilder();
518 if (la.getMetric() != null) {
519 ilab.setMetric(la.getMetric().getValue());
521 ilab.setName(la.getLinkName());
523 augmentProtocolId(value, ilab, la, l.getLinkDescriptors());
525 final LinkBuilder lb = new LinkBuilder();
526 lb.setLinkId(buildLinkId(base, l));
527 lb.addAugmentation(Link1.class, new Link1Builder().setIgpLinkAttributes(ilab.build()).build());
529 final NodeId srcNode = buildNodeId(base, l.getLocalNodeDescriptors());
530 LOG.debug("Link {} implies source node {}", l, srcNode);
532 final NodeId dstNode = buildNodeId(base, l.getRemoteNodeDescriptors());
533 LOG.debug("Link {} implies destination node {}", l, dstNode);
535 final TerminationPoint srcTp = buildLocalTp(base, l.getLinkDescriptors());
536 LOG.debug("Link {} implies source TP {}", l, srcTp);
538 final TerminationPoint dstTp = buildRemoteTp(base, l.getLinkDescriptors());
539 LOG.debug("Link {} implies destination TP {}", l, dstTp);
541 lb.setSource(new SourceBuilder().setSourceNode(srcNode).setSourceTp(srcTp.getTpId()).build());
542 lb.setDestination(new DestinationBuilder().setDestNode(dstNode).setDestTp(dstTp.getTpId()).build());
544 final NodeHolder snh = getNode(srcNode);
545 snh.addTp(srcTp, lb.getLinkId(), false);
546 LOG.debug("Created TP {} as link source", srcTp);
549 final NodeHolder dnh = getNode(dstNode);
550 dnh.addTp(dstTp, lb.getLinkId(), true);
551 LOG.debug("Created TP {} as link destination", dstTp);
554 final InstanceIdentifier<Link> lid = buildLinkIdentifier(lb.getLinkId());
555 final Link link = lb.build();
557 trans.put(LogicalDatastoreType.OPERATIONAL, lid, link);
558 LOG.debug("Created link {} at {} for {}", link, lid, l);
561 private void removeTp(final WriteTransaction trans, final NodeId node, final TpId tp,
562 final LinkId link, final boolean isRemote) {
563 final NodeHolder nh = this.nodes.get(node);
565 nh.removeTp(tp, link, isRemote);
568 LOG.warn("Removed non-existent node {}", node);
572 private void removeLink(final WriteTransaction trans, final UriBuilder base, final LinkCase l) {
573 final LinkId id = buildLinkId(base, l);
574 final InstanceIdentifier<?> lid = buildLinkIdentifier(id);
575 trans.delete(LogicalDatastoreType.OPERATIONAL, lid);
576 LOG.debug("Removed link {}", lid);
578 removeTp(trans, buildNodeId(base, l.getLocalNodeDescriptors()), buildLocalTpId(base, l.getLinkDescriptors()), id, false);
579 removeTp(trans, buildNodeId(base, l.getRemoteNodeDescriptors()), buildRemoteTpId(base, l.getLinkDescriptors()), id, true);
582 private List<Short> nodeMultiTopology(final List<TopologyIdentifier> list) {
583 final List<Short> ret = new ArrayList<>(list.size());
584 for (final TopologyIdentifier id : list) {
585 ret.add(id.getValue().shortValue());
590 private org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1 isisNodeAttributes(
591 final NodeIdentifier node, final NodeAttributes na) {
592 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();
593 final IsisNodeAttributesBuilder ab = new IsisNodeAttributesBuilder();
596 if (na.getIpv4RouterId() != null) {
597 tb.setTeRouterIdIpv4(na.getIpv4RouterId());
599 if (na.getIpv6RouterId() != null) {
600 tb.setTeRouterIdIpv6(na.getIpv6RouterId());
602 if (na.getTopologyIdentifier() != null) {
603 ab.setMultiTopologyId(nodeMultiTopology(na.getTopologyIdentifier()));
607 final CRouterIdentifier ri = node.getCRouterIdentifier();
608 if (ri instanceof IsisPseudonodeCase) {
609 final IsisPseudonode pn = ((IsisPseudonodeCase) ri).getIsisPseudonode();
610 final IsoBuilder b = new IsoBuilder();
611 b.setIsoSystemId(new IsoSystemId(UriBuilder.isoId(pn.getIsIsRouterIdentifier().getIsoSystemId())));
612 b.setIsoPseudonodeId(new IsoPseudonodeId(Hex.encodeHexString(new byte[] {pn.getPsn().byteValue()})));
613 ab.setIso(b.build());
614 } else if (ri instanceof IsisNodeCase) {
615 final IsisNode in = ((IsisNodeCase) ri).getIsisNode();
616 ab.setIso(new IsoBuilder().setIsoSystemId(new IsoSystemId(UriBuilder.isoId(in.getIsoSystemId()))).build());
619 ab.setTed(tb.build());
621 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1Builder().setIsisNodeAttributes(
625 private org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1 ospfNodeAttributes(
626 final NodeIdentifier node, final NodeAttributes na) {
627 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();
628 final OspfNodeAttributesBuilder ab = new OspfNodeAttributesBuilder();
630 if (na.getIpv4RouterId() != null) {
631 tb.setTeRouterIdIpv4(na.getIpv4RouterId());
633 if (na.getIpv6RouterId() != null) {
634 tb.setTeRouterIdIpv6(na.getIpv6RouterId());
636 if (na.getTopologyIdentifier() != null) {
637 ab.setMultiTopologyId(nodeMultiTopology(na.getTopologyIdentifier()));
639 final CRouterIdentifier ri = node.getCRouterIdentifier();
640 if (ri instanceof OspfPseudonodeCase) {
641 final OspfPseudonode pn = ((OspfPseudonodeCase) ri).getOspfPseudonode();
643 ab.setRouterType(new PseudonodeBuilder().setPseudonode(Boolean.TRUE).build());
644 ab.setDrInterfaceId(pn.getLanInterface().getValue());
645 } else if (ri instanceof OspfNodeCase && na.getNodeFlags() != null) {
646 // TODO: what should we do with in.getOspfRouterId()?
648 final NodeFlagBits nf = na.getNodeFlags();
649 if (nf.isAbr() != null) {
650 ab.setRouterType(new AbrBuilder().setAbr(nf.isAbr()).build());
651 } else if (nf.isExternal() != null) {
652 ab.setRouterType(new InternalBuilder().setInternal(!nf.isExternal()).build());
656 ab.setTed(tb.build());
657 return new org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1Builder().setOspfNodeAttributes(
661 private void augmentProtocolId(final LinkstateRoute value, final IgpNodeAttributesBuilder inab, final NodeAttributes na, final NodeIdentifier nd) {
662 switch (value.getProtocolId()) {
669 inab.addAugmentation(
670 org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.isis.topology.rev131021.IgpNodeAttributes1.class,
671 isisNodeAttributes(nd, na));
674 inab.addAugmentation(org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.ospf.topology.rev131021.IgpNodeAttributes1.class,
675 ospfNodeAttributes(nd, na));
682 private void createNode(final WriteTransaction trans, final UriBuilder base,
683 final LinkstateRoute value, final NodeCase n, final Attributes attributes) {
684 final NodeAttributes na;
686 final Attributes1 attr = attributes.getAugmentation(Attributes1.class);
688 final LinkStateAttribute attrType = attr.getLinkStateAttribute();
689 if (attrType != null) {
690 na = ((NodeAttributesCase)attrType).getNodeAttributes();
692 LOG.debug("Missing attribute type in node {} route {}, skipping it", n, value);
696 LOG.debug("Missing attributes in node {} route {}, skipping it", n, value);
699 final IgpNodeAttributesBuilder inab = new IgpNodeAttributesBuilder();
700 final List<IpAddress> ids = new ArrayList<>();
702 if (na.getIpv4RouterId() != null) {
703 ids.add(new IpAddress(na.getIpv4RouterId()));
705 if (na.getIpv6RouterId() != null) {
706 ids.add(new IpAddress(na.getIpv6RouterId()));
708 if (na.getDynamicHostname() != null) {
709 inab.setName(new DomainName(na.getDynamicHostname()));
712 if (!ids.isEmpty()) {
713 inab.setRouterId(ids);
715 augmentProtocolId(value, inab, na, n.getNodeDescriptors());
717 final NodeId nid = buildNodeId(base, n.getNodeDescriptors());
718 final NodeHolder nh = getNode(nid);
720 * Eventhough the the holder creates a dummy structure, we need to duplicate it here,
721 * as that is the API requirement. The reason for it is the possible presence of supporting
722 * node -- something which the holder does not track.
724 final NodeBuilder nb = new NodeBuilder();
726 nb.setKey(new NodeKey(nb.getNodeId()));
728 nh.advertized(nb, inab);
732 private void removeNode(final WriteTransaction trans, final UriBuilder base, final NodeCase n) {
733 final NodeId id = buildNodeId(base, n.getNodeDescriptors());
734 final NodeHolder nh = this.nodes.get(id);
739 LOG.warn("Node {} does not have a holder", id);
743 private void augmentProtocolId(final LinkstateRoute value, final PrefixAttributes pa, final PrefixBuilder pb) {
744 switch (value.getProtocolId()) {
752 if (pa != null && pa.getOspfForwardingAddress() != null) {
755 new Prefix1Builder().setOspfPrefixAttributes(
756 new OspfPrefixAttributesBuilder().setForwardingAddress(pa.getOspfForwardingAddress().getIpv4Address()).build()).build());
764 private void createPrefix(final WriteTransaction trans, final UriBuilder base,
765 final LinkstateRoute value, final PrefixCase p, final Attributes attributes) {
766 final IpPrefix ippfx = p.getPrefixDescriptors().getIpReachabilityInformation();
768 LOG.warn("IP reachability not present in prefix {} route {}, skipping it", p, value);
771 final PrefixBuilder pb = new PrefixBuilder();
772 pb.setKey(new PrefixKey(ippfx));
775 final PrefixAttributes pa;
776 // Very defensive lookup
777 final Attributes1 attr = attributes.getAugmentation(Attributes1.class);
779 final LinkStateAttribute attrType = attr.getLinkStateAttribute();
780 if (attrType != null) {
781 pa = ((PrefixAttributesCase)attrType).getPrefixAttributes();
783 LOG.debug("Missing attribute type in IP {} prefix {} route {}, skipping it", ippfx, p, value);
787 LOG.debug("Missing attributes in IP {} prefix {} route {}, skipping it", ippfx, p, value);
791 pb.setMetric(pa.getPrefixMetric().getValue());
793 augmentProtocolId(value, pa, pb);
795 final Prefix pfx = pb.build();
798 * All set, but... the hosting node may not exist, we may need to fake it.
800 final NodeId node = buildNodeId(base, p.getAdvertisingNodeDescriptors());
801 final NodeHolder nh = getNode(node);
803 LOG.debug("Created prefix {} for {}", pfx, p);
807 private void removePrefix(final WriteTransaction trans, final UriBuilder base, final PrefixCase p) {
808 final NodeId node = buildNodeId(base, p.getAdvertisingNodeDescriptors());
809 final NodeHolder nh = this.nodes.get(node);
812 LOG.debug("Removed prefix {}", p);
815 LOG.warn("Removing prefix from non-existing node {}", node);
820 protected void createObject(final ReadWriteTransaction trans,
821 final InstanceIdentifier<LinkstateRoute> id, final LinkstateRoute value) {
822 final UriBuilder base = new UriBuilder(value);
824 final ObjectType t = value.getObjectType();
825 Preconditions.checkArgument(t != null, "Route %s value %s has null object type", id, value);
827 if (t instanceof LinkCase) {
828 createLink(trans, base, value, (LinkCase) t, value.getAttributes());
829 } else if (t instanceof NodeCase) {
830 createNode(trans, base, value, (NodeCase) t, value.getAttributes());
831 } else if (t instanceof PrefixCase) {
832 createPrefix(trans, base, value, (PrefixCase) t, value.getAttributes());
834 throw new IllegalStateException("Unhandled object class " + t.getImplementedInterface());
839 protected void removeObject(final ReadWriteTransaction trans,
840 final InstanceIdentifier<LinkstateRoute> id, final LinkstateRoute value) {
841 final UriBuilder base = new UriBuilder(value);
843 final ObjectType t = value.getObjectType();
844 if (t instanceof LinkCase) {
845 removeLink(trans, base, (LinkCase) t);
846 } else if (t instanceof NodeCase) {
847 removeNode(trans, base, (NodeCase) t);
848 } else if (t instanceof PrefixCase) {
849 removePrefix(trans, base, (PrefixCase) t);
851 throw new IllegalStateException("Unhandled object class " + t.getImplementedInterface());