blob: 32ea13f43e9ffdc6a59170815c35fdcc47afb260 [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'Connor1aa99eb2014-10-10 16:18:58 -070016package org.onlab.onos.cli.net;
17
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;
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'Connor1aa99eb2014-10-10 16:18:58 -070023import org.onlab.onos.cli.AbstractShellCommand;
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080024import org.onlab.onos.core.ApplicationId;
25import org.onlab.onos.core.CoreService;
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070026import org.onlab.onos.net.ConnectPoint;
27import org.onlab.onos.net.DeviceId;
28import org.onlab.onos.net.PortNumber;
29import org.onlab.onos.net.flow.DefaultTrafficSelector;
30import org.onlab.onos.net.flow.DefaultTrafficTreatment;
31import org.onlab.onos.net.flow.TrafficSelector;
32import org.onlab.onos.net.flow.TrafficTreatment;
33import org.onlab.onos.net.intent.Intent;
34import org.onlab.onos.net.intent.IntentEvent;
35import org.onlab.onos.net.intent.IntentEvent.Type;
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070036import org.onlab.onos.net.intent.IntentListener;
Brian O'Connorfa81eae2014-10-30 13:20:05 -070037import org.onlab.onos.net.intent.IntentOperations;
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070038import org.onlab.onos.net.intent.IntentService;
39import org.onlab.onos.net.intent.PointToPointIntent;
40import org.onlab.packet.Ethernet;
41import org.onlab.packet.MacAddress;
42
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -080043import java.util.List;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070044import java.util.concurrent.CountDownLatch;
45import java.util.concurrent.TimeUnit;
46
47import static org.onlab.onos.net.DeviceId.deviceId;
48import static org.onlab.onos.net.PortNumber.portNumber;
49
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070050/**
51 * Installs point-to-point connectivity intents.
52 */
53@Command(scope = "onos", name = "push-test-intents",
54 description = "Installs random intents to test throughput")
55public class IntentPushTestCommand extends AbstractShellCommand
Thomas Vachuskab97cf282014-10-20 23:31:12 -070056 implements IntentListener {
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070057
58 @Argument(index = 0, name = "ingressDevice",
59 description = "Ingress Device/Port Description",
60 required = true, multiValued = false)
61 String ingressDeviceString = null;
62
63 @Argument(index = 1, name = "egressDevice",
64 description = "Egress Device/Port Description",
65 required = true, multiValued = false)
66 String egressDeviceString = null;
67
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080068
69 @Argument(index = 2, name = "Intents per appId",
70 description = "Number of intents per appId",
71 required = true, multiValued = false)
72 String intentsPerAppId = null;
73
74 @Argument(index = 3, name = "apps",
75 description = "Number of appIds",
76 required = false, multiValued = false)
77 String appIds = null;
78
Brian O'Connora0d31002014-12-01 13:43:18 -080079 @Argument(index = 4, name = "appIdBase",
80 description = "Base Value for Application IDs",
81 required = false, multiValued = false)
82 String appIdBaseStr = null;
83
84 @Option(name = "-i", aliases = "--install",
85 description = "Install intents",
86 required = false, multiValued = false)
87 private boolean installOnly = false;
88
89 @Option(name = "-w", aliases = "--withdraw",
90 description = "Withdraw intents",
91 required = false, multiValued = false)
92 private boolean withdrawOnly = false;
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070093
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070094 private IntentService service;
95 private CountDownLatch latch;
96 private long start, end;
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080097 private int apps;
98 private int intentsPerApp;
Brian O'Connora0d31002014-12-01 13:43:18 -080099 private int appIdBase;
Brian O'Connor510132a2014-11-19 14:09:20 -0800100 private int count;
101 private boolean add;
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700102
103 @Override
104 protected void execute() {
105 service = get(IntentService.class);
106
Brian O'Connor6741a5f2014-11-19 20:40:11 -0800107
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700108 DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString));
109 PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700110 ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber);
111
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700112 DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString));
113 PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700114 ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber);
115
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800116 apps = appIds != null ? Integer.parseInt(appIds) : 1;
117 intentsPerApp = Integer.parseInt(intentsPerAppId);
Brian O'Connora0d31002014-12-01 13:43:18 -0800118 appIdBase = appIdBaseStr != null ? Integer.parseInt(appIdBaseStr) : 1;
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800119
120 count = intentsPerApp * apps;
121
Brian O'Connor510132a2014-11-19 14:09:20 -0800122 service.addListener(this);
123
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800124 ArrayListMultimap<Integer, Intent> operations = generateIntents(ingress, egress);
125
Brian O'Connora0d31002014-12-01 13:43:18 -0800126 boolean both = !(installOnly ^ withdrawOnly);
Brian O'Connor510132a2014-11-19 14:09:20 -0800127
Brian O'Connora0d31002014-12-01 13:43:18 -0800128 if (installOnly || both) {
129 add = true;
130 latch = new CountDownLatch(count);
131 submitIntents(operations);
132 }
133
134 if (withdrawOnly || both) {
135 if (withdrawOnly && !both) {
136 print("This should fail for now...");
137 }
138 add = false;
139 latch = new CountDownLatch(count);
140 submitIntents(operations);
141 }
Brian O'Connor510132a2014-11-19 14:09:20 -0800142
143 service.removeListener(this);
144 }
145
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800146 private ArrayListMultimap<Integer, Intent> generateIntents(ConnectPoint ingress, ConnectPoint egress) {
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700147 TrafficSelector.Builder selector = DefaultTrafficSelector.builder()
148 .matchEthType(Ethernet.TYPE_IPV4);
149 TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
150
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800151 ArrayListMultimap<Integer, Intent> intents = ArrayListMultimap.create();
Brian O'Connora0d31002014-12-01 13:43:18 -0800152 for (int app = 0; app < apps; app++) {
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800153 for (int i = 1; i <= intentsPerApp; i++) {
154 TrafficSelector s = selector
155 .matchEthSrc(MacAddress.valueOf(i))
156 .build();
Brian O'Connora0d31002014-12-01 13:43:18 -0800157 intents.put(app, new PointToPointIntent(appId(app), s, treatment,
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800158 ingress, egress));
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800159
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800160 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700161 }
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800162 return intents;
Brian O'Connor510132a2014-11-19 14:09:20 -0800163 }
164
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800165 private void submitIntents(ArrayListMultimap<Integer, Intent> intents) {
166 List<IntentOperations> opList = Lists.newArrayList();
167 for (Integer app : intents.keySet()) {
168 IntentOperations.Builder builder = IntentOperations.builder(appId(app));
169 for (Intent intent : intents.get(app)) {
170 if (add) {
171 builder.addSubmitOperation(intent);
172 } else {
173 builder.addWithdrawOperation(intent.id());
174 }
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800175 }
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800176 opList.add(builder.build());
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800177 }
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800178
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700179 start = System.currentTimeMillis();
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800180 opList.forEach(ops -> service.execute(ops));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700181 try {
Yuta HIGUCHI82e53262014-11-27 10:28:51 -0800182 if (latch.await(100 + count * 200, TimeUnit.MILLISECONDS)) {
alshabib7911a052014-10-16 17:49:37 -0700183 printResults(count);
184 } else {
Brian O'Connor510132a2014-11-19 14:09:20 -0800185 print("Failure: %d intents not installed", latch.getCount());
alshabib7911a052014-10-16 17:49:37 -0700186 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700187 } catch (InterruptedException e) {
188 print(e.toString());
189 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700190 }
191
192 private void printResults(int count) {
193 long delta = end - start;
Brian O'Connor510132a2014-11-19 14:09:20 -0800194 String text = add ? "install" : "withdraw";
195 print("Time to %s %d intents: %d ms", text, count, delta);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700196 }
197
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800198
199 /**
200 * Returns application ID for the CLI.
201 *
202 * @return command-line application identifier
203 */
204 protected ApplicationId appId(Integer id) {
Brian O'Connora0d31002014-12-01 13:43:18 -0800205 return get(CoreService.class).registerApplication("org.onlab.onos.cli-"
206 + (id + appIdBase));
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800207 }
208
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700209 /**
210 * Extracts the port number portion of the ConnectPoint.
211 *
212 * @param deviceString string representing the device/port
213 * @return port number as a string, empty string if the port is not found
214 */
215 private String getPortNumber(String deviceString) {
216 int slash = deviceString.indexOf('/');
217 if (slash <= 0) {
218 return "";
219 }
220 return deviceString.substring(slash + 1, deviceString.length());
221 }
222
223 /**
224 * Extracts the device ID portion of the ConnectPoint.
225 *
226 * @param deviceString string representing the device/port
227 * @return device ID string
228 */
229 private String getDeviceId(String deviceString) {
230 int slash = deviceString.indexOf('/');
231 if (slash <= 0) {
232 return "";
233 }
234 return deviceString.substring(0, slash);
235 }
236
237 @Override
238 public void event(IntentEvent event) {
Brian O'Connor510132a2014-11-19 14:09:20 -0800239 Type expected = add ? Type.INSTALLED : Type.WITHDRAWN;
240 if (event.type() == expected) {
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700241 end = event.time();
Brian O'Connorea9021e2014-10-10 23:12:02 -0500242 if (latch != null) {
243 latch.countDown();
244 } else {
245 log.warn("install event latch is null");
246 }
Yuta HIGUCHI963c6562014-11-24 18:59:54 -0800247 } else if (event.type() != Type.SUBMITTED) {
Brian O'Connor510132a2014-11-19 14:09:20 -0800248 log.info("Unexpected intent event: {}", event);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700249 }
250 }
251}