blob: 25d50cce050103c263f15c8e487b64f133c7a47c [file] [log] [blame]
Ray Milkeya058c732014-10-08 13:52:34 -07001package org.onlab.onos.net.intent.impl;
2
Ray Milkeya058c732014-10-08 13:52:34 -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.DefaultEdgeLink;
10import org.onlab.onos.net.DefaultPath;
11import org.onlab.onos.net.Link;
12import org.onlab.onos.net.Path;
13import org.onlab.onos.net.host.HostService;
Ray Milkeya058c732014-10-08 13:52:34 -070014import org.onlab.onos.net.intent.Intent;
15import org.onlab.onos.net.intent.IntentCompiler;
16import org.onlab.onos.net.intent.IntentExtensionService;
Ray Milkeya058c732014-10-08 13:52:34 -070017import org.onlab.onos.net.intent.PathIntent;
18import org.onlab.onos.net.intent.PointToPointIntent;
19import org.onlab.onos.net.provider.ProviderId;
20import org.onlab.onos.net.topology.PathService;
21
Thomas Vachuskab97cf282014-10-20 23:31:12 -070022import java.util.ArrayList;
23import java.util.Arrays;
24import java.util.List;
25import java.util.Set;
26
Ray Milkeya058c732014-10-08 13:52:34 -070027/**
28 * A intent compiler for {@link org.onlab.onos.net.intent.HostToHostIntent}.
29 */
30@Component(immediate = true)
31public class PointToPointIntentCompiler
32 implements IntentCompiler<PointToPointIntent> {
33
34 private static final ProviderId PID = new ProviderId("core", "org.onlab.onos.core", true);
35 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
36 protected IntentExtensionService intentManager;
37
38 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
39 protected PathService pathService;
40
41 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
42 protected HostService hostService;
43
Ray Milkeya058c732014-10-08 13:52:34 -070044 @Activate
45 public void activate() {
Ray Milkeya058c732014-10-08 13:52:34 -070046 intentManager.registerCompiler(PointToPointIntent.class, this);
47 }
48
49 @Deactivate
50 public void deactivate() {
51 intentManager.unregisterCompiler(PointToPointIntent.class);
52 }
53
54 @Override
55 public List<Intent> compile(PointToPointIntent intent) {
56 Path path = getPath(intent.ingressPoint(), intent.egressPoint());
57
58 List<Link> links = new ArrayList<>();
59 links.add(DefaultEdgeLink.createEdgeLink(intent.ingressPoint(), true));
60 links.addAll(path.links());
61 links.add(DefaultEdgeLink.createEdgeLink(intent.egressPoint(), false));
62
63 return Arrays.asList(createPathIntent(new DefaultPath(PID, links, path.cost() + 2,
Thomas Vachuskab97cf282014-10-20 23:31:12 -070064 path.annotations()),
65 intent));
Ray Milkeya058c732014-10-08 13:52:34 -070066 }
67
68 /**
69 * Creates a path intent from the specified path and original
70 * connectivity intent.
71 *
Thomas Vachuskab97cf282014-10-20 23:31:12 -070072 * @param path path to create an intent for
Ray Milkeya058c732014-10-08 13:52:34 -070073 * @param intent original intent
74 */
75 private Intent createPathIntent(Path path,
76 PointToPointIntent intent) {
Thomas Vachuskab97cf282014-10-20 23:31:12 -070077 return new PathIntent(intent.appId(),
78 intent.selector(), intent.treatment(), path);
Ray Milkeya058c732014-10-08 13:52:34 -070079 }
80
81 /**
82 * Computes a path between two ConnectPoints.
83 *
84 * @param one start of the path
85 * @param two end of the path
86 * @return Path between the two
87 * @throws PathNotFoundException if a path cannot be found
88 */
89 private Path getPath(ConnectPoint one, ConnectPoint two) {
90 Set<Path> paths = pathService.getPaths(one.deviceId(), two.deviceId());
91 if (paths.isEmpty()) {
92 throw new PathNotFoundException("No path from " + one + " to " + two);
93 }
94 // TODO: let's be more intelligent about this eventually
95 return paths.iterator().next();
96 }
97}