blob: 403c006479beca5d86aba7f9bc98097517bc9428 [file] [log] [blame]
alshabibab984662014-12-04 18:56:18 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
alshabibab984662014-12-04 18:56:18 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.cli.net;
alshabib5afcbd72014-10-30 16:28:40 +010017
Ray Milkey9f87e512016-01-05 10:00:22 -080018import java.util.ArrayList;
19import java.util.concurrent.CountDownLatch;
20import java.util.concurrent.TimeUnit;
21
Brian O'Connor72cb19a2015-01-16 16:14:41 -080022import org.apache.commons.lang.math.RandomUtils;
alshabib5afcbd72014-10-30 16:28:40 +010023import org.apache.karaf.shell.commands.Argument;
24import org.apache.karaf.shell.commands.Command;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080025import org.onlab.packet.MacAddress;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.cli.AbstractShellCommand;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080027import org.onosproject.core.ApplicationId;
28import org.onosproject.core.CoreService;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.net.Device;
30import org.onosproject.net.PortNumber;
31import org.onosproject.net.device.DeviceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.flow.DefaultFlowRule;
33import org.onosproject.net.flow.DefaultTrafficSelector;
34import org.onosproject.net.flow.DefaultTrafficTreatment;
Yuta HIGUCHI160732b2017-05-22 16:36:43 -070035import org.onosproject.net.flow.FlowEntry.FlowEntryState;
Ray Milkeyd13a37b2015-06-12 11:55:17 -070036import org.onosproject.net.flow.FlowRule;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080037import org.onosproject.net.flow.FlowRuleOperations;
38import org.onosproject.net.flow.FlowRuleOperationsContext;
Brian O'Connorabafb502014-12-02 22:26:20 -080039import org.onosproject.net.flow.FlowRuleService;
40import org.onosproject.net.flow.TrafficSelector;
41import org.onosproject.net.flow.TrafficTreatment;
alshabib5afcbd72014-10-30 16:28:40 +010042
Ray Milkey9f87e512016-01-05 10:00:22 -080043import com.fasterxml.jackson.databind.ObjectMapper;
44import com.fasterxml.jackson.databind.node.ArrayNode;
45import com.fasterxml.jackson.databind.node.ObjectNode;
46import com.google.common.base.Stopwatch;
Yuta HIGUCHI160732b2017-05-22 16:36:43 -070047import com.google.common.collect.Iterables;
Ray Milkey9f87e512016-01-05 10:00:22 -080048import com.google.common.collect.Lists;
Yuta HIGUCHI160732b2017-05-22 16:36:43 -070049import com.google.common.collect.Streams;
Ray Milkey9f87e512016-01-05 10:00:22 -080050
alshabib5afcbd72014-10-30 16:28:40 +010051/**
Charles M.C. Chan6f5bdc62015-04-22 01:21:09 +080052 * Installs bulk flows.
alshabib5afcbd72014-10-30 16:28:40 +010053 */
suibin zhangcb7f0d12015-08-10 11:14:15 -070054@Command(scope = "onos", name = "add-test-flows",
Ray Milkey82895d72015-01-22 17:06:00 -080055 description = "Installs a number of test flow rules - for testing only")
suibin zhangcb7f0d12015-08-10 11:14:15 -070056public class AddTestFlowsCommand extends AbstractShellCommand {
alshabib5afcbd72014-10-30 16:28:40 +010057
Carmelo Casconec9b39d62017-08-24 13:35:02 +020058 private static final int MAX_OUT_PORT = 254;
Deepa Vaddireddy2f4883d2016-06-07 22:51:01 +053059
Brian O'Connor72cb19a2015-01-16 16:14:41 -080060 private CountDownLatch latch;
61
alshabib5afcbd72014-10-30 16:28:40 +010062 @Argument(index = 0, name = "flowPerDevice", description = "Number of flows to add per device",
63 required = true, multiValued = false)
64 String flows = null;
65
alshabib3460da12014-10-30 17:26:49 +010066 @Argument(index = 1, name = "numOfRuns", description = "Number of iterations",
67 required = true, multiValued = false)
68 String numOfRuns = null;
alshabib5afcbd72014-10-30 16:28:40 +010069
70 @Override
71 protected void execute() {
alshabib5afcbd72014-10-30 16:28:40 +010072 FlowRuleService flowService = get(FlowRuleService.class);
73 DeviceService deviceService = get(DeviceService.class);
Brian O'Connor72cb19a2015-01-16 16:14:41 -080074 CoreService coreService = get(CoreService.class);
75
76 ApplicationId appId = coreService.registerApplication("onos.test.flow.installer");
alshabib5afcbd72014-10-30 16:28:40 +010077
78 int flowsPerDevice = Integer.parseInt(flows);
alshabib3460da12014-10-30 17:26:49 +010079 int num = Integer.parseInt(numOfRuns);
alshabib5afcbd72014-10-30 16:28:40 +010080
alshabib3460da12014-10-30 17:26:49 +010081 ArrayList<Long> results = Lists.newArrayList();
alshabib5afcbd72014-10-30 16:28:40 +010082 Iterable<Device> devices = deviceService.getDevices();
83 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
Deepa Vaddireddy2f4883d2016-06-07 22:51:01 +053084 .setOutput(PortNumber.portNumber(RandomUtils.nextInt(MAX_OUT_PORT))).build();
alshabib5afcbd72014-10-30 16:28:40 +010085 TrafficSelector.Builder sbuilder;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080086 FlowRuleOperations.Builder rules = FlowRuleOperations.builder();
87 FlowRuleOperations.Builder remove = FlowRuleOperations.builder();
88
alshabib5afcbd72014-10-30 16:28:40 +010089 for (Device d : devices) {
90 for (int i = 0; i < flowsPerDevice; i++) {
91 sbuilder = DefaultTrafficSelector.builder();
Brian O'Connor72cb19a2015-01-16 16:14:41 -080092
93 sbuilder.matchEthSrc(MacAddress.valueOf(RandomUtils.nextInt() * i))
94 .matchEthDst(MacAddress.valueOf((Integer.MAX_VALUE - i) * RandomUtils.nextInt()));
95
96
Carolina Fernandezfb4b04a2016-12-24 14:48:16 +010097 int randomPriority = RandomUtils.nextInt(
98 FlowRule.MAX_PRIORITY - FlowRule.MIN_PRIORITY + 1) + FlowRule.MIN_PRIORITY;
Ray Milkeyd13a37b2015-06-12 11:55:17 -070099
100 FlowRule addRule = DefaultFlowRule.builder()
101 .forDevice(d.id())
102 .withSelector(sbuilder.build())
103 .withTreatment(treatment)
104 .withPriority(randomPriority)
105 .fromApp(appId)
106 .makeTemporary(10)
107 .build();
108 FlowRule removeRule = DefaultFlowRule.builder()
109 .forDevice(d.id())
110 .withSelector(sbuilder.build())
111 .withTreatment(treatment)
112 .withPriority(randomPriority)
113 .fromApp(appId)
114 .makeTemporary(10)
115 .build();
116
117 rules.add(addRule);
118 remove.remove(removeRule);
alshabib5afcbd72014-10-30 16:28:40 +0100119
120 }
121 }
Yuta HIGUCHI160732b2017-05-22 16:36:43 -0700122 // close stages
123 rules.newStage();
124 remove.newStage();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800125
alshabib3460da12014-10-30 17:26:49 +0100126 for (int i = 0; i < num; i++) {
Yuta HIGUCHI160732b2017-05-22 16:36:43 -0700127 printProgress("Run %d:", i);
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800128 latch = new CountDownLatch(2);
Yuta HIGUCHI160732b2017-05-22 16:36:43 -0700129 final CountDownLatch addSuccess = new CountDownLatch(1);
130 printProgress("..batch add request");
131 Stopwatch add = Stopwatch.createStarted();
132
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800133 flowService.apply(rules.build(new FlowRuleOperationsContext() {
134
135 private final Stopwatch timer = Stopwatch.createStarted();
136
137 @Override
138 public void onSuccess(FlowRuleOperations ops) {
139
140 timer.stop();
Yuta HIGUCHI160732b2017-05-22 16:36:43 -0700141 printProgress("..add success");
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800142 results.add(timer.elapsed(TimeUnit.MILLISECONDS));
143 if (results.size() == num) {
144 if (outputJson()) {
145 print("%s", json(new ObjectMapper(), true, results));
146 } else {
147 printTime(true, results);
148 }
149 }
150 latch.countDown();
Yuta HIGUCHI160732b2017-05-22 16:36:43 -0700151 addSuccess.countDown();
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800152 }
153 }));
154
Yuta HIGUCHI160732b2017-05-22 16:36:43 -0700155 try {
156 addSuccess.await();
157 // wait until all flows reaches ADDED state
158 while (!Streams.stream(flowService.getFlowEntriesById(appId))
159 .allMatch(fr -> fr.state() == FlowEntryState.ADDED)) {
160 Thread.sleep(100);
161 }
162 add.stop();
163 printProgress("..completed %d ± 100 ms", add.elapsed(TimeUnit.MILLISECONDS));
164 } catch (InterruptedException e1) {
165 printProgress("Interrupted");
166 Thread.currentThread().interrupt();
167 }
168
169 printProgress("..cleaning up");
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800170 flowService.apply(remove.build(new FlowRuleOperationsContext() {
171 @Override
172 public void onSuccess(FlowRuleOperations ops) {
173 latch.countDown();
174 }
175 }));
Yuta HIGUCHI160732b2017-05-22 16:36:43 -0700176
alshabib3460da12014-10-30 17:26:49 +0100177 try {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800178 latch.await();
Yuta HIGUCHI160732b2017-05-22 16:36:43 -0700179 while (!Iterables.isEmpty(flowService.getFlowEntriesById(appId))) {
180 Thread.sleep(500);
181 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800182 } catch (InterruptedException e) {
Yuta HIGUCHI160732b2017-05-22 16:36:43 -0700183 printProgress("Interrupted.");
184 Thread.currentThread().interrupt();
alshabib3460da12014-10-30 17:26:49 +0100185 }
Yuta HIGUCHI160732b2017-05-22 16:36:43 -0700186 }
187 if (outputJson()) {
188 print("%s", json(new ObjectMapper(), true, results));
189 } else {
190 printTime(true, results);
alshabib5afcbd72014-10-30 16:28:40 +0100191 }
alshabib5afcbd72014-10-30 16:28:40 +0100192 }
193
alshabib3460da12014-10-30 17:26:49 +0100194 private Object json(ObjectMapper mapper, boolean isSuccess, ArrayList<Long> elapsed) {
195 ObjectNode result = mapper.createObjectNode();
196 result.put("Success", isSuccess);
197 ArrayNode node = result.putArray("elapsed-time");
198 for (Long v : elapsed) {
199 node.add(v);
200 }
201 return result;
202 }
203
Yuta HIGUCHI160732b2017-05-22 16:36:43 -0700204 private void printProgress(String format, Object... args) {
205 if (!outputJson()) {
206 print(format, args);
207 }
208 }
209
210
alshabib3460da12014-10-30 17:26:49 +0100211 private void printTime(boolean isSuccess, ArrayList<Long> elapsed) {
212 print("Run is %s.", isSuccess ? "success" : "failure");
213 for (int i = 0; i < elapsed.size(); i++) {
Yuta HIGUCHI160732b2017-05-22 16:36:43 -0700214 print(" Run %s : %s ms", i, elapsed.get(i));
alshabib3460da12014-10-30 17:26:49 +0100215 }
216 }
alshabib5afcbd72014-10-30 16:28:40 +0100217}