blob: 2cb402aaccd9481faabc733f5030a530af32965d [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;
70
71 @Override
72 protected void execute() {
73 service = get(IntentService.class);
74
Thomas Vachuskab97cf282014-10-20 23:31:12 -070075 DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString));
76 PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070077 ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber);
78
Thomas Vachuskab97cf282014-10-20 23:31:12 -070079 DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString));
80 PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070081 ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber);
82
83 TrafficSelector.Builder selector = DefaultTrafficSelector.builder()
84 .matchEthType(Ethernet.TYPE_IPV4);
85 TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
86
87 int count = Integer.parseInt(countString);
88
89 service.addListener(this);
Brian O'Connorea9021e2014-10-10 23:12:02 -050090 latch = new CountDownLatch(count);
91
Brian O'Connorfa81eae2014-10-30 13:20:05 -070092 IntentOperations.Builder ops = IntentOperations.builder();
93 for (int i = 1; i <= count; i++) {
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070094 TrafficSelector s = selector
Thomas Vachuskab97cf282014-10-20 23:31:12 -070095 .matchEthSrc(MacAddress.valueOf(i))
96 .build();
97 Intent intent = new PointToPointIntent(appId(), s, treatment,
98 ingress, egress);
Brian O'Connorfa81eae2014-10-30 13:20:05 -070099 ops.addSubmitOperation(intent);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700100 }
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700101 IntentOperations operations = ops.build();
102 start = System.currentTimeMillis();
103 service.execute(operations);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700104 try {
alshabib7911a052014-10-16 17:49:37 -0700105 if (latch.await(10, TimeUnit.SECONDS)) {
106 printResults(count);
107 } else {
108 print("I FAIL MISERABLY -> %d", latch.getCount());
109 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700110 } catch (InterruptedException e) {
111 print(e.toString());
112 }
alshabib7911a052014-10-16 17:49:37 -0700113
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700114 service.removeListener(this);
115 }
116
117 private void printResults(int count) {
118 long delta = end - start;
119 print("Time to install %d intents: %d ms", count, delta);
120 }
121
122 /**
123 * Extracts the port number portion of the ConnectPoint.
124 *
125 * @param deviceString string representing the device/port
126 * @return port number as a string, empty string if the port is not found
127 */
128 private String getPortNumber(String deviceString) {
129 int slash = deviceString.indexOf('/');
130 if (slash <= 0) {
131 return "";
132 }
133 return deviceString.substring(slash + 1, deviceString.length());
134 }
135
136 /**
137 * Extracts the device ID portion of the ConnectPoint.
138 *
139 * @param deviceString string representing the device/port
140 * @return device ID string
141 */
142 private String getDeviceId(String deviceString) {
143 int slash = deviceString.indexOf('/');
144 if (slash <= 0) {
145 return "";
146 }
147 return deviceString.substring(0, slash);
148 }
149
150 @Override
151 public void event(IntentEvent event) {
152 if (event.type() == Type.INSTALLED) {
153 end = event.time();
Brian O'Connorea9021e2014-10-10 23:12:02 -0500154 if (latch != null) {
155 latch.countDown();
156 } else {
157 log.warn("install event latch is null");
158 }
alshabib7911a052014-10-16 17:49:37 -0700159 } else {
160 log.info("I FAIL -> {}", event);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700161 }
162 }
163}