blob: 709f6d56735a892700d8cab1807a2ce7f36671f1 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 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
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080018import com.google.common.collect.ArrayListMultimap;
Yuta HIGUCHI87695b82014-12-03 15:19:35 -080019
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070020import org.apache.karaf.shell.commands.Argument;
21import org.apache.karaf.shell.commands.Command;
Brian O'Connora0d31002014-12-01 13:43:18 -080022import org.apache.karaf.shell.commands.Option;
Brian O'Connorabafb502014-12-02 22:26:20 -080023import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.core.ApplicationId;
25import org.onosproject.core.CoreService;
26import org.onosproject.net.ConnectPoint;
27import org.onosproject.net.DeviceId;
28import 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;
38import org.onosproject.net.intent.PointToPointIntent;
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070039import org.onlab.packet.Ethernet;
40import org.onlab.packet.MacAddress;
41
Brian O'Connorae3e7332014-12-02 16:02:49 -080042import java.util.EnumSet;
Yuta HIGUCHI87695b82014-12-03 15:19:35 -080043import java.util.HashSet;
Yuta HIGUCHI87695b82014-12-03 15:19:35 -080044import java.util.Set;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070045import java.util.concurrent.CountDownLatch;
46import java.util.concurrent.TimeUnit;
47
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/**
52 * Installs point-to-point connectivity intents.
53 */
54@Command(scope = "onos", name = "push-test-intents",
55 description = "Installs random intents to test throughput")
56public class IntentPushTestCommand extends AbstractShellCommand
Thomas Vachuskab97cf282014-10-20 23:31:12 -070057 implements IntentListener {
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070058
59 @Argument(index = 0, name = "ingressDevice",
60 description = "Ingress Device/Port Description",
61 required = true, multiValued = false)
62 String ingressDeviceString = null;
63
64 @Argument(index = 1, name = "egressDevice",
65 description = "Egress Device/Port Description",
66 required = true, multiValued = false)
67 String egressDeviceString = null;
68
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080069
Yuta HIGUCHI4712e212014-12-04 12:06:27 -080070 @Argument(index = 2, name = "IntentsPerAppId",
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080071 description = "Number of intents per appId",
72 required = true, multiValued = false)
73 String intentsPerAppId = null;
74
75 @Argument(index = 3, name = "apps",
76 description = "Number of appIds",
77 required = false, multiValued = false)
78 String appIds = null;
79
Brian O'Connora0d31002014-12-01 13:43:18 -080080 @Argument(index = 4, name = "appIdBase",
81 description = "Base Value for Application IDs",
82 required = false, multiValued = false)
83 String appIdBaseStr = null;
84
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;
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080098 private int apps;
99 private int intentsPerApp;
Brian O'Connora0d31002014-12-01 13:43:18 -0800100 private int appIdBase;
Brian O'Connor510132a2014-11-19 14:09:20 -0800101 private int count;
102 private boolean add;
Yuta HIGUCHI87695b82014-12-03 15:19:35 -0800103 private final Set<ApplicationId> myAppIds = new HashSet<>();
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700104
105 @Override
106 protected void execute() {
107 service = get(IntentService.class);
108
Brian O'Connor6741a5f2014-11-19 20:40:11 -0800109
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700110 DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString));
111 PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700112 ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber);
113
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700114 DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString));
115 PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700116 ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber);
117
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800118 apps = appIds != null ? Integer.parseInt(appIds) : 1;
119 intentsPerApp = Integer.parseInt(intentsPerAppId);
Brian O'Connora0d31002014-12-01 13:43:18 -0800120 appIdBase = appIdBaseStr != null ? Integer.parseInt(appIdBaseStr) : 1;
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800121
122 count = intentsPerApp * apps;
123
Brian O'Connor510132a2014-11-19 14:09:20 -0800124 service.addListener(this);
125
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800126 ArrayListMultimap<Integer, Intent> operations = generateIntents(ingress, egress);
127
Brian O'Connora0d31002014-12-01 13:43:18 -0800128 boolean both = !(installOnly ^ withdrawOnly);
Brian O'Connor510132a2014-11-19 14:09:20 -0800129
Brian O'Connora0d31002014-12-01 13:43:18 -0800130 if (installOnly || both) {
131 add = true;
132 latch = new CountDownLatch(count);
133 submitIntents(operations);
134 }
135
136 if (withdrawOnly || both) {
137 if (withdrawOnly && !both) {
138 print("This should fail for now...");
139 }
140 add = false;
141 latch = new CountDownLatch(count);
142 submitIntents(operations);
143 }
Brian O'Connor510132a2014-11-19 14:09:20 -0800144
145 service.removeListener(this);
146 }
147
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800148 private ArrayListMultimap<Integer, Intent> generateIntents(ConnectPoint ingress, ConnectPoint egress) {
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700149 TrafficSelector.Builder selector = DefaultTrafficSelector.builder()
150 .matchEthType(Ethernet.TYPE_IPV4);
151 TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
152
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800153 ArrayListMultimap<Integer, Intent> intents = ArrayListMultimap.create();
Brian O'Connora0d31002014-12-01 13:43:18 -0800154 for (int app = 0; app < apps; app++) {
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800155 for (int i = 1; i <= intentsPerApp; i++) {
156 TrafficSelector s = selector
Yuta HIGUCHI87695b82014-12-03 15:19:35 -0800157 .matchEthSrc(MacAddress.valueOf(i + (app + appIdBase) * intentsPerApp))
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800158 .build();
Brian O'Connora0d31002014-12-01 13:43:18 -0800159 intents.put(app, new PointToPointIntent(appId(app), s, treatment,
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800160 ingress, egress));
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800161
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800162 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700163 }
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800164 return intents;
Brian O'Connor510132a2014-11-19 14:09:20 -0800165 }
166
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800167 private void submitIntents(ArrayListMultimap<Integer, Intent> intents) {
Brian O'Connor03406a42015-02-03 17:28:57 -0800168 start = System.currentTimeMillis();
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800169 for (Integer app : intents.keySet()) {
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800170 for (Intent intent : intents.get(app)) {
171 if (add) {
Brian O'Connor03406a42015-02-03 17:28:57 -0800172 service.submit(intent);
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800173 } else {
Brian O'Connor03406a42015-02-03 17:28:57 -0800174 service.withdraw(intent);
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800175 }
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800176 }
177 }
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800178
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700179 try {
Yuta HIGUCHI82e53262014-11-27 10:28:51 -0800180 if (latch.await(100 + count * 200, TimeUnit.MILLISECONDS)) {
alshabib7911a052014-10-16 17:49:37 -0700181 printResults(count);
182 } else {
Brian O'Connor510132a2014-11-19 14:09:20 -0800183 print("Failure: %d intents not installed", latch.getCount());
alshabib7911a052014-10-16 17:49:37 -0700184 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700185 } catch (InterruptedException e) {
186 print(e.toString());
187 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700188 }
189
190 private void printResults(int count) {
191 long delta = end - start;
Brian O'Connor510132a2014-11-19 14:09:20 -0800192 String text = add ? "install" : "withdraw";
193 print("Time to %s %d intents: %d ms", text, count, delta);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700194 }
195
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800196
197 /**
198 * Returns application ID for the CLI.
199 *
alshabiba9819bf2014-11-30 18:15:52 -0800200 * @param id application id
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800201 * @return command-line application identifier
202 */
203 protected ApplicationId appId(Integer id) {
Yuta HIGUCHI87695b82014-12-03 15:19:35 -0800204 ApplicationId appId = get(CoreService.class)
205 .registerApplication("org.onosproject.cli-"
206 + (id + appIdBase));
207 myAppIds.add(appId);
208 return appId;
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800209 }
210
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700211 /**
212 * Extracts the port number portion of the ConnectPoint.
213 *
214 * @param deviceString string representing the device/port
215 * @return port number as a string, empty string if the port is not found
216 */
217 private String getPortNumber(String deviceString) {
218 int slash = deviceString.indexOf('/');
219 if (slash <= 0) {
220 return "";
221 }
222 return deviceString.substring(slash + 1, deviceString.length());
223 }
224
225 /**
226 * Extracts the device ID portion of the ConnectPoint.
227 *
228 * @param deviceString string representing the device/port
229 * @return device ID string
230 */
231 private String getDeviceId(String deviceString) {
232 int slash = deviceString.indexOf('/');
233 if (slash <= 0) {
234 return "";
235 }
236 return deviceString.substring(0, slash);
237 }
238
Brian O'Connorae3e7332014-12-02 16:02:49 -0800239 private static final EnumSet<IntentEvent.Type> IGNORE_EVENT
240 = EnumSet.of(Type.INSTALL_REQ, Type.WITHDRAW_REQ);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700241 @Override
Yuta HIGUCHIad492992014-12-03 14:13:54 -0800242 public synchronized void event(IntentEvent event) {
Yuta HIGUCHI87695b82014-12-03 15:19:35 -0800243 if (!myAppIds.contains(event.subject().appId())) {
244 // not my event, ignore
245 return;
246 }
Brian O'Connor510132a2014-11-19 14:09:20 -0800247 Type expected = add ? Type.INSTALLED : Type.WITHDRAWN;
248 if (event.type() == expected) {
Yuta HIGUCHIad492992014-12-03 14:13:54 -0800249 end = Math.max(end, event.time());
Brian O'Connorea9021e2014-10-10 23:12:02 -0500250 if (latch != null) {
Yuta HIGUCHIad492992014-12-03 14:13:54 -0800251 if (latch.getCount() == 0) {
252 log.warn("Latch was already 0 before counting down?");
253 }
Brian O'Connorea9021e2014-10-10 23:12:02 -0500254 latch.countDown();
255 } else {
256 log.warn("install event latch is null");
257 }
Brian O'Connorae3e7332014-12-02 16:02:49 -0800258 } else if (IGNORE_EVENT.contains(event.type())) {
Brian O'Connor510132a2014-11-19 14:09:20 -0800259 log.info("Unexpected intent event: {}", event);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700260 }
261 }
262}