blob: 792ebbfe7516916d3fd40b99abfe788f77aa0641 [file] [log] [blame]
alshabibab984662014-12-04 18:56:18 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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;
Ray Milkeyd13a37b2015-06-12 11:55:17 -070035import org.onosproject.net.flow.FlowRule;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080036import org.onosproject.net.flow.FlowRuleOperations;
37import org.onosproject.net.flow.FlowRuleOperationsContext;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import org.onosproject.net.flow.FlowRuleService;
39import org.onosproject.net.flow.TrafficSelector;
40import org.onosproject.net.flow.TrafficTreatment;
alshabib5afcbd72014-10-30 16:28:40 +010041
Ray Milkey9f87e512016-01-05 10:00:22 -080042import com.fasterxml.jackson.databind.ObjectMapper;
43import com.fasterxml.jackson.databind.node.ArrayNode;
44import com.fasterxml.jackson.databind.node.ObjectNode;
45import com.google.common.base.Stopwatch;
46import com.google.common.collect.Lists;
47
alshabib5afcbd72014-10-30 16:28:40 +010048/**
Charles M.C. Chan6f5bdc62015-04-22 01:21:09 +080049 * Installs bulk flows.
alshabib5afcbd72014-10-30 16:28:40 +010050 */
suibin zhangcb7f0d12015-08-10 11:14:15 -070051@Command(scope = "onos", name = "add-test-flows",
Ray Milkey82895d72015-01-22 17:06:00 -080052 description = "Installs a number of test flow rules - for testing only")
suibin zhangcb7f0d12015-08-10 11:14:15 -070053public class AddTestFlowsCommand extends AbstractShellCommand {
alshabib5afcbd72014-10-30 16:28:40 +010054
Deepa Vaddireddy2f4883d2016-06-07 22:51:01 +053055 private static final int MAX_OUT_PORT = 65279;
56
Brian O'Connor72cb19a2015-01-16 16:14:41 -080057 private CountDownLatch latch;
58
alshabib5afcbd72014-10-30 16:28:40 +010059 @Argument(index = 0, name = "flowPerDevice", description = "Number of flows to add per device",
60 required = true, multiValued = false)
61 String flows = null;
62
alshabib3460da12014-10-30 17:26:49 +010063 @Argument(index = 1, name = "numOfRuns", description = "Number of iterations",
64 required = true, multiValued = false)
65 String numOfRuns = null;
alshabib5afcbd72014-10-30 16:28:40 +010066
67 @Override
Ray Milkeyaef45852016-01-11 17:13:19 -080068 @java.lang.SuppressWarnings("squid:S1148")
alshabib5afcbd72014-10-30 16:28:40 +010069 protected void execute() {
alshabib5afcbd72014-10-30 16:28:40 +010070 FlowRuleService flowService = get(FlowRuleService.class);
71 DeviceService deviceService = get(DeviceService.class);
Brian O'Connor72cb19a2015-01-16 16:14:41 -080072 CoreService coreService = get(CoreService.class);
73
74 ApplicationId appId = coreService.registerApplication("onos.test.flow.installer");
alshabib5afcbd72014-10-30 16:28:40 +010075
76 int flowsPerDevice = Integer.parseInt(flows);
alshabib3460da12014-10-30 17:26:49 +010077 int num = Integer.parseInt(numOfRuns);
alshabib5afcbd72014-10-30 16:28:40 +010078
alshabib3460da12014-10-30 17:26:49 +010079 ArrayList<Long> results = Lists.newArrayList();
alshabib5afcbd72014-10-30 16:28:40 +010080 Iterable<Device> devices = deviceService.getDevices();
81 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
Deepa Vaddireddy2f4883d2016-06-07 22:51:01 +053082 .setOutput(PortNumber.portNumber(RandomUtils.nextInt(MAX_OUT_PORT))).build();
alshabib5afcbd72014-10-30 16:28:40 +010083 TrafficSelector.Builder sbuilder;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080084 FlowRuleOperations.Builder rules = FlowRuleOperations.builder();
85 FlowRuleOperations.Builder remove = FlowRuleOperations.builder();
86
alshabib5afcbd72014-10-30 16:28:40 +010087 for (Device d : devices) {
88 for (int i = 0; i < flowsPerDevice; i++) {
89 sbuilder = DefaultTrafficSelector.builder();
Brian O'Connor72cb19a2015-01-16 16:14:41 -080090
91 sbuilder.matchEthSrc(MacAddress.valueOf(RandomUtils.nextInt() * i))
92 .matchEthDst(MacAddress.valueOf((Integer.MAX_VALUE - i) * RandomUtils.nextInt()));
93
94
95 int randomPriority = RandomUtils.nextInt();
Ray Milkeyd13a37b2015-06-12 11:55:17 -070096
97 FlowRule addRule = DefaultFlowRule.builder()
98 .forDevice(d.id())
99 .withSelector(sbuilder.build())
100 .withTreatment(treatment)
101 .withPriority(randomPriority)
102 .fromApp(appId)
103 .makeTemporary(10)
104 .build();
105 FlowRule removeRule = DefaultFlowRule.builder()
106 .forDevice(d.id())
107 .withSelector(sbuilder.build())
108 .withTreatment(treatment)
109 .withPriority(randomPriority)
110 .fromApp(appId)
111 .makeTemporary(10)
112 .build();
113
114 rules.add(addRule);
115 remove.remove(removeRule);
alshabib5afcbd72014-10-30 16:28:40 +0100116
117 }
118 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800119
alshabib3460da12014-10-30 17:26:49 +0100120 for (int i = 0; i < num; i++) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800121 latch = new CountDownLatch(2);
122 flowService.apply(rules.build(new FlowRuleOperationsContext() {
123
124 private final Stopwatch timer = Stopwatch.createStarted();
125
126 @Override
127 public void onSuccess(FlowRuleOperations ops) {
128
129 timer.stop();
130 results.add(timer.elapsed(TimeUnit.MILLISECONDS));
131 if (results.size() == num) {
132 if (outputJson()) {
133 print("%s", json(new ObjectMapper(), true, results));
134 } else {
135 printTime(true, results);
136 }
137 }
138 latch.countDown();
139 }
140 }));
141
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800142 flowService.apply(remove.build(new FlowRuleOperationsContext() {
143 @Override
144 public void onSuccess(FlowRuleOperations ops) {
145 latch.countDown();
146 }
147 }));
alshabib3460da12014-10-30 17:26:49 +0100148 try {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800149 latch.await();
150 } catch (InterruptedException e) {
alshabib3460da12014-10-30 17:26:49 +0100151 e.printStackTrace();
152 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800153
alshabib5afcbd72014-10-30 16:28:40 +0100154 }
alshabib5afcbd72014-10-30 16:28:40 +0100155 }
156
alshabib3460da12014-10-30 17:26:49 +0100157 private Object json(ObjectMapper mapper, boolean isSuccess, ArrayList<Long> elapsed) {
158 ObjectNode result = mapper.createObjectNode();
159 result.put("Success", isSuccess);
160 ArrayNode node = result.putArray("elapsed-time");
161 for (Long v : elapsed) {
162 node.add(v);
163 }
164 return result;
165 }
166
167 private void printTime(boolean isSuccess, ArrayList<Long> elapsed) {
168 print("Run is %s.", isSuccess ? "success" : "failure");
169 for (int i = 0; i < elapsed.size(); i++) {
170 print(" Run %s : %s", i, elapsed.get(i));
171 }
172 }
alshabib5afcbd72014-10-30 16:28:40 +0100173}