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 | a0c9e01 | 2014-08-20 16:29:28 -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 | a0c9e01 | 2014-08-20 16:29:28 -0700 | [diff] [blame] | 6 | import java.util.Arrays; |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 7 | import java.util.Collection; |
Toshio Koide | a0c9e01 | 2014-08-20 16:29:28 -0700 | [diff] [blame] | 8 | import java.util.HashMap; |
| 9 | import java.util.HashSet; |
Toshio Koide | 2c67a2d | 2014-08-27 11:30:56 -0700 | [diff] [blame] | 10 | import java.util.LinkedList; |
Toshio Koide | 9aa4c0f | 2014-08-11 16:06:44 -0700 | [diff] [blame] | 11 | import java.util.List; |
Toshio Koide | a0c9e01 | 2014-08-20 16:29:28 -0700 | [diff] [blame] | 12 | import java.util.Map; |
| 13 | import java.util.Map.Entry; |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 14 | import java.util.Set; |
| 15 | |
Toshio Koide | 2c67a2d | 2014-08-27 11:30:56 -0700 | [diff] [blame] | 16 | import com.google.common.collect.ImmutableList; |
| 17 | import com.google.common.collect.ImmutableSet; |
| 18 | |
Toshio Koide | d7d550c | 2014-08-21 16:08:55 -0700 | [diff] [blame] | 19 | import net.onrc.onos.api.flowmanager.FlowBatchOperation.Operator; |
Toshio Koide | a0c9e01 | 2014-08-20 16:29:28 -0700 | [diff] [blame] | 20 | import net.onrc.onos.core.matchaction.MatchAction; |
Toshio Koide | d7d550c | 2014-08-21 16:08:55 -0700 | [diff] [blame] | 21 | import net.onrc.onos.core.matchaction.MatchActionIdGenerator; |
Toshio Koide | a0c9e01 | 2014-08-20 16:29:28 -0700 | [diff] [blame] | 22 | import net.onrc.onos.core.matchaction.MatchActionOperationEntry; |
Ray Milkey | 42ae1b5 | 2014-08-15 16:37:06 -0700 | [diff] [blame] | 23 | import net.onrc.onos.core.matchaction.MatchActionOperations; |
Toshio Koide | d7d550c | 2014-08-21 16:08:55 -0700 | [diff] [blame] | 24 | import net.onrc.onos.core.matchaction.MatchActionOperationsIdGenerator; |
Toshio Koide | 9aa4c0f | 2014-08-11 16:06:44 -0700 | [diff] [blame] | 25 | import net.onrc.onos.core.matchaction.action.Action; |
Toshio Koide | a0c9e01 | 2014-08-20 16:29:28 -0700 | [diff] [blame] | 26 | import net.onrc.onos.core.matchaction.action.OutputAction; |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 27 | import net.onrc.onos.core.matchaction.match.PacketMatch; |
Toshio Koide | a0c9e01 | 2014-08-20 16:29:28 -0700 | [diff] [blame] | 28 | import net.onrc.onos.core.util.Dpid; |
| 29 | import net.onrc.onos.core.util.PortNumber; |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 30 | import net.onrc.onos.core.util.SwitchPort; |
| 31 | |
| 32 | /** |
Toshio Koide | a0c9e01 | 2014-08-20 16:29:28 -0700 | [diff] [blame] | 33 | * A {@link Flow} object expressing the multipoints-to-point tree flow for the |
| 34 | * packet layer. |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 35 | * <p> |
Toshio Koide | a0c9e01 | 2014-08-20 16:29:28 -0700 | [diff] [blame] | 36 | * NOTE: This class might generate the {@link MatchAction} operations which |
| 37 | * includes the MAC address modifications or other the label-switching-like |
| 38 | * schemes. |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 39 | */ |
Toshio Koide | 9aa4c0f | 2014-08-11 16:06:44 -0700 | [diff] [blame] | 40 | public class SingleDstTreeFlow extends Flow { |
| 41 | private final PacketMatch match; |
| 42 | private final Set<SwitchPort> ingressPorts; |
| 43 | private final Tree tree; |
Toshio Koide | a0c9e01 | 2014-08-20 16:29:28 -0700 | [diff] [blame] | 44 | private final List<Action> egressActions; |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 45 | |
| 46 | /** |
Toshio Koide | 2c67a2d | 2014-08-27 11:30:56 -0700 | [diff] [blame] | 47 | * Default constructor for Kryo deserialization. |
| 48 | */ |
| 49 | @Deprecated |
| 50 | protected SingleDstTreeFlow() { |
| 51 | match = null; |
| 52 | ingressPorts = null; |
| 53 | tree = null; |
| 54 | egressActions = null; |
| 55 | } |
| 56 | |
| 57 | /** |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 58 | * Creates new instance using Tree object. |
| 59 | * |
Toshio Koide | 9aa4c0f | 2014-08-11 16:06:44 -0700 | [diff] [blame] | 60 | * @param id ID for this object |
| 61 | * @param match the traffic filter for the tree |
| 62 | * @param ingressPorts the set of ingress ports of the tree |
| 63 | * @param tree the Tree object specifying tree topology for this object |
Toshio Koide | a0c9e01 | 2014-08-20 16:29:28 -0700 | [diff] [blame] | 64 | * @param egressActions the list of {@link Action} objects to be executed at |
| 65 | * the egress edge switch |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 66 | */ |
Toshio Koide | 9aa4c0f | 2014-08-11 16:06:44 -0700 | [diff] [blame] | 67 | public SingleDstTreeFlow(FlowId id, PacketMatch match, |
Toshio Koide | a0c9e01 | 2014-08-20 16:29:28 -0700 | [diff] [blame] | 68 | Collection<SwitchPort> ingressPorts, Tree tree, List<Action> egressActions) { |
Toshio Koide | 9aa4c0f | 2014-08-11 16:06:44 -0700 | [diff] [blame] | 69 | super(id); |
| 70 | this.match = checkNotNull(match); |
Toshio Koide | 2c67a2d | 2014-08-27 11:30:56 -0700 | [diff] [blame] | 71 | this.ingressPorts = new HashSet<>(checkNotNull(ingressPorts)); |
Toshio Koide | 9aa4c0f | 2014-08-11 16:06:44 -0700 | [diff] [blame] | 72 | this.tree = checkNotNull(tree); |
Toshio Koide | 2c67a2d | 2014-08-27 11:30:56 -0700 | [diff] [blame] | 73 | this.egressActions = new LinkedList<>(checkNotNull(egressActions)); |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 74 | |
| 75 | // TODO: check if the tree is a MP2P tree. |
Toshio Koide | a0c9e01 | 2014-08-20 16:29:28 -0700 | [diff] [blame] | 76 | // TODO: check consistency between ingressPorts and tree topology. |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 79 | /** |
| 80 | * Gets the ingress ports of the tree. |
| 81 | * |
Toshio Koide | 9aa4c0f | 2014-08-11 16:06:44 -0700 | [diff] [blame] | 82 | * @return the ingress ports of the tree |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 83 | */ |
| 84 | public Collection<SwitchPort> getIngressPorts() { |
Toshio Koide | 2c67a2d | 2014-08-27 11:30:56 -0700 | [diff] [blame] | 85 | return ImmutableSet.copyOf(ingressPorts); |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Gets the tree. |
| 90 | * |
Toshio Koide | 9aa4c0f | 2014-08-11 16:06:44 -0700 | [diff] [blame] | 91 | * @return the tree object |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 92 | */ |
| 93 | public Tree getTree() { |
| 94 | return tree; |
| 95 | } |
| 96 | |
| 97 | /** |
Toshio Koide | 9aa4c0f | 2014-08-11 16:06:44 -0700 | [diff] [blame] | 98 | * Gets the list of actions at the egress edge switch. |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 99 | * |
Toshio Koide | 9aa4c0f | 2014-08-11 16:06:44 -0700 | [diff] [blame] | 100 | * @return the list of actions at the egress edge switch |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 101 | */ |
Toshio Koide | 9aa4c0f | 2014-08-11 16:06:44 -0700 | [diff] [blame] | 102 | public List<Action> getEgressActions() { |
Toshio Koide | 2c67a2d | 2014-08-27 11:30:56 -0700 | [diff] [blame] | 103 | return ImmutableList.copyOf(egressActions); |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 104 | } |
Toshio Koide | 5c5ca10 | 2014-08-19 00:49:52 -0700 | [diff] [blame] | 105 | |
| 106 | @Override |
| 107 | public PacketMatch getMatch() { |
| 108 | return match; |
| 109 | } |
| 110 | |
| 111 | @Override |
Toshio Koide | d7d550c | 2014-08-21 16:08:55 -0700 | [diff] [blame] | 112 | public List<MatchActionOperations> compile(Operator op, |
| 113 | MatchActionIdGenerator maIdGenerator, |
| 114 | MatchActionOperationsIdGenerator maoIdGenerator) { |
Toshio Koide | a0c9e01 | 2014-08-20 16:29:28 -0700 | [diff] [blame] | 115 | switch (op) { |
| 116 | case ADD: |
| 117 | return compileAddOperation(maIdGenerator, maoIdGenerator); |
| 118 | case REMOVE: |
| 119 | return compileRemoveOperation(); |
| 120 | default: |
| 121 | throw new UnsupportedOperationException("Unknown operation."); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | private MatchAction createMatchAction(SwitchPort port, List<Action> actions, |
| 126 | MatchActionIdGenerator maIdGenerator) { |
| 127 | checkNotNull(port); |
| 128 | checkNotNull(actions); |
| 129 | |
| 130 | return new MatchAction(maIdGenerator.getNewId(), port, getMatch(), actions); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Generates MatchAactionOperations at inner ports and at the egress switch. |
| 135 | * |
| 136 | * @param egressSwitch the egress switch of the tree |
| 137 | * @param inPorts a map of a set of incoming ports on each switch in the |
| 138 | * tree |
| 139 | * @param outPorts a map of outgoing port on each switch in the tree |
| 140 | * @param maIdGenerator ID generator for MatchAction objects |
| 141 | * @param maoIdGenerator ID generator for MatchActionOperations objects |
| 142 | * @return the operations at inner ports and egress switch |
| 143 | */ |
| 144 | private MatchActionOperations generateFirstAddOperations( |
| 145 | Dpid egressSwitch, |
| 146 | Map<Dpid, Set<PortNumber>> inPorts, |
| 147 | Map<Dpid, PortNumber> outPorts, |
| 148 | MatchActionIdGenerator maIdGenerator, |
| 149 | MatchActionOperationsIdGenerator maoIdGenerator) { |
| 150 | MatchActionOperations firstOps = |
| 151 | new MatchActionOperations(maoIdGenerator.getNewId()); |
| 152 | for (Entry<Dpid, Set<PortNumber>> innerSw : inPorts.entrySet()) { |
| 153 | for (PortNumber innerPortNumber : innerSw.getValue()) { |
| 154 | SwitchPort innerPort = new SwitchPort(innerSw.getKey(), innerPortNumber); |
| 155 | MatchAction ma; |
| 156 | if (innerPort.getDpid().equals(egressSwitch)) { |
| 157 | ma = createMatchAction(innerPort, getEgressActions(), maIdGenerator); |
| 158 | } else { |
| 159 | PortNumber outputPortNumber = checkNotNull( |
| 160 | outPorts.get(innerPort.getDpid()), |
| 161 | String.format("The tree has no output port at %s", |
| 162 | innerPort.getDpid())); |
| 163 | ma = createMatchAction(innerPort, |
| 164 | Arrays.asList((Action) new OutputAction(outputPortNumber)), |
| 165 | maIdGenerator); |
| 166 | } |
| 167 | firstOps.addOperation(new MatchActionOperationEntry( |
| 168 | MatchActionOperations.Operator.ADD, ma)); |
| 169 | } |
| 170 | } |
| 171 | return firstOps; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Generates MatchActionOperations for ingress switches in the tree. |
| 176 | * |
| 177 | * @param egressSwitch the egress switch of the tree |
| 178 | * @param outPorts a map of outgoing port on each switch in the tree |
| 179 | * @param maIdGenerator ID generator for MatchAction objects |
| 180 | * @param maoIdGenerator ID generator for MatchActionOperations objects |
| 181 | * @return operations at ingress switches in the tree |
| 182 | */ |
| 183 | private MatchActionOperations generateSecondAddOperations( |
| 184 | Dpid egressSwitch, |
| 185 | Map<Dpid, PortNumber> outPorts, |
| 186 | MatchActionIdGenerator maIdGenerator, |
| 187 | MatchActionOperationsIdGenerator maoIdGenerator) { |
| 188 | MatchActionOperations secondOps = |
| 189 | new MatchActionOperations(maoIdGenerator.getNewId()); |
| 190 | for (SwitchPort port : getIngressPorts()) { |
| 191 | PortNumber outputPort = outPorts.get(port.getDpid()); |
| 192 | if (outputPort == null) { |
| 193 | if (port.getDpid().equals(egressSwitch)) { |
| 194 | MatchAction ma = createMatchAction( |
| 195 | port, getEgressActions(), maIdGenerator); |
| 196 | secondOps.addOperation(new MatchActionOperationEntry( |
| 197 | MatchActionOperations.Operator.ADD, ma)); |
| 198 | } else { |
| 199 | throw new IllegalStateException(String.format( |
| 200 | "The switch %s specified as one of ingress ports " |
| 201 | + "does not have path to the egress switch.", |
| 202 | port.getDpid())); |
| 203 | } |
| 204 | } else { |
| 205 | MatchAction ma = createMatchAction(port, |
| 206 | Arrays.asList((Action) new OutputAction(outputPort)), |
| 207 | maIdGenerator); |
| 208 | secondOps.addOperation(new MatchActionOperationEntry( |
| 209 | MatchActionOperations.Operator.ADD, ma)); |
| 210 | } |
| 211 | } |
| 212 | return secondOps; |
| 213 | } |
| 214 | |
| 215 | private List<MatchActionOperations> compileAddOperation( |
| 216 | MatchActionIdGenerator maIdGenerator, |
| 217 | MatchActionOperationsIdGenerator maoIdGenerator) { |
| 218 | checkNotNull(tree); |
| 219 | checkState(tree.size() > 0, "Tree object has no link."); |
| 220 | |
| 221 | // TODO: check consistency of the tree topology |
| 222 | |
| 223 | // collect input ports and output ports checking consistency |
| 224 | Map<Dpid, PortNumber> outPorts = new HashMap<>(); |
| 225 | Map<Dpid, Set<PortNumber>> inPorts = new HashMap<>(); |
| 226 | for (FlowLink link : tree) { |
| 227 | SwitchPort srcPort = link.getSrcSwitchPort(); |
| 228 | if (outPorts.containsKey(srcPort.getDpid())) { |
| 229 | throw new IllegalStateException( |
| 230 | String.format("Dpid:%s has multiple output ports.", |
| 231 | srcPort.getDpid())); |
| 232 | } |
| 233 | outPorts.put(srcPort.getDpid(), srcPort.getPortNumber()); |
| 234 | |
| 235 | SwitchPort dstPort = link.getDstSwitchPort(); |
| 236 | Set<PortNumber> inPortNumbers = inPorts.get(dstPort.getDpid()); |
| 237 | if (inPortNumbers == null) { |
| 238 | inPortNumbers = new HashSet<>(); |
| 239 | } |
| 240 | inPortNumbers.add(dstPort.getPortNumber()); |
| 241 | inPorts.put(dstPort.getDpid(), inPortNumbers); |
| 242 | } |
| 243 | |
| 244 | // find the egress switch |
| 245 | Set<Dpid> egressSwitches = new HashSet<>(inPorts.keySet()); |
| 246 | egressSwitches.removeAll(outPorts.keySet()); |
| 247 | checkState(egressSwitches.size() == 1, |
| 248 | "The specified tree is not a single destination tree."); |
| 249 | Dpid egressSwitch = egressSwitches.iterator().next(); |
| 250 | |
| 251 | MatchActionOperations firstOps = generateFirstAddOperations( |
| 252 | egressSwitch, inPorts, outPorts, maIdGenerator, maoIdGenerator); |
| 253 | checkState(firstOps.size() > 0, |
| 254 | "No operations found for the first set of operations."); |
| 255 | |
| 256 | MatchActionOperations secondOps = generateSecondAddOperations( |
| 257 | egressSwitch, outPorts, maIdGenerator, maoIdGenerator); |
| 258 | checkState(secondOps.size() > 0, |
| 259 | "No operations found for the second set of operations."); |
| 260 | |
| 261 | return Arrays.asList(firstOps, secondOps); |
| 262 | } |
| 263 | |
| 264 | private List<MatchActionOperations> compileRemoveOperation() { |
Toshio Koide | 5c5ca10 | 2014-08-19 00:49:52 -0700 | [diff] [blame] | 265 | // TODO Auto-generated method stub |
Toshio Koide | a0c9e01 | 2014-08-20 16:29:28 -0700 | [diff] [blame] | 266 | throw new UnsupportedOperationException( |
| 267 | "REMOVE operation is not implemented yet."); |
Toshio Koide | 5c5ca10 | 2014-08-19 00:49:52 -0700 | [diff] [blame] | 268 | } |
Toshio Koide | a03915e | 2014-07-01 18:39:52 -0700 | [diff] [blame] | 269 | } |