blob: 541a702b8d04b2421c725feadfaa0770550e93ee [file] [log] [blame]
Brian O'Connor66630c82014-10-02 21:08:19 -07001package org.onlab.onos.net.intent.impl;
2
Brian O'Connor66630c82014-10-02 21:08:19 -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;
tomf5c9d922014-10-03 15:22:03 -07008import org.onlab.onos.net.Host;
9import org.onlab.onos.net.HostId;
Brian O'Connor66630c82014-10-02 21:08:19 -070010import org.onlab.onos.net.Path;
tomf5c9d922014-10-03 15:22:03 -070011import org.onlab.onos.net.flow.TrafficSelector;
12import org.onlab.onos.net.host.HostService;
Brian O'Connor66630c82014-10-02 21:08:19 -070013import org.onlab.onos.net.intent.HostToHostIntent;
14import org.onlab.onos.net.intent.IdGenerator;
15import org.onlab.onos.net.intent.Intent;
16import org.onlab.onos.net.intent.IntentCompiler;
17import org.onlab.onos.net.intent.IntentExtensionService;
18import org.onlab.onos.net.intent.IntentId;
19import org.onlab.onos.net.intent.PathIntent;
20import org.onlab.onos.net.topology.PathService;
21
tomf5c9d922014-10-03 15:22:03 -070022import java.util.Arrays;
23import java.util.List;
24import java.util.Set;
25
26import static org.onlab.onos.net.flow.DefaultTrafficSelector.builder;
27
Brian O'Connor66630c82014-10-02 21:08:19 -070028/**
29 * A intent compiler for {@link HostToHostIntent}.
30 */
31@Component(immediate = true)
32public class HostToHostIntentCompiler
33 implements IntentCompiler<HostToHostIntent> {
34
35 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
36 protected IntentExtensionService intentManager;
37
38 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
39 protected PathService pathService;
40
tomf5c9d922014-10-03 15:22:03 -070041 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
42 protected HostService hostService;
43
Brian O'Connor66630c82014-10-02 21:08:19 -070044 private IdGenerator<IntentId> intentIdGenerator;
45
46 @Activate
47 public void activate() {
48 IdBlockAllocator idBlockAllocator = new DummyIdBlockAllocator();
49 intentIdGenerator = new IdBlockAllocatorBasedIntentIdGenerator(idBlockAllocator);
50 intentManager.registerCompiler(HostToHostIntent.class, this);
51 }
52
53 @Deactivate
54 public void deactivate() {
55 intentManager.unregisterCompiler(HostToHostIntent.class);
56 }
57
58 @Override
59 public List<Intent> compile(HostToHostIntent intent) {
tomf5c9d922014-10-03 15:22:03 -070060 Path pathOne = getPath(intent.one(), intent.two());
61 Path pathTwo = getPath(intent.two(), intent.one());
Brian O'Connor66630c82014-10-02 21:08:19 -070062
tomf5c9d922014-10-03 15:22:03 -070063 Host one = hostService.getHost(intent.one());
64 Host two = hostService.getHost(intent.two());
65
66 return Arrays.asList(createPathIntent(pathOne, one, two, intent),
67 createPathIntent(pathTwo, two, one, intent));
68 }
69
70 // Creates a path intent from the specified path and original connectivity intent.
71 private Intent createPathIntent(Path path, Host src, Host dst,
72 HostToHostIntent intent) {
73
74 TrafficSelector selector = builder(intent.getTrafficSelector())
75 .matchEthSrc(src.mac()).matchEthDst(dst.mac()).build();
76
77 return new PathIntent(intentIdGenerator.getNewId(),
78 selector, intent.getTrafficTreatment(),
79 path.src(), path.dst(), path);
80 }
81
82 private Path getPath(HostId one, HostId two) {
83 Set<Path> paths = pathService.getPaths(one, two);
84 if (paths.isEmpty()) {
85 throw new PathNotFoundException("No path from host " + one + " to " + two);
86 }
87 // TODO: let's be more intelligent about this eventually
88 return paths.iterator().next();
Brian O'Connor66630c82014-10-02 21:08:19 -070089 }
90}