blob: 02770505bc55cde32c5d0cdf6c3d1e68418939bf [file] [log] [blame]
Sho SHIMIZUf7b693e2014-08-15 16:17:13 -07001package net.onrc.onos.core.newintent;
2
3import com.google.common.base.Predicates;
4import com.google.common.collect.FluentIterable;
5import com.google.common.collect.ImmutableList;
Sho SHIMIZU7cd8a422014-08-27 16:05:21 -07006import net.onrc.onos.api.flowmanager.FlowId;
Sho SHIMIZUf7b693e2014-08-15 16:17:13 -07007import net.onrc.onos.api.flowmanager.FlowLink;
8import net.onrc.onos.api.flowmanager.PacketPathFlow;
9import net.onrc.onos.api.flowmanager.Path;
10import net.onrc.onos.api.newintent.Intent;
Sho SHIMIZU7cd8a422014-08-27 16:05:21 -070011import net.onrc.onos.api.newintent.IntentId;
Sho SHIMIZUf7b693e2014-08-15 16:17:13 -070012import net.onrc.onos.api.newintent.PathIntent;
13import net.onrc.onos.core.matchaction.match.Match;
14import net.onrc.onos.core.matchaction.match.PacketMatch;
Sho SHIMIZU7cd8a422014-08-27 16:05:21 -070015import net.onrc.onos.core.util.IdGenerator;
Sho SHIMIZUf7b693e2014-08-15 16:17:13 -070016import net.onrc.onos.core.util.LinkTuple;
17
18import java.util.Arrays;
19import java.util.List;
20
21/**
22 * An intent compiler for {@link PathIntent}.
23 */
24public class PathIntentCompiler
25 extends AbstractFlowGeneratingIntentCompiler<PathIntent> {
26
27 /**
28 * Construct an {@link net.onrc.onos.api.newintent.IntentCompiler}
29 * for {@link PathIntent}.
30 *
31 * @param intentIdGenerator intent ID generator
32 * @param flowIdGenerator flow ID generator
33 */
Sho SHIMIZU7cd8a422014-08-27 16:05:21 -070034 public PathIntentCompiler(IdGenerator<IntentId> intentIdGenerator,
35 IdGenerator<FlowId> flowIdGenerator) {
Sho SHIMIZUf7b693e2014-08-15 16:17:13 -070036 super(intentIdGenerator, flowIdGenerator);
37 }
38
39 @Override
40 public List<Intent> compile(PathIntent intent) {
41 Match match = intent.getMatch();
42 if (!(match instanceof PacketMatch)) {
43 throw new IntentCompilationException(
44 "intent has unsupported type of match object: " + match
45 );
46 }
47
48 Path path = convertPath(intent.getPath());
49 PacketPathFlow flow = new PacketPathFlow(
50 getNextFlowId(),
51 (PacketMatch) match,
52 intent.getIngressPort().getPortNumber(),
53 path,
54 packActions(intent, intent.getEgressPort()),
55 0, 0
56 );
57 Intent compiled = new PathFlowIntent(getNextId(), flow);
58 return Arrays.asList(compiled);
59 }
60
61 /**
62 * Converts list of {@link LinkTuple LinkTuples} to a {@link Path}.
63 *
64 * @param tuples original list of {@link LinkTuple LinkTuples}
65 * @return converted {@link Path}
66 */
67 private Path convertPath(List<LinkTuple> tuples) {
68 // would like to use filter and transform, but Findbugs detects
69 // inconsistency of use of @Nullable annotation. Then, use of the
70 // transform is avoided.
71 // Ref: https://code.google.com/p/guava-libraries/issues/detail?id=1812
72 // TODO: replace with transform when the above issue is resolved
73 ImmutableList<LinkTuple> links = FluentIterable.from(tuples)
74 .filter(Predicates.notNull())
75 .toList();
76
77 Path path = new Path();
78 for (LinkTuple link: links) {
79 path.add(new FlowLink(link.getSrc(), link.getDst()));
80 }
81 return path;
82 }
83}