blob: 932622190ef8eba58c973c3dcab406abd219ed70 [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;
22import org.onlab.onos.cli.AbstractShellCommand;
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080023import org.onlab.onos.core.ApplicationId;
24import org.onlab.onos.core.CoreService;
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070025import org.onlab.onos.net.ConnectPoint;
26import org.onlab.onos.net.DeviceId;
27import org.onlab.onos.net.PortNumber;
28import org.onlab.onos.net.flow.DefaultTrafficSelector;
29import org.onlab.onos.net.flow.DefaultTrafficTreatment;
30import org.onlab.onos.net.flow.TrafficSelector;
31import org.onlab.onos.net.flow.TrafficTreatment;
32import org.onlab.onos.net.intent.Intent;
33import org.onlab.onos.net.intent.IntentEvent;
34import org.onlab.onos.net.intent.IntentEvent.Type;
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070035import org.onlab.onos.net.intent.IntentListener;
Brian O'Connorfa81eae2014-10-30 13:20:05 -070036import org.onlab.onos.net.intent.IntentOperations;
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070037import org.onlab.onos.net.intent.IntentService;
38import org.onlab.onos.net.intent.PointToPointIntent;
39import org.onlab.packet.Ethernet;
40import org.onlab.packet.MacAddress;
41
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -080042import java.util.List;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070043import java.util.concurrent.CountDownLatch;
44import java.util.concurrent.TimeUnit;
45
46import static org.onlab.onos.net.DeviceId.deviceId;
47import static org.onlab.onos.net.PortNumber.portNumber;
48
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
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080067
68 @Argument(index = 2, name = "Intents per appId",
69 description = "Number of intents per appId",
70 required = true, multiValued = false)
71 String intentsPerAppId = null;
72
73 @Argument(index = 3, name = "apps",
74 description = "Number of appIds",
75 required = false, multiValued = false)
76 String appIds = null;
77
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070078
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070079 private IntentService service;
80 private CountDownLatch latch;
81 private long start, end;
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080082 private int apps;
83 private int intentsPerApp;
Brian O'Connor510132a2014-11-19 14:09:20 -080084 private int count;
85 private boolean add;
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070086
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -080087
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070088 @Override
89 protected void execute() {
90 service = get(IntentService.class);
91
Brian O'Connor6741a5f2014-11-19 20:40:11 -080092
Thomas Vachuskab97cf282014-10-20 23:31:12 -070093 DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString));
94 PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070095 ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber);
96
Thomas Vachuskab97cf282014-10-20 23:31:12 -070097 DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString));
98 PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070099 ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber);
100
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800101 apps = appIds != null ? Integer.parseInt(appIds) : 1;
102 intentsPerApp = Integer.parseInt(intentsPerAppId);
103
104 count = intentsPerApp * apps;
105
Brian O'Connor510132a2014-11-19 14:09:20 -0800106
107 service.addListener(this);
108
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800109 ArrayListMultimap<Integer, Intent> operations = generateIntents(ingress, egress);
110
Brian O'Connor510132a2014-11-19 14:09:20 -0800111 add = true;
112 latch = new CountDownLatch(count);
Brian O'Connor510132a2014-11-19 14:09:20 -0800113 submitIntents(operations);
114
115 add = false;
116 latch = new CountDownLatch(count);
Brian O'Connor510132a2014-11-19 14:09:20 -0800117 submitIntents(operations);
118
119 service.removeListener(this);
120 }
121
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800122 private ArrayListMultimap<Integer, Intent> generateIntents(ConnectPoint ingress, ConnectPoint egress) {
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700123 TrafficSelector.Builder selector = DefaultTrafficSelector.builder()
124 .matchEthType(Ethernet.TYPE_IPV4);
125 TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
126
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800127 ArrayListMultimap<Integer, Intent> intents = ArrayListMultimap.create();
128 for (int app = 1; app <= apps; app++) {
129 for (int i = 1; i <= intentsPerApp; i++) {
130 TrafficSelector s = selector
131 .matchEthSrc(MacAddress.valueOf(i))
132 .build();
133 intents.put(app, new PointToPointIntent(appId(), s, treatment,
134 ingress, egress));
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800135
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800136 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700137 }
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800138 return intents;
Brian O'Connor510132a2014-11-19 14:09:20 -0800139 }
140
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800141 private void submitIntents(ArrayListMultimap<Integer, Intent> intents) {
142 List<IntentOperations> opList = Lists.newArrayList();
143 for (Integer app : intents.keySet()) {
144 IntentOperations.Builder builder = IntentOperations.builder(appId(app));
145 for (Intent intent : intents.get(app)) {
146 if (add) {
147 builder.addSubmitOperation(intent);
148 } else {
149 builder.addWithdrawOperation(intent.id());
150 }
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800151 }
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800152 opList.add(builder.build());
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800153 }
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800154
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700155 start = System.currentTimeMillis();
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800156 opList.forEach(ops -> service.execute(ops));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700157 try {
Yuta HIGUCHI82e53262014-11-27 10:28:51 -0800158 if (latch.await(100 + count * 200, TimeUnit.MILLISECONDS)) {
alshabib7911a052014-10-16 17:49:37 -0700159 printResults(count);
160 } else {
Brian O'Connor510132a2014-11-19 14:09:20 -0800161 print("Failure: %d intents not installed", latch.getCount());
alshabib7911a052014-10-16 17:49:37 -0700162 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700163 } catch (InterruptedException e) {
164 print(e.toString());
165 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700166 }
167
168 private void printResults(int count) {
169 long delta = end - start;
Brian O'Connor510132a2014-11-19 14:09:20 -0800170 String text = add ? "install" : "withdraw";
171 print("Time to %s %d intents: %d ms", text, count, delta);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700172 }
173
Yuta HIGUCHIc2bf3d82014-11-28 18:50:41 -0800174
175 /**
176 * Returns application ID for the CLI.
177 *
178 * @return command-line application identifier
179 */
180 protected ApplicationId appId(Integer id) {
181 return get(CoreService.class).registerApplication("org.onlab.onos.cli-" + id);
182 }
183
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700184 /**
185 * Extracts the port number portion of the ConnectPoint.
186 *
187 * @param deviceString string representing the device/port
188 * @return port number as a string, empty string if the port is not found
189 */
190 private String getPortNumber(String deviceString) {
191 int slash = deviceString.indexOf('/');
192 if (slash <= 0) {
193 return "";
194 }
195 return deviceString.substring(slash + 1, deviceString.length());
196 }
197
198 /**
199 * Extracts the device ID portion of the ConnectPoint.
200 *
201 * @param deviceString string representing the device/port
202 * @return device ID string
203 */
204 private String getDeviceId(String deviceString) {
205 int slash = deviceString.indexOf('/');
206 if (slash <= 0) {
207 return "";
208 }
209 return deviceString.substring(0, slash);
210 }
211
212 @Override
213 public void event(IntentEvent event) {
Brian O'Connor510132a2014-11-19 14:09:20 -0800214 Type expected = add ? Type.INSTALLED : Type.WITHDRAWN;
215 if (event.type() == expected) {
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700216 end = event.time();
Brian O'Connorea9021e2014-10-10 23:12:02 -0500217 if (latch != null) {
218 latch.countDown();
219 } else {
220 log.warn("install event latch is null");
221 }
Yuta HIGUCHI963c6562014-11-24 18:59:54 -0800222 } else if (event.type() != Type.SUBMITTED) {
Brian O'Connor510132a2014-11-19 14:09:20 -0800223 log.info("Unexpected intent event: {}", event);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700224 }
225 }
226}