blob: bc51f88ca60e00a844ebc52510188a91a828c1d8 [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;
Ray Milkey0068fd02018-10-11 15:45:39 -070021import org.apache.karaf.shell.api.action.Completion;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070022import org.apache.karaf.shell.api.action.lifecycle.Service;
23import org.apache.karaf.shell.api.action.Option;
Ray Milkey0068fd02018-10-11 15:45:39 -070024import org.apache.karaf.shell.support.completers.NullCompleter;
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -080025import org.onlab.packet.Ethernet;
26import org.onlab.packet.MacAddress;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.cli.AbstractShellCommand;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.net.ConnectPoint;
29import org.onosproject.net.DeviceId;
Ray Milkeya2cf3a12018-02-15 16:13:56 -080030import org.onosproject.net.FilteredConnectPoint;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import 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
suibin zhang4d2b4902016-01-20 09:15:56 -080044import java.util.ArrayList;
45import java.util.EnumSet;
46import java.util.List;
47import java.util.concurrent.CountDownLatch;
48import java.util.concurrent.TimeUnit;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070049
Brian O'Connorabafb502014-12-02 22:26:20 -080050import static org.onosproject.net.DeviceId.deviceId;
51import static org.onosproject.net.PortNumber.portNumber;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070052
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070053/**
Charles M.C. Chan6f5bdc62015-04-22 01:21:09 +080054 * Installs bulk point-to-point connectivity intents between given ingress/egress devices.
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070055 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070056@Service
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070057@Command(scope = "onos", name = "push-test-intents",
58 description = "Installs random intents to test throughput")
59public class IntentPushTestCommand extends AbstractShellCommand
Thomas Vachuskab97cf282014-10-20 23:31:12 -070060 implements IntentListener {
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070061
62 @Argument(index = 0, name = "ingressDevice",
63 description = "Ingress Device/Port Description",
64 required = true, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070065 @Completion(ConnectPointCompleter.class)
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070066 String ingressDeviceString = null;
67
68 @Argument(index = 1, name = "egressDevice",
69 description = "Egress Device/Port Description",
70 required = true, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070071 @Completion(ConnectPointCompleter.class)
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070072 String egressDeviceString = null;
73
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -080074 @Argument(index = 2, name = "numberOfIntents",
75 description = "Number of intents to install/withdraw",
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080076 required = true, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070077 @Completion(NullCompleter.class)
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -080078 String numberOfIntents = null;
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080079
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -080080 @Argument(index = 3, name = "keyOffset",
81 description = "Starting point for first key (default: 1)",
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080082 required = false, multiValued = false)
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -080083 String keyOffsetStr = null;
Brian O'Connora0d31002014-12-01 13:43:18 -080084
85 @Option(name = "-i", aliases = "--install",
86 description = "Install intents",
87 required = false, multiValued = false)
88 private boolean installOnly = false;
89
90 @Option(name = "-w", aliases = "--withdraw",
91 description = "Withdraw intents",
92 required = false, multiValued = false)
93 private boolean withdrawOnly = false;
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070094
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070095 private IntentService service;
96 private CountDownLatch latch;
Yuta HIGUCHIad492992014-12-03 14:13:54 -080097 private volatile long start, end;
Brian O'Connor510132a2014-11-19 14:09:20 -080098 private int count;
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -080099 private int keyOffset;
Brian O'Connor510132a2014-11-19 14:09:20 -0800100 private boolean add;
suibin zhang4d2b4902016-01-20 09:15:56 -0800101 List<Key> keysForInstall = new ArrayList<>();
102 List<Key> keysForWithdraw = new ArrayList<>();
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700103
104 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700105 protected void doExecute() {
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700106 service = get(IntentService.class);
107
Brian O'Connor6741a5f2014-11-19 20:40:11 -0800108
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700109 DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString));
110 PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700111 ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber);
112
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700113 DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString));
114 PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700115 ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber);
116
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800117 count = Integer.parseInt(numberOfIntents);
118 keyOffset = (keyOffsetStr != null) ? Integer.parseInt(keyOffsetStr) : 1;
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800119
Brian O'Connor510132a2014-11-19 14:09:20 -0800120 service.addListener(this);
121
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800122 List<Intent> operations = generateIntents(ingress, egress);
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800123
Brian O'Connora0d31002014-12-01 13:43:18 -0800124 boolean both = !(installOnly ^ withdrawOnly);
Brian O'Connor510132a2014-11-19 14:09:20 -0800125
Brian O'Connora0d31002014-12-01 13:43:18 -0800126 if (installOnly || both) {
127 add = true;
Brian O'Connora0d31002014-12-01 13:43:18 -0800128 submitIntents(operations);
129 }
130
131 if (withdrawOnly || both) {
Brian O'Connora0d31002014-12-01 13:43:18 -0800132 add = false;
Brian O'Connora0d31002014-12-01 13:43:18 -0800133 submitIntents(operations);
134 }
Brian O'Connor510132a2014-11-19 14:09:20 -0800135
136 service.removeListener(this);
137 }
138
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800139 private List<Intent> generateIntents(ConnectPoint ingress, ConnectPoint egress) {
140 TrafficSelector.Builder selectorBldr = DefaultTrafficSelector.builder()
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700141 .matchEthType(Ethernet.TYPE_IPV4);
Brian O'Connor6b528132015-03-10 16:39:52 -0700142 TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700143
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800144 List<Intent> intents = Lists.newArrayList();
Ray Milkey3717e602018-02-01 13:49:47 -0800145 for (long i = 0; i < count; i++) {
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800146 TrafficSelector selector = selectorBldr
147 .matchEthSrc(MacAddress.valueOf(i + keyOffset))
148 .build();
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700149 intents.add(PointToPointIntent.builder()
150 .appId(appId())
151 .key(Key.of(i + keyOffset, appId()))
152 .selector(selector)
153 .treatment(treatment)
Ray Milkeya2cf3a12018-02-15 16:13:56 -0800154 .filteredIngressPoint(new FilteredConnectPoint(ingress))
155 .filteredEgressPoint(new FilteredConnectPoint(egress))
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700156 .build());
suibin zhang4d2b4902016-01-20 09:15:56 -0800157 keysForInstall.add(Key.of(i + keyOffset, appId()));
158 keysForWithdraw.add(Key.of(i + keyOffset, appId()));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700159 }
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800160 return intents;
Brian O'Connor510132a2014-11-19 14:09:20 -0800161 }
162
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800163 private void submitIntents(List<Intent> intents) {
164 latch = new CountDownLatch(count);
suibin zhang4d2b4902016-01-20 09:15:56 -0800165 log.info("CountDownLatch is set with count of {}", count);
Brian O'Connor03406a42015-02-03 17:28:57 -0800166 start = System.currentTimeMillis();
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800167 for (Intent intent : intents) {
168 if (add) {
169 service.submit(intent);
170 } else {
171 service.withdraw(intent);
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800172 }
173 }
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800174
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700175 try {
Pier Luigib69b6cc2017-01-10 15:13:14 -0800176 // In this way with the tests in place the timeout will be
177 // 61 seconds.
Ray Milkey3717e602018-02-01 13:49:47 -0800178 if (latch.await(1000L + count * 60L, TimeUnit.MILLISECONDS)) {
alshabib7911a052014-10-16 17:49:37 -0700179 printResults(count);
180 } else {
Brian O'Connor510132a2014-11-19 14:09:20 -0800181 print("Failure: %d intents not installed", latch.getCount());
alshabib7911a052014-10-16 17:49:37 -0700182 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700183 } catch (InterruptedException e) {
184 print(e.toString());
185 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700186 }
187
188 private void printResults(int count) {
189 long delta = end - start;
Brian O'Connor510132a2014-11-19 14:09:20 -0800190 String text = add ? "install" : "withdraw";
191 print("Time to %s %d intents: %d ms", text, count, delta);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700192 }
193
194 /**
195 * Extracts the port number portion of the ConnectPoint.
196 *
197 * @param deviceString string representing the device/port
198 * @return port number as a string, empty string if the port is not found
199 */
200 private String getPortNumber(String deviceString) {
201 int slash = deviceString.indexOf('/');
202 if (slash <= 0) {
203 return "";
204 }
205 return deviceString.substring(slash + 1, deviceString.length());
206 }
207
208 /**
209 * Extracts the device ID portion of the ConnectPoint.
210 *
211 * @param deviceString string representing the device/port
212 * @return device ID string
213 */
214 private String getDeviceId(String deviceString) {
215 int slash = deviceString.indexOf('/');
216 if (slash <= 0) {
217 return "";
218 }
219 return deviceString.substring(0, slash);
220 }
221
Brian O'Connorae3e7332014-12-02 16:02:49 -0800222 private static final EnumSet<IntentEvent.Type> IGNORE_EVENT
223 = EnumSet.of(Type.INSTALL_REQ, Type.WITHDRAW_REQ);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700224 @Override
Yuta HIGUCHIad492992014-12-03 14:13:54 -0800225 public synchronized void event(IntentEvent event) {
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800226 if (!appId().equals(event.subject().appId())) {
Yuta HIGUCHI87695b82014-12-03 15:19:35 -0800227 // not my event, ignore
228 return;
229 }
Brian O'Connor510132a2014-11-19 14:09:20 -0800230 Type expected = add ? Type.INSTALLED : Type.WITHDRAWN;
suibin zhang4d2b4902016-01-20 09:15:56 -0800231 List keylist = add ? keysForInstall : keysForWithdraw;
232 log.debug("Event generated: {}", event);
233 if (event.type() == expected && keylist.contains(event.subject().key())) {
Yuta HIGUCHIad492992014-12-03 14:13:54 -0800234 end = Math.max(end, event.time());
suibin zhang4d2b4902016-01-20 09:15:56 -0800235 keylist.remove(event.subject().key());
Brian O'Connorea9021e2014-10-10 23:12:02 -0500236 if (latch != null) {
Yuta HIGUCHIad492992014-12-03 14:13:54 -0800237 if (latch.getCount() == 0) {
238 log.warn("Latch was already 0 before counting down?");
239 }
Brian O'Connorea9021e2014-10-10 23:12:02 -0500240 latch.countDown();
suibin zhang4d2b4902016-01-20 09:15:56 -0800241 log.debug("Latch count is {}", latch.getCount());
Brian O'Connorea9021e2014-10-10 23:12:02 -0500242 } else {
243 log.warn("install event latch is null");
244 }
Brian O'Connorae3e7332014-12-02 16:02:49 -0800245 } else if (IGNORE_EVENT.contains(event.type())) {
Brian O'Connor8016f342015-02-24 17:00:39 -0800246 log.debug("Unexpected intent event: {}", event);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700247 }
248 }
249}