Sho SHIMIZU | f7b693e | 2014-08-15 16:17:13 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.newintent; |
| 2 | |
Sho SHIMIZU | 7cd8a42 | 2014-08-27 16:05:21 -0700 | [diff] [blame] | 3 | import net.onrc.onos.api.flowmanager.FlowId; |
Sho SHIMIZU | f7b693e | 2014-08-15 16:17:13 -0700 | [diff] [blame] | 4 | import net.onrc.onos.api.flowmanager.SingleDstTreeFlow; |
| 5 | import net.onrc.onos.api.flowmanager.Tree; |
| 6 | import net.onrc.onos.api.newintent.Intent; |
Sho SHIMIZU | 7cd8a42 | 2014-08-27 16:05:21 -0700 | [diff] [blame] | 7 | import net.onrc.onos.api.newintent.IntentId; |
Sho SHIMIZU | f7b693e | 2014-08-15 16:17:13 -0700 | [diff] [blame] | 8 | import net.onrc.onos.api.newintent.MultiPointToSinglePointIntent; |
| 9 | import net.onrc.onos.core.intent.ConstrainedBFSTree; |
| 10 | import net.onrc.onos.core.intent.Path; |
| 11 | import net.onrc.onos.core.matchaction.match.Match; |
| 12 | import net.onrc.onos.core.matchaction.match.PacketMatch; |
| 13 | import net.onrc.onos.core.topology.BaseTopology; |
| 14 | import net.onrc.onos.core.topology.ITopologyService; |
| 15 | import net.onrc.onos.core.topology.Switch; |
Sho SHIMIZU | 7cd8a42 | 2014-08-27 16:05:21 -0700 | [diff] [blame] | 16 | import net.onrc.onos.core.util.IdGenerator; |
Sho SHIMIZU | f7b693e | 2014-08-15 16:17:13 -0700 | [diff] [blame] | 17 | import net.onrc.onos.core.util.SwitchPort; |
| 18 | |
| 19 | import java.util.Arrays; |
| 20 | import java.util.List; |
| 21 | import java.util.Set; |
| 22 | |
| 23 | import static com.google.common.base.Preconditions.checkNotNull; |
| 24 | import static net.onrc.onos.core.newintent.PointToPointIntentCompiler.convertPath; |
| 25 | |
| 26 | /** |
| 27 | * An intent compiler for {@link MultiPointToSinglePointIntent}. |
| 28 | */ |
| 29 | public class MultiPointToSinglePointIntentCompiler |
| 30 | extends AbstractFlowGeneratingIntentCompiler<MultiPointToSinglePointIntent> { |
| 31 | |
| 32 | private final ITopologyService topologyService; |
| 33 | |
| 34 | /** |
| 35 | * Constructs an intent compiler for {@link MultiPointToSinglePointIntent}. |
| 36 | * |
| 37 | * @param intentIdGenerator intent ID generator |
| 38 | * @param flowIdGenerator flow ID generator |
| 39 | * @param topologyService topology service |
| 40 | */ |
Sho SHIMIZU | 7cd8a42 | 2014-08-27 16:05:21 -0700 | [diff] [blame] | 41 | public MultiPointToSinglePointIntentCompiler(IdGenerator<IntentId> intentIdGenerator, |
| 42 | IdGenerator<FlowId> flowIdGenerator, |
Sho SHIMIZU | f7b693e | 2014-08-15 16:17:13 -0700 | [diff] [blame] | 43 | ITopologyService topologyService) { |
| 44 | super(intentIdGenerator, flowIdGenerator); |
| 45 | this.topologyService = checkNotNull(topologyService); |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public List<Intent> compile(MultiPointToSinglePointIntent intent) { |
| 50 | Match match = intent.getMatch(); |
| 51 | if (!(match instanceof PacketMatch)) { |
| 52 | throw new IntentCompilationException( |
| 53 | "intent has unsupported type of match object: " + match |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | SingleDstTreeFlow treeFlow = new SingleDstTreeFlow( |
| 58 | getNextFlowId(), |
| 59 | (PacketMatch) intent.getMatch(), // down-cast, but it is safe due to the guard above |
| 60 | intent.getIngressPorts(), |
| 61 | calculateTree(intent.getIngressPorts(), intent.getEgressPort()), |
| 62 | packActions(intent, intent.getEgressPort()) |
| 63 | ); |
| 64 | Intent compiled = new SingleDstTreeFlowIntent(getNextId(), treeFlow); |
| 65 | return Arrays.asList(compiled); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Calculates a tree with the specified ingress ports and egress port. |
| 70 | * |
| 71 | * {@link PathNotFoundException} is thrown when no tree found or |
| 72 | * the specified egress port is not found in the topology. |
| 73 | * |
| 74 | * @param ingressPorts ingress ports |
| 75 | * @param egressPort egress port |
| 76 | * @return tree |
| 77 | * @throws PathNotFoundException if the specified egress switch is not |
| 78 | * found or no tree is found. |
| 79 | */ |
| 80 | private Tree calculateTree(Set<SwitchPort> ingressPorts, SwitchPort egressPort) { |
| 81 | BaseTopology topology = topologyService.getTopology(); |
| 82 | Switch egressSwitch = topology.getSwitch(egressPort.getDpid()); |
| 83 | if (egressSwitch == null) { |
| 84 | throw new PathNotFoundException("destination switch not found: " + egressPort.getDpid()); |
| 85 | } |
| 86 | |
| 87 | ConstrainedBFSTree bfs = new ConstrainedBFSTree(egressSwitch); |
| 88 | Tree tree = new Tree(); |
| 89 | |
Yuta HIGUCHI | 04f16a6 | 2014-09-02 15:00:01 -0700 | [diff] [blame] | 90 | for (SwitchPort ingressPort : ingressPorts) { |
Sho SHIMIZU | f7b693e | 2014-08-15 16:17:13 -0700 | [diff] [blame] | 91 | Switch ingressSwitch = topology.getSwitch(ingressPort.getDpid()); |
| 92 | if (ingressSwitch == null) { |
| 93 | continue; |
| 94 | } |
| 95 | |
| 96 | Path path = bfs.getPath(ingressSwitch); |
| 97 | if (path.isEmpty()) { |
| 98 | continue; |
| 99 | } |
| 100 | |
| 101 | tree.addAll(convertPath(path)); |
| 102 | } |
| 103 | |
| 104 | if (tree.isEmpty()) { |
| 105 | throw new PathNotFoundException( |
| 106 | String.format("No tree found (ingress: %s, egress: %s", ingressPorts, egressPort) |
| 107 | ); |
| 108 | } |
| 109 | |
| 110 | return tree; |
| 111 | } |
| 112 | } |