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.pcep.tunnel.provider;
10 import java.util.HashSet;
14 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
15 import org.opendaylight.controller.md.sal.common.api.data.DataChangeEvent;
16 import org.opendaylight.controller.sal.binding.api.data.DataChangeListener;
17 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
18 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.AdministrativeStatus;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.ReportedLsp1;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.identifiers.tlv.lsp.identifiers.AddressFamily;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv4Case;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv6Case;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.ipv4._case.Ipv4;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.ipv6._case.Ipv6;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.Node1;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.PathComputationClient;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.path.computation.client.ReportedLsp;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.tunnel.pcep.rev130820.Link1;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.tunnel.pcep.rev130820.Link1Builder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.tunnel.pcep.rev130820.SupportingNode1;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.tunnel.pcep.rev130820.SupportingNode1Builder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.tunnel.pcep.rev130820.tunnel.pcep.supporting.node.attributes.PathComputationClientBuilder;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.LinkId;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.DestinationBuilder;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.SourceBuilder;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkBuilder;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkKey;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
50 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.node.attributes.SupportingNode;
51 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.node.attributes.SupportingNodeBuilder;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.node.attributes.SupportingNodeKey;
53 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TerminationPoint1;
54 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TerminationPoint1Builder;
55 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.termination.point.attributes.IgpTerminationPointAttributesBuilder;
56 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;
57 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.Ip;
58 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;
59 import org.opendaylight.yangtools.yang.binding.DataObject;
60 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
61 import org.opendaylight.yangtools.yang.common.RpcResult;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
65 import com.google.common.base.Preconditions;
66 import com.google.common.collect.Lists;
67 import com.google.common.util.concurrent.FutureCallback;
68 import com.google.common.util.concurrent.Futures;
69 import com.google.common.util.concurrent.JdkFutureAdapters;
71 public final class NodeChangedListener implements DataChangeListener {
72 public static final Logger LOG = LoggerFactory.getLogger(NodeChangedListener.class);
73 private final InstanceIdentifier<Topology> target;
74 private final DataProviderService dataProvider;
76 NodeChangedListener(final DataProviderService dataProvider, final InstanceIdentifier<Topology> target) {
77 this.dataProvider = Preconditions.checkNotNull(dataProvider);
78 this.target = Preconditions.checkNotNull(target);
81 private static void categorizeIdentifier(final InstanceIdentifier<?> i, final Set<InstanceIdentifier<ReportedLsp>> changedLsps,
82 final Set<InstanceIdentifier<Node>> changedNodes) {
83 final InstanceIdentifier<ReportedLsp> li = i.firstIdentifierOf(ReportedLsp.class);
85 final InstanceIdentifier<Node> ni = i.firstIdentifierOf(Node.class);
87 LOG.warn("Ignoring uncategorized identifier {}", i);
96 private static void enumerateLsps(final InstanceIdentifier<Node> id, final Node node, final Set<InstanceIdentifier<ReportedLsp>> lsps) {
98 LOG.trace("Skipping null node", id);
101 final Node1 pccnode = node.getAugmentation(Node1.class);
102 if (pccnode == null) {
103 LOG.trace("Skipping non-PCEP-enabled node {}", id);
107 for (final ReportedLsp l : pccnode.getPathComputationClient().getReportedLsp()) {
108 lsps.add(InstanceIdentifier.builder(id).augmentation(Node1.class).child(PathComputationClient.class).child(ReportedLsp.class,
109 l.getKey()).toInstance());
113 private static LinkId linkIdForLsp(final InstanceIdentifier<ReportedLsp> i, final ReportedLsp lsp) {
114 return new LinkId(i.firstKeyOf(Node.class, NodeKey.class).getNodeId().getValue() + "/lsps/" + lsp.getName());
117 private InstanceIdentifier<Link> linkForLsp(final LinkId linkId) {
118 return InstanceIdentifier.builder(this.target).child(Link.class, new LinkKey(linkId)).toInstance();
121 private SupportingNode createSupportingNode(final NodeId sni, final Boolean inControl) {
122 final SupportingNodeKey sk = new SupportingNodeKey(sni);
123 final SupportingNodeBuilder snb = new SupportingNodeBuilder();
127 SupportingNode1.class,
128 new SupportingNode1Builder().setPathComputationClient(new PathComputationClientBuilder().setControlling(inControl).build()).build());
133 private InstanceIdentifier<TerminationPoint> getIpTerminationPoint(final DataModificationTransaction trans, final IpAddress addr,
134 final InstanceIdentifier<Node> sni, final Boolean inControl) {
135 for (final Node n : ((Topology) trans.readOperationalData(this.target)).getNode()) {
136 for (final TerminationPoint tp : n.getTerminationPoint()) {
137 final TerminationPoint1 tpa = tp.getAugmentation(TerminationPoint1.class);
140 final TerminationPointType tpt = tpa.getIgpTerminationPointAttributes().getTerminationPointType();
142 if (tpt instanceof Ip) {
143 for (final IpAddress a : ((Ip) tpt).getIpAddress()) {
144 if (addr.equals(a.getIpv6Address())) {
146 final NodeKey k = InstanceIdentifier.keyOf(sni);
147 boolean have = false;
150 * We may have found a termination point which has been created as a destination,
151 * so it does not have a supporting node pointer. Since we now know what it is,
154 for (final SupportingNode sn : n.getSupportingNode()) {
155 if (sn.getNodeRef().equals(k.getNodeId())) {
162 final SupportingNode sn = createSupportingNode(k.getNodeId(), inControl);
164 trans.putOperationalData(
165 InstanceIdentifier.builder(this.target).child(Node.class, n.getKey()).child(
166 SupportingNode.class, sn.getKey()).toInstance(), sn);
169 return InstanceIdentifier.builder(this.target).child(Node.class, n.getKey()).child(TerminationPoint.class,
170 tp.getKey()).toInstance();
174 LOG.debug("Ignoring termination point type {}", tpt);
180 LOG.debug("Termination point for {} not found, creating a new one", addr);
182 final String url = "ip://" + addr.toString();
183 final TerminationPointKey tpk = new TerminationPointKey(new TpId(url));
184 final TerminationPointBuilder tpb = new TerminationPointBuilder();
185 tpb.setKey(tpk).setTpId(tpk.getTpId());
187 TerminationPoint1.class,
188 new TerminationPoint1Builder().setIgpTerminationPointAttributes(
189 new IgpTerminationPointAttributesBuilder().setTerminationPointType(
190 new IpBuilder().setIpAddress(Lists.newArrayList(addr)).build()).build()).build());
192 final NodeKey nk = new NodeKey(new NodeId(url));
193 final NodeBuilder nb = new NodeBuilder();
194 nb.setKey(nk).setNodeId(nk.getNodeId());
195 nb.setTerminationPoint(Lists.newArrayList(tpb.build()));
197 nb.setSupportingNode(Lists.newArrayList(createSupportingNode(InstanceIdentifier.keyOf(sni).getNodeId(), inControl)));
200 trans.putOperationalData(InstanceIdentifier.builder(this.target).child(Node.class, nb.getKey()).toInstance(), nb.build());
201 return InstanceIdentifier.builder(this.target).child(Node.class, nb.getKey()).child(TerminationPoint.class, tpb.getKey()).toInstance();
204 private void create(final DataModificationTransaction trans, final InstanceIdentifier<ReportedLsp> i, final ReportedLsp value) {
205 final InstanceIdentifier<Node> ni = i.firstIdentifierOf(Node.class);
206 final ReportedLsp1 rl = value.getAugmentation(ReportedLsp1.class);
208 final AddressFamily af = rl.getLsp().getTlvs().getLspIdentifiers().getAddressFamily();
211 * We are trying to ensure we have source and destination nodes.
213 final IpAddress srcIp, dstIp;
214 if (af instanceof Ipv4Case) {
215 final Ipv4 ipv4 = ((Ipv4Case) af).getIpv4();
216 srcIp = new IpAddress(ipv4.getSourceIpv4Address());
217 dstIp = new IpAddress(ipv4.getDestinationIpv4Address());
218 } else if (af instanceof Ipv6Case) {
219 final Ipv6 ipv6 = ((Ipv6Case) af).getIpv6();
220 srcIp = new IpAddress(ipv6.getSourceIpv6Address());
221 dstIp = new IpAddress(ipv6.getDestinationIpv6Address());
223 throw new IllegalArgumentException("Unsupported address family: " + af.getImplementedInterface());
226 final Link1Builder lab = new Link1Builder(value.getPath().getLspa());
227 lab.setBandwidth(value.getPath().getBandwidth().getBandwidth());
228 lab.setClassType(value.getPath().getClassType().getClassType());
229 lab.setSymbolicPathName(value.getName());
231 final InstanceIdentifier<TerminationPoint> dst = getIpTerminationPoint(trans, dstIp, null, Boolean.FALSE);
232 final InstanceIdentifier<TerminationPoint> src = getIpTerminationPoint(trans, srcIp, ni, rl.getLsp().isDelegate());
234 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Link1Builder slab = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Link1Builder();
235 slab.setOperationalStatus(rl.getLsp().getOperational());
236 slab.setAdministrativeStatus(rl.getLsp().isAdministrative() ? AdministrativeStatus.Active : AdministrativeStatus.Inactive);
238 final LinkId id = linkIdForLsp(i, value);
239 final LinkBuilder lb = new LinkBuilder();
242 lb.setSource(new SourceBuilder().setSourceNode(src.firstKeyOf(Node.class, NodeKey.class).getNodeId()).setSourceTp(
243 src.firstKeyOf(TerminationPoint.class, TerminationPointKey.class).getTpId()).build());
244 lb.setDestination(new DestinationBuilder().setDestNode(dst.firstKeyOf(Node.class, NodeKey.class).getNodeId()).setDestTp(
245 dst.firstKeyOf(TerminationPoint.class, TerminationPointKey.class).getTpId()).build());
246 lb.addAugmentation(Link1.class, lab.build());
247 lb.addAugmentation(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Link1.class, slab.build());
249 trans.putOperationalData(linkForLsp(id), lb.build());
252 private InstanceIdentifier<TerminationPoint> tpIdentifier(final NodeId node, final TpId tp) {
253 return InstanceIdentifier.builder(this.target).child(Node.class, new NodeKey(node)).child(TerminationPoint.class, new TerminationPointKey(tp)).toInstance();
256 private InstanceIdentifier<Node> nodeIdentifier(final NodeId node) {
257 return InstanceIdentifier.builder(this.target).child(Node.class, new NodeKey(node)).toInstance();
260 private void remove(final DataModificationTransaction trans, final InstanceIdentifier<ReportedLsp> i, final ReportedLsp value) {
261 final InstanceIdentifier<Link> li = linkForLsp(linkIdForLsp(i, value));
263 final Link l = (Link) trans.readOperationalData(li);
265 LOG.debug("Removing link {} (was {})", li, l);
266 trans.removeOperationalData(li);
268 LOG.debug("Searching for orphan links/nodes");
269 final Topology t = (Topology) trans.readOperationalData(InstanceIdentifier.builder(this.target).toInstance());
271 NodeId srcNode = l.getSource().getSourceNode();
272 NodeId dstNode = l.getDestination().getDestNode();
273 TpId srcTp = l.getSource().getSourceTp();
274 TpId dstTp = l.getDestination().getDestTp();
276 boolean orphSrcNode = true, orphDstNode = true, orphDstTp = true, orphSrcTp = true;
277 for (final Link lw : t.getLink()) {
278 LOG.trace("Checking link {}", lw);
280 final NodeId sn = lw.getSource().getSourceNode();
281 final NodeId dn = lw.getDestination().getDestNode();
282 final TpId st = lw.getSource().getSourceTp();
283 final TpId dt = lw.getDestination().getDestTp();
285 // Source node checks
286 if (srcNode.equals(sn)) {
288 LOG.debug("Node {} held by source of link {}", srcNode, lw);
291 if (orphSrcTp && srcTp.equals(st)) {
292 LOG.debug("TP {} held by source of link {}", srcTp, lw);
296 if (srcNode.equals(dn)) {
298 LOG.debug("Node {} held by destination of link {}", srcNode, lw);
301 if (orphSrcTp && srcTp.equals(dt)) {
302 LOG.debug("TP {} held by destination of link {}", srcTp, lw);
307 // Destination node checks
308 if (dstNode.equals(sn)) {
310 LOG.debug("Node {} held by source of link {}", dstNode, lw);
313 if (orphDstTp && dstTp.equals(st)) {
314 LOG.debug("TP {} held by source of link {}", dstTp, lw);
318 if (dstNode.equals(dn)) {
320 LOG.debug("Node {} held by destination of link {}", dstNode, lw);
323 if (orphDstTp && dstTp.equals(dt)) {
324 LOG.debug("TP {} held by destination of link {}", dstTp, lw);
330 if (orphSrcNode && !orphSrcTp) {
331 LOG.warn("Orphan source node {} but not TP {}, retaining the node", srcNode, srcTp);
334 if (orphDstNode && !orphDstTp) {
335 LOG.warn("Orphan destination node {} but not TP {}, retaining the node", dstNode, dstTp);
340 LOG.debug("Removing orphan node {}", srcNode);
341 trans.removeOperationalData(nodeIdentifier(srcNode));
342 } else if (orphSrcTp) {
343 LOG.debug("Removing orphan TP {} on node {}", srcTp, srcNode);
344 trans.removeOperationalData(tpIdentifier(srcNode, srcTp));
347 LOG.debug("Removing orphan node {}", dstNode);
348 trans.removeOperationalData(nodeIdentifier(dstNode));
349 } else if (orphDstTp) {
350 LOG.debug("Removing orphan TP {} on node {}", dstTp, dstNode);
351 trans.removeOperationalData(tpIdentifier(dstNode, dstTp));
357 public void onDataChanged(final DataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
358 final DataModificationTransaction trans = this.dataProvider.beginTransaction();
360 final Set<InstanceIdentifier<ReportedLsp>> lsps = new HashSet<>();
361 final Set<InstanceIdentifier<Node>> nodes = new HashSet<>();
363 // Categorize reported identifiers
364 for (final InstanceIdentifier<?> i : change.getRemovedOperationalData()) {
365 categorizeIdentifier(i, lsps, nodes);
367 for (final InstanceIdentifier<?> i : change.getUpdatedOperationalData().keySet()) {
368 categorizeIdentifier(i, lsps, nodes);
370 for (final InstanceIdentifier<?> i : change.getCreatedOperationalData().keySet()) {
371 categorizeIdentifier(i, lsps, nodes);
375 final Map<InstanceIdentifier<?>, DataObject> o = change.getOriginalOperationalData();
376 final Map<InstanceIdentifier<?>, DataObject> n = change.getUpdatedOperationalData();
378 // Now walk all nodes, check for removals/additions and cascade them to LSPs
379 for (final InstanceIdentifier<Node> i : nodes) {
380 enumerateLsps(i, (Node) o.get(i), lsps);
381 enumerateLsps(i, (Node) n.get(i), lsps);
384 // We now have list of all affected LSPs. Walk them create/remove them
385 for (final InstanceIdentifier<ReportedLsp> i : lsps) {
386 final ReportedLsp oldValue = (ReportedLsp) o.get(i);
387 final ReportedLsp newValue = (ReportedLsp) n.get(i);
389 LOG.debug("Updating lsp {} value {} -> {}", i, oldValue, newValue);
390 if (oldValue != null) {
391 remove(trans, i, oldValue);
393 if (newValue != null) {
394 create(trans, i, newValue);
398 Futures.addCallback(JdkFutureAdapters.listenInPoolThread(trans.commit()), new FutureCallback<RpcResult<TransactionStatus>>() {
400 public void onSuccess(final RpcResult<TransactionStatus> result) {
401 LOG.trace("Topology change committed successfully");
405 public void onFailure(final Throwable t) {
406 LOG.error("Failed to propagate a topology change, target topology became inconsistent", t);
411 public static InstanceIdentifier<Link> linkIdentifier(final InstanceIdentifier<Topology> topology, final NodeId node,
413 return InstanceIdentifier.builder(topology).child(Link.class, new LinkKey(new LinkId(node.getValue() + "/lsp/" + name))).toInstance();