blob: ed2f860bf71aed9e6f86f83af13140378b641a9a [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;
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -080019import com.google.common.collect.Lists;
Yuta HIGUCHI87695b82014-12-03 15:19:35 -080020
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070021import org.apache.karaf.shell.commands.Argument;
22import org.apache.karaf.shell.commands.Command;
Brian O'Connora0d31002014-12-01 13:43:18 -080023import org.apache.karaf.shell.commands.Option;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.cli.AbstractShellCommand;
25import org.onosproject.core.ApplicationId;
26import org.onosproject.core.CoreService;
27import org.onosproject.net.ConnectPoint;
28import org.onosproject.net.DeviceId;
29import 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;
38import org.onosproject.net.intent.IntentOperations;
39import org.onosproject.net.intent.IntentService;
40import org.onosproject.net.intent.PointToPointIntent;
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070041import org.onlab.packet.Ethernet;
42import org.onlab.packet.MacAddress;
43
Brian O'Connorae3e7332014-12-02 16:02:49 -080044import java.util.EnumSet;
Yuta HIGUCHI87695b82014-12-03 15:19:35 -080045import java.util.HashSet;
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -080046import java.util.List;
Yuta HIGUCHI87695b82014-12-03 15:19:35 -080047import java.util.Set;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070048import java.util.concurrent.CountDownLatch;
49import java.util.concurrent.TimeUnit;
50
Brian O'Connorabafb502014-12-02 22:26:20 -080051import static org.onosproject.net.DeviceId.deviceId;
52import static org.onosproject.net.PortNumber.portNumber;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070053
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070054/**
55 * Installs point-to-point connectivity intents.
56 */
57@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)
65 String ingressDeviceString = null;
66
67 @Argument(index = 1, name = "egressDevice",
68 description = "Egress Device/Port Description",
69 required = true, multiValued = false)
70 String egressDeviceString = null;
71
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080072
Yuta HIGUCHI4712e212014-12-04 12:06:27 -080073 @Argument(index = 2, name = "IntentsPerAppId",
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080074 description = "Number of intents per appId",
75 required = true, multiValued = false)
76 String intentsPerAppId = null;
77
78 @Argument(index = 3, name = "apps",
79 description = "Number of appIds",
80 required = false, multiValued = false)
81 String appIds = null;
82
Brian O'Connora0d31002014-12-01 13:43:18 -080083 @Argument(index = 4, name = "appIdBase",
84 description = "Base Value for Application IDs",
85 required = false, multiValued = false)
86 String appIdBaseStr = null;
87
88 @Option(name = "-i", aliases = "--install",
89 description = "Install intents",
90 required = false, multiValued = false)
91 private boolean installOnly = false;
92
93 @Option(name = "-w", aliases = "--withdraw",
94 description = "Withdraw intents",
95 required = false, multiValued = false)
96 private boolean withdrawOnly = false;
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070097
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070098 private IntentService service;
99 private CountDownLatch latch;
Yuta HIGUCHIad492992014-12-03 14:13:54 -0800100 private volatile long start, end;
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800101 private int apps;
102 private int intentsPerApp;
Brian O'Connora0d31002014-12-01 13:43:18 -0800103 private int appIdBase;
Brian O'Connor510132a2014-11-19 14:09:20 -0800104 private int count;
105 private boolean add;
Yuta HIGUCHI87695b82014-12-03 15:19:35 -0800106 private final Set<ApplicationId> myAppIds = new HashSet<>();
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700107
108 @Override
109 protected void execute() {
110 service = get(IntentService.class);
111
Brian O'Connor6741a5f2014-11-19 20:40:11 -0800112
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700113 DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString));
114 PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700115 ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber);
116
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700117 DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString));
118 PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700119 ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber);
120
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800121 apps = appIds != null ? Integer.parseInt(appIds) : 1;
122 intentsPerApp = Integer.parseInt(intentsPerAppId);
Brian O'Connora0d31002014-12-01 13:43:18 -0800123 appIdBase = appIdBaseStr != null ? Integer.parseInt(appIdBaseStr) : 1;
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800124
125 count = intentsPerApp * apps;
126
Brian O'Connor510132a2014-11-19 14:09:20 -0800127 service.addListener(this);
128
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800129 ArrayListMultimap<Integer, Intent> operations = generateIntents(ingress, egress);
130
Brian O'Connora0d31002014-12-01 13:43:18 -0800131 boolean both = !(installOnly ^ withdrawOnly);
Brian O'Connor510132a2014-11-19 14:09:20 -0800132
Brian O'Connora0d31002014-12-01 13:43:18 -0800133 if (installOnly || both) {
134 add = true;
135 latch = new CountDownLatch(count);
136 submitIntents(operations);
137 }
138
139 if (withdrawOnly || both) {
140 if (withdrawOnly && !both) {
141 print("This should fail for now...");
142 }
143 add = false;
144 latch = new CountDownLatch(count);
145 submitIntents(operations);
146 }
Brian O'Connor510132a2014-11-19 14:09:20 -0800147
148 service.removeListener(this);
149 }
150
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800151 private ArrayListMultimap<Integer, Intent> generateIntents(ConnectPoint ingress, ConnectPoint egress) {
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700152 TrafficSelector.Builder selector = DefaultTrafficSelector.builder()
153 .matchEthType(Ethernet.TYPE_IPV4);
154 TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
155
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800156 ArrayListMultimap<Integer, Intent> intents = ArrayListMultimap.create();
Brian O'Connora0d31002014-12-01 13:43:18 -0800157 for (int app = 0; app < apps; app++) {
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800158 for (int i = 1; i <= intentsPerApp; i++) {
159 TrafficSelector s = selector
Yuta HIGUCHI87695b82014-12-03 15:19:35 -0800160 .matchEthSrc(MacAddress.valueOf(i + (app + appIdBase) * intentsPerApp))
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800161 .build();
Brian O'Connora0d31002014-12-01 13:43:18 -0800162 intents.put(app, new PointToPointIntent(appId(app), s, treatment,
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800163 ingress, egress));
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800164
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800165 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700166 }
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800167 return intents;
Brian O'Connor510132a2014-11-19 14:09:20 -0800168 }
169
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800170 private void submitIntents(ArrayListMultimap<Integer, Intent> intents) {
171 List<IntentOperations> opList = Lists.newArrayList();
172 for (Integer app : intents.keySet()) {
173 IntentOperations.Builder builder = IntentOperations.builder(appId(app));
174 for (Intent intent : intents.get(app)) {
175 if (add) {
176 builder.addSubmitOperation(intent);
177 } else {
178 builder.addWithdrawOperation(intent.id());
179 }
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800180 }
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800181 opList.add(builder.build());
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800182 }
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800183
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700184 start = System.currentTimeMillis();
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800185 opList.forEach(ops -> service.execute(ops));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700186 try {
Yuta HIGUCHI82e53262014-11-27 10:28:51 -0800187 if (latch.await(100 + count * 200, TimeUnit.MILLISECONDS)) {
alshabib7911a052014-10-16 17:49:37 -0700188 printResults(count);
189 } else {
Brian O'Connor510132a2014-11-19 14:09:20 -0800190 print("Failure: %d intents not installed", latch.getCount());
alshabib7911a052014-10-16 17:49:37 -0700191 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700192 } catch (InterruptedException e) {
193 print(e.toString());
194 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700195 }
196
197 private void printResults(int count) {
198 long delta = end - start;
Brian O'Connor510132a2014-11-19 14:09:20 -0800199 String text = add ? "install" : "withdraw";
200 print("Time to %s %d intents: %d ms", text, count, delta);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700201 }
202
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800203
204 /**
205 * Returns application ID for the CLI.
206 *
alshabiba9819bf2014-11-30 18:15:52 -0800207 * @param id application id
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800208 * @return command-line application identifier
209 */
210 protected ApplicationId appId(Integer id) {
Yuta HIGUCHI87695b82014-12-03 15:19:35 -0800211 ApplicationId appId = get(CoreService.class)
212 .registerApplication("org.onosproject.cli-"
213 + (id + appIdBase));
214 myAppIds.add(appId);
215 return appId;
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800216 }
217
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700218 /**
219 * Extracts the port number portion of the ConnectPoint.
220 *
221 * @param deviceString string representing the device/port
222 * @return port number as a string, empty string if the port is not found
223 */
224 private String getPortNumber(String deviceString) {
225 int slash = deviceString.indexOf('/');
226 if (slash <= 0) {
227 return "";
228 }
229 return deviceString.substring(slash + 1, deviceString.length());
230 }
231
232 /**
233 * Extracts the device ID portion of the ConnectPoint.
234 *
235 * @param deviceString string representing the device/port
236 * @return device ID string
237 */
238 private String getDeviceId(String deviceString) {
239 int slash = deviceString.indexOf('/');
240 if (slash <= 0) {
241 return "";
242 }
243 return deviceString.substring(0, slash);
244 }
245
Brian O'Connorae3e7332014-12-02 16:02:49 -0800246 private static final EnumSet<IntentEvent.Type> IGNORE_EVENT
247 = EnumSet.of(Type.INSTALL_REQ, Type.WITHDRAW_REQ);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700248 @Override
Yuta HIGUCHIad492992014-12-03 14:13:54 -0800249 public synchronized void event(IntentEvent event) {
Yuta HIGUCHI87695b82014-12-03 15:19:35 -0800250 if (!myAppIds.contains(event.subject().appId())) {
251 // not my event, ignore
252 return;
253 }
Brian O'Connor510132a2014-11-19 14:09:20 -0800254 Type expected = add ? Type.INSTALLED : Type.WITHDRAWN;
255 if (event.type() == expected) {
Yuta HIGUCHIad492992014-12-03 14:13:54 -0800256 end = Math.max(end, event.time());
Brian O'Connorea9021e2014-10-10 23:12:02 -0500257 if (latch != null) {
Yuta HIGUCHIad492992014-12-03 14:13:54 -0800258 if (latch.getCount() == 0) {
259 log.warn("Latch was already 0 before counting down?");
260 }
Brian O'Connorea9021e2014-10-10 23:12:02 -0500261 latch.countDown();
262 } else {
263 log.warn("install event latch is null");
264 }
Brian O'Connorae3e7332014-12-02 16:02:49 -0800265 } else if (IGNORE_EVENT.contains(event.type())) {
Brian O'Connor510132a2014-11-19 14:09:20 -0800266 log.info("Unexpected intent event: {}", event);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700267 }
268 }
269}