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 com.google.common.base.Preconditions;
11 import com.google.common.collect.Lists;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.JdkFutureAdapters;
16 import java.util.HashSet;
20 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
21 import org.opendaylight.controller.md.sal.common.api.data.DataChangeEvent;
22 import org.opendaylight.controller.sal.binding.api.data.DataChangeListener;
23 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
24 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.AdministrativeStatus;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Path1;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.identifiers.tlv.lsp.identifiers.AddressFamily;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv4Case;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.Ipv6Case;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.ipv4._case.Ipv4;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.endpoints.address.family.ipv6._case.Ipv6;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.Node1;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.PathComputationClient;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev131024.pcep.client.attributes.path.computation.client.ReportedLsp;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.tunnel.pcep.rev130820.Link1;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.tunnel.pcep.rev130820.Link1Builder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.tunnel.pcep.rev130820.SupportingNode1;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.tunnel.pcep.rev130820.SupportingNode1Builder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.tunnel.pcep.rev130820.tunnel.pcep.supporting.node.attributes.PathComputationClientBuilder;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.LinkId;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.DestinationBuilder;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.SourceBuilder;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkBuilder;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkKey;
50 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
51 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
53 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
54 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
55 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
56 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.node.attributes.SupportingNode;
57 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.node.attributes.SupportingNodeBuilder;
58 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.node.attributes.SupportingNodeKey;
59 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TerminationPoint1;
60 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.TerminationPoint1Builder;
61 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.nt.l3.unicast.igp.topology.rev131021.igp.termination.point.attributes.IgpTerminationPointAttributesBuilder;
62 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;
63 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;
64 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;
65 import org.opendaylight.yangtools.yang.binding.DataObject;
66 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
67 import org.opendaylight.yangtools.yang.common.RpcResult;
68 import org.slf4j.Logger;
69 import org.slf4j.LoggerFactory;
71 public final class NodeChangedListener implements DataChangeListener {
72 private 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(id.builder().augmentation(Node1.class).child(PathComputationClient.class).child(ReportedLsp.class, l.getKey()).toInstance());
112 private static LinkId linkIdForLsp(final InstanceIdentifier<ReportedLsp> i, final ReportedLsp lsp) {
113 return new LinkId(i.firstKeyOf(Node.class, NodeKey.class).getNodeId().getValue() + "/lsps/" + lsp.getName());
116 private InstanceIdentifier<Link> linkForLsp(final LinkId linkId) {
117 return this.target.child(Link.class, new LinkKey(linkId));
120 private SupportingNode createSupportingNode(final NodeId sni, final Boolean inControl) {
121 final SupportingNodeKey sk = new SupportingNodeKey(sni);
122 final SupportingNodeBuilder snb = new SupportingNodeBuilder();
125 snb.addAugmentation(SupportingNode1.class, new SupportingNode1Builder().setPathComputationClient(
126 new PathComputationClientBuilder().setControlling(inControl).build()).build());
131 private InstanceIdentifier<TerminationPoint> getIpTerminationPoint(final DataModificationTransaction trans, final IpAddress addr,
132 final InstanceIdentifier<Node> sni, final Boolean inControl) {
133 for (final Node n : ((Topology) trans.readOperationalData(this.target)).getNode()) {
134 for (final TerminationPoint tp : n.getTerminationPoint()) {
135 final TerminationPoint1 tpa = tp.getAugmentation(TerminationPoint1.class);
138 final TerminationPointType tpt = tpa.getIgpTerminationPointAttributes().getTerminationPointType();
140 if (tpt instanceof Ip) {
141 for (final IpAddress a : ((Ip) tpt).getIpAddress()) {
142 if (addr.equals(a.getIpv6Address())) {
144 final NodeKey k = InstanceIdentifier.keyOf(sni);
145 boolean have = false;
148 * We may have found a termination point which has been created as a destination,
149 * so it does not have a supporting node pointer. Since we now know what it is,
152 for (final SupportingNode sn : n.getSupportingNode()) {
153 if (sn.getNodeRef().equals(k.getNodeId())) {
160 final SupportingNode sn = createSupportingNode(k.getNodeId(), inControl);
162 trans.putOperationalData(this.target.builder().child(Node.class, n.getKey()).child(
163 SupportingNode.class, sn.getKey()).toInstance(), sn);
166 return this.target.builder().child(Node.class, n.getKey()).child(TerminationPoint.class, tp.getKey()).toInstance();
170 LOG.debug("Ignoring termination point type {}", tpt);
176 LOG.debug("Termination point for {} not found, creating a new one", addr);
178 final String url = "ip://" + addr.toString();
179 final TerminationPointKey tpk = new TerminationPointKey(new TpId(url));
180 final TerminationPointBuilder tpb = new TerminationPointBuilder();
181 tpb.setKey(tpk).setTpId(tpk.getTpId());
182 tpb.addAugmentation(TerminationPoint1.class, new TerminationPoint1Builder().setIgpTerminationPointAttributes(
183 new IgpTerminationPointAttributesBuilder().setTerminationPointType(
184 new IpBuilder().setIpAddress(Lists.newArrayList(addr)).build()).build()).build());
186 final NodeKey nk = new NodeKey(new NodeId(url));
187 final NodeBuilder nb = new NodeBuilder();
188 nb.setKey(nk).setNodeId(nk.getNodeId());
189 nb.setTerminationPoint(Lists.newArrayList(tpb.build()));
191 nb.setSupportingNode(Lists.newArrayList(createSupportingNode(InstanceIdentifier.keyOf(sni).getNodeId(), inControl)));
194 final InstanceIdentifier<Node> nid = this.target.child(Node.class, nb.getKey());
195 trans.putOperationalData(nid, nb.build());
196 return nid.child(TerminationPoint.class, tpb.getKey());
199 private void create(final DataModificationTransaction trans, final InstanceIdentifier<ReportedLsp> i, final ReportedLsp value) {
200 final InstanceIdentifier<Node> ni = i.firstIdentifierOf(Node.class);
202 final Path1 rl = value.getPath().get(0).getAugmentation(Path1.class);
204 final AddressFamily af = rl.getLsp().getTlvs().getLspIdentifiers().getAddressFamily();
207 * We are trying to ensure we have source and destination nodes.
209 final IpAddress srcIp, dstIp;
210 if (af instanceof Ipv4Case) {
211 final Ipv4 ipv4 = ((Ipv4Case) af).getIpv4();
212 srcIp = new IpAddress(ipv4.getSourceIpv4Address());
213 dstIp = new IpAddress(ipv4.getDestinationIpv4Address());
214 } else if (af instanceof Ipv6Case) {
215 final Ipv6 ipv6 = ((Ipv6Case) af).getIpv6();
216 srcIp = new IpAddress(ipv6.getSourceIpv6Address());
217 dstIp = new IpAddress(ipv6.getDestinationIpv6Address());
219 throw new IllegalArgumentException("Unsupported address family: " + af.getImplementedInterface());
222 final Link1Builder lab = new Link1Builder(value.getPath().get(0).getLspa());
223 lab.setBandwidth(value.getPath().get(0).getBandwidth().getBandwidth());
224 lab.setClassType(value.getPath().get(0).getClassType().getClassType());
225 lab.setSymbolicPathName(value.getName());
227 final InstanceIdentifier<TerminationPoint> dst = getIpTerminationPoint(trans, dstIp, null, Boolean.FALSE);
228 final InstanceIdentifier<TerminationPoint> src = getIpTerminationPoint(trans, srcIp, ni, rl.getLsp().isDelegate());
230 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();
231 slab.setOperationalStatus(rl.getLsp().getOperational());
232 slab.setAdministrativeStatus(rl.getLsp().isAdministrative() ? AdministrativeStatus.Active : AdministrativeStatus.Inactive);
234 final LinkId id = linkIdForLsp(i, value);
235 final LinkBuilder lb = new LinkBuilder();
238 lb.setSource(new SourceBuilder().setSourceNode(src.firstKeyOf(Node.class, NodeKey.class).getNodeId()).setSourceTp(
239 src.firstKeyOf(TerminationPoint.class, TerminationPointKey.class).getTpId()).build());
240 lb.setDestination(new DestinationBuilder().setDestNode(dst.firstKeyOf(Node.class, NodeKey.class).getNodeId()).setDestTp(
241 dst.firstKeyOf(TerminationPoint.class, TerminationPointKey.class).getTpId()).build());
242 lb.addAugmentation(Link1.class, lab.build());
243 lb.addAugmentation(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.Link1.class,
246 trans.putOperationalData(linkForLsp(id), lb.build());
249 private InstanceIdentifier<TerminationPoint> tpIdentifier(final NodeId node, final TpId tp) {
250 return this.target.builder().child(Node.class, new NodeKey(node)).child(TerminationPoint.class, new TerminationPointKey(tp)).toInstance();
253 private InstanceIdentifier<Node> nodeIdentifier(final NodeId node) {
254 return this.target.child(Node.class, new NodeKey(node));
257 private void remove(final DataModificationTransaction trans, final InstanceIdentifier<ReportedLsp> i, final ReportedLsp value) {
258 final InstanceIdentifier<Link> li = linkForLsp(linkIdForLsp(i, value));
260 final Link l = (Link) trans.readOperationalData(li);
262 LOG.debug("Removing link {} (was {})", li, l);
263 trans.removeOperationalData(li);
265 LOG.debug("Searching for orphan links/nodes");
266 final Topology t = (Topology) trans.readOperationalData(this.target);
268 NodeId srcNode = l.getSource().getSourceNode();
269 NodeId dstNode = l.getDestination().getDestNode();
270 TpId srcTp = l.getSource().getSourceTp();
271 TpId dstTp = l.getDestination().getDestTp();
273 boolean orphSrcNode = true, orphDstNode = true, orphDstTp = true, orphSrcTp = true;
274 for (final Link lw : t.getLink()) {
275 LOG.trace("Checking link {}", lw);
277 final NodeId sn = lw.getSource().getSourceNode();
278 final NodeId dn = lw.getDestination().getDestNode();
279 final TpId st = lw.getSource().getSourceTp();
280 final TpId dt = lw.getDestination().getDestTp();
282 // Source node checks
283 if (srcNode.equals(sn)) {
285 LOG.debug("Node {} held by source of link {}", srcNode, lw);
288 if (orphSrcTp && srcTp.equals(st)) {
289 LOG.debug("TP {} held by source of link {}", srcTp, lw);
293 if (srcNode.equals(dn)) {
295 LOG.debug("Node {} held by destination of link {}", srcNode, lw);
298 if (orphSrcTp && srcTp.equals(dt)) {
299 LOG.debug("TP {} held by destination of link {}", srcTp, lw);
304 // Destination node checks
305 if (dstNode.equals(sn)) {
307 LOG.debug("Node {} held by source of link {}", dstNode, lw);
310 if (orphDstTp && dstTp.equals(st)) {
311 LOG.debug("TP {} held by source of link {}", dstTp, lw);
315 if (dstNode.equals(dn)) {
317 LOG.debug("Node {} held by destination of link {}", dstNode, lw);
320 if (orphDstTp && dstTp.equals(dt)) {
321 LOG.debug("TP {} held by destination of link {}", dstTp, lw);
327 if (orphSrcNode && !orphSrcTp) {
328 LOG.warn("Orphan source node {} but not TP {}, retaining the node", srcNode, srcTp);
331 if (orphDstNode && !orphDstTp) {
332 LOG.warn("Orphan destination node {} but not TP {}, retaining the node", dstNode, dstTp);
337 LOG.debug("Removing orphan node {}", srcNode);
338 trans.removeOperationalData(nodeIdentifier(srcNode));
339 } else if (orphSrcTp) {
340 LOG.debug("Removing orphan TP {} on node {}", srcTp, srcNode);
341 trans.removeOperationalData(tpIdentifier(srcNode, srcTp));
344 LOG.debug("Removing orphan node {}", dstNode);
345 trans.removeOperationalData(nodeIdentifier(dstNode));
346 } else if (orphDstTp) {
347 LOG.debug("Removing orphan TP {} on node {}", dstTp, dstNode);
348 trans.removeOperationalData(tpIdentifier(dstNode, dstTp));
354 public void onDataChanged(final DataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
355 final DataModificationTransaction trans = this.dataProvider.beginTransaction();
357 final Set<InstanceIdentifier<ReportedLsp>> lsps = new HashSet<>();
358 final Set<InstanceIdentifier<Node>> nodes = new HashSet<>();
360 // Categorize reported identifiers
361 for (final InstanceIdentifier<?> i : change.getRemovedOperationalData()) {
362 categorizeIdentifier(i, lsps, nodes);
364 for (final InstanceIdentifier<?> i : change.getUpdatedOperationalData().keySet()) {
365 categorizeIdentifier(i, lsps, nodes);
367 for (final InstanceIdentifier<?> i : change.getCreatedOperationalData().keySet()) {
368 categorizeIdentifier(i, lsps, nodes);
372 final Map<InstanceIdentifier<?>, DataObject> o = change.getOriginalOperationalData();
373 final Map<InstanceIdentifier<?>, DataObject> n = change.getUpdatedOperationalData();
375 // Now walk all nodes, check for removals/additions and cascade them to LSPs
376 for (final InstanceIdentifier<Node> i : nodes) {
377 enumerateLsps(i, (Node) o.get(i), lsps);
378 enumerateLsps(i, (Node) n.get(i), lsps);
381 // We now have list of all affected LSPs. Walk them create/remove them
382 for (final InstanceIdentifier<ReportedLsp> i : lsps) {
383 final ReportedLsp oldValue = (ReportedLsp) o.get(i);
384 final ReportedLsp newValue = (ReportedLsp) n.get(i);
386 LOG.debug("Updating lsp {} value {} -> {}", i, oldValue, newValue);
387 if (oldValue != null) {
388 remove(trans, i, oldValue);
390 if (newValue != null) {
391 create(trans, i, newValue);
395 Futures.addCallback(JdkFutureAdapters.listenInPoolThread(trans.commit()), new FutureCallback<RpcResult<TransactionStatus>>() {
397 public void onSuccess(final RpcResult<TransactionStatus> result) {
398 LOG.trace("Topology change committed successfully");
402 public void onFailure(final Throwable t) {
403 LOG.error("Failed to propagate a topology change, target topology became inconsistent", t);
408 public static InstanceIdentifier<Link> linkIdentifier(final InstanceIdentifier<Topology> topology, final NodeId node, final String name) {
409 return topology.child(Link.class, new LinkKey(new LinkId(node.getValue() + "/lsp/" + name)));