Sho SHIMIZU | f7b693e | 2014-08-15 16:17:13 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.newintent; |
| 2 | |
| 3 | import com.google.common.base.Predicates; |
| 4 | import com.google.common.collect.FluentIterable; |
| 5 | import com.google.common.collect.ImmutableList; |
Sho SHIMIZU | 7cd8a42 | 2014-08-27 16:05:21 -0700 | [diff] [blame] | 6 | import net.onrc.onos.api.flowmanager.FlowId; |
Sho SHIMIZU | f7b693e | 2014-08-15 16:17:13 -0700 | [diff] [blame] | 7 | import net.onrc.onos.api.flowmanager.FlowLink; |
| 8 | import net.onrc.onos.api.flowmanager.PacketPathFlow; |
| 9 | import net.onrc.onos.api.flowmanager.Path; |
| 10 | import net.onrc.onos.api.newintent.Intent; |
Sho SHIMIZU | 7cd8a42 | 2014-08-27 16:05:21 -0700 | [diff] [blame] | 11 | import net.onrc.onos.api.newintent.IntentId; |
Sho SHIMIZU | f7b693e | 2014-08-15 16:17:13 -0700 | [diff] [blame] | 12 | import net.onrc.onos.api.newintent.PathIntent; |
| 13 | import net.onrc.onos.core.matchaction.match.Match; |
| 14 | import net.onrc.onos.core.matchaction.match.PacketMatch; |
Sho SHIMIZU | 7cd8a42 | 2014-08-27 16:05:21 -0700 | [diff] [blame] | 15 | import net.onrc.onos.core.util.IdGenerator; |
Sho SHIMIZU | f7b693e | 2014-08-15 16:17:13 -0700 | [diff] [blame] | 16 | import net.onrc.onos.core.util.LinkTuple; |
| 17 | |
| 18 | import java.util.Arrays; |
| 19 | import java.util.List; |
| 20 | |
| 21 | /** |
| 22 | * An intent compiler for {@link PathIntent}. |
| 23 | */ |
| 24 | public 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 SHIMIZU | 7cd8a42 | 2014-08-27 16:05:21 -0700 | [diff] [blame] | 34 | public PathIntentCompiler(IdGenerator<IntentId> intentIdGenerator, |
| 35 | IdGenerator<FlowId> flowIdGenerator) { |
Sho SHIMIZU | f7b693e | 2014-08-15 16:17:13 -0700 | [diff] [blame] | 36 | 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(); |
Yuta HIGUCHI | 04f16a6 | 2014-09-02 15:00:01 -0700 | [diff] [blame] | 78 | for (LinkTuple link : links) { |
Sho SHIMIZU | f7b693e | 2014-08-15 16:17:13 -0700 | [diff] [blame] | 79 | path.add(new FlowLink(link.getSrc(), link.getDst())); |
| 80 | } |
| 81 | return path; |
| 82 | } |
| 83 | } |