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