blob: 92209d774bea37ab7e8f40262e4d606f08cf3e92 [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;
27import org.onosproject.net.PortNumber;
28import org.onosproject.net.flow.DefaultTrafficSelector;
29import org.onosproject.net.flow.DefaultTrafficTreatment;
30import org.onosproject.net.flow.TrafficSelector;
31import org.onosproject.net.flow.TrafficTreatment;
32import org.onosproject.net.intent.Intent;
33import org.onosproject.net.intent.IntentEvent;
34import org.onosproject.net.intent.IntentEvent.Type;
35import org.onosproject.net.intent.IntentListener;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import org.onosproject.net.intent.IntentService;
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -080037import org.onosproject.net.intent.Key;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import org.onosproject.net.intent.PointToPointIntent;
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070039
suibin zhang4d2b4902016-01-20 09:15:56 -080040import java.util.ArrayList;
41import java.util.EnumSet;
42import java.util.List;
43import java.util.concurrent.CountDownLatch;
44import java.util.concurrent.TimeUnit;
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;
suibin zhang4d2b4902016-01-20 09:15:56 -080093 List<Key> keysForInstall = new ArrayList<>();
94 List<Key> keysForWithdraw = new ArrayList<>();
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070095
96 @Override
97 protected void execute() {
98 service = get(IntentService.class);
99
Brian O'Connor6741a5f2014-11-19 20:40:11 -0800100
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700101 DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString));
102 PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700103 ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber);
104
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700105 DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString));
106 PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700107 ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber);
108
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800109 count = Integer.parseInt(numberOfIntents);
110 keyOffset = (keyOffsetStr != null) ? Integer.parseInt(keyOffsetStr) : 1;
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800111
Brian O'Connor510132a2014-11-19 14:09:20 -0800112 service.addListener(this);
113
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800114 List<Intent> operations = generateIntents(ingress, egress);
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800115
Brian O'Connora0d31002014-12-01 13:43:18 -0800116 boolean both = !(installOnly ^ withdrawOnly);
Brian O'Connor510132a2014-11-19 14:09:20 -0800117
Brian O'Connora0d31002014-12-01 13:43:18 -0800118 if (installOnly || both) {
119 add = true;
Brian O'Connora0d31002014-12-01 13:43:18 -0800120 submitIntents(operations);
121 }
122
123 if (withdrawOnly || both) {
Brian O'Connora0d31002014-12-01 13:43:18 -0800124 add = false;
Brian O'Connora0d31002014-12-01 13:43:18 -0800125 submitIntents(operations);
126 }
Brian O'Connor510132a2014-11-19 14:09:20 -0800127
128 service.removeListener(this);
129 }
130
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800131 private List<Intent> generateIntents(ConnectPoint ingress, ConnectPoint egress) {
132 TrafficSelector.Builder selectorBldr = DefaultTrafficSelector.builder()
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700133 .matchEthType(Ethernet.TYPE_IPV4);
Brian O'Connor6b528132015-03-10 16:39:52 -0700134 TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700135
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800136 List<Intent> intents = Lists.newArrayList();
137 for (int i = 0; i < count; i++) {
138 TrafficSelector selector = selectorBldr
139 .matchEthSrc(MacAddress.valueOf(i + keyOffset))
140 .build();
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700141 intents.add(PointToPointIntent.builder()
142 .appId(appId())
143 .key(Key.of(i + keyOffset, appId()))
144 .selector(selector)
145 .treatment(treatment)
146 .ingressPoint(ingress)
147 .egressPoint(egress)
148 .build());
suibin zhang4d2b4902016-01-20 09:15:56 -0800149 keysForInstall.add(Key.of(i + keyOffset, appId()));
150 keysForWithdraw.add(Key.of(i + keyOffset, appId()));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700151 }
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800152 return intents;
Brian O'Connor510132a2014-11-19 14:09:20 -0800153 }
154
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800155 private void submitIntents(List<Intent> intents) {
156 latch = new CountDownLatch(count);
suibin zhang4d2b4902016-01-20 09:15:56 -0800157 log.info("CountDownLatch is set with count of {}", count);
Brian O'Connor03406a42015-02-03 17:28:57 -0800158 start = System.currentTimeMillis();
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800159 for (Intent intent : intents) {
160 if (add) {
161 service.submit(intent);
162 } else {
163 service.withdraw(intent);
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800164 }
165 }
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800166
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700167 try {
Pier Luigib69b6cc2017-01-10 15:13:14 -0800168 // In this way with the tests in place the timeout will be
169 // 61 seconds.
170 if (latch.await(1000 + count * 60, TimeUnit.MILLISECONDS)) {
alshabib7911a052014-10-16 17:49:37 -0700171 printResults(count);
172 } else {
Brian O'Connor510132a2014-11-19 14:09:20 -0800173 print("Failure: %d intents not installed", latch.getCount());
alshabib7911a052014-10-16 17:49:37 -0700174 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700175 } catch (InterruptedException e) {
176 print(e.toString());
177 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700178 }
179
180 private void printResults(int count) {
181 long delta = end - start;
Brian O'Connor510132a2014-11-19 14:09:20 -0800182 String text = add ? "install" : "withdraw";
183 print("Time to %s %d intents: %d ms", text, count, delta);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700184 }
185
186 /**
187 * Extracts the port number portion of the ConnectPoint.
188 *
189 * @param deviceString string representing the device/port
190 * @return port number as a string, empty string if the port is not found
191 */
192 private String getPortNumber(String deviceString) {
193 int slash = deviceString.indexOf('/');
194 if (slash <= 0) {
195 return "";
196 }
197 return deviceString.substring(slash + 1, deviceString.length());
198 }
199
200 /**
201 * Extracts the device ID portion of the ConnectPoint.
202 *
203 * @param deviceString string representing the device/port
204 * @return device ID string
205 */
206 private String getDeviceId(String deviceString) {
207 int slash = deviceString.indexOf('/');
208 if (slash <= 0) {
209 return "";
210 }
211 return deviceString.substring(0, slash);
212 }
213
Brian O'Connorae3e7332014-12-02 16:02:49 -0800214 private static final EnumSet<IntentEvent.Type> IGNORE_EVENT
215 = EnumSet.of(Type.INSTALL_REQ, Type.WITHDRAW_REQ);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700216 @Override
Yuta HIGUCHIad492992014-12-03 14:13:54 -0800217 public synchronized void event(IntentEvent event) {
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800218 if (!appId().equals(event.subject().appId())) {
Yuta HIGUCHI87695b82014-12-03 15:19:35 -0800219 // not my event, ignore
220 return;
221 }
Brian O'Connor510132a2014-11-19 14:09:20 -0800222 Type expected = add ? Type.INSTALLED : Type.WITHDRAWN;
suibin zhang4d2b4902016-01-20 09:15:56 -0800223 List keylist = add ? keysForInstall : keysForWithdraw;
224 log.debug("Event generated: {}", event);
225 if (event.type() == expected && keylist.contains(event.subject().key())) {
Yuta HIGUCHIad492992014-12-03 14:13:54 -0800226 end = Math.max(end, event.time());
suibin zhang4d2b4902016-01-20 09:15:56 -0800227 keylist.remove(event.subject().key());
Brian O'Connorea9021e2014-10-10 23:12:02 -0500228 if (latch != null) {
Yuta HIGUCHIad492992014-12-03 14:13:54 -0800229 if (latch.getCount() == 0) {
230 log.warn("Latch was already 0 before counting down?");
231 }
Brian O'Connorea9021e2014-10-10 23:12:02 -0500232 latch.countDown();
suibin zhang4d2b4902016-01-20 09:15:56 -0800233 log.debug("Latch count is {}", latch.getCount());
Brian O'Connorea9021e2014-10-10 23:12:02 -0500234 } else {
235 log.warn("install event latch is null");
236 }
Brian O'Connorae3e7332014-12-02 16:02:49 -0800237 } else if (IGNORE_EVENT.contains(event.type())) {
Brian O'Connor8016f342015-02-24 17:00:39 -0800238 log.debug("Unexpected intent event: {}", event);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700239 }
240 }
241}