blob: e769982e47f4c706b47a7e2b6437e43dc0f81e45 [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;
Brian O'Connor66630c82014-10-02 21:08:19 -070014import org.onlab.onos.net.intent.Intent;
15import org.onlab.onos.net.intent.IntentCompiler;
16import org.onlab.onos.net.intent.IntentExtensionService;
Brian O'Connor66630c82014-10-02 21:08:19 -070017import org.onlab.onos.net.intent.PathIntent;
18import org.onlab.onos.net.topology.PathService;
19
tomf5c9d922014-10-03 15:22:03 -070020import java.util.Arrays;
21import java.util.List;
22import java.util.Set;
23
24import static org.onlab.onos.net.flow.DefaultTrafficSelector.builder;
25
Brian O'Connor66630c82014-10-02 21:08:19 -070026/**
27 * A intent compiler for {@link HostToHostIntent}.
28 */
29@Component(immediate = true)
30public class HostToHostIntentCompiler
31 implements IntentCompiler<HostToHostIntent> {
32
33 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
34 protected IntentExtensionService intentManager;
35
36 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
37 protected PathService pathService;
38
tomf5c9d922014-10-03 15:22:03 -070039 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
40 protected HostService hostService;
41
Brian O'Connor66630c82014-10-02 21:08:19 -070042 @Activate
43 public void activate() {
Brian O'Connor66630c82014-10-02 21:08:19 -070044 intentManager.registerCompiler(HostToHostIntent.class, this);
45 }
46
47 @Deactivate
48 public void deactivate() {
49 intentManager.unregisterCompiler(HostToHostIntent.class);
50 }
51
52 @Override
53 public List<Intent> compile(HostToHostIntent intent) {
tomf5c9d922014-10-03 15:22:03 -070054 Path pathOne = getPath(intent.one(), intent.two());
55 Path pathTwo = getPath(intent.two(), intent.one());
Brian O'Connor66630c82014-10-02 21:08:19 -070056
tomf5c9d922014-10-03 15:22:03 -070057 Host one = hostService.getHost(intent.one());
58 Host two = hostService.getHost(intent.two());
59
60 return Arrays.asList(createPathIntent(pathOne, one, two, intent),
61 createPathIntent(pathTwo, two, one, intent));
62 }
63
64 // Creates a path intent from the specified path and original connectivity intent.
65 private Intent createPathIntent(Path path, Host src, Host dst,
66 HostToHostIntent intent) {
tom85258ee2014-10-07 00:10:02 -070067 TrafficSelector selector = builder(intent.selector())
tomf5c9d922014-10-03 15:22:03 -070068 .matchEthSrc(src.mac()).matchEthDst(dst.mac()).build();
Thomas Vachuskab97cf282014-10-20 23:31:12 -070069 return new PathIntent(intent.appId(), selector, intent.treatment(),
70 path);
tomf5c9d922014-10-03 15:22:03 -070071 }
72
73 private Path getPath(HostId one, HostId two) {
74 Set<Path> paths = pathService.getPaths(one, two);
75 if (paths.isEmpty()) {
76 throw new PathNotFoundException("No path from host " + one + " to " + two);
77 }
78 // TODO: let's be more intelligent about this eventually
79 return paths.iterator().next();
Brian O'Connor66630c82014-10-02 21:08:19 -070080 }
81}