blob: 4c3ed8e7c35dc33e0da61db0a83ea3c446d2dd4d [file] [log] [blame]
Brian O'Connor1aa99eb2014-10-10 16:18:58 -07001package org.onlab.onos.cli.net;
2
3import java.util.concurrent.CountDownLatch;
4import java.util.concurrent.TimeUnit;
5
6import org.apache.karaf.shell.commands.Argument;
7import org.apache.karaf.shell.commands.Command;
8import org.onlab.onos.cli.AbstractShellCommand;
9import org.onlab.onos.net.ConnectPoint;
10import org.onlab.onos.net.DeviceId;
11import org.onlab.onos.net.PortNumber;
12import org.onlab.onos.net.flow.DefaultTrafficSelector;
13import org.onlab.onos.net.flow.DefaultTrafficTreatment;
14import org.onlab.onos.net.flow.TrafficSelector;
15import org.onlab.onos.net.flow.TrafficTreatment;
16import org.onlab.onos.net.intent.Intent;
17import org.onlab.onos.net.intent.IntentEvent;
18import org.onlab.onos.net.intent.IntentEvent.Type;
19import org.onlab.onos.net.intent.IntentId;
20import org.onlab.onos.net.intent.IntentListener;
21import org.onlab.onos.net.intent.IntentService;
22import org.onlab.onos.net.intent.PointToPointIntent;
23import org.onlab.packet.Ethernet;
24import org.onlab.packet.MacAddress;
25
26/**
27 * Installs point-to-point connectivity intents.
28 */
29@Command(scope = "onos", name = "push-test-intents",
30 description = "Installs random intents to test throughput")
31public class IntentPushTestCommand extends AbstractShellCommand
32 implements IntentListener {
33
34 @Argument(index = 0, name = "ingressDevice",
35 description = "Ingress Device/Port Description",
36 required = true, multiValued = false)
37 String ingressDeviceString = null;
38
39 @Argument(index = 1, name = "egressDevice",
40 description = "Egress Device/Port Description",
41 required = true, multiValued = false)
42 String egressDeviceString = null;
43
44 @Argument(index = 2, name = "count",
45 description = "Number of intents to push",
46 required = true, multiValued = false)
47 String countString = null;
48
49
50 private static long id = 0x7870001;
51
52 private IntentService service;
53 private CountDownLatch latch;
54 private long start, end;
55
56 @Override
57 protected void execute() {
58 service = get(IntentService.class);
59
60 DeviceId ingressDeviceId = DeviceId.deviceId(getDeviceId(ingressDeviceString));
61 PortNumber ingressPortNumber =
62 PortNumber.portNumber(getPortNumber(ingressDeviceString));
63 ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber);
64
65 DeviceId egressDeviceId = DeviceId.deviceId(getDeviceId(egressDeviceString));
66 PortNumber egressPortNumber =
67 PortNumber.portNumber(getPortNumber(egressDeviceString));
68 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
82 .matchEthSrc(MacAddress.valueOf(i))
83 .build();
84 Intent intent =
85 new PointToPointIntent(new IntentId(id++),
86 s,
87 treatment,
88 ingress,
89 egress);
90 service.submit(intent);
91 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070092 try {
alshabib7911a052014-10-16 17:49:37 -070093 if (latch.await(10, TimeUnit.SECONDS)) {
94 printResults(count);
95 } else {
96 print("I FAIL MISERABLY -> %d", latch.getCount());
97 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070098 } catch (InterruptedException e) {
99 print(e.toString());
100 }
alshabib7911a052014-10-16 17:49:37 -0700101
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700102 service.removeListener(this);
103 }
104
105 private void printResults(int count) {
106 long delta = end - start;
107 print("Time to install %d intents: %d ms", count, delta);
108 }
109
110 /**
111 * Extracts the port number portion of the ConnectPoint.
112 *
113 * @param deviceString string representing the device/port
114 * @return port number as a string, empty string if the port is not found
115 */
116 private String getPortNumber(String deviceString) {
117 int slash = deviceString.indexOf('/');
118 if (slash <= 0) {
119 return "";
120 }
121 return deviceString.substring(slash + 1, deviceString.length());
122 }
123
124 /**
125 * Extracts the device ID portion of the ConnectPoint.
126 *
127 * @param deviceString string representing the device/port
128 * @return device ID string
129 */
130 private String getDeviceId(String deviceString) {
131 int slash = deviceString.indexOf('/');
132 if (slash <= 0) {
133 return "";
134 }
135 return deviceString.substring(0, slash);
136 }
137
138 @Override
139 public void event(IntentEvent event) {
140 if (event.type() == Type.INSTALLED) {
141 end = event.time();
Brian O'Connorea9021e2014-10-10 23:12:02 -0500142 if (latch != null) {
143 latch.countDown();
144 } else {
145 log.warn("install event latch is null");
146 }
alshabib7911a052014-10-16 17:49:37 -0700147 } else {
148 log.info("I FAIL -> {}", event);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700149 }
150 }
151}