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; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 32 | import org.onlab.onos.net.intent.IntentOperations; |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 33 | import org.onlab.onos.net.intent.IntentService; |
| 34 | import org.onlab.onos.net.intent.PointToPointIntent; |
| 35 | import org.onlab.packet.Ethernet; |
| 36 | import org.onlab.packet.MacAddress; |
| 37 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 38 | import java.util.concurrent.CountDownLatch; |
| 39 | import java.util.concurrent.TimeUnit; |
| 40 | |
| 41 | import static org.onlab.onos.net.DeviceId.deviceId; |
| 42 | import static org.onlab.onos.net.PortNumber.portNumber; |
| 43 | |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 44 | /** |
| 45 | * Installs point-to-point connectivity intents. |
| 46 | */ |
| 47 | @Command(scope = "onos", name = "push-test-intents", |
| 48 | description = "Installs random intents to test throughput") |
| 49 | public class IntentPushTestCommand extends AbstractShellCommand |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 50 | implements IntentListener { |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 51 | |
| 52 | @Argument(index = 0, name = "ingressDevice", |
| 53 | description = "Ingress Device/Port Description", |
| 54 | required = true, multiValued = false) |
| 55 | String ingressDeviceString = null; |
| 56 | |
| 57 | @Argument(index = 1, name = "egressDevice", |
| 58 | description = "Egress Device/Port Description", |
| 59 | required = true, multiValued = false) |
| 60 | String egressDeviceString = null; |
| 61 | |
| 62 | @Argument(index = 2, name = "count", |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 63 | description = "Number of intents to push", |
| 64 | required = true, multiValued = false) |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 65 | String countString = null; |
| 66 | |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 67 | private IntentService service; |
| 68 | private CountDownLatch latch; |
| 69 | private long start, end; |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 70 | private int count; |
| 71 | private boolean add; |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 72 | |
| 73 | @Override |
| 74 | protected void execute() { |
| 75 | service = get(IntentService.class); |
| 76 | |
Brian O'Connor | 6741a5f | 2014-11-19 20:40:11 -0800 | [diff] [blame] | 77 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 78 | DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString)); |
| 79 | PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString)); |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 80 | ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber); |
| 81 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 82 | DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString)); |
| 83 | PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString)); |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 84 | ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber); |
| 85 | |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 86 | count = Integer.parseInt(countString); |
| 87 | |
| 88 | service.addListener(this); |
| 89 | |
| 90 | add = true; |
| 91 | latch = new CountDownLatch(count); |
| 92 | IntentOperations operations = generateIntents(ingress, egress); |
| 93 | submitIntents(operations); |
| 94 | |
| 95 | add = false; |
| 96 | latch = new CountDownLatch(count); |
| 97 | operations = generateIntents(ingress, egress); |
| 98 | submitIntents(operations); |
| 99 | |
| 100 | service.removeListener(this); |
| 101 | } |
| 102 | |
| 103 | private IntentOperations generateIntents(ConnectPoint ingress, ConnectPoint egress) { |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 104 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder() |
| 105 | .matchEthType(Ethernet.TYPE_IPV4); |
| 106 | TrafficTreatment treatment = DefaultTrafficTreatment.builder().build(); |
| 107 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 108 | IntentOperations.Builder ops = IntentOperations.builder(); |
| 109 | for (int i = 1; i <= count; i++) { |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 110 | TrafficSelector s = selector |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 111 | .matchEthSrc(MacAddress.valueOf(i)) |
| 112 | .build(); |
| 113 | Intent intent = new PointToPointIntent(appId(), s, treatment, |
| 114 | ingress, egress); |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 115 | if (add) { |
| 116 | ops.addSubmitOperation(intent); |
| 117 | } else { |
| 118 | ops.addWithdrawOperation(intent.id()); |
| 119 | } |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 120 | } |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 121 | return ops.build(); |
| 122 | } |
| 123 | |
| 124 | private void submitIntents(IntentOperations ops) { |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 125 | start = System.currentTimeMillis(); |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 126 | service.execute(ops); |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 127 | try { |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 128 | if (latch.await(10, TimeUnit.SECONDS)) { |
| 129 | printResults(count); |
| 130 | } else { |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 131 | print("Failure: %d intents not installed", latch.getCount()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 132 | } |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 133 | } catch (InterruptedException e) { |
| 134 | print(e.toString()); |
| 135 | } |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | private void printResults(int count) { |
| 139 | long delta = end - start; |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 140 | String text = add ? "install" : "withdraw"; |
| 141 | print("Time to %s %d intents: %d ms", text, count, delta); |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Extracts the port number portion of the ConnectPoint. |
| 146 | * |
| 147 | * @param deviceString string representing the device/port |
| 148 | * @return port number as a string, empty string if the port is not found |
| 149 | */ |
| 150 | private String getPortNumber(String deviceString) { |
| 151 | int slash = deviceString.indexOf('/'); |
| 152 | if (slash <= 0) { |
| 153 | return ""; |
| 154 | } |
| 155 | return deviceString.substring(slash + 1, deviceString.length()); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Extracts the device ID portion of the ConnectPoint. |
| 160 | * |
| 161 | * @param deviceString string representing the device/port |
| 162 | * @return device ID string |
| 163 | */ |
| 164 | private String getDeviceId(String deviceString) { |
| 165 | int slash = deviceString.indexOf('/'); |
| 166 | if (slash <= 0) { |
| 167 | return ""; |
| 168 | } |
| 169 | return deviceString.substring(0, slash); |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public void event(IntentEvent event) { |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 174 | Type expected = add ? Type.INSTALLED : Type.WITHDRAWN; |
| 175 | if (event.type() == expected) { |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 176 | end = event.time(); |
Brian O'Connor | ea9021e | 2014-10-10 23:12:02 -0500 | [diff] [blame] | 177 | if (latch != null) { |
| 178 | latch.countDown(); |
| 179 | } else { |
| 180 | log.warn("install event latch is null"); |
| 181 | } |
Yuta HIGUCHI | 963c656 | 2014-11-24 18:59:54 -0800 | [diff] [blame] | 182 | } else if (event.type() != Type.SUBMITTED) { |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 183 | log.info("Unexpected intent event: {}", event); |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | } |