blob: 23032ca76a47a6194484fde7b59bc90495cc0265 [file] [log] [blame]
alshabib3460da12014-10-30 17:26:49 +01001
alshabibab984662014-12-04 18:56:18 -08002/*
Ray Milkey34c95902015-04-15 09:47:53 -07003 * Copyright 2014-2015 Open Networking Laboratory
alshabibab984662014-12-04 18:56:18 -08004 *
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
Ray Milkey9f87e512016-01-05 10:00:22 -080019import java.util.ArrayList;
20import java.util.concurrent.CountDownLatch;
21import java.util.concurrent.TimeUnit;
22
Brian O'Connor72cb19a2015-01-16 16:14:41 -080023import org.apache.commons.lang.math.RandomUtils;
alshabib5afcbd72014-10-30 16:28:40 +010024import org.apache.karaf.shell.commands.Argument;
25import org.apache.karaf.shell.commands.Command;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080026import org.onlab.packet.MacAddress;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.cli.AbstractShellCommand;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080028import org.onosproject.core.ApplicationId;
29import org.onosproject.core.CoreService;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.Device;
31import org.onosproject.net.PortNumber;
32import org.onosproject.net.device.DeviceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.net.flow.DefaultFlowRule;
34import org.onosproject.net.flow.DefaultTrafficSelector;
35import org.onosproject.net.flow.DefaultTrafficTreatment;
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;
47import com.google.common.collect.Lists;
48
alshabib5afcbd72014-10-30 16:28:40 +010049/**
Charles M.C. Chan6f5bdc62015-04-22 01:21:09 +080050 * Installs bulk flows.
alshabib5afcbd72014-10-30 16:28:40 +010051 */
suibin zhangcb7f0d12015-08-10 11:14:15 -070052@Command(scope = "onos", name = "add-test-flows",
Ray Milkey82895d72015-01-22 17:06:00 -080053 description = "Installs a number of test flow rules - for testing only")
suibin zhangcb7f0d12015-08-10 11:14:15 -070054public class AddTestFlowsCommand extends AbstractShellCommand {
alshabib5afcbd72014-10-30 16:28:40 +010055
Brian O'Connor72cb19a2015-01-16 16:14:41 -080056 private CountDownLatch latch;
57
alshabib5afcbd72014-10-30 16:28:40 +010058 @Argument(index = 0, name = "flowPerDevice", description = "Number of flows to add per device",
59 required = true, multiValued = false)
60 String flows = null;
61
alshabib3460da12014-10-30 17:26:49 +010062 @Argument(index = 1, name = "numOfRuns", description = "Number of iterations",
63 required = true, multiValued = false)
64 String numOfRuns = null;
alshabib5afcbd72014-10-30 16:28:40 +010065
66 @Override
Ray Milkeyaef45852016-01-11 17:13:19 -080067 @java.lang.SuppressWarnings("squid:S1148")
alshabib5afcbd72014-10-30 16:28:40 +010068 protected void execute() {
alshabib5afcbd72014-10-30 16:28:40 +010069 FlowRuleService flowService = get(FlowRuleService.class);
70 DeviceService deviceService = get(DeviceService.class);
Brian O'Connor72cb19a2015-01-16 16:14:41 -080071 CoreService coreService = get(CoreService.class);
72
73 ApplicationId appId = coreService.registerApplication("onos.test.flow.installer");
alshabib5afcbd72014-10-30 16:28:40 +010074
75 int flowsPerDevice = Integer.parseInt(flows);
alshabib3460da12014-10-30 17:26:49 +010076 int num = Integer.parseInt(numOfRuns);
alshabib5afcbd72014-10-30 16:28:40 +010077
alshabib3460da12014-10-30 17:26:49 +010078 ArrayList<Long> results = Lists.newArrayList();
alshabib5afcbd72014-10-30 16:28:40 +010079 Iterable<Device> devices = deviceService.getDevices();
80 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
Brian O'Connor72cb19a2015-01-16 16:14:41 -080081 .setOutput(PortNumber.portNumber(RandomUtils.nextInt())).build();
alshabib5afcbd72014-10-30 16:28:40 +010082 TrafficSelector.Builder sbuilder;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080083 FlowRuleOperations.Builder rules = FlowRuleOperations.builder();
84 FlowRuleOperations.Builder remove = FlowRuleOperations.builder();
85
alshabib5afcbd72014-10-30 16:28:40 +010086 for (Device d : devices) {
87 for (int i = 0; i < flowsPerDevice; i++) {
88 sbuilder = DefaultTrafficSelector.builder();
Brian O'Connor72cb19a2015-01-16 16:14:41 -080089
90 sbuilder.matchEthSrc(MacAddress.valueOf(RandomUtils.nextInt() * i))
91 .matchEthDst(MacAddress.valueOf((Integer.MAX_VALUE - i) * RandomUtils.nextInt()));
92
93
94 int randomPriority = RandomUtils.nextInt();
Ray Milkeyd13a37b2015-06-12 11:55:17 -070095
96 FlowRule addRule = DefaultFlowRule.builder()
97 .forDevice(d.id())
98 .withSelector(sbuilder.build())
99 .withTreatment(treatment)
100 .withPriority(randomPriority)
101 .fromApp(appId)
102 .makeTemporary(10)
103 .build();
104 FlowRule removeRule = DefaultFlowRule.builder()
105 .forDevice(d.id())
106 .withSelector(sbuilder.build())
107 .withTreatment(treatment)
108 .withPriority(randomPriority)
109 .fromApp(appId)
110 .makeTemporary(10)
111 .build();
112
113 rules.add(addRule);
114 remove.remove(removeRule);
alshabib5afcbd72014-10-30 16:28:40 +0100115
116 }
117 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800118
alshabib3460da12014-10-30 17:26:49 +0100119 for (int i = 0; i < num; i++) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800120 latch = new CountDownLatch(2);
121 flowService.apply(rules.build(new FlowRuleOperationsContext() {
122
123 private final Stopwatch timer = Stopwatch.createStarted();
124
125 @Override
126 public void onSuccess(FlowRuleOperations ops) {
127
128 timer.stop();
129 results.add(timer.elapsed(TimeUnit.MILLISECONDS));
130 if (results.size() == num) {
131 if (outputJson()) {
132 print("%s", json(new ObjectMapper(), true, results));
133 } else {
134 printTime(true, results);
135 }
136 }
137 latch.countDown();
138 }
139 }));
140
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800141 flowService.apply(remove.build(new FlowRuleOperationsContext() {
142 @Override
143 public void onSuccess(FlowRuleOperations ops) {
144 latch.countDown();
145 }
146 }));
alshabib3460da12014-10-30 17:26:49 +0100147 try {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800148 latch.await();
149 } catch (InterruptedException e) {
alshabib3460da12014-10-30 17:26:49 +0100150 e.printStackTrace();
151 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800152
alshabib5afcbd72014-10-30 16:28:40 +0100153 }
alshabib5afcbd72014-10-30 16:28:40 +0100154 }
155
alshabib3460da12014-10-30 17:26:49 +0100156 private Object json(ObjectMapper mapper, boolean isSuccess, ArrayList<Long> elapsed) {
157 ObjectNode result = mapper.createObjectNode();
158 result.put("Success", isSuccess);
159 ArrayNode node = result.putArray("elapsed-time");
160 for (Long v : elapsed) {
161 node.add(v);
162 }
163 return result;
164 }
165
166 private void printTime(boolean isSuccess, ArrayList<Long> elapsed) {
167 print("Run is %s.", isSuccess ? "success" : "failure");
168 for (int i = 0; i < elapsed.size(); i++) {
169 print(" Run %s : %s", i, elapsed.get(i));
170 }
171 }
alshabib5afcbd72014-10-30 16:28:40 +0100172}