blob: 30c289e70ac17b1b715a48c999346b14c2f9e061 [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);
77 start = System.currentTimeMillis();
78 for (int i = 0; i < count; i++) {
79 TrafficSelector s = selector
80 .matchEthSrc(MacAddress.valueOf(i))
81 .build();
82 Intent intent =
83 new PointToPointIntent(new IntentId(id++),
84 s,
85 treatment,
86 ingress,
87 egress);
88 service.submit(intent);
89 }
90 latch = new CountDownLatch(count);
91 try {
92 latch.await(3, TimeUnit.SECONDS);
93 printResults(count);
94 } catch (InterruptedException e) {
95 print(e.toString());
96 }
97 service.removeListener(this);
98 }
99
100 private void printResults(int count) {
101 long delta = end - start;
102 print("Time to install %d intents: %d ms", count, delta);
103 }
104
105 /**
106 * Extracts the port number portion of the ConnectPoint.
107 *
108 * @param deviceString string representing the device/port
109 * @return port number as a string, empty string if the port is not found
110 */
111 private String getPortNumber(String deviceString) {
112 int slash = deviceString.indexOf('/');
113 if (slash <= 0) {
114 return "";
115 }
116 return deviceString.substring(slash + 1, deviceString.length());
117 }
118
119 /**
120 * Extracts the device ID portion of the ConnectPoint.
121 *
122 * @param deviceString string representing the device/port
123 * @return device ID string
124 */
125 private String getDeviceId(String deviceString) {
126 int slash = deviceString.indexOf('/');
127 if (slash <= 0) {
128 return "";
129 }
130 return deviceString.substring(0, slash);
131 }
132
133 @Override
134 public void event(IntentEvent event) {
135 if (event.type() == Type.INSTALLED) {
136 end = event.time();
137 latch.countDown();
138 }
139 }
140}