blob: 4fcb0c83988bdcb8f738ca183310bfbcb61f5a77 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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
suibin zhang4d2b4902016-01-20 09:15:56 -080018import com.google.common.collect.Lists;
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070019import org.apache.karaf.shell.commands.Argument;
20import org.apache.karaf.shell.commands.Command;
Brian O'Connora0d31002014-12-01 13:43:18 -080021import org.apache.karaf.shell.commands.Option;
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -080022import org.onlab.packet.Ethernet;
23import org.onlab.packet.MacAddress;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.cli.AbstractShellCommand;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.net.ConnectPoint;
26import org.onosproject.net.DeviceId;
Ray Milkeya2cf3a12018-02-15 16:13:56 -080027import org.onosproject.net.FilteredConnectPoint;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.net.PortNumber;
29import org.onosproject.net.flow.DefaultTrafficSelector;
30import org.onosproject.net.flow.DefaultTrafficTreatment;
31import org.onosproject.net.flow.TrafficSelector;
32import org.onosproject.net.flow.TrafficTreatment;
33import org.onosproject.net.intent.Intent;
34import org.onosproject.net.intent.IntentEvent;
35import org.onosproject.net.intent.IntentEvent.Type;
36import org.onosproject.net.intent.IntentListener;
Brian O'Connorabafb502014-12-02 22:26:20 -080037import org.onosproject.net.intent.IntentService;
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -080038import org.onosproject.net.intent.Key;
Brian O'Connorabafb502014-12-02 22:26:20 -080039import org.onosproject.net.intent.PointToPointIntent;
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070040
suibin zhang4d2b4902016-01-20 09:15:56 -080041import java.util.ArrayList;
42import java.util.EnumSet;
43import java.util.List;
44import java.util.concurrent.CountDownLatch;
45import java.util.concurrent.TimeUnit;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070046
Brian O'Connorabafb502014-12-02 22:26:20 -080047import static org.onosproject.net.DeviceId.deviceId;
48import static org.onosproject.net.PortNumber.portNumber;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070049
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070050/**
Charles M.C. Chan6f5bdc62015-04-22 01:21:09 +080051 * Installs bulk point-to-point connectivity intents between given ingress/egress devices.
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070052 */
53@Command(scope = "onos", name = "push-test-intents",
54 description = "Installs random intents to test throughput")
55public class IntentPushTestCommand extends AbstractShellCommand
Thomas Vachuskab97cf282014-10-20 23:31:12 -070056 implements IntentListener {
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070057
58 @Argument(index = 0, name = "ingressDevice",
59 description = "Ingress Device/Port Description",
60 required = true, multiValued = false)
61 String ingressDeviceString = null;
62
63 @Argument(index = 1, name = "egressDevice",
64 description = "Egress Device/Port Description",
65 required = true, multiValued = false)
66 String egressDeviceString = null;
67
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -080068 @Argument(index = 2, name = "numberOfIntents",
69 description = "Number of intents to install/withdraw",
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080070 required = true, multiValued = false)
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -080071 String numberOfIntents = null;
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080072
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -080073 @Argument(index = 3, name = "keyOffset",
74 description = "Starting point for first key (default: 1)",
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080075 required = false, multiValued = false)
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -080076 String keyOffsetStr = null;
Brian O'Connora0d31002014-12-01 13:43:18 -080077
78 @Option(name = "-i", aliases = "--install",
79 description = "Install intents",
80 required = false, multiValued = false)
81 private boolean installOnly = false;
82
83 @Option(name = "-w", aliases = "--withdraw",
84 description = "Withdraw intents",
85 required = false, multiValued = false)
86 private boolean withdrawOnly = false;
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070087
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070088 private IntentService service;
89 private CountDownLatch latch;
Yuta HIGUCHIad492992014-12-03 14:13:54 -080090 private volatile long start, end;
Brian O'Connor510132a2014-11-19 14:09:20 -080091 private int count;
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -080092 private int keyOffset;
Brian O'Connor510132a2014-11-19 14:09:20 -080093 private boolean add;
suibin zhang4d2b4902016-01-20 09:15:56 -080094 List<Key> keysForInstall = new ArrayList<>();
95 List<Key> keysForWithdraw = new ArrayList<>();
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070096
97 @Override
98 protected void execute() {
99 service = get(IntentService.class);
100
Brian O'Connor6741a5f2014-11-19 20:40:11 -0800101
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700102 DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString));
103 PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700104 ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber);
105
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700106 DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString));
107 PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700108 ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber);
109
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800110 count = Integer.parseInt(numberOfIntents);
111 keyOffset = (keyOffsetStr != null) ? Integer.parseInt(keyOffsetStr) : 1;
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800112
Brian O'Connor510132a2014-11-19 14:09:20 -0800113 service.addListener(this);
114
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800115 List<Intent> operations = generateIntents(ingress, egress);
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800116
Brian O'Connora0d31002014-12-01 13:43:18 -0800117 boolean both = !(installOnly ^ withdrawOnly);
Brian O'Connor510132a2014-11-19 14:09:20 -0800118
Brian O'Connora0d31002014-12-01 13:43:18 -0800119 if (installOnly || both) {
120 add = true;
Brian O'Connora0d31002014-12-01 13:43:18 -0800121 submitIntents(operations);
122 }
123
124 if (withdrawOnly || both) {
Brian O'Connora0d31002014-12-01 13:43:18 -0800125 add = false;
Brian O'Connora0d31002014-12-01 13:43:18 -0800126 submitIntents(operations);
127 }
Brian O'Connor510132a2014-11-19 14:09:20 -0800128
129 service.removeListener(this);
130 }
131
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800132 private List<Intent> generateIntents(ConnectPoint ingress, ConnectPoint egress) {
133 TrafficSelector.Builder selectorBldr = DefaultTrafficSelector.builder()
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700134 .matchEthType(Ethernet.TYPE_IPV4);
Brian O'Connor6b528132015-03-10 16:39:52 -0700135 TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700136
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800137 List<Intent> intents = Lists.newArrayList();
Ray Milkey3717e602018-02-01 13:49:47 -0800138 for (long i = 0; i < count; i++) {
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800139 TrafficSelector selector = selectorBldr
140 .matchEthSrc(MacAddress.valueOf(i + keyOffset))
141 .build();
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700142 intents.add(PointToPointIntent.builder()
143 .appId(appId())
144 .key(Key.of(i + keyOffset, appId()))
145 .selector(selector)
146 .treatment(treatment)
Ray Milkeya2cf3a12018-02-15 16:13:56 -0800147 .filteredIngressPoint(new FilteredConnectPoint(ingress))
148 .filteredEgressPoint(new FilteredConnectPoint(egress))
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700149 .build());
suibin zhang4d2b4902016-01-20 09:15:56 -0800150 keysForInstall.add(Key.of(i + keyOffset, appId()));
151 keysForWithdraw.add(Key.of(i + keyOffset, appId()));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700152 }
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800153 return intents;
Brian O'Connor510132a2014-11-19 14:09:20 -0800154 }
155
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800156 private void submitIntents(List<Intent> intents) {
157 latch = new CountDownLatch(count);
suibin zhang4d2b4902016-01-20 09:15:56 -0800158 log.info("CountDownLatch is set with count of {}", count);
Brian O'Connor03406a42015-02-03 17:28:57 -0800159 start = System.currentTimeMillis();
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800160 for (Intent intent : intents) {
161 if (add) {
162 service.submit(intent);
163 } else {
164 service.withdraw(intent);
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800165 }
166 }
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800167
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700168 try {
Pier Luigib69b6cc2017-01-10 15:13:14 -0800169 // In this way with the tests in place the timeout will be
170 // 61 seconds.
Ray Milkey3717e602018-02-01 13:49:47 -0800171 if (latch.await(1000L + count * 60L, TimeUnit.MILLISECONDS)) {
alshabib7911a052014-10-16 17:49:37 -0700172 printResults(count);
173 } else {
Brian O'Connor510132a2014-11-19 14:09:20 -0800174 print("Failure: %d intents not installed", latch.getCount());
alshabib7911a052014-10-16 17:49:37 -0700175 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700176 } catch (InterruptedException e) {
177 print(e.toString());
178 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700179 }
180
181 private void printResults(int count) {
182 long delta = end - start;
Brian O'Connor510132a2014-11-19 14:09:20 -0800183 String text = add ? "install" : "withdraw";
184 print("Time to %s %d intents: %d ms", text, count, delta);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700185 }
186
187 /**
188 * Extracts the port number portion of the ConnectPoint.
189 *
190 * @param deviceString string representing the device/port
191 * @return port number as a string, empty string if the port is not found
192 */
193 private String getPortNumber(String deviceString) {
194 int slash = deviceString.indexOf('/');
195 if (slash <= 0) {
196 return "";
197 }
198 return deviceString.substring(slash + 1, deviceString.length());
199 }
200
201 /**
202 * Extracts the device ID portion of the ConnectPoint.
203 *
204 * @param deviceString string representing the device/port
205 * @return device ID string
206 */
207 private String getDeviceId(String deviceString) {
208 int slash = deviceString.indexOf('/');
209 if (slash <= 0) {
210 return "";
211 }
212 return deviceString.substring(0, slash);
213 }
214
Brian O'Connorae3e7332014-12-02 16:02:49 -0800215 private static final EnumSet<IntentEvent.Type> IGNORE_EVENT
216 = EnumSet.of(Type.INSTALL_REQ, Type.WITHDRAW_REQ);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700217 @Override
Yuta HIGUCHIad492992014-12-03 14:13:54 -0800218 public synchronized void event(IntentEvent event) {
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800219 if (!appId().equals(event.subject().appId())) {
Yuta HIGUCHI87695b82014-12-03 15:19:35 -0800220 // not my event, ignore
221 return;
222 }
Brian O'Connor510132a2014-11-19 14:09:20 -0800223 Type expected = add ? Type.INSTALLED : Type.WITHDRAWN;
suibin zhang4d2b4902016-01-20 09:15:56 -0800224 List keylist = add ? keysForInstall : keysForWithdraw;
225 log.debug("Event generated: {}", event);
226 if (event.type() == expected && keylist.contains(event.subject().key())) {
Yuta HIGUCHIad492992014-12-03 14:13:54 -0800227 end = Math.max(end, event.time());
suibin zhang4d2b4902016-01-20 09:15:56 -0800228 keylist.remove(event.subject().key());
Brian O'Connorea9021e2014-10-10 23:12:02 -0500229 if (latch != null) {
Yuta HIGUCHIad492992014-12-03 14:13:54 -0800230 if (latch.getCount() == 0) {
231 log.warn("Latch was already 0 before counting down?");
232 }
Brian O'Connorea9021e2014-10-10 23:12:02 -0500233 latch.countDown();
suibin zhang4d2b4902016-01-20 09:15:56 -0800234 log.debug("Latch count is {}", latch.getCount());
Brian O'Connorea9021e2014-10-10 23:12:02 -0500235 } else {
236 log.warn("install event latch is null");
237 }
Brian O'Connorae3e7332014-12-02 16:02:49 -0800238 } else if (IGNORE_EVENT.contains(event.type())) {
Brian O'Connor8016f342015-02-24 17:00:39 -0800239 log.debug("Unexpected intent event: {}", event);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700240 }
241 }
242}