blob: a8a760b1f05cd6f8b846d61064f3556bce65538d [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
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -080018import com.google.common.collect.Lists;
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070019import org.apache.karaf.shell.commands.Argument;
20import org.apache.karaf.shell.commands.Command;
Brian O'Connora0d31002014-12-01 13:43:18 -080021import org.apache.karaf.shell.commands.Option;
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -080022import org.onlab.packet.Ethernet;
23import org.onlab.packet.MacAddress;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.cli.AbstractShellCommand;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.net.ConnectPoint;
26import org.onosproject.net.DeviceId;
27import org.onosproject.net.PortNumber;
28import org.onosproject.net.flow.DefaultTrafficSelector;
29import org.onosproject.net.flow.DefaultTrafficTreatment;
30import org.onosproject.net.flow.TrafficSelector;
31import org.onosproject.net.flow.TrafficTreatment;
32import org.onosproject.net.intent.Intent;
33import org.onosproject.net.intent.IntentEvent;
34import org.onosproject.net.intent.IntentEvent.Type;
35import org.onosproject.net.intent.IntentListener;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import org.onosproject.net.intent.IntentService;
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -080037import org.onosproject.net.intent.Key;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import org.onosproject.net.intent.PointToPointIntent;
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070039
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -080040import java.util.Collections;
Brian O'Connorae3e7332014-12-02 16:02:49 -080041import java.util.EnumSet;
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -080042import java.util.List;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070043import java.util.concurrent.CountDownLatch;
44import java.util.concurrent.TimeUnit;
45
Brian O'Connorabafb502014-12-02 22:26:20 -080046import static org.onosproject.net.DeviceId.deviceId;
47import static org.onosproject.net.PortNumber.portNumber;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070048
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070049/**
50 * Installs point-to-point connectivity intents.
51 */
52@Command(scope = "onos", name = "push-test-intents",
53 description = "Installs random intents to test throughput")
54public class IntentPushTestCommand extends AbstractShellCommand
Thomas Vachuskab97cf282014-10-20 23:31:12 -070055 implements IntentListener {
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070056
57 @Argument(index = 0, name = "ingressDevice",
58 description = "Ingress Device/Port Description",
59 required = true, multiValued = false)
60 String ingressDeviceString = null;
61
62 @Argument(index = 1, name = "egressDevice",
63 description = "Egress Device/Port Description",
64 required = true, multiValued = false)
65 String egressDeviceString = null;
66
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -080067 @Argument(index = 2, name = "numberOfIntents",
68 description = "Number of intents to install/withdraw",
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080069 required = true, multiValued = false)
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -080070 String numberOfIntents = null;
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080071
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -080072 @Argument(index = 3, name = "keyOffset",
73 description = "Starting point for first key (default: 1)",
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080074 required = false, multiValued = false)
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -080075 String keyOffsetStr = null;
Brian O'Connora0d31002014-12-01 13:43:18 -080076
77 @Option(name = "-i", aliases = "--install",
78 description = "Install intents",
79 required = false, multiValued = false)
80 private boolean installOnly = false;
81
82 @Option(name = "-w", aliases = "--withdraw",
83 description = "Withdraw intents",
84 required = false, multiValued = false)
85 private boolean withdrawOnly = false;
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070086
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070087 private IntentService service;
88 private CountDownLatch latch;
Yuta HIGUCHIad492992014-12-03 14:13:54 -080089 private volatile long start, end;
Brian O'Connor510132a2014-11-19 14:09:20 -080090 private int count;
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -080091 private int keyOffset;
Brian O'Connor510132a2014-11-19 14:09:20 -080092 private boolean add;
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070093
94 @Override
95 protected void execute() {
96 service = get(IntentService.class);
97
Brian O'Connor6741a5f2014-11-19 20:40:11 -080098
Thomas Vachuskab97cf282014-10-20 23:31:12 -070099 DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString));
100 PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700101 ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber);
102
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700103 DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString));
104 PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700105 ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber);
106
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800107 count = Integer.parseInt(numberOfIntents);
108 keyOffset = (keyOffsetStr != null) ? Integer.parseInt(keyOffsetStr) : 1;
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800109
Brian O'Connor510132a2014-11-19 14:09:20 -0800110 service.addListener(this);
111
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800112 List<Intent> operations = generateIntents(ingress, egress);
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800113
Brian O'Connora0d31002014-12-01 13:43:18 -0800114 boolean both = !(installOnly ^ withdrawOnly);
Brian O'Connor510132a2014-11-19 14:09:20 -0800115
Brian O'Connora0d31002014-12-01 13:43:18 -0800116 if (installOnly || both) {
117 add = true;
Brian O'Connora0d31002014-12-01 13:43:18 -0800118 submitIntents(operations);
119 }
120
121 if (withdrawOnly || both) {
Brian O'Connora0d31002014-12-01 13:43:18 -0800122 add = false;
Brian O'Connora0d31002014-12-01 13:43:18 -0800123 submitIntents(operations);
124 }
Brian O'Connor510132a2014-11-19 14:09:20 -0800125
126 service.removeListener(this);
127 }
128
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800129 private List<Intent> generateIntents(ConnectPoint ingress, ConnectPoint egress) {
130 TrafficSelector.Builder selectorBldr = DefaultTrafficSelector.builder()
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700131 .matchEthType(Ethernet.TYPE_IPV4);
132 TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
133
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800134 List<Intent> intents = Lists.newArrayList();
135 for (int i = 0; i < count; i++) {
136 TrafficSelector selector = selectorBldr
137 .matchEthSrc(MacAddress.valueOf(i + keyOffset))
138 .build();
139 intents.add(new PointToPointIntent(appId(), Key.of(i + keyOffset, appId()),
140 selector, treatment,
141 ingress, egress,
142 Collections.emptyList()));
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800143
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700144 }
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800145 return intents;
Brian O'Connor510132a2014-11-19 14:09:20 -0800146 }
147
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800148 private void submitIntents(List<Intent> intents) {
149 latch = new CountDownLatch(count);
Brian O'Connor03406a42015-02-03 17:28:57 -0800150 start = System.currentTimeMillis();
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800151 for (Intent intent : intents) {
152 if (add) {
153 service.submit(intent);
154 } else {
155 service.withdraw(intent);
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800156 }
157 }
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800158
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700159 try {
Brian O'Connor7d405fe2015-02-24 11:38:16 -0800160 if (latch.await(500 + count * 30, TimeUnit.MILLISECONDS)) {
alshabib7911a052014-10-16 17:49:37 -0700161 printResults(count);
162 } else {
Brian O'Connor510132a2014-11-19 14:09:20 -0800163 print("Failure: %d intents not installed", latch.getCount());
alshabib7911a052014-10-16 17:49:37 -0700164 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700165 } catch (InterruptedException e) {
166 print(e.toString());
167 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700168 }
169
170 private void printResults(int count) {
171 long delta = end - start;
Brian O'Connor510132a2014-11-19 14:09:20 -0800172 String text = add ? "install" : "withdraw";
173 print("Time to %s %d intents: %d ms", text, count, delta);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700174 }
175
176 /**
177 * Extracts the port number portion of the ConnectPoint.
178 *
179 * @param deviceString string representing the device/port
180 * @return port number as a string, empty string if the port is not found
181 */
182 private String getPortNumber(String deviceString) {
183 int slash = deviceString.indexOf('/');
184 if (slash <= 0) {
185 return "";
186 }
187 return deviceString.substring(slash + 1, deviceString.length());
188 }
189
190 /**
191 * Extracts the device ID portion of the ConnectPoint.
192 *
193 * @param deviceString string representing the device/port
194 * @return device ID string
195 */
196 private String getDeviceId(String deviceString) {
197 int slash = deviceString.indexOf('/');
198 if (slash <= 0) {
199 return "";
200 }
201 return deviceString.substring(0, slash);
202 }
203
Brian O'Connorae3e7332014-12-02 16:02:49 -0800204 private static final EnumSet<IntentEvent.Type> IGNORE_EVENT
205 = EnumSet.of(Type.INSTALL_REQ, Type.WITHDRAW_REQ);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700206 @Override
Yuta HIGUCHIad492992014-12-03 14:13:54 -0800207 public synchronized void event(IntentEvent event) {
Brian O'Connor5b9dfdc2015-02-12 09:51:11 -0800208 if (!appId().equals(event.subject().appId())) {
Yuta HIGUCHI87695b82014-12-03 15:19:35 -0800209 // not my event, ignore
210 return;
211 }
Brian O'Connor510132a2014-11-19 14:09:20 -0800212 Type expected = add ? Type.INSTALLED : Type.WITHDRAWN;
213 if (event.type() == expected) {
Yuta HIGUCHIad492992014-12-03 14:13:54 -0800214 end = Math.max(end, event.time());
Brian O'Connorea9021e2014-10-10 23:12:02 -0500215 if (latch != null) {
Yuta HIGUCHIad492992014-12-03 14:13:54 -0800216 if (latch.getCount() == 0) {
217 log.warn("Latch was already 0 before counting down?");
218 }
Brian O'Connorea9021e2014-10-10 23:12:02 -0500219 latch.countDown();
220 } else {
221 log.warn("install event latch is null");
222 }
Brian O'Connorae3e7332014-12-02 16:02:49 -0800223 } else if (IGNORE_EVENT.contains(event.type())) {
Brian O'Connor510132a2014-11-19 14:09:20 -0800224 log.info("Unexpected intent event: {}", event);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700225 }
226 }
227}