blob: 60181bdd97f66ec496401466ff2176e029c75329 [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 {
Brian O'Connorea9021e2014-10-10 23:12:02 -050093 latch.await(5, TimeUnit.SECONDS);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070094 printResults(count);
95 } catch (InterruptedException e) {
96 print(e.toString());
97 }
98 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 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700143 }
144 }
145}