blob: c8d91d54137ca21735c74a6d73df3645d9269efe [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;
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
alshabib3460da12014-10-30 17:26:49 +010043import java.util.ArrayList;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080044import java.util.concurrent.CountDownLatch;
45import java.util.concurrent.TimeUnit;
alshabib5afcbd72014-10-30 16:28:40 +010046
47/**
Charles M.C. Chan6f5bdc62015-04-22 01:21:09 +080048 * Installs bulk flows.
alshabib5afcbd72014-10-30 16:28:40 +010049 */
50@Command(scope = "onos", name = "add-flows",
Ray Milkey82895d72015-01-22 17:06:00 -080051 description = "Installs a number of test flow rules - for testing only")
alshabib5afcbd72014-10-30 16:28:40 +010052public class AddFlowsCommand extends AbstractShellCommand {
53
Brian O'Connor72cb19a2015-01-16 16:14:41 -080054 private CountDownLatch latch;
55
alshabib5afcbd72014-10-30 16:28:40 +010056 @Argument(index = 0, name = "flowPerDevice", description = "Number of flows to add per device",
57 required = true, multiValued = false)
58 String flows = null;
59
alshabib3460da12014-10-30 17:26:49 +010060 @Argument(index = 1, name = "numOfRuns", description = "Number of iterations",
61 required = true, multiValued = false)
62 String numOfRuns = null;
alshabib5afcbd72014-10-30 16:28:40 +010063
64 @Override
65 protected void execute() {
alshabib5afcbd72014-10-30 16:28:40 +010066 FlowRuleService flowService = get(FlowRuleService.class);
67 DeviceService deviceService = get(DeviceService.class);
Brian O'Connor72cb19a2015-01-16 16:14:41 -080068 CoreService coreService = get(CoreService.class);
69
70 ApplicationId appId = coreService.registerApplication("onos.test.flow.installer");
alshabib5afcbd72014-10-30 16:28:40 +010071
72 int flowsPerDevice = Integer.parseInt(flows);
alshabib3460da12014-10-30 17:26:49 +010073 int num = Integer.parseInt(numOfRuns);
alshabib5afcbd72014-10-30 16:28:40 +010074
alshabib3460da12014-10-30 17:26:49 +010075 ArrayList<Long> results = Lists.newArrayList();
alshabib5afcbd72014-10-30 16:28:40 +010076 Iterable<Device> devices = deviceService.getDevices();
77 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
Brian O'Connor72cb19a2015-01-16 16:14:41 -080078 .setOutput(PortNumber.portNumber(RandomUtils.nextInt())).build();
alshabib5afcbd72014-10-30 16:28:40 +010079 TrafficSelector.Builder sbuilder;
Brian O'Connor72cb19a2015-01-16 16:14:41 -080080 FlowRuleOperations.Builder rules = FlowRuleOperations.builder();
81 FlowRuleOperations.Builder remove = FlowRuleOperations.builder();
82
alshabib5afcbd72014-10-30 16:28:40 +010083 for (Device d : devices) {
84 for (int i = 0; i < flowsPerDevice; i++) {
85 sbuilder = DefaultTrafficSelector.builder();
Brian O'Connor72cb19a2015-01-16 16:14:41 -080086
87 sbuilder.matchEthSrc(MacAddress.valueOf(RandomUtils.nextInt() * i))
88 .matchEthDst(MacAddress.valueOf((Integer.MAX_VALUE - i) * RandomUtils.nextInt()));
89
90
91 int randomPriority = RandomUtils.nextInt();
92 rules.add(new DefaultFlowRule(d.id(), sbuilder.build(), treatment,
93 randomPriority, appId, 10, false));
94 remove.remove(new DefaultFlowRule(d.id(), sbuilder.build(), treatment,
95 randomPriority, appId, 10, false));
alshabib5afcbd72014-10-30 16:28:40 +010096
97 }
98 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -080099
alshabib3460da12014-10-30 17:26:49 +0100100 for (int i = 0; i < num; i++) {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800101 latch = new CountDownLatch(2);
102 flowService.apply(rules.build(new FlowRuleOperationsContext() {
103
104 private final Stopwatch timer = Stopwatch.createStarted();
105
106 @Override
107 public void onSuccess(FlowRuleOperations ops) {
108
109 timer.stop();
110 results.add(timer.elapsed(TimeUnit.MILLISECONDS));
111 if (results.size() == num) {
112 if (outputJson()) {
113 print("%s", json(new ObjectMapper(), true, results));
114 } else {
115 printTime(true, results);
116 }
117 }
118 latch.countDown();
119 }
120 }));
121
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800122 flowService.apply(remove.build(new FlowRuleOperationsContext() {
123 @Override
124 public void onSuccess(FlowRuleOperations ops) {
125 latch.countDown();
126 }
127 }));
alshabib3460da12014-10-30 17:26:49 +0100128 try {
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800129 latch.await();
130 } catch (InterruptedException e) {
alshabib3460da12014-10-30 17:26:49 +0100131 e.printStackTrace();
132 }
Brian O'Connor72cb19a2015-01-16 16:14:41 -0800133
alshabib5afcbd72014-10-30 16:28:40 +0100134 }
alshabib5afcbd72014-10-30 16:28:40 +0100135 }
136
alshabib3460da12014-10-30 17:26:49 +0100137 private Object json(ObjectMapper mapper, boolean isSuccess, ArrayList<Long> elapsed) {
138 ObjectNode result = mapper.createObjectNode();
139 result.put("Success", isSuccess);
140 ArrayNode node = result.putArray("elapsed-time");
141 for (Long v : elapsed) {
142 node.add(v);
143 }
144 return result;
145 }
146
147 private void printTime(boolean isSuccess, ArrayList<Long> elapsed) {
148 print("Run is %s.", isSuccess ? "success" : "failure");
149 for (int i = 0; i < elapsed.size(); i++) {
150 print(" Run %s : %s", i, elapsed.get(i));
151 }
152 }
alshabib5afcbd72014-10-30 16:28:40 +0100153}