blob: ae91799720906d04feb9de66e64ae6e40f824625 [file] [log] [blame]
alshabib3460da12014-10-30 17:26:49 +01001
Brian O'Connorabafb502014-12-02 22:26:20 -08002package org.onosproject.cli.net;
alshabib5afcbd72014-10-30 16:28:40 +01003
alshabib3460da12014-10-30 17:26:49 +01004import com.fasterxml.jackson.databind.ObjectMapper;
5import com.fasterxml.jackson.databind.node.ArrayNode;
6import com.fasterxml.jackson.databind.node.ObjectNode;
7import com.google.common.collect.Lists;
alshabib5afcbd72014-10-30 16:28:40 +01008import com.google.common.collect.Sets;
9import org.apache.karaf.shell.commands.Argument;
10import org.apache.karaf.shell.commands.Command;
Brian O'Connorabafb502014-12-02 22:26:20 -080011import org.onosproject.cli.AbstractShellCommand;
12import org.onosproject.net.Device;
13import org.onosproject.net.PortNumber;
14import org.onosproject.net.device.DeviceService;
15import org.onosproject.net.flow.CompletedBatchOperation;
16import org.onosproject.net.flow.DefaultFlowRule;
17import org.onosproject.net.flow.DefaultTrafficSelector;
18import org.onosproject.net.flow.DefaultTrafficTreatment;
19import org.onosproject.net.flow.FlowRuleBatchEntry;
20import org.onosproject.net.flow.FlowRuleBatchOperation;
21import org.onosproject.net.flow.FlowRuleService;
22import org.onosproject.net.flow.TrafficSelector;
23import org.onosproject.net.flow.TrafficTreatment;
alshabib5afcbd72014-10-30 16:28:40 +010024import org.onlab.packet.MacAddress;
25
alshabib3460da12014-10-30 17:26:49 +010026import java.util.ArrayList;
alshabib5afcbd72014-10-30 16:28:40 +010027import java.util.Set;
28import java.util.concurrent.ExecutionException;
29import 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")
36public 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
alshabib3460da12014-10-30 17:26:49 +010042 @Argument(index = 1, name = "numOfRuns", description = "Number of iterations",
43 required = true, multiValued = false)
44 String numOfRuns = null;
alshabib5afcbd72014-10-30 16:28:40 +010045
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);
alshabib3460da12014-10-30 17:26:49 +010053 int num = Integer.parseInt(numOfRuns);
alshabib5afcbd72014-10-30 16:28:40 +010054
alshabib3460da12014-10-30 17:26:49 +010055 ArrayList<Long> results = Lists.newArrayList();
alshabib5afcbd72014-10-30 16:28:40 +010056 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 }
alshabib3460da12014-10-30 17:26:49 +010076 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));
alshabib5afcbd72014-10-30 16:28:40 +010092 }
alshabib3460da12014-10-30 17:26:49 +010093 if (outputJson()) {
94 print("%s", json(new ObjectMapper(), isSuccess, results));
95 } else {
96 printTime(isSuccess, results);
97 }
alshabib5afcbd72014-10-30 16:28:40 +010098
alshabib3460da12014-10-30 17:26:49 +010099
alshabib5afcbd72014-10-30 16:28:40 +0100100
101 }
102
alshabib3460da12014-10-30 17:26:49 +0100103 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
alshabib5afcbd72014-10-30 16:28:40 +0100121}