Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 1 | package org.onlab.onos.cli.net; |
| 2 | |
| 3 | import java.util.concurrent.CountDownLatch; |
| 4 | import java.util.concurrent.TimeUnit; |
| 5 | |
| 6 | import org.apache.karaf.shell.commands.Argument; |
| 7 | import org.apache.karaf.shell.commands.Command; |
| 8 | import org.onlab.onos.cli.AbstractShellCommand; |
| 9 | import org.onlab.onos.net.ConnectPoint; |
| 10 | import org.onlab.onos.net.DeviceId; |
| 11 | import org.onlab.onos.net.PortNumber; |
| 12 | import org.onlab.onos.net.flow.DefaultTrafficSelector; |
| 13 | import org.onlab.onos.net.flow.DefaultTrafficTreatment; |
| 14 | import org.onlab.onos.net.flow.TrafficSelector; |
| 15 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 16 | import org.onlab.onos.net.intent.Intent; |
| 17 | import org.onlab.onos.net.intent.IntentEvent; |
| 18 | import org.onlab.onos.net.intent.IntentEvent.Type; |
| 19 | import org.onlab.onos.net.intent.IntentId; |
| 20 | import org.onlab.onos.net.intent.IntentListener; |
| 21 | import org.onlab.onos.net.intent.IntentService; |
| 22 | import org.onlab.onos.net.intent.PointToPointIntent; |
| 23 | import org.onlab.packet.Ethernet; |
| 24 | import 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") |
| 31 | public 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'Connor | ea9021e | 2014-10-10 23:12:02 -0500 | [diff] [blame] | 77 | latch = new CountDownLatch(count); |
| 78 | |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 79 | 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'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 92 | try { |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame^] | 93 | if (latch.await(10, TimeUnit.SECONDS)) { |
| 94 | printResults(count); |
| 95 | } else { |
| 96 | print("I FAIL MISERABLY -> %d", latch.getCount()); |
| 97 | } |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 98 | } catch (InterruptedException e) { |
| 99 | print(e.toString()); |
| 100 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame^] | 101 | |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 102 | 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'Connor | ea9021e | 2014-10-10 23:12:02 -0500 | [diff] [blame] | 142 | if (latch != null) { |
| 143 | latch.countDown(); |
| 144 | } else { |
| 145 | log.warn("install event latch is null"); |
| 146 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame^] | 147 | } else { |
| 148 | log.info("I FAIL -> {}", event); |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | } |