blob: 6f140f2e704bb2542f134e2898c3711e578ad828 [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
Brian O'Connorae3e7332014-12-02 16:02:49 -080043import java.util.EnumSet;
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -080044import java.util.List;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070045import java.util.concurrent.CountDownLatch;
46import java.util.concurrent.TimeUnit;
47
48import static org.onlab.onos.net.DeviceId.deviceId;
49import static org.onlab.onos.net.PortNumber.portNumber;
50
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
70 @Argument(index = 2, name = "Intents per appId",
71 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;
97 private 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;
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700103
104 @Override
105 protected void execute() {
106 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
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800117 apps = appIds != null ? Integer.parseInt(appIds) : 1;
118 intentsPerApp = Integer.parseInt(intentsPerAppId);
Brian O'Connora0d31002014-12-01 13:43:18 -0800119 appIdBase = appIdBaseStr != null ? Integer.parseInt(appIdBaseStr) : 1;
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800120
121 count = intentsPerApp * apps;
122
Brian O'Connor510132a2014-11-19 14:09:20 -0800123 service.addListener(this);
124
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800125 ArrayListMultimap<Integer, Intent> operations = generateIntents(ingress, egress);
126
Brian O'Connora0d31002014-12-01 13:43:18 -0800127 boolean both = !(installOnly ^ withdrawOnly);
Brian O'Connor510132a2014-11-19 14:09:20 -0800128
Brian O'Connora0d31002014-12-01 13:43:18 -0800129 if (installOnly || both) {
130 add = true;
131 latch = new CountDownLatch(count);
132 submitIntents(operations);
133 }
134
135 if (withdrawOnly || both) {
136 if (withdrawOnly && !both) {
137 print("This should fail for now...");
138 }
139 add = false;
140 latch = new CountDownLatch(count);
141 submitIntents(operations);
142 }
Brian O'Connor510132a2014-11-19 14:09:20 -0800143
144 service.removeListener(this);
145 }
146
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800147 private ArrayListMultimap<Integer, Intent> generateIntents(ConnectPoint ingress, ConnectPoint egress) {
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700148 TrafficSelector.Builder selector = DefaultTrafficSelector.builder()
149 .matchEthType(Ethernet.TYPE_IPV4);
150 TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
151
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800152 ArrayListMultimap<Integer, Intent> intents = ArrayListMultimap.create();
Brian O'Connora0d31002014-12-01 13:43:18 -0800153 for (int app = 0; app < apps; app++) {
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800154 for (int i = 1; i <= intentsPerApp; i++) {
155 TrafficSelector s = selector
156 .matchEthSrc(MacAddress.valueOf(i))
157 .build();
Brian O'Connora0d31002014-12-01 13:43:18 -0800158 intents.put(app, new PointToPointIntent(appId(app), s, treatment,
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800159 ingress, egress));
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800160
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800161 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700162 }
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800163 return intents;
Brian O'Connor510132a2014-11-19 14:09:20 -0800164 }
165
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800166 private void submitIntents(ArrayListMultimap<Integer, Intent> intents) {
167 List<IntentOperations> opList = Lists.newArrayList();
168 for (Integer app : intents.keySet()) {
169 IntentOperations.Builder builder = IntentOperations.builder(appId(app));
170 for (Intent intent : intents.get(app)) {
171 if (add) {
172 builder.addSubmitOperation(intent);
173 } else {
174 builder.addWithdrawOperation(intent.id());
175 }
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800176 }
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800177 opList.add(builder.build());
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800178 }
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800179
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700180 start = System.currentTimeMillis();
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800181 opList.forEach(ops -> service.execute(ops));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700182 try {
Yuta HIGUCHI82e53262014-11-27 10:28:51 -0800183 if (latch.await(100 + count * 200, TimeUnit.MILLISECONDS)) {
alshabib7911a052014-10-16 17:49:37 -0700184 printResults(count);
185 } else {
Brian O'Connor510132a2014-11-19 14:09:20 -0800186 print("Failure: %d intents not installed", latch.getCount());
alshabib7911a052014-10-16 17:49:37 -0700187 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700188 } catch (InterruptedException e) {
189 print(e.toString());
190 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700191 }
192
193 private void printResults(int count) {
194 long delta = end - start;
Brian O'Connor510132a2014-11-19 14:09:20 -0800195 String text = add ? "install" : "withdraw";
196 print("Time to %s %d intents: %d ms", text, count, delta);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700197 }
198
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800199
200 /**
201 * Returns application ID for the CLI.
202 *
alshabiba9819bf2014-11-30 18:15:52 -0800203 * @param id application id
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800204 * @return command-line application identifier
205 */
206 protected ApplicationId appId(Integer id) {
Brian O'Connora0d31002014-12-01 13:43:18 -0800207 return get(CoreService.class).registerApplication("org.onlab.onos.cli-"
208 + (id + appIdBase));
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
242 public void event(IntentEvent event) {
Brian O'Connor510132a2014-11-19 14:09:20 -0800243 Type expected = add ? Type.INSTALLED : Type.WITHDRAWN;
244 if (event.type() == expected) {
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700245 end = event.time();
Brian O'Connorea9021e2014-10-10 23:12:02 -0500246 if (latch != null) {
247 latch.countDown();
248 } else {
249 log.warn("install event latch is null");
250 }
Brian O'Connorae3e7332014-12-02 16:02:49 -0800251 } else if (IGNORE_EVENT.contains(event.type())) {
Brian O'Connor510132a2014-11-19 14:09:20 -0800252 log.info("Unexpected intent event: {}", event);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700253 }
254 }
255}