blob: bd010ae00a8fdec5e4545ce3a4bb2ed141c09b54 [file] [log] [blame]
Brian O'Connor1aa99eb2014-10-10 16:18:58 -07001package org.onlab.onos.cli.net;
2
Brian O'Connor1aa99eb2014-10-10 16:18:58 -07003import org.apache.karaf.shell.commands.Argument;
4import org.apache.karaf.shell.commands.Command;
5import org.onlab.onos.cli.AbstractShellCommand;
6import org.onlab.onos.net.ConnectPoint;
7import org.onlab.onos.net.DeviceId;
8import org.onlab.onos.net.PortNumber;
9import org.onlab.onos.net.flow.DefaultTrafficSelector;
10import org.onlab.onos.net.flow.DefaultTrafficTreatment;
11import org.onlab.onos.net.flow.TrafficSelector;
12import org.onlab.onos.net.flow.TrafficTreatment;
13import org.onlab.onos.net.intent.Intent;
14import org.onlab.onos.net.intent.IntentEvent;
15import org.onlab.onos.net.intent.IntentEvent.Type;
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070016import org.onlab.onos.net.intent.IntentListener;
17import org.onlab.onos.net.intent.IntentService;
18import org.onlab.onos.net.intent.PointToPointIntent;
19import org.onlab.packet.Ethernet;
20import org.onlab.packet.MacAddress;
21
Thomas Vachuskab97cf282014-10-20 23:31:12 -070022import java.util.concurrent.CountDownLatch;
23import java.util.concurrent.TimeUnit;
24
25import static org.onlab.onos.net.DeviceId.deviceId;
26import static org.onlab.onos.net.PortNumber.portNumber;
27
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070028/**
29 * Installs point-to-point connectivity intents.
30 */
31@Command(scope = "onos", name = "push-test-intents",
32 description = "Installs random intents to test throughput")
33public class IntentPushTestCommand extends AbstractShellCommand
Thomas Vachuskab97cf282014-10-20 23:31:12 -070034 implements IntentListener {
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070035
36 @Argument(index = 0, name = "ingressDevice",
37 description = "Ingress Device/Port Description",
38 required = true, multiValued = false)
39 String ingressDeviceString = null;
40
41 @Argument(index = 1, name = "egressDevice",
42 description = "Egress Device/Port Description",
43 required = true, multiValued = false)
44 String egressDeviceString = null;
45
46 @Argument(index = 2, name = "count",
Thomas Vachuskab97cf282014-10-20 23:31:12 -070047 description = "Number of intents to push",
48 required = true, multiValued = false)
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070049 String countString = null;
50
51
52 private static long id = 0x7870001;
53
54 private IntentService service;
55 private CountDownLatch latch;
56 private long start, end;
57
58 @Override
59 protected void execute() {
60 service = get(IntentService.class);
61
Thomas Vachuskab97cf282014-10-20 23:31:12 -070062 DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString));
63 PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070064 ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber);
65
Thomas Vachuskab97cf282014-10-20 23:31:12 -070066 DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString));
67 PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070068 ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber);
69
70 TrafficSelector.Builder selector = DefaultTrafficSelector.builder()
71 .matchEthType(Ethernet.TYPE_IPV4);
72 TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
73
74 int count = Integer.parseInt(countString);
75
76 service.addListener(this);
Brian O'Connorea9021e2014-10-10 23:12:02 -050077 latch = new CountDownLatch(count);
78
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070079 start = System.currentTimeMillis();
80 for (int i = 0; i < count; i++) {
81 TrafficSelector s = selector
Thomas Vachuskab97cf282014-10-20 23:31:12 -070082 .matchEthSrc(MacAddress.valueOf(i))
83 .build();
84 Intent intent = new PointToPointIntent(appId(), s, treatment,
85 ingress, egress);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070086 service.submit(intent);
87 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070088 try {
alshabib7911a052014-10-16 17:49:37 -070089 if (latch.await(10, TimeUnit.SECONDS)) {
90 printResults(count);
91 } else {
92 print("I FAIL MISERABLY -> %d", latch.getCount());
93 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070094 } catch (InterruptedException e) {
95 print(e.toString());
96 }
alshabib7911a052014-10-16 17:49:37 -070097
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070098 service.removeListener(this);
99 }
100
101 private void printResults(int count) {
102 long delta = end - start;
103 print("Time to install %d intents: %d ms", count, delta);
104 }
105
106 /**
107 * Extracts the port number portion of the ConnectPoint.
108 *
109 * @param deviceString string representing the device/port
110 * @return port number as a string, empty string if the port is not found
111 */
112 private String getPortNumber(String deviceString) {
113 int slash = deviceString.indexOf('/');
114 if (slash <= 0) {
115 return "";
116 }
117 return deviceString.substring(slash + 1, deviceString.length());
118 }
119
120 /**
121 * Extracts the device ID portion of the ConnectPoint.
122 *
123 * @param deviceString string representing the device/port
124 * @return device ID string
125 */
126 private String getDeviceId(String deviceString) {
127 int slash = deviceString.indexOf('/');
128 if (slash <= 0) {
129 return "";
130 }
131 return deviceString.substring(0, slash);
132 }
133
134 @Override
135 public void event(IntentEvent event) {
136 if (event.type() == Type.INSTALLED) {
137 end = event.time();
Brian O'Connorea9021e2014-10-10 23:12:02 -0500138 if (latch != null) {
139 latch.countDown();
140 } else {
141 log.warn("install event latch is null");
142 }
alshabib7911a052014-10-16 17:49:37 -0700143 } else {
144 log.info("I FAIL -> {}", event);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700145 }
146 }
147}