blob: 0bd17033294464c196a3ec3dd857e3d234a834d4 [file] [log] [blame]
Ray Milkeya058c732014-10-08 13:52:34 -07001package org.onlab.onos.net.intent.impl;
2
3import java.util.ArrayList;
4import java.util.Arrays;
5import java.util.List;
6import java.util.Set;
7
8import org.apache.felix.scr.annotations.Activate;
9import org.apache.felix.scr.annotations.Component;
10import org.apache.felix.scr.annotations.Deactivate;
11import org.apache.felix.scr.annotations.Reference;
12import org.apache.felix.scr.annotations.ReferenceCardinality;
13import org.onlab.onos.net.ConnectPoint;
14import org.onlab.onos.net.DefaultEdgeLink;
15import org.onlab.onos.net.DefaultPath;
16import org.onlab.onos.net.Link;
17import org.onlab.onos.net.Path;
18import org.onlab.onos.net.host.HostService;
19import org.onlab.onos.net.intent.IdGenerator;
20import org.onlab.onos.net.intent.Intent;
21import org.onlab.onos.net.intent.IntentCompiler;
22import org.onlab.onos.net.intent.IntentExtensionService;
23import org.onlab.onos.net.intent.IntentId;
24import org.onlab.onos.net.intent.PathIntent;
25import org.onlab.onos.net.intent.PointToPointIntent;
26import org.onlab.onos.net.provider.ProviderId;
27import org.onlab.onos.net.topology.PathService;
28
29/**
30 * A intent compiler for {@link org.onlab.onos.net.intent.HostToHostIntent}.
31 */
32@Component(immediate = true)
33public class PointToPointIntentCompiler
34 implements IntentCompiler<PointToPointIntent> {
35
36 private static final ProviderId PID = new ProviderId("core", "org.onlab.onos.core", true);
37 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
38 protected IntentExtensionService intentManager;
39
40 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
41 protected PathService pathService;
42
43 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
44 protected HostService hostService;
45
46 private IdGenerator<IntentId> intentIdGenerator;
47
48 @Activate
49 public void activate() {
50 IdBlockAllocator idBlockAllocator = new DummyIdBlockAllocator();
51 intentIdGenerator = new IdBlockAllocatorBasedIntentIdGenerator(idBlockAllocator);
52 intentManager.registerCompiler(PointToPointIntent.class, this);
53 }
54
55 @Deactivate
56 public void deactivate() {
57 intentManager.unregisterCompiler(PointToPointIntent.class);
58 }
59
60 @Override
61 public List<Intent> compile(PointToPointIntent intent) {
62 Path path = getPath(intent.ingressPoint(), intent.egressPoint());
63
64 List<Link> links = new ArrayList<>();
65 links.add(DefaultEdgeLink.createEdgeLink(intent.ingressPoint(), true));
66 links.addAll(path.links());
67 links.add(DefaultEdgeLink.createEdgeLink(intent.egressPoint(), false));
68
69 return Arrays.asList(createPathIntent(new DefaultPath(PID, links, path.cost() + 2,
70 path.annotations()),
71 intent));
72 }
73
74 /**
75 * Creates a path intent from the specified path and original
76 * connectivity intent.
77 *
78 * @param path path to create an intent for
79 * @param intent original intent
80 */
81 private Intent createPathIntent(Path path,
82 PointToPointIntent intent) {
83
84 return new PathIntent(intentIdGenerator.getNewId(),
85 intent.selector(), intent.treatment(),
86 path.src(), path.dst(), path);
87 }
88
89 /**
90 * Computes a path between two ConnectPoints.
91 *
92 * @param one start of the path
93 * @param two end of the path
94 * @return Path between the two
95 * @throws PathNotFoundException if a path cannot be found
96 */
97 private Path getPath(ConnectPoint one, ConnectPoint two) {
98 Set<Path> paths = pathService.getPaths(one.deviceId(), two.deviceId());
99 if (paths.isEmpty()) {
100 throw new PathNotFoundException("No path from " + one + " to " + two);
101 }
102 // TODO: let's be more intelligent about this eventually
103 return paths.iterator().next();
104 }
105}