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; |
| 6 | import net.onrc.onos.api.flowmanager.FlowIdGenerator; |
| 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; |
| 11 | import net.onrc.onos.api.newintent.IntentIdGenerator; |
| 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; |
| 15 | import net.onrc.onos.core.util.LinkTuple; |
| 16 | |
| 17 | import java.util.Arrays; |
| 18 | import java.util.List; |
| 19 | |
| 20 | /** |
| 21 | * An intent compiler for {@link PathIntent}. |
| 22 | */ |
| 23 | public 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 | } |