Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 1 | package net.onrc.onos.api.flowmanager; |
| 2 | |
Toshio Koide | 9aa4c0f | 2014-08-11 16:06:44 -0700 | [diff] [blame] | 3 | import static com.google.common.base.Preconditions.checkNotNull; |
Toshio Koide | c79d264 | 2014-08-19 01:09:08 -0700 | [diff] [blame^] | 4 | import static com.google.common.base.Preconditions.checkState; |
Toshio Koide | 9aa4c0f | 2014-08-11 16:06:44 -0700 | [diff] [blame] | 5 | |
Toshio Koide | c79d264 | 2014-08-19 01:09:08 -0700 | [diff] [blame^] | 6 | import java.util.Arrays; |
| 7 | import java.util.Iterator; |
| 8 | import java.util.LinkedList; |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 9 | import java.util.List; |
| 10 | |
Toshio Koide | d7d550c | 2014-08-21 16:08:55 -0700 | [diff] [blame] | 11 | import net.onrc.onos.api.flowmanager.FlowBatchOperation.Operator; |
Toshio Koide | c79d264 | 2014-08-19 01:09:08 -0700 | [diff] [blame^] | 12 | import net.onrc.onos.core.matchaction.MatchAction; |
Toshio Koide | d7d550c | 2014-08-21 16:08:55 -0700 | [diff] [blame] | 13 | import net.onrc.onos.core.matchaction.MatchActionIdGenerator; |
Toshio Koide | c79d264 | 2014-08-19 01:09:08 -0700 | [diff] [blame^] | 14 | import net.onrc.onos.core.matchaction.MatchActionOperationEntry; |
Ray Milkey | 42ae1b5 | 2014-08-15 16:37:06 -0700 | [diff] [blame] | 15 | import net.onrc.onos.core.matchaction.MatchActionOperations; |
Toshio Koide | d7d550c | 2014-08-21 16:08:55 -0700 | [diff] [blame] | 16 | import net.onrc.onos.core.matchaction.MatchActionOperationsIdGenerator; |
Toshio Koide | d8b077a | 2014-08-13 10:47:21 -0700 | [diff] [blame] | 17 | import net.onrc.onos.core.matchaction.action.Action; |
Toshio Koide | c79d264 | 2014-08-19 01:09:08 -0700 | [diff] [blame^] | 18 | import net.onrc.onos.core.matchaction.action.OutputAction; |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 19 | import net.onrc.onos.core.matchaction.match.PacketMatch; |
| 20 | import net.onrc.onos.core.util.PortNumber; |
Toshio Koide | c79d264 | 2014-08-19 01:09:08 -0700 | [diff] [blame^] | 21 | import net.onrc.onos.core.util.SwitchPort; |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 22 | |
| 23 | /** |
Toshio Koide | 7894ca0 | 2014-08-15 14:30:13 -0700 | [diff] [blame] | 24 | * Flow object representing a packet path. |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 25 | * <p> |
| 26 | * TODO: Think this: Do we need a bandwidth constraint? |
| 27 | */ |
| 28 | public class PacketPathFlow extends PathFlow { |
Toshio Koide | 9aa4c0f | 2014-08-11 16:06:44 -0700 | [diff] [blame] | 29 | private final PacketMatch match; |
| 30 | private final int hardTimeout; |
| 31 | private final int idleTimeout; |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Constructor. |
| 35 | * |
Toshio Koide | 9aa4c0f | 2014-08-11 16:06:44 -0700 | [diff] [blame] | 36 | * @param id ID for this new Flow object |
| 37 | * @param match the Match object at the source node of the path |
| 38 | * @param ingressPort the Ingress port number at the ingress edge node |
| 39 | * @param path the Path between ingress and egress edge node |
| 40 | * @param egressActions the list of Action objects at the egress edge node |
| 41 | * @param hardTimeout the hard-timeout value in seconds, or 0 for no timeout |
| 42 | * @param idleTimeout the idle-timeout value in seconds, or 0 for no timeout |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 43 | */ |
Toshio Koide | 9aa4c0f | 2014-08-11 16:06:44 -0700 | [diff] [blame] | 44 | public PacketPathFlow(FlowId id, |
| 45 | PacketMatch match, PortNumber ingressPort, Path path, |
| 46 | List<Action> egressActions, |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 47 | int hardTimeout, int idleTimeout) { |
Toshio Koide | 9aa4c0f | 2014-08-11 16:06:44 -0700 | [diff] [blame] | 48 | super(id, ingressPort, path, egressActions); |
| 49 | this.match = checkNotNull(match); |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 50 | this.hardTimeout = hardTimeout; |
| 51 | this.idleTimeout = idleTimeout; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Gets idle-timeout value. |
| 56 | * |
| 57 | * @return Idle-timeout value (seconds) |
| 58 | */ |
| 59 | public int getIdleTimeout() { |
| 60 | return idleTimeout; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Gets hard-timeout value. |
| 65 | * |
| 66 | * @return Hard-timeout value (seconds) |
| 67 | */ |
| 68 | public int getHardTimeout() { |
| 69 | return hardTimeout; |
| 70 | } |
| 71 | |
| 72 | @Override |
Toshio Koide | 5c5ca10 | 2014-08-19 00:49:52 -0700 | [diff] [blame] | 73 | public PacketMatch getMatch() { |
| 74 | return match; |
| 75 | } |
| 76 | |
| 77 | @Override |
Toshio Koide | d7d550c | 2014-08-21 16:08:55 -0700 | [diff] [blame] | 78 | public List<MatchActionOperations> compile(Operator op, |
| 79 | MatchActionIdGenerator maIdGenerator, |
| 80 | MatchActionOperationsIdGenerator maoIdGenerator) { |
Toshio Koide | c79d264 | 2014-08-19 01:09:08 -0700 | [diff] [blame^] | 81 | switch (op) { |
| 82 | case ADD: |
| 83 | return compileAddOperation(maIdGenerator, maoIdGenerator); |
| 84 | case REMOVE: |
| 85 | return compileRemoveOperation(maIdGenerator, maoIdGenerator); |
| 86 | default: |
| 87 | throw new UnsupportedOperationException("Unknown operation."); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Creates the next {@link MatchAction} object using iterators. |
| 93 | * |
| 94 | * @param portIterator the iterator for {@link SwitchPort} objects |
| 95 | * @param actionsIterator the iterator for the lists of {@link Action} |
| 96 | * @param maIdGenerator the ID generator of {@link MatchAction} |
| 97 | * @return {@link MatchAction} object based on the specified iterators |
| 98 | */ |
| 99 | private MatchAction createNextMatchAction(Iterator<SwitchPort> portIterator, |
| 100 | Iterator<List<Action>> actionsIterator, |
| 101 | MatchActionIdGenerator maIdGenerator) { |
| 102 | if (portIterator == null || actionsIterator == null || |
| 103 | !portIterator.hasNext() || !actionsIterator.hasNext()) { |
| 104 | return null; |
| 105 | } |
| 106 | |
| 107 | // TODO: Update this after merging the new MatchAction API. |
| 108 | return new MatchAction( |
| 109 | maIdGenerator.getNewId(), |
| 110 | portIterator.next(), |
| 111 | getMatch(), actionsIterator.next()); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Generates the list of {@link MatchActionOperations} objects with |
| 116 | * add-operation. |
| 117 | * |
| 118 | * @return the list of {@link MatchActionOperations} objects |
| 119 | */ |
| 120 | private List<MatchActionOperations> compileAddOperation( |
| 121 | MatchActionIdGenerator maIdGenerator, |
| 122 | MatchActionOperationsIdGenerator maoIdGenerator) { |
| 123 | Path path = checkNotNull(getPath()); |
| 124 | checkState(path.size() > 0, "Path object has no link."); |
| 125 | |
| 126 | // Preparing each actions and ingress port for each switch |
| 127 | List<List<Action>> actionsList = new LinkedList<>(); |
| 128 | List<SwitchPort> portList = new LinkedList<>(); |
| 129 | for (FlowLink link : path) { |
| 130 | portList.add(link.getDstSwitchPort()); |
| 131 | actionsList.add(Arrays.asList( |
| 132 | (Action) new OutputAction(link.getSrcPortNumber()))); |
| 133 | } |
| 134 | |
| 135 | // The head switch's ingress port |
| 136 | portList.add(0, new SwitchPort(path.getSrcDpid(), getIngressPortNumber())); |
| 137 | |
| 138 | // The tail switch's action |
| 139 | actionsList.add(getEgressActions()); |
| 140 | |
| 141 | Iterator<SwitchPort> portIterator = portList.iterator(); |
| 142 | Iterator<List<Action>> actionsIterator = actionsList.iterator(); |
| 143 | |
| 144 | // Creates the second phase operation |
| 145 | // using the head switch's match action |
| 146 | MatchAction headMatchAction = createNextMatchAction(portIterator, |
| 147 | actionsIterator, maIdGenerator); |
| 148 | if (headMatchAction == null) { |
| 149 | return null; |
| 150 | } |
| 151 | MatchActionOperations secondOp = new MatchActionOperations( |
| 152 | maoIdGenerator.getNewId()); |
| 153 | secondOp.addOperation(new MatchActionOperationEntry( |
| 154 | MatchActionOperations.Operator.ADD, headMatchAction)); |
| 155 | |
| 156 | // Creates the first phase operation |
| 157 | // using the remaining switches' match actions |
| 158 | MatchActionOperations firstOp = new MatchActionOperations( |
| 159 | maoIdGenerator.getNewId()); |
| 160 | MatchAction ma; |
| 161 | while ((ma = createNextMatchAction(portIterator, actionsIterator, maIdGenerator)) != null) { |
| 162 | firstOp.addOperation(new MatchActionOperationEntry( |
| 163 | MatchActionOperations.Operator.ADD, ma)); |
| 164 | } |
| 165 | |
| 166 | return Arrays.asList(firstOp, secondOp); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Generates the list of {@link MatchActionOperations} objects with |
| 171 | * remote-operation. |
| 172 | * |
| 173 | * @return the list of {@link MatchActionOperations} objects |
| 174 | */ |
| 175 | private List<MatchActionOperations> compileRemoveOperation( |
| 176 | MatchActionIdGenerator maIdGenerator, |
| 177 | MatchActionOperationsIdGenerator maoIdGenerator) { |
| 178 | // TODO implement it |
| 179 | throw new UnsupportedOperationException( |
| 180 | "REMOVE operation is not implemented yet."); |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 181 | } |
| 182 | } |