blob: bca7935377f33d0a0ca66346a796e5e89fe5df48 [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;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070019import org.apache.karaf.shell.api.action.Argument;
20import org.apache.karaf.shell.api.action.Command;
21import org.apache.karaf.shell.api.action.lifecycle.Service;
22import org.apache.karaf.shell.api.action.Option;
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -080023import org.onlab.packet.Ethernet;
24import org.onlab.packet.MacAddress;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.cli.AbstractShellCommand;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.net.ConnectPoint;
27import org.onosproject.net.DeviceId;
Ray Milkeya2cf3a12018-02-15 16:13:56 -080028import org.onosproject.net.FilteredConnectPoint;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.net.PortNumber;
30import org.onosproject.net.flow.DefaultTrafficSelector;
31import org.onosproject.net.flow.DefaultTrafficTreatment;
32import org.onosproject.net.flow.TrafficSelector;
33import org.onosproject.net.flow.TrafficTreatment;
34import org.onosproject.net.intent.Intent;
35import org.onosproject.net.intent.IntentEvent;
36import org.onosproject.net.intent.IntentEvent.Type;
37import org.onosproject.net.intent.IntentListener;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import org.onosproject.net.intent.IntentService;
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -080039import org.onosproject.net.intent.Key;
Brian O'Connorabafb502014-12-02 22:26:20 -080040import org.onosproject.net.intent.PointToPointIntent;
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070041
suibin zhang4d2b4902016-01-20 09:15:56 -080042import java.util.ArrayList;
43import java.util.EnumSet;
44import java.util.List;
45import java.util.concurrent.CountDownLatch;
46import java.util.concurrent.TimeUnit;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070047
Brian O'Connorabafb502014-12-02 22:26:20 -080048import static org.onosproject.net.DeviceId.deviceId;
49import static org.onosproject.net.PortNumber.portNumber;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070050
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070051/**
Charles M.C. Chan6f5bdc62015-04-22 01:21:09 +080052 * Installs bulk point-to-point connectivity intents between given ingress/egress devices.
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070053 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070054@Service
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070055@Command(scope = "onos", name = "push-test-intents",
56 description = "Installs random intents to test throughput")
57public class IntentPushTestCommand extends AbstractShellCommand
Thomas Vachuskab97cf282014-10-20 23:31:12 -070058 implements IntentListener {
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070059
60 @Argument(index = 0, name = "ingressDevice",
61 description = "Ingress Device/Port Description",
62 required = true, multiValued = false)
63 String ingressDeviceString = null;
64
65 @Argument(index = 1, name = "egressDevice",
66 description = "Egress Device/Port Description",
67 required = true, multiValued = false)
68 String egressDeviceString = null;
69
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -080070 @Argument(index = 2, name = "numberOfIntents",
71 description = "Number of intents to install/withdraw",
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080072 required = true, multiValued = false)
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -080073 String numberOfIntents = null;
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080074
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -080075 @Argument(index = 3, name = "keyOffset",
76 description = "Starting point for first key (default: 1)",
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080077 required = false, multiValued = false)
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -080078 String keyOffsetStr = null;
Brian O'Connora0d31002014-12-01 13:43:18 -080079
80 @Option(name = "-i", aliases = "--install",
81 description = "Install intents",
82 required = false, multiValued = false)
83 private boolean installOnly = false;
84
85 @Option(name = "-w", aliases = "--withdraw",
86 description = "Withdraw intents",
87 required = false, multiValued = false)
88 private boolean withdrawOnly = false;
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070089
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070090 private IntentService service;
91 private CountDownLatch latch;
Yuta HIGUCHIad492992014-12-03 14:13:54 -080092 private volatile long start, end;
Brian O'Connor510132a2014-11-19 14:09:20 -080093 private int count;
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -080094 private int keyOffset;
Brian O'Connor510132a2014-11-19 14:09:20 -080095 private boolean add;
suibin zhang4d2b4902016-01-20 09:15:56 -080096 List<Key> keysForInstall = new ArrayList<>();
97 List<Key> keysForWithdraw = new ArrayList<>();
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070098
99 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700100 protected void doExecute() {
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700101 service = get(IntentService.class);
102
Brian O'Connor6741a5f2014-11-19 20:40:11 -0800103
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700104 DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString));
105 PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700106 ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber);
107
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700108 DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString));
109 PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700110 ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber);
111
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800112 count = Integer.parseInt(numberOfIntents);
113 keyOffset = (keyOffsetStr != null) ? Integer.parseInt(keyOffsetStr) : 1;
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800114
Brian O'Connor510132a2014-11-19 14:09:20 -0800115 service.addListener(this);
116
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800117 List<Intent> operations = generateIntents(ingress, egress);
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800118
Brian O'Connora0d31002014-12-01 13:43:18 -0800119 boolean both = !(installOnly ^ withdrawOnly);
Brian O'Connor510132a2014-11-19 14:09:20 -0800120
Brian O'Connora0d31002014-12-01 13:43:18 -0800121 if (installOnly || both) {
122 add = true;
Brian O'Connora0d31002014-12-01 13:43:18 -0800123 submitIntents(operations);
124 }
125
126 if (withdrawOnly || both) {
Brian O'Connora0d31002014-12-01 13:43:18 -0800127 add = false;
Brian O'Connora0d31002014-12-01 13:43:18 -0800128 submitIntents(operations);
129 }
Brian O'Connor510132a2014-11-19 14:09:20 -0800130
131 service.removeListener(this);
132 }
133
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800134 private List<Intent> generateIntents(ConnectPoint ingress, ConnectPoint egress) {
135 TrafficSelector.Builder selectorBldr = DefaultTrafficSelector.builder()
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700136 .matchEthType(Ethernet.TYPE_IPV4);
Brian O'Connor6b528132015-03-10 16:39:52 -0700137 TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700138
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800139 List<Intent> intents = Lists.newArrayList();
Ray Milkey3717e602018-02-01 13:49:47 -0800140 for (long i = 0; i < count; i++) {
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800141 TrafficSelector selector = selectorBldr
142 .matchEthSrc(MacAddress.valueOf(i + keyOffset))
143 .build();
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700144 intents.add(PointToPointIntent.builder()
145 .appId(appId())
146 .key(Key.of(i + keyOffset, appId()))
147 .selector(selector)
148 .treatment(treatment)
Ray Milkeya2cf3a12018-02-15 16:13:56 -0800149 .filteredIngressPoint(new FilteredConnectPoint(ingress))
150 .filteredEgressPoint(new FilteredConnectPoint(egress))
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700151 .build());
suibin zhang4d2b4902016-01-20 09:15:56 -0800152 keysForInstall.add(Key.of(i + keyOffset, appId()));
153 keysForWithdraw.add(Key.of(i + keyOffset, appId()));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700154 }
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800155 return intents;
Brian O'Connor510132a2014-11-19 14:09:20 -0800156 }
157
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800158 private void submitIntents(List<Intent> intents) {
159 latch = new CountDownLatch(count);
suibin zhang4d2b4902016-01-20 09:15:56 -0800160 log.info("CountDownLatch is set with count of {}", count);
Brian O'Connor03406a42015-02-03 17:28:57 -0800161 start = System.currentTimeMillis();
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800162 for (Intent intent : intents) {
163 if (add) {
164 service.submit(intent);
165 } else {
166 service.withdraw(intent);
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800167 }
168 }
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800169
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700170 try {
Pier Luigib69b6cc2017-01-10 15:13:14 -0800171 // In this way with the tests in place the timeout will be
172 // 61 seconds.
Ray Milkey3717e602018-02-01 13:49:47 -0800173 if (latch.await(1000L + count * 60L, TimeUnit.MILLISECONDS)) {
alshabib7911a052014-10-16 17:49:37 -0700174 printResults(count);
175 } else {
Brian O'Connor510132a2014-11-19 14:09:20 -0800176 print("Failure: %d intents not installed", latch.getCount());
alshabib7911a052014-10-16 17:49:37 -0700177 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700178 } catch (InterruptedException e) {
179 print(e.toString());
180 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700181 }
182
183 private void printResults(int count) {
184 long delta = end - start;
Brian O'Connor510132a2014-11-19 14:09:20 -0800185 String text = add ? "install" : "withdraw";
186 print("Time to %s %d intents: %d ms", text, count, delta);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700187 }
188
189 /**
190 * Extracts the port number portion of the ConnectPoint.
191 *
192 * @param deviceString string representing the device/port
193 * @return port number as a string, empty string if the port is not found
194 */
195 private String getPortNumber(String deviceString) {
196 int slash = deviceString.indexOf('/');
197 if (slash <= 0) {
198 return "";
199 }
200 return deviceString.substring(slash + 1, deviceString.length());
201 }
202
203 /**
204 * Extracts the device ID portion of the ConnectPoint.
205 *
206 * @param deviceString string representing the device/port
207 * @return device ID string
208 */
209 private String getDeviceId(String deviceString) {
210 int slash = deviceString.indexOf('/');
211 if (slash <= 0) {
212 return "";
213 }
214 return deviceString.substring(0, slash);
215 }
216
Brian O'Connorae3e7332014-12-02 16:02:49 -0800217 private static final EnumSet<IntentEvent.Type> IGNORE_EVENT
218 = EnumSet.of(Type.INSTALL_REQ, Type.WITHDRAW_REQ);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700219 @Override
Yuta HIGUCHIad492992014-12-03 14:13:54 -0800220 public synchronized void event(IntentEvent event) {
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800221 if (!appId().equals(event.subject().appId())) {
Yuta HIGUCHI87695b82014-12-03 15:19:35 -0800222 // not my event, ignore
223 return;
224 }
Brian O'Connor510132a2014-11-19 14:09:20 -0800225 Type expected = add ? Type.INSTALLED : Type.WITHDRAWN;
suibin zhang4d2b4902016-01-20 09:15:56 -0800226 List keylist = add ? keysForInstall : keysForWithdraw;
227 log.debug("Event generated: {}", event);
228 if (event.type() == expected && keylist.contains(event.subject().key())) {
Yuta HIGUCHIad492992014-12-03 14:13:54 -0800229 end = Math.max(end, event.time());
suibin zhang4d2b4902016-01-20 09:15:56 -0800230 keylist.remove(event.subject().key());
Brian O'Connorea9021e2014-10-10 23:12:02 -0500231 if (latch != null) {
Yuta HIGUCHIad492992014-12-03 14:13:54 -0800232 if (latch.getCount() == 0) {
233 log.warn("Latch was already 0 before counting down?");
234 }
Brian O'Connorea9021e2014-10-10 23:12:02 -0500235 latch.countDown();
suibin zhang4d2b4902016-01-20 09:15:56 -0800236 log.debug("Latch count is {}", latch.getCount());
Brian O'Connorea9021e2014-10-10 23:12:02 -0500237 } else {
238 log.warn("install event latch is null");
239 }
Brian O'Connorae3e7332014-12-02 16:02:49 -0800240 } else if (IGNORE_EVENT.contains(event.type())) {
Brian O'Connor8016f342015-02-24 17:00:39 -0800241 log.debug("Unexpected intent event: {}", event);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700242 }
243 }
244}