blob: 15921fed04138c5a9630e8a5592fd89b357eec2a [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska7d693f52014-10-21 19:17:57 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuska7d693f52014-10-21 19:17:57 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska7d693f52014-10-21 19:17:57 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.cli.net;
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070017
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070018import java.util.EnumSet;
19import java.util.List;
20import java.util.concurrent.CountDownLatch;
21import java.util.concurrent.TimeUnit;
22
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070023import org.apache.karaf.shell.commands.Argument;
24import org.apache.karaf.shell.commands.Command;
Brian O'Connora0d31002014-12-01 13:43:18 -080025import org.apache.karaf.shell.commands.Option;
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -080026import org.onlab.packet.Ethernet;
27import org.onlab.packet.MacAddress;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.cli.AbstractShellCommand;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.net.ConnectPoint;
30import org.onosproject.net.DeviceId;
31import org.onosproject.net.PortNumber;
32import org.onosproject.net.flow.DefaultTrafficSelector;
33import org.onosproject.net.flow.DefaultTrafficTreatment;
34import org.onosproject.net.flow.TrafficSelector;
35import org.onosproject.net.flow.TrafficTreatment;
36import org.onosproject.net.intent.Intent;
37import org.onosproject.net.intent.IntentEvent;
38import org.onosproject.net.intent.IntentEvent.Type;
39import org.onosproject.net.intent.IntentListener;
Brian O'Connorabafb502014-12-02 22:26:20 -080040import org.onosproject.net.intent.IntentService;
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -080041import org.onosproject.net.intent.Key;
Brian O'Connorabafb502014-12-02 22:26:20 -080042import org.onosproject.net.intent.PointToPointIntent;
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070043
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070044import com.google.common.collect.Lists;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070045
Brian O'Connorabafb502014-12-02 22:26:20 -080046import static org.onosproject.net.DeviceId.deviceId;
47import static org.onosproject.net.PortNumber.portNumber;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070048
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070049/**
Charles M.C. Chan6f5bdc62015-04-22 01:21:09 +080050 * Installs bulk point-to-point connectivity intents between given ingress/egress devices.
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070051 */
52@Command(scope = "onos", name = "push-test-intents",
53 description = "Installs random intents to test throughput")
54public class IntentPushTestCommand extends AbstractShellCommand
Thomas Vachuskab97cf282014-10-20 23:31:12 -070055 implements IntentListener {
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070056
57 @Argument(index = 0, name = "ingressDevice",
58 description = "Ingress Device/Port Description",
59 required = true, multiValued = false)
60 String ingressDeviceString = null;
61
62 @Argument(index = 1, name = "egressDevice",
63 description = "Egress Device/Port Description",
64 required = true, multiValued = false)
65 String egressDeviceString = null;
66
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -080067 @Argument(index = 2, name = "numberOfIntents",
68 description = "Number of intents to install/withdraw",
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080069 required = true, multiValued = false)
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -080070 String numberOfIntents = null;
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080071
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -080072 @Argument(index = 3, name = "keyOffset",
73 description = "Starting point for first key (default: 1)",
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080074 required = false, multiValued = false)
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -080075 String keyOffsetStr = null;
Brian O'Connora0d31002014-12-01 13:43:18 -080076
77 @Option(name = "-i", aliases = "--install",
78 description = "Install intents",
79 required = false, multiValued = false)
80 private boolean installOnly = false;
81
82 @Option(name = "-w", aliases = "--withdraw",
83 description = "Withdraw intents",
84 required = false, multiValued = false)
85 private boolean withdrawOnly = false;
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070086
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070087 private IntentService service;
88 private CountDownLatch latch;
Yuta HIGUCHIad492992014-12-03 14:13:54 -080089 private volatile long start, end;
Brian O'Connor510132a2014-11-19 14:09:20 -080090 private int count;
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -080091 private int keyOffset;
Brian O'Connor510132a2014-11-19 14:09:20 -080092 private boolean add;
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070093
94 @Override
95 protected void execute() {
96 service = get(IntentService.class);
97
Brian O'Connor6741a5f2014-11-19 20:40:11 -080098
Thomas Vachuskab97cf282014-10-20 23:31:12 -070099 DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString));
100 PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700101 ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber);
102
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700103 DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString));
104 PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700105 ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber);
106
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800107 count = Integer.parseInt(numberOfIntents);
108 keyOffset = (keyOffsetStr != null) ? Integer.parseInt(keyOffsetStr) : 1;
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800109
Brian O'Connor510132a2014-11-19 14:09:20 -0800110 service.addListener(this);
111
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800112 List<Intent> operations = generateIntents(ingress, egress);
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800113
Brian O'Connora0d31002014-12-01 13:43:18 -0800114 boolean both = !(installOnly ^ withdrawOnly);
Brian O'Connor510132a2014-11-19 14:09:20 -0800115
Brian O'Connora0d31002014-12-01 13:43:18 -0800116 if (installOnly || both) {
117 add = true;
Brian O'Connora0d31002014-12-01 13:43:18 -0800118 submitIntents(operations);
119 }
120
121 if (withdrawOnly || both) {
Brian O'Connora0d31002014-12-01 13:43:18 -0800122 add = false;
Brian O'Connora0d31002014-12-01 13:43:18 -0800123 submitIntents(operations);
124 }
Brian O'Connor510132a2014-11-19 14:09:20 -0800125
126 service.removeListener(this);
127 }
128
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800129 private List<Intent> generateIntents(ConnectPoint ingress, ConnectPoint egress) {
130 TrafficSelector.Builder selectorBldr = DefaultTrafficSelector.builder()
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700131 .matchEthType(Ethernet.TYPE_IPV4);
Brian O'Connor6b528132015-03-10 16:39:52 -0700132 TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700133
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800134 List<Intent> intents = Lists.newArrayList();
135 for (int i = 0; i < count; i++) {
136 TrafficSelector selector = selectorBldr
137 .matchEthSrc(MacAddress.valueOf(i + keyOffset))
138 .build();
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700139 intents.add(PointToPointIntent.builder()
140 .appId(appId())
141 .key(Key.of(i + keyOffset, appId()))
142 .selector(selector)
143 .treatment(treatment)
144 .ingressPoint(ingress)
145 .egressPoint(egress)
146 .build());
147
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800148
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700149 }
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800150 return intents;
Brian O'Connor510132a2014-11-19 14:09:20 -0800151 }
152
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800153 private void submitIntents(List<Intent> intents) {
154 latch = new CountDownLatch(count);
Brian O'Connor03406a42015-02-03 17:28:57 -0800155 start = System.currentTimeMillis();
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800156 for (Intent intent : intents) {
157 if (add) {
158 service.submit(intent);
159 } else {
160 service.withdraw(intent);
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800161 }
162 }
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800163
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700164 try {
Brian O'Connor7d405fe2015-02-24 11:38:16 -0800165 if (latch.await(500 + count * 30, TimeUnit.MILLISECONDS)) {
alshabib7911a052014-10-16 17:49:37 -0700166 printResults(count);
167 } else {
Brian O'Connor510132a2014-11-19 14:09:20 -0800168 print("Failure: %d intents not installed", latch.getCount());
alshabib7911a052014-10-16 17:49:37 -0700169 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700170 } catch (InterruptedException e) {
171 print(e.toString());
172 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700173 }
174
175 private void printResults(int count) {
176 long delta = end - start;
Brian O'Connor510132a2014-11-19 14:09:20 -0800177 String text = add ? "install" : "withdraw";
178 print("Time to %s %d intents: %d ms", text, count, delta);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700179 }
180
181 /**
182 * Extracts the port number portion of the ConnectPoint.
183 *
184 * @param deviceString string representing the device/port
185 * @return port number as a string, empty string if the port is not found
186 */
187 private String getPortNumber(String deviceString) {
188 int slash = deviceString.indexOf('/');
189 if (slash <= 0) {
190 return "";
191 }
192 return deviceString.substring(slash + 1, deviceString.length());
193 }
194
195 /**
196 * Extracts the device ID portion of the ConnectPoint.
197 *
198 * @param deviceString string representing the device/port
199 * @return device ID string
200 */
201 private String getDeviceId(String deviceString) {
202 int slash = deviceString.indexOf('/');
203 if (slash <= 0) {
204 return "";
205 }
206 return deviceString.substring(0, slash);
207 }
208
Brian O'Connorae3e7332014-12-02 16:02:49 -0800209 private static final EnumSet<IntentEvent.Type> IGNORE_EVENT
210 = EnumSet.of(Type.INSTALL_REQ, Type.WITHDRAW_REQ);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700211 @Override
Yuta HIGUCHIad492992014-12-03 14:13:54 -0800212 public synchronized void event(IntentEvent event) {
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800213 if (!appId().equals(event.subject().appId())) {
Yuta HIGUCHI87695b82014-12-03 15:19:35 -0800214 // not my event, ignore
215 return;
216 }
Brian O'Connor510132a2014-11-19 14:09:20 -0800217 Type expected = add ? Type.INSTALLED : Type.WITHDRAWN;
218 if (event.type() == expected) {
Yuta HIGUCHIad492992014-12-03 14:13:54 -0800219 end = Math.max(end, event.time());
Brian O'Connorea9021e2014-10-10 23:12:02 -0500220 if (latch != null) {
Yuta HIGUCHIad492992014-12-03 14:13:54 -0800221 if (latch.getCount() == 0) {
222 log.warn("Latch was already 0 before counting down?");
223 }
Brian O'Connorea9021e2014-10-10 23:12:02 -0500224 latch.countDown();
225 } else {
226 log.warn("install event latch is null");
227 }
Brian O'Connorae3e7332014-12-02 16:02:49 -0800228 } else if (IGNORE_EVENT.contains(event.type())) {
Brian O'Connor8016f342015-02-24 17:00:39 -0800229 log.debug("Unexpected intent event: {}", event);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700230 }
231 }
232}