blob: 929c7749fa43ed99d0a7e8112d5ea3ada497e870 [file] [log] [blame]
alshabib3460da12014-10-30 17:26:49 +01001
alshabibab984662014-12-04 18:56:18 -08002/*
3 * Copyright 2014 Open Networking Laboratory
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
Brian O'Connorabafb502014-12-02 22:26:20 -080017package org.onosproject.cli.net;
alshabib5afcbd72014-10-30 16:28:40 +010018
alshabib3460da12014-10-30 17:26:49 +010019import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.fasterxml.jackson.databind.node.ObjectNode;
22import com.google.common.collect.Lists;
alshabib5afcbd72014-10-30 16:28:40 +010023import com.google.common.collect.Sets;
24import org.apache.karaf.shell.commands.Argument;
25import org.apache.karaf.shell.commands.Command;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.cli.AbstractShellCommand;
27import org.onosproject.net.Device;
28import org.onosproject.net.PortNumber;
29import org.onosproject.net.device.DeviceService;
30import org.onosproject.net.flow.CompletedBatchOperation;
31import org.onosproject.net.flow.DefaultFlowRule;
32import org.onosproject.net.flow.DefaultTrafficSelector;
33import org.onosproject.net.flow.DefaultTrafficTreatment;
34import org.onosproject.net.flow.FlowRuleBatchEntry;
35import org.onosproject.net.flow.FlowRuleBatchOperation;
36import org.onosproject.net.flow.FlowRuleService;
37import org.onosproject.net.flow.TrafficSelector;
38import org.onosproject.net.flow.TrafficTreatment;
alshabib5afcbd72014-10-30 16:28:40 +010039import org.onlab.packet.MacAddress;
40
alshabib3460da12014-10-30 17:26:49 +010041import java.util.ArrayList;
alshabib5afcbd72014-10-30 16:28:40 +010042import java.util.Set;
43import java.util.concurrent.ExecutionException;
44import java.util.concurrent.Future;
45
46/**
47 * Installs many many flows.
48 */
49@Command(scope = "onos", name = "add-flows",
50 description = "Installs a flow rules")
51public class AddFlowsCommand extends AbstractShellCommand {
52
53 @Argument(index = 0, name = "flowPerDevice", description = "Number of flows to add per device",
54 required = true, multiValued = false)
55 String flows = null;
56
alshabib3460da12014-10-30 17:26:49 +010057 @Argument(index = 1, name = "numOfRuns", description = "Number of iterations",
58 required = true, multiValued = false)
59 String numOfRuns = null;
alshabib5afcbd72014-10-30 16:28:40 +010060
61 @Override
62 protected void execute() {
63
64 FlowRuleService flowService = get(FlowRuleService.class);
65 DeviceService deviceService = get(DeviceService.class);
66
67 int flowsPerDevice = Integer.parseInt(flows);
alshabib3460da12014-10-30 17:26:49 +010068 int num = Integer.parseInt(numOfRuns);
alshabib5afcbd72014-10-30 16:28:40 +010069
alshabib3460da12014-10-30 17:26:49 +010070 ArrayList<Long> results = Lists.newArrayList();
alshabib5afcbd72014-10-30 16:28:40 +010071 Iterable<Device> devices = deviceService.getDevices();
72 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
73 .setOutput(PortNumber.portNumber(1)).build();
74 TrafficSelector.Builder sbuilder;
75 Set<FlowRuleBatchEntry> rules = Sets.newHashSet();
76 Set<FlowRuleBatchEntry> remove = Sets.newHashSet();
77 for (Device d : devices) {
78 for (int i = 0; i < flowsPerDevice; i++) {
79 sbuilder = DefaultTrafficSelector.builder();
80 sbuilder.matchEthSrc(MacAddress.valueOf(i))
81 .matchEthDst(MacAddress.valueOf(Integer.MAX_VALUE - i));
82 rules.add(new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.ADD,
83 new DefaultFlowRule(d.id(), sbuilder.build(), treatment,
84 100, (long) 0, 10, false)));
85 remove.add(new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.REMOVE,
86 new DefaultFlowRule(d.id(), sbuilder.build(), treatment,
87 100, (long) 0, 10, false)));
88
89 }
90 }
alshabib3460da12014-10-30 17:26:49 +010091 boolean isSuccess = true;
92 for (int i = 0; i < num; i++) {
93 long startTime = System.currentTimeMillis();
94 Future<CompletedBatchOperation> op = flowService.applyBatch(
95 new FlowRuleBatchOperation(rules));
96 try {
97 isSuccess &= op.get().isSuccess();
98 } catch (InterruptedException e) {
99 e.printStackTrace();
100 } catch (ExecutionException e) {
101 e.printStackTrace();
102 }
103 long endTime = System.currentTimeMillis();
104 results.add(endTime - startTime);
105 flowService.applyBatch(
106 new FlowRuleBatchOperation(remove));
alshabib5afcbd72014-10-30 16:28:40 +0100107 }
alshabib3460da12014-10-30 17:26:49 +0100108 if (outputJson()) {
109 print("%s", json(new ObjectMapper(), isSuccess, results));
110 } else {
111 printTime(isSuccess, results);
112 }
alshabib5afcbd72014-10-30 16:28:40 +0100113
alshabib3460da12014-10-30 17:26:49 +0100114
alshabib5afcbd72014-10-30 16:28:40 +0100115
116 }
117
alshabib3460da12014-10-30 17:26:49 +0100118 private Object json(ObjectMapper mapper, boolean isSuccess, ArrayList<Long> elapsed) {
119 ObjectNode result = mapper.createObjectNode();
120 result.put("Success", isSuccess);
121 ArrayNode node = result.putArray("elapsed-time");
122 for (Long v : elapsed) {
123 node.add(v);
124 }
125 return result;
126 }
127
128 private void printTime(boolean isSuccess, ArrayList<Long> elapsed) {
129 print("Run is %s.", isSuccess ? "success" : "failure");
130 for (int i = 0; i < elapsed.size(); i++) {
131 print(" Run %s : %s", i, elapsed.get(i));
132 }
133 }
134
135
alshabib5afcbd72014-10-30 16:28:40 +0100136}