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