blob: 1f691904da6df671a7cfd339e09004a998b7379c [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
Carolina Fernandezfb4b04a2016-12-24 14:48:16 +010095 int randomPriority = RandomUtils.nextInt(
96 FlowRule.MAX_PRIORITY - FlowRule.MIN_PRIORITY + 1) + FlowRule.MIN_PRIORITY;
Ray Milkeyd13a37b2015-06-12 11:55:17 -070097
98 FlowRule addRule = DefaultFlowRule.builder()
99 .forDevice(d.id())
100 .withSelector(sbuilder.build())
101 .withTreatment(treatment)
102 .withPriority(randomPriority)
103 .fromApp(appId)
104 .makeTemporary(10)
105 .build();
106 FlowRule removeRule = DefaultFlowRule.builder()
107 .forDevice(d.id())
108 .withSelector(sbuilder.build())
109 .withTreatment(treatment)
110 .withPriority(randomPriority)
111 .fromApp(appId)
112 .makeTemporary(10)
113 .build();
114
115 rules.add(addRule);
116 remove.remove(removeRule);
alshabib5afcbd72014-10-30 16:28:40 +0100117
118 }
119 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800120
alshabib3460da12014-10-30 17:26:49 +0100121 for (int i = 0; i < num; i++) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800122 latch = new CountDownLatch(2);
123 flowService.apply(rules.build(new FlowRuleOperationsContext() {
124
125 private final Stopwatch timer = Stopwatch.createStarted();
126
127 @Override
128 public void onSuccess(FlowRuleOperations ops) {
129
130 timer.stop();
131 results.add(timer.elapsed(TimeUnit.MILLISECONDS));
132 if (results.size() == num) {
133 if (outputJson()) {
134 print("%s", json(new ObjectMapper(), true, results));
135 } else {
136 printTime(true, results);
137 }
138 }
139 latch.countDown();
140 }
141 }));
142
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800143 flowService.apply(remove.build(new FlowRuleOperationsContext() {
144 @Override
145 public void onSuccess(FlowRuleOperations ops) {
146 latch.countDown();
147 }
148 }));
alshabib3460da12014-10-30 17:26:49 +0100149 try {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800150 latch.await();
151 } catch (InterruptedException e) {
alshabib3460da12014-10-30 17:26:49 +0100152 e.printStackTrace();
153 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800154
alshabib5afcbd72014-10-30 16:28:40 +0100155 }
alshabib5afcbd72014-10-30 16:28:40 +0100156 }
157
alshabib3460da12014-10-30 17:26:49 +0100158 private Object json(ObjectMapper mapper, boolean isSuccess, ArrayList<Long> elapsed) {
159 ObjectNode result = mapper.createObjectNode();
160 result.put("Success", isSuccess);
161 ArrayNode node = result.putArray("elapsed-time");
162 for (Long v : elapsed) {
163 node.add(v);
164 }
165 return result;
166 }
167
168 private void printTime(boolean isSuccess, ArrayList<Long> elapsed) {
169 print("Run is %s.", isSuccess ? "success" : "failure");
170 for (int i = 0; i < elapsed.size(); i++) {
171 print(" Run %s : %s", i, elapsed.get(i));
172 }
173 }
alshabib5afcbd72014-10-30 16:28:40 +0100174}