Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 16 | package org.onlab.onos.cli.net; |
| 17 | |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 18 | import org.apache.karaf.shell.commands.Argument; |
| 19 | import org.apache.karaf.shell.commands.Command; |
| 20 | import org.onlab.onos.cli.AbstractShellCommand; |
| 21 | import org.onlab.onos.net.ConnectPoint; |
| 22 | import org.onlab.onos.net.DeviceId; |
| 23 | import org.onlab.onos.net.PortNumber; |
| 24 | import org.onlab.onos.net.flow.DefaultTrafficSelector; |
| 25 | import org.onlab.onos.net.flow.DefaultTrafficTreatment; |
| 26 | import org.onlab.onos.net.flow.TrafficSelector; |
| 27 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 28 | import org.onlab.onos.net.intent.Intent; |
| 29 | import org.onlab.onos.net.intent.IntentEvent; |
| 30 | import org.onlab.onos.net.intent.IntentEvent.Type; |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 31 | import org.onlab.onos.net.intent.IntentListener; |
| 32 | import org.onlab.onos.net.intent.IntentService; |
| 33 | import org.onlab.onos.net.intent.PointToPointIntent; |
| 34 | import org.onlab.packet.Ethernet; |
| 35 | import org.onlab.packet.MacAddress; |
| 36 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 37 | import java.util.concurrent.CountDownLatch; |
| 38 | import java.util.concurrent.TimeUnit; |
| 39 | |
| 40 | import static org.onlab.onos.net.DeviceId.deviceId; |
| 41 | import static org.onlab.onos.net.PortNumber.portNumber; |
| 42 | |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 43 | /** |
| 44 | * Installs point-to-point connectivity intents. |
| 45 | */ |
| 46 | @Command(scope = "onos", name = "push-test-intents", |
| 47 | description = "Installs random intents to test throughput") |
| 48 | public class IntentPushTestCommand extends AbstractShellCommand |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 49 | implements IntentListener { |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 50 | |
| 51 | @Argument(index = 0, name = "ingressDevice", |
| 52 | description = "Ingress Device/Port Description", |
| 53 | required = true, multiValued = false) |
| 54 | String ingressDeviceString = null; |
| 55 | |
| 56 | @Argument(index = 1, name = "egressDevice", |
| 57 | description = "Egress Device/Port Description", |
| 58 | required = true, multiValued = false) |
| 59 | String egressDeviceString = null; |
| 60 | |
| 61 | @Argument(index = 2, name = "count", |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 62 | description = "Number of intents to push", |
| 63 | required = true, multiValued = false) |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 64 | String countString = null; |
| 65 | |
| 66 | |
| 67 | private static long id = 0x7870001; |
| 68 | |
| 69 | private IntentService service; |
| 70 | private CountDownLatch latch; |
| 71 | private long start, end; |
| 72 | |
| 73 | @Override |
| 74 | protected void execute() { |
| 75 | service = get(IntentService.class); |
| 76 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 77 | DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString)); |
| 78 | PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString)); |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 79 | ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber); |
| 80 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 81 | DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString)); |
| 82 | PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString)); |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 83 | ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber); |
| 84 | |
| 85 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder() |
| 86 | .matchEthType(Ethernet.TYPE_IPV4); |
| 87 | TrafficTreatment treatment = DefaultTrafficTreatment.builder().build(); |
| 88 | |
| 89 | int count = Integer.parseInt(countString); |
| 90 | |
| 91 | service.addListener(this); |
Brian O'Connor | ea9021e | 2014-10-10 23:12:02 -0500 | [diff] [blame] | 92 | latch = new CountDownLatch(count); |
| 93 | |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 94 | start = System.currentTimeMillis(); |
| 95 | for (int i = 0; i < count; i++) { |
| 96 | TrafficSelector s = selector |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 97 | .matchEthSrc(MacAddress.valueOf(i)) |
| 98 | .build(); |
| 99 | Intent intent = new PointToPointIntent(appId(), s, treatment, |
| 100 | ingress, egress); |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 101 | service.submit(intent); |
| 102 | } |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 103 | try { |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 104 | if (latch.await(10, TimeUnit.SECONDS)) { |
| 105 | printResults(count); |
| 106 | } else { |
| 107 | print("I FAIL MISERABLY -> %d", latch.getCount()); |
| 108 | } |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 109 | } catch (InterruptedException e) { |
| 110 | print(e.toString()); |
| 111 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 112 | |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 113 | service.removeListener(this); |
| 114 | } |
| 115 | |
| 116 | private void printResults(int count) { |
| 117 | long delta = end - start; |
| 118 | print("Time to install %d intents: %d ms", count, delta); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Extracts the port number portion of the ConnectPoint. |
| 123 | * |
| 124 | * @param deviceString string representing the device/port |
| 125 | * @return port number as a string, empty string if the port is not found |
| 126 | */ |
| 127 | private String getPortNumber(String deviceString) { |
| 128 | int slash = deviceString.indexOf('/'); |
| 129 | if (slash <= 0) { |
| 130 | return ""; |
| 131 | } |
| 132 | return deviceString.substring(slash + 1, deviceString.length()); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Extracts the device ID portion of the ConnectPoint. |
| 137 | * |
| 138 | * @param deviceString string representing the device/port |
| 139 | * @return device ID string |
| 140 | */ |
| 141 | private String getDeviceId(String deviceString) { |
| 142 | int slash = deviceString.indexOf('/'); |
| 143 | if (slash <= 0) { |
| 144 | return ""; |
| 145 | } |
| 146 | return deviceString.substring(0, slash); |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public void event(IntentEvent event) { |
| 151 | if (event.type() == Type.INSTALLED) { |
| 152 | end = event.time(); |
Brian O'Connor | ea9021e | 2014-10-10 23:12:02 -0500 | [diff] [blame] | 153 | if (latch != null) { |
| 154 | latch.countDown(); |
| 155 | } else { |
| 156 | log.warn("install event latch is null"); |
| 157 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 158 | } else { |
| 159 | log.info("I FAIL -> {}", event); |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | } |