blob: 4d597ca84e6b78d25ac569335b0e8d3660b4106a [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
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070018import org.apache.karaf.shell.commands.Argument;
19import org.apache.karaf.shell.commands.Command;
20import org.onlab.onos.cli.AbstractShellCommand;
21import org.onlab.onos.net.ConnectPoint;
22import org.onlab.onos.net.DeviceId;
23import org.onlab.onos.net.PortNumber;
24import org.onlab.onos.net.flow.DefaultTrafficSelector;
25import org.onlab.onos.net.flow.DefaultTrafficTreatment;
26import org.onlab.onos.net.flow.TrafficSelector;
27import org.onlab.onos.net.flow.TrafficTreatment;
28import org.onlab.onos.net.intent.Intent;
29import org.onlab.onos.net.intent.IntentEvent;
30import org.onlab.onos.net.intent.IntentEvent.Type;
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070031import org.onlab.onos.net.intent.IntentListener;
Brian O'Connorfa81eae2014-10-30 13:20:05 -070032import org.onlab.onos.net.intent.IntentOperations;
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070033import org.onlab.onos.net.intent.IntentService;
34import org.onlab.onos.net.intent.PointToPointIntent;
35import org.onlab.packet.Ethernet;
36import org.onlab.packet.MacAddress;
37
Thomas Vachuskab97cf282014-10-20 23:31:12 -070038import java.util.concurrent.CountDownLatch;
39import java.util.concurrent.TimeUnit;
40
41import static org.onlab.onos.net.DeviceId.deviceId;
42import static org.onlab.onos.net.PortNumber.portNumber;
43
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070044/**
45 * Installs point-to-point connectivity intents.
46 */
47@Command(scope = "onos", name = "push-test-intents",
48 description = "Installs random intents to test throughput")
49public class IntentPushTestCommand extends AbstractShellCommand
Thomas Vachuskab97cf282014-10-20 23:31:12 -070050 implements IntentListener {
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070051
52 @Argument(index = 0, name = "ingressDevice",
53 description = "Ingress Device/Port Description",
54 required = true, multiValued = false)
55 String ingressDeviceString = null;
56
57 @Argument(index = 1, name = "egressDevice",
58 description = "Egress Device/Port Description",
59 required = true, multiValued = false)
60 String egressDeviceString = null;
61
62 @Argument(index = 2, name = "count",
Thomas Vachuskab97cf282014-10-20 23:31:12 -070063 description = "Number of intents to push",
64 required = true, multiValued = false)
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070065 String countString = null;
66
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070067 private IntentService service;
68 private CountDownLatch latch;
69 private long start, end;
Brian O'Connor510132a2014-11-19 14:09:20 -080070 private int count;
71 private boolean add;
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070072
73 @Override
74 protected void execute() {
75 service = get(IntentService.class);
76
Brian O'Connor6741a5f2014-11-19 20:40:11 -080077
Thomas Vachuskab97cf282014-10-20 23:31:12 -070078 DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString));
79 PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070080 ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber);
81
Thomas Vachuskab97cf282014-10-20 23:31:12 -070082 DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString));
83 PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070084 ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber);
85
Brian O'Connor510132a2014-11-19 14:09:20 -080086 count = Integer.parseInt(countString);
87
88 service.addListener(this);
89
90 add = true;
91 latch = new CountDownLatch(count);
92 IntentOperations operations = generateIntents(ingress, egress);
93 submitIntents(operations);
94
95 add = false;
96 latch = new CountDownLatch(count);
97 operations = generateIntents(ingress, egress);
98 submitIntents(operations);
99
100 service.removeListener(this);
101 }
102
103 private IntentOperations generateIntents(ConnectPoint ingress, ConnectPoint egress) {
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700104 TrafficSelector.Builder selector = DefaultTrafficSelector.builder()
105 .matchEthType(Ethernet.TYPE_IPV4);
106 TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
107
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700108 IntentOperations.Builder ops = IntentOperations.builder();
109 for (int i = 1; i <= count; i++) {
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700110 TrafficSelector s = selector
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700111 .matchEthSrc(MacAddress.valueOf(i))
112 .build();
113 Intent intent = new PointToPointIntent(appId(), s, treatment,
114 ingress, egress);
Brian O'Connor510132a2014-11-19 14:09:20 -0800115 if (add) {
116 ops.addSubmitOperation(intent);
117 } else {
118 ops.addWithdrawOperation(intent.id());
119 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700120 }
Brian O'Connor510132a2014-11-19 14:09:20 -0800121 return ops.build();
122 }
123
124 private void submitIntents(IntentOperations ops) {
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700125 start = System.currentTimeMillis();
Brian O'Connor510132a2014-11-19 14:09:20 -0800126 service.execute(ops);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700127 try {
alshabib7911a052014-10-16 17:49:37 -0700128 if (latch.await(10, TimeUnit.SECONDS)) {
129 printResults(count);
130 } else {
Brian O'Connor510132a2014-11-19 14:09:20 -0800131 print("Failure: %d intents not installed", latch.getCount());
alshabib7911a052014-10-16 17:49:37 -0700132 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700133 } catch (InterruptedException e) {
134 print(e.toString());
135 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700136 }
137
138 private void printResults(int count) {
139 long delta = end - start;
Brian O'Connor510132a2014-11-19 14:09:20 -0800140 String text = add ? "install" : "withdraw";
141 print("Time to %s %d intents: %d ms", text, count, delta);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700142 }
143
144 /**
145 * Extracts the port number portion of the ConnectPoint.
146 *
147 * @param deviceString string representing the device/port
148 * @return port number as a string, empty string if the port is not found
149 */
150 private String getPortNumber(String deviceString) {
151 int slash = deviceString.indexOf('/');
152 if (slash <= 0) {
153 return "";
154 }
155 return deviceString.substring(slash + 1, deviceString.length());
156 }
157
158 /**
159 * Extracts the device ID portion of the ConnectPoint.
160 *
161 * @param deviceString string representing the device/port
162 * @return device ID string
163 */
164 private String getDeviceId(String deviceString) {
165 int slash = deviceString.indexOf('/');
166 if (slash <= 0) {
167 return "";
168 }
169 return deviceString.substring(0, slash);
170 }
171
172 @Override
173 public void event(IntentEvent event) {
Brian O'Connor510132a2014-11-19 14:09:20 -0800174 Type expected = add ? Type.INSTALLED : Type.WITHDRAWN;
175 if (event.type() == expected) {
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700176 end = event.time();
Brian O'Connorea9021e2014-10-10 23:12:02 -0500177 if (latch != null) {
178 latch.countDown();
179 } else {
180 log.warn("install event latch is null");
181 }
Yuta HIGUCHI963c6562014-11-24 18:59:54 -0800182 } else if (event.type() != Type.SUBMITTED) {
Brian O'Connor510132a2014-11-19 14:09:20 -0800183 log.info("Unexpected intent event: {}", event);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700184 }
185 }
186}