alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 1 | |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 2 | package org.onlab.onos.cli.net; |
| 3 | |
alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 4 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 6 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 7 | import com.google.common.collect.Lists; |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 8 | import com.google.common.collect.Sets; |
| 9 | import org.apache.karaf.shell.commands.Argument; |
| 10 | import org.apache.karaf.shell.commands.Command; |
| 11 | import org.onlab.onos.cli.AbstractShellCommand; |
| 12 | import org.onlab.onos.net.Device; |
| 13 | import org.onlab.onos.net.PortNumber; |
| 14 | import org.onlab.onos.net.device.DeviceService; |
| 15 | import org.onlab.onos.net.flow.CompletedBatchOperation; |
| 16 | import org.onlab.onos.net.flow.DefaultFlowRule; |
| 17 | import org.onlab.onos.net.flow.DefaultTrafficSelector; |
| 18 | import org.onlab.onos.net.flow.DefaultTrafficTreatment; |
| 19 | import org.onlab.onos.net.flow.FlowRuleBatchEntry; |
| 20 | import org.onlab.onos.net.flow.FlowRuleBatchOperation; |
| 21 | import org.onlab.onos.net.flow.FlowRuleService; |
| 22 | import org.onlab.onos.net.flow.TrafficSelector; |
| 23 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 24 | import org.onlab.packet.MacAddress; |
| 25 | |
alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 26 | import java.util.ArrayList; |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 27 | import java.util.Set; |
| 28 | import java.util.concurrent.ExecutionException; |
| 29 | import java.util.concurrent.Future; |
| 30 | |
| 31 | /** |
| 32 | * Installs many many flows. |
| 33 | */ |
| 34 | @Command(scope = "onos", name = "add-flows", |
| 35 | description = "Installs a flow rules") |
| 36 | public class AddFlowsCommand extends AbstractShellCommand { |
| 37 | |
| 38 | @Argument(index = 0, name = "flowPerDevice", description = "Number of flows to add per device", |
| 39 | required = true, multiValued = false) |
| 40 | String flows = null; |
| 41 | |
alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 42 | @Argument(index = 1, name = "numOfRuns", description = "Number of iterations", |
| 43 | required = true, multiValued = false) |
| 44 | String numOfRuns = null; |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 45 | |
| 46 | @Override |
| 47 | protected void execute() { |
| 48 | |
| 49 | FlowRuleService flowService = get(FlowRuleService.class); |
| 50 | DeviceService deviceService = get(DeviceService.class); |
| 51 | |
| 52 | int flowsPerDevice = Integer.parseInt(flows); |
alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 53 | int num = Integer.parseInt(numOfRuns); |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 54 | |
alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 55 | ArrayList<Long> results = Lists.newArrayList(); |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 56 | Iterable<Device> devices = deviceService.getDevices(); |
| 57 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 58 | .setOutput(PortNumber.portNumber(1)).build(); |
| 59 | TrafficSelector.Builder sbuilder; |
| 60 | Set<FlowRuleBatchEntry> rules = Sets.newHashSet(); |
| 61 | Set<FlowRuleBatchEntry> remove = Sets.newHashSet(); |
| 62 | for (Device d : devices) { |
| 63 | for (int i = 0; i < flowsPerDevice; i++) { |
| 64 | sbuilder = DefaultTrafficSelector.builder(); |
| 65 | sbuilder.matchEthSrc(MacAddress.valueOf(i)) |
| 66 | .matchEthDst(MacAddress.valueOf(Integer.MAX_VALUE - i)); |
| 67 | rules.add(new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.ADD, |
| 68 | new DefaultFlowRule(d.id(), sbuilder.build(), treatment, |
| 69 | 100, (long) 0, 10, false))); |
| 70 | remove.add(new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.REMOVE, |
| 71 | new DefaultFlowRule(d.id(), sbuilder.build(), treatment, |
| 72 | 100, (long) 0, 10, false))); |
| 73 | |
| 74 | } |
| 75 | } |
alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 76 | boolean isSuccess = true; |
| 77 | for (int i = 0; i < num; i++) { |
| 78 | long startTime = System.currentTimeMillis(); |
| 79 | Future<CompletedBatchOperation> op = flowService.applyBatch( |
| 80 | new FlowRuleBatchOperation(rules)); |
| 81 | try { |
| 82 | isSuccess &= op.get().isSuccess(); |
| 83 | } catch (InterruptedException e) { |
| 84 | e.printStackTrace(); |
| 85 | } catch (ExecutionException e) { |
| 86 | e.printStackTrace(); |
| 87 | } |
| 88 | long endTime = System.currentTimeMillis(); |
| 89 | results.add(endTime - startTime); |
| 90 | flowService.applyBatch( |
| 91 | new FlowRuleBatchOperation(remove)); |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 92 | } |
alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 93 | if (outputJson()) { |
| 94 | print("%s", json(new ObjectMapper(), isSuccess, results)); |
| 95 | } else { |
| 96 | printTime(isSuccess, results); |
| 97 | } |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 98 | |
alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 99 | |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 100 | |
| 101 | } |
| 102 | |
alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 103 | private Object json(ObjectMapper mapper, boolean isSuccess, ArrayList<Long> elapsed) { |
| 104 | ObjectNode result = mapper.createObjectNode(); |
| 105 | result.put("Success", isSuccess); |
| 106 | ArrayNode node = result.putArray("elapsed-time"); |
| 107 | for (Long v : elapsed) { |
| 108 | node.add(v); |
| 109 | } |
| 110 | return result; |
| 111 | } |
| 112 | |
| 113 | private void printTime(boolean isSuccess, ArrayList<Long> elapsed) { |
| 114 | print("Run is %s.", isSuccess ? "success" : "failure"); |
| 115 | for (int i = 0; i < elapsed.size(); i++) { |
| 116 | print(" Run %s : %s", i, elapsed.get(i)); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 121 | } |