alshabib

polished add-flow command

Change-Id: I67b957d18820b2447ebddf09f4aa95ab67e6c0b8
1 -/* 1 +
2 - * Licensed to the Apache Software Foundation (ASF) under one
3 - * or more contributor license agreements. See the NOTICE file
4 - * distributed with this work for additional information
5 - * regarding copyright ownership. The ASF licenses this file
6 - * to you under the Apache License, Version 2.0 (the
7 - * "License"); you may not use this file except in compliance
8 - * with the License. You may obtain a copy of the License at
9 - *
10 - * http://www.apache.org/licenses/LICENSE-2.0
11 - *
12 - * Unless required by applicable law or agreed to in writing,
13 - * software distributed under the License is distributed on an
14 - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 - * KIND, either express or implied. See the License for the
16 - * specific language governing permissions and limitations
17 - * under the License.
18 - */
19 package org.onlab.onos.cli.net; 2 package org.onlab.onos.cli.net;
20 3
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;
21 import com.google.common.collect.Sets; 8 import com.google.common.collect.Sets;
22 import org.apache.karaf.shell.commands.Argument; 9 import org.apache.karaf.shell.commands.Argument;
23 import org.apache.karaf.shell.commands.Command; 10 import org.apache.karaf.shell.commands.Command;
...@@ -36,6 +23,7 @@ import org.onlab.onos.net.flow.TrafficSelector; ...@@ -36,6 +23,7 @@ import org.onlab.onos.net.flow.TrafficSelector;
36 import org.onlab.onos.net.flow.TrafficTreatment; 23 import org.onlab.onos.net.flow.TrafficTreatment;
37 import org.onlab.packet.MacAddress; 24 import org.onlab.packet.MacAddress;
38 25
26 +import java.util.ArrayList;
39 import java.util.Set; 27 import java.util.Set;
40 import java.util.concurrent.ExecutionException; 28 import java.util.concurrent.ExecutionException;
41 import java.util.concurrent.Future; 29 import java.util.concurrent.Future;
...@@ -51,6 +39,9 @@ public class AddFlowsCommand extends AbstractShellCommand { ...@@ -51,6 +39,9 @@ public class AddFlowsCommand extends AbstractShellCommand {
51 required = true, multiValued = false) 39 required = true, multiValued = false)
52 String flows = null; 40 String flows = null;
53 41
42 + @Argument(index = 1, name = "numOfRuns", description = "Number of iterations",
43 + required = true, multiValued = false)
44 + String numOfRuns = null;
54 45
55 @Override 46 @Override
56 protected void execute() { 47 protected void execute() {
...@@ -59,7 +50,9 @@ public class AddFlowsCommand extends AbstractShellCommand { ...@@ -59,7 +50,9 @@ public class AddFlowsCommand extends AbstractShellCommand {
59 DeviceService deviceService = get(DeviceService.class); 50 DeviceService deviceService = get(DeviceService.class);
60 51
61 int flowsPerDevice = Integer.parseInt(flows); 52 int flowsPerDevice = Integer.parseInt(flows);
53 + int num = Integer.parseInt(numOfRuns);
62 54
55 + ArrayList<Long> results = Lists.newArrayList();
63 Iterable<Device> devices = deviceService.getDevices(); 56 Iterable<Device> devices = deviceService.getDevices();
64 TrafficTreatment treatment = DefaultTrafficTreatment.builder() 57 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
65 .setOutput(PortNumber.portNumber(1)).build(); 58 .setOutput(PortNumber.portNumber(1)).build();
...@@ -80,24 +73,49 @@ public class AddFlowsCommand extends AbstractShellCommand { ...@@ -80,24 +73,49 @@ public class AddFlowsCommand extends AbstractShellCommand {
80 73
81 } 74 }
82 } 75 }
83 - long startTime = System.currentTimeMillis(); 76 + boolean isSuccess = true;
84 - Future<CompletedBatchOperation> op = flowService.applyBatch( 77 + for (int i = 0; i < num; i++) {
85 - new FlowRuleBatchOperation(rules)); 78 + long startTime = System.currentTimeMillis();
86 - CompletedBatchOperation completed = null; 79 + Future<CompletedBatchOperation> op = flowService.applyBatch(
87 - try { 80 + new FlowRuleBatchOperation(rules));
88 - completed = op.get(); 81 + try {
89 - } catch (InterruptedException e) { 82 + isSuccess &= op.get().isSuccess();
90 - e.printStackTrace(); 83 + } catch (InterruptedException e) {
91 - } catch (ExecutionException e) { 84 + e.printStackTrace();
92 - 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));
93 } 92 }
94 - long endTime = System.currentTimeMillis(); 93 + if (outputJson()) {
95 - print("%s AFTER ELAPSED TIME %s", completed.isSuccess() ? "SUCCESS" : "FAILURE", 94 + print("%s", json(new ObjectMapper(), isSuccess, results));
96 - endTime - startTime); 95 + } else {
96 + printTime(isSuccess, results);
97 + }
98 +
97 99
98 - flowService.applyBatch(
99 - new FlowRuleBatchOperation(remove));
100 100
101 } 101 }
102 102
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 +
103 } 121 }
......