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