blob: 8fbdb3210b169372c732f3a2d805b740f5b4ea29 [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
Thomas Vachuskab97cf282014-10-20 23:31:12 -070077 DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString));
78 PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070079 ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber);
80
Thomas Vachuskab97cf282014-10-20 23:31:12 -070081 DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString));
82 PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070083 ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber);
84
Brian O'Connor510132a2014-11-19 14:09:20 -080085 count = Integer.parseInt(countString);
86
87 service.addListener(this);
88
89 add = true;
90 latch = new CountDownLatch(count);
91 IntentOperations operations = generateIntents(ingress, egress);
92 submitIntents(operations);
93
94 add = false;
95 latch = new CountDownLatch(count);
96 operations = generateIntents(ingress, egress);
97 submitIntents(operations);
98
99 service.removeListener(this);
100 }
101
102 private IntentOperations generateIntents(ConnectPoint ingress, ConnectPoint egress) {
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700103 TrafficSelector.Builder selector = DefaultTrafficSelector.builder()
104 .matchEthType(Ethernet.TYPE_IPV4);
105 TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
106
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700107 IntentOperations.Builder ops = IntentOperations.builder();
108 for (int i = 1; i <= count; i++) {
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700109 TrafficSelector s = selector
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700110 .matchEthSrc(MacAddress.valueOf(i))
111 .build();
112 Intent intent = new PointToPointIntent(appId(), s, treatment,
113 ingress, egress);
Brian O'Connor510132a2014-11-19 14:09:20 -0800114 if (add) {
115 ops.addSubmitOperation(intent);
116 } else {
117 ops.addWithdrawOperation(intent.id());
118 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700119 }
Brian O'Connor510132a2014-11-19 14:09:20 -0800120 return ops.build();
121 }
122
123 private void submitIntents(IntentOperations ops) {
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700124 start = System.currentTimeMillis();
Brian O'Connor510132a2014-11-19 14:09:20 -0800125 service.execute(ops);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700126 try {
alshabib7911a052014-10-16 17:49:37 -0700127 if (latch.await(10, TimeUnit.SECONDS)) {
128 printResults(count);
129 } else {
Brian O'Connor510132a2014-11-19 14:09:20 -0800130 print("Failure: %d intents not installed", latch.getCount());
alshabib7911a052014-10-16 17:49:37 -0700131 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700132 } catch (InterruptedException e) {
133 print(e.toString());
134 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700135 }
136
137 private void printResults(int count) {
138 long delta = end - start;
Brian O'Connor510132a2014-11-19 14:09:20 -0800139 String text = add ? "install" : "withdraw";
140 print("Time to %s %d intents: %d ms", text, count, delta);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700141 }
142
143 /**
144 * Extracts the port number portion of the ConnectPoint.
145 *
146 * @param deviceString string representing the device/port
147 * @return port number as a string, empty string if the port is not found
148 */
149 private String getPortNumber(String deviceString) {
150 int slash = deviceString.indexOf('/');
151 if (slash <= 0) {
152 return "";
153 }
154 return deviceString.substring(slash + 1, deviceString.length());
155 }
156
157 /**
158 * Extracts the device ID portion of the ConnectPoint.
159 *
160 * @param deviceString string representing the device/port
161 * @return device ID string
162 */
163 private String getDeviceId(String deviceString) {
164 int slash = deviceString.indexOf('/');
165 if (slash <= 0) {
166 return "";
167 }
168 return deviceString.substring(0, slash);
169 }
170
171 @Override
172 public void event(IntentEvent event) {
Brian O'Connor510132a2014-11-19 14:09:20 -0800173 Type expected = add ? Type.INSTALLED : Type.WITHDRAWN;
174 if (event.type() == expected) {
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700175 end = event.time();
Brian O'Connorea9021e2014-10-10 23:12:02 -0500176 if (latch != null) {
177 latch.countDown();
178 } else {
179 log.warn("install event latch is null");
180 }
alshabib7911a052014-10-16 17:49:37 -0700181 } else {
Brian O'Connor510132a2014-11-19 14:09:20 -0800182 log.info("Unexpected intent event: {}", event);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700183 }
184 }
185}