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