blob: cfc8d73e8708ca244cd0412e3c79dab66c9f450b [file] [log] [blame]
Ray Milkey0742ec92014-10-13 08:39:55 -07001package org.onlab.onos.net.intent.impl;
2
Ray Milkey0742ec92014-10-13 08:39:55 -07003import org.apache.felix.scr.annotations.Activate;
4import org.apache.felix.scr.annotations.Component;
5import org.apache.felix.scr.annotations.Deactivate;
6import org.apache.felix.scr.annotations.Reference;
7import org.apache.felix.scr.annotations.ReferenceCardinality;
8import org.onlab.onos.net.ConnectPoint;
9import org.onlab.onos.net.Link;
10import org.onlab.onos.net.Path;
Ray Milkey0742ec92014-10-13 08:39:55 -070011import org.onlab.onos.net.intent.Intent;
12import org.onlab.onos.net.intent.IntentCompiler;
13import org.onlab.onos.net.intent.IntentExtensionService;
Ray Milkey0742ec92014-10-13 08:39:55 -070014import org.onlab.onos.net.intent.LinkCollectionIntent;
15import org.onlab.onos.net.intent.MultiPointToSinglePointIntent;
16import org.onlab.onos.net.intent.PointToPointIntent;
17import org.onlab.onos.net.topology.PathService;
18
Thomas Vachuskab97cf282014-10-20 23:31:12 -070019import java.util.Arrays;
20import java.util.HashSet;
21import java.util.List;
22import java.util.Set;
23
Ray Milkey0742ec92014-10-13 08:39:55 -070024/**
25 * An intent compiler for
26 * {@link org.onlab.onos.net.intent.MultiPointToSinglePointIntent}.
27 */
28@Component(immediate = true)
29public class MultiPointToSinglePointIntentCompiler
30 implements IntentCompiler<MultiPointToSinglePointIntent> {
31
32 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
33 protected IntentExtensionService intentManager;
34
35 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
36 protected PathService pathService;
37
Ray Milkey0742ec92014-10-13 08:39:55 -070038 @Activate
39 public void activate() {
Ray Milkey0742ec92014-10-13 08:39:55 -070040 intentManager.registerCompiler(MultiPointToSinglePointIntent.class, this);
41 }
42
43 @Deactivate
44 public void deactivate() {
45 intentManager.unregisterCompiler(PointToPointIntent.class);
46 }
47
48 @Override
49 public List<Intent> compile(MultiPointToSinglePointIntent intent) {
50 Set<Link> links = new HashSet<>();
51
52 for (ConnectPoint ingressPoint : intent.ingressPoints()) {
53 Path path = getPath(ingressPoint, intent.egressPoint());
54 links.addAll(path.links());
55 }
56
Thomas Vachuskab97cf282014-10-20 23:31:12 -070057 Intent result = new LinkCollectionIntent(intent.appId(),
58 intent.selector(), intent.treatment(),
59 links, intent.egressPoint());
Ray Milkey0742ec92014-10-13 08:39:55 -070060 return Arrays.asList(result);
61 }
62
63 /**
64 * Computes a path between two ConnectPoints.
65 *
66 * @param one start of the path
67 * @param two end of the path
68 * @return Path between the two
69 * @throws org.onlab.onos.net.intent.impl.PathNotFoundException if a path cannot be found
70 */
71 private Path getPath(ConnectPoint one, ConnectPoint two) {
72 Set<Path> paths = pathService.getPaths(one.deviceId(), two.deviceId());
73 if (paths.isEmpty()) {
74 throw new PathNotFoundException("No path from " + one + " to " + two);
75 }
76 // TODO: let's be more intelligent about this eventually
77 return paths.iterator().next();
78 }
79}