blob: 4a741ed4174f8e5cf6f2a33c94d1c5850618b875 [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
alshabib3460da12014-10-30 17:26:49 +010019import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.fasterxml.jackson.databind.node.ObjectNode;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080022import com.google.common.base.Stopwatch;
alshabib3460da12014-10-30 17:26:49 +010023import com.google.common.collect.Lists;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080024import org.apache.commons.lang.math.RandomUtils;
alshabib5afcbd72014-10-30 16:28:40 +010025import org.apache.karaf.shell.commands.Argument;
26import org.apache.karaf.shell.commands.Command;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080027import org.onlab.packet.MacAddress;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.cli.AbstractShellCommand;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080029import org.onosproject.core.ApplicationId;
30import org.onosproject.core.CoreService;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.net.Device;
32import org.onosproject.net.PortNumber;
33import org.onosproject.net.device.DeviceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import org.onosproject.net.flow.DefaultFlowRule;
35import org.onosproject.net.flow.DefaultTrafficSelector;
36import org.onosproject.net.flow.DefaultTrafficTreatment;
Ray Milkeyd13a37b2015-06-12 11:55:17 -070037import org.onosproject.net.flow.FlowRule;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080038import org.onosproject.net.flow.FlowRuleOperations;
39import org.onosproject.net.flow.FlowRuleOperationsContext;
Brian O'Connorabafb502014-12-02 22:26:20 -080040import org.onosproject.net.flow.FlowRuleService;
41import org.onosproject.net.flow.TrafficSelector;
42import org.onosproject.net.flow.TrafficTreatment;
alshabib5afcbd72014-10-30 16:28:40 +010043
alshabib3460da12014-10-30 17:26:49 +010044import java.util.ArrayList;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080045import java.util.concurrent.CountDownLatch;
46import java.util.concurrent.TimeUnit;
alshabib5afcbd72014-10-30 16:28:40 +010047
48/**
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
Brian O'Connor72cb19a2015-01-16 16:14:41 -080055 private CountDownLatch latch;
56
alshabib5afcbd72014-10-30 16:28:40 +010057 @Argument(index = 0, name = "flowPerDevice", description = "Number of flows to add per device",
58 required = true, multiValued = false)
59 String flows = null;
60
alshabib3460da12014-10-30 17:26:49 +010061 @Argument(index = 1, name = "numOfRuns", description = "Number of iterations",
62 required = true, multiValued = false)
63 String numOfRuns = null;
alshabib5afcbd72014-10-30 16:28:40 +010064
65 @Override
Ray Milkey4fd3ceb2015-12-10 14:43:08 -080066 @java.lang.SuppressWarnings("squid:S1148")
alshabib5afcbd72014-10-30 16:28:40 +010067 protected void execute() {
alshabib5afcbd72014-10-30 16:28:40 +010068 FlowRuleService flowService = get(FlowRuleService.class);
69 DeviceService deviceService = get(DeviceService.class);
Brian O'Connor72cb19a2015-01-16 16:14:41 -080070 CoreService coreService = get(CoreService.class);
71
72 ApplicationId appId = coreService.registerApplication("onos.test.flow.installer");
alshabib5afcbd72014-10-30 16:28:40 +010073
74 int flowsPerDevice = Integer.parseInt(flows);
alshabib3460da12014-10-30 17:26:49 +010075 int num = Integer.parseInt(numOfRuns);
alshabib5afcbd72014-10-30 16:28:40 +010076
alshabib3460da12014-10-30 17:26:49 +010077 ArrayList<Long> results = Lists.newArrayList();
alshabib5afcbd72014-10-30 16:28:40 +010078 Iterable<Device> devices = deviceService.getDevices();
79 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
Brian O'Connor72cb19a2015-01-16 16:14:41 -080080 .setOutput(PortNumber.portNumber(RandomUtils.nextInt())).build();
alshabib5afcbd72014-10-30 16:28:40 +010081 TrafficSelector.Builder sbuilder;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080082 FlowRuleOperations.Builder rules = FlowRuleOperations.builder();
83 FlowRuleOperations.Builder remove = FlowRuleOperations.builder();
84
alshabib5afcbd72014-10-30 16:28:40 +010085 for (Device d : devices) {
86 for (int i = 0; i < flowsPerDevice; i++) {
87 sbuilder = DefaultTrafficSelector.builder();
Brian O'Connor72cb19a2015-01-16 16:14:41 -080088
89 sbuilder.matchEthSrc(MacAddress.valueOf(RandomUtils.nextInt() * i))
90 .matchEthDst(MacAddress.valueOf((Integer.MAX_VALUE - i) * RandomUtils.nextInt()));
91
92
93 int randomPriority = RandomUtils.nextInt();
Ray Milkeyd13a37b2015-06-12 11:55:17 -070094
95 FlowRule addRule = DefaultFlowRule.builder()
96 .forDevice(d.id())
97 .withSelector(sbuilder.build())
98 .withTreatment(treatment)
99 .withPriority(randomPriority)
100 .fromApp(appId)
101 .makeTemporary(10)
102 .build();
103 FlowRule removeRule = DefaultFlowRule.builder()
104 .forDevice(d.id())
105 .withSelector(sbuilder.build())
106 .withTreatment(treatment)
107 .withPriority(randomPriority)
108 .fromApp(appId)
109 .makeTemporary(10)
110 .build();
111
112 rules.add(addRule);
113 remove.remove(removeRule);
alshabib5afcbd72014-10-30 16:28:40 +0100114
115 }
116 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800117
alshabib3460da12014-10-30 17:26:49 +0100118 for (int i = 0; i < num; i++) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800119 latch = new CountDownLatch(2);
120 flowService.apply(rules.build(new FlowRuleOperationsContext() {
121
122 private final Stopwatch timer = Stopwatch.createStarted();
123
124 @Override
125 public void onSuccess(FlowRuleOperations ops) {
126
127 timer.stop();
128 results.add(timer.elapsed(TimeUnit.MILLISECONDS));
129 if (results.size() == num) {
130 if (outputJson()) {
131 print("%s", json(new ObjectMapper(), true, results));
132 } else {
133 printTime(true, results);
134 }
135 }
136 latch.countDown();
137 }
138 }));
139
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800140 flowService.apply(remove.build(new FlowRuleOperationsContext() {
141 @Override
142 public void onSuccess(FlowRuleOperations ops) {
143 latch.countDown();
144 }
145 }));
alshabib3460da12014-10-30 17:26:49 +0100146 try {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800147 latch.await();
148 } catch (InterruptedException e) {
alshabib3460da12014-10-30 17:26:49 +0100149 e.printStackTrace();
150 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800151
alshabib5afcbd72014-10-30 16:28:40 +0100152 }
alshabib5afcbd72014-10-30 16:28:40 +0100153 }
154
alshabib3460da12014-10-30 17:26:49 +0100155 private Object json(ObjectMapper mapper, boolean isSuccess, ArrayList<Long> elapsed) {
156 ObjectNode result = mapper.createObjectNode();
157 result.put("Success", isSuccess);
158 ArrayNode node = result.putArray("elapsed-time");
159 for (Long v : elapsed) {
160 node.add(v);
161 }
162 return result;
163 }
164
165 private void printTime(boolean isSuccess, ArrayList<Long> elapsed) {
166 print("Run is %s.", isSuccess ? "success" : "failure");
167 for (int i = 0; i < elapsed.size(); i++) {
168 print(" Run %s : %s", i, elapsed.get(i));
169 }
170 }
alshabib5afcbd72014-10-30 16:28:40 +0100171}