blob: 93939e939d71bdbac4de2a224dfb8ca65bb760bd [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
49import static org.onlab.util.SonarSuppressionConstants.SONAR_PRINT_STACK_TRACE;
alshabib5afcbd72014-10-30 16:28:40 +010050
51/**
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
Brian O'Connor72cb19a2015-01-16 16:14:41 -080058 private CountDownLatch latch;
59
alshabib5afcbd72014-10-30 16:28:40 +010060 @Argument(index = 0, name = "flowPerDevice", description = "Number of flows to add per device",
61 required = true, multiValued = false)
62 String flows = null;
63
alshabib3460da12014-10-30 17:26:49 +010064 @Argument(index = 1, name = "numOfRuns", description = "Number of iterations",
65 required = true, multiValued = false)
66 String numOfRuns = null;
alshabib5afcbd72014-10-30 16:28:40 +010067
68 @Override
Ray Milkey9f87e512016-01-05 10:00:22 -080069 @java.lang.SuppressWarnings(SONAR_PRINT_STACK_TRACE)
alshabib5afcbd72014-10-30 16:28:40 +010070 protected void execute() {
alshabib5afcbd72014-10-30 16:28:40 +010071 FlowRuleService flowService = get(FlowRuleService.class);
72 DeviceService deviceService = get(DeviceService.class);
Brian O'Connor72cb19a2015-01-16 16:14:41 -080073 CoreService coreService = get(CoreService.class);
74
75 ApplicationId appId = coreService.registerApplication("onos.test.flow.installer");
alshabib5afcbd72014-10-30 16:28:40 +010076
77 int flowsPerDevice = Integer.parseInt(flows);
alshabib3460da12014-10-30 17:26:49 +010078 int num = Integer.parseInt(numOfRuns);
alshabib5afcbd72014-10-30 16:28:40 +010079
alshabib3460da12014-10-30 17:26:49 +010080 ArrayList<Long> results = Lists.newArrayList();
alshabib5afcbd72014-10-30 16:28:40 +010081 Iterable<Device> devices = deviceService.getDevices();
82 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
Brian O'Connor72cb19a2015-01-16 16:14:41 -080083 .setOutput(PortNumber.portNumber(RandomUtils.nextInt())).build();
alshabib5afcbd72014-10-30 16:28:40 +010084 TrafficSelector.Builder sbuilder;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080085 FlowRuleOperations.Builder rules = FlowRuleOperations.builder();
86 FlowRuleOperations.Builder remove = FlowRuleOperations.builder();
87
alshabib5afcbd72014-10-30 16:28:40 +010088 for (Device d : devices) {
89 for (int i = 0; i < flowsPerDevice; i++) {
90 sbuilder = DefaultTrafficSelector.builder();
Brian O'Connor72cb19a2015-01-16 16:14:41 -080091
92 sbuilder.matchEthSrc(MacAddress.valueOf(RandomUtils.nextInt() * i))
93 .matchEthDst(MacAddress.valueOf((Integer.MAX_VALUE - i) * RandomUtils.nextInt()));
94
95
96 int randomPriority = RandomUtils.nextInt();
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}