blob: ef9b5b521d449f1ba6494372bb5f566eadd0e52f [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
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -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;
21import org.onlab.onos.cli.AbstractShellCommand;
22import org.onlab.onos.net.ConnectPoint;
23import org.onlab.onos.net.DeviceId;
24import org.onlab.onos.net.PortNumber;
25import org.onlab.onos.net.flow.DefaultTrafficSelector;
26import org.onlab.onos.net.flow.DefaultTrafficTreatment;
27import org.onlab.onos.net.flow.TrafficSelector;
28import org.onlab.onos.net.flow.TrafficTreatment;
29import org.onlab.onos.net.intent.Intent;
30import org.onlab.onos.net.intent.IntentEvent;
31import org.onlab.onos.net.intent.IntentEvent.Type;
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070032import org.onlab.onos.net.intent.IntentListener;
Brian O'Connorfa81eae2014-10-30 13:20:05 -070033import org.onlab.onos.net.intent.IntentOperations;
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070034import org.onlab.onos.net.intent.IntentService;
35import org.onlab.onos.net.intent.PointToPointIntent;
36import org.onlab.packet.Ethernet;
37import org.onlab.packet.MacAddress;
38
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -080039import java.util.List;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070040import java.util.concurrent.CountDownLatch;
41import java.util.concurrent.TimeUnit;
42
43import static org.onlab.onos.net.DeviceId.deviceId;
44import static org.onlab.onos.net.PortNumber.portNumber;
45
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070046/**
47 * Installs point-to-point connectivity intents.
48 */
49@Command(scope = "onos", name = "push-test-intents",
50 description = "Installs random intents to test throughput")
51public class IntentPushTestCommand extends AbstractShellCommand
Thomas Vachuskab97cf282014-10-20 23:31:12 -070052 implements IntentListener {
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070053
54 @Argument(index = 0, name = "ingressDevice",
55 description = "Ingress Device/Port Description",
56 required = true, multiValued = false)
57 String ingressDeviceString = null;
58
59 @Argument(index = 1, name = "egressDevice",
60 description = "Egress Device/Port Description",
61 required = true, multiValued = false)
62 String egressDeviceString = null;
63
64 @Argument(index = 2, name = "count",
Thomas Vachuskab97cf282014-10-20 23:31:12 -070065 description = "Number of intents to push",
66 required = true, multiValued = false)
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070067 String countString = null;
68
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070069 private IntentService service;
70 private CountDownLatch latch;
71 private long start, end;
Brian O'Connor510132a2014-11-19 14:09:20 -080072 private int count;
73 private boolean add;
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070074
75 @Override
76 protected void execute() {
77 service = get(IntentService.class);
78
Brian O'Connor6741a5f2014-11-19 20:40:11 -080079
Thomas Vachuskab97cf282014-10-20 23:31:12 -070080 DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString));
81 PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070082 ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber);
83
Thomas Vachuskab97cf282014-10-20 23:31:12 -070084 DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString));
85 PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070086 ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber);
87
Brian O'Connor510132a2014-11-19 14:09:20 -080088 count = Integer.parseInt(countString);
89
90 service.addListener(this);
91
92 add = true;
93 latch = new CountDownLatch(count);
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -080094 List<Intent> operations = generateIntents(ingress, egress);
Brian O'Connor510132a2014-11-19 14:09:20 -080095 submitIntents(operations);
96
97 add = false;
98 latch = new CountDownLatch(count);
Brian O'Connor510132a2014-11-19 14:09:20 -080099 submitIntents(operations);
100
101 service.removeListener(this);
102 }
103
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800104 private List<Intent> generateIntents(ConnectPoint ingress, ConnectPoint egress) {
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700105 TrafficSelector.Builder selector = DefaultTrafficSelector.builder()
106 .matchEthType(Ethernet.TYPE_IPV4);
107 TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
108
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800109 List<Intent> intents = Lists.newArrayList();
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700110 for (int i = 1; i <= count; i++) {
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700111 TrafficSelector s = selector
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700112 .matchEthSrc(MacAddress.valueOf(i))
113 .build();
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800114 intents.add(new PointToPointIntent(appId(), s, treatment,
115 ingress, egress));
116
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700117 }
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800118 return intents;
Brian O'Connor510132a2014-11-19 14:09:20 -0800119 }
120
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800121 private void submitIntents(List<Intent> intents) {
Brian O'Connor72a034c2014-11-26 18:24:23 -0800122 IntentOperations.Builder builder = IntentOperations.builder(appId());
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -0800123 for (Intent intent : intents) {
124 if (add) {
125 builder.addSubmitOperation(intent);
126 } else {
127 builder.addWithdrawOperation(intent.id());
128 }
129 }
130 IntentOperations ops = builder.build();
131
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700132 start = System.currentTimeMillis();
Brian O'Connor510132a2014-11-19 14:09:20 -0800133 service.execute(ops);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700134 try {
Brian O'Connor72a034c2014-11-26 18:24:23 -0800135 if (latch.await(30, TimeUnit.SECONDS)) {
alshabib7911a052014-10-16 17:49:37 -0700136 printResults(count);
137 } else {
Brian O'Connor510132a2014-11-19 14:09:20 -0800138 print("Failure: %d intents not installed", latch.getCount());
alshabib7911a052014-10-16 17:49:37 -0700139 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700140 } catch (InterruptedException e) {
141 print(e.toString());
142 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700143 }
144
145 private void printResults(int count) {
146 long delta = end - start;
Brian O'Connor510132a2014-11-19 14:09:20 -0800147 String text = add ? "install" : "withdraw";
148 print("Time to %s %d intents: %d ms", text, count, delta);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700149 }
150
151 /**
152 * Extracts the port number portion of the ConnectPoint.
153 *
154 * @param deviceString string representing the device/port
155 * @return port number as a string, empty string if the port is not found
156 */
157 private String getPortNumber(String deviceString) {
158 int slash = deviceString.indexOf('/');
159 if (slash <= 0) {
160 return "";
161 }
162 return deviceString.substring(slash + 1, deviceString.length());
163 }
164
165 /**
166 * Extracts the device ID portion of the ConnectPoint.
167 *
168 * @param deviceString string representing the device/port
169 * @return device ID string
170 */
171 private String getDeviceId(String deviceString) {
172 int slash = deviceString.indexOf('/');
173 if (slash <= 0) {
174 return "";
175 }
176 return deviceString.substring(0, slash);
177 }
178
179 @Override
180 public void event(IntentEvent event) {
Brian O'Connor510132a2014-11-19 14:09:20 -0800181 Type expected = add ? Type.INSTALLED : Type.WITHDRAWN;
182 if (event.type() == expected) {
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700183 end = event.time();
Brian O'Connorea9021e2014-10-10 23:12:02 -0500184 if (latch != null) {
185 latch.countDown();
186 } else {
187 log.warn("install event latch is null");
188 }
Yuta HIGUCHI963c6562014-11-24 18:59:54 -0800189 } else if (event.type() != Type.SUBMITTED) {
Brian O'Connor510132a2014-11-19 14:09:20 -0800190 log.info("Unexpected intent event: {}", event);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700191 }
192 }
193}