Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 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 | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.cli.net; |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 17 | |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 18 | import java.util.EnumSet; |
| 19 | import java.util.List; |
| 20 | import java.util.concurrent.CountDownLatch; |
| 21 | import java.util.concurrent.TimeUnit; |
| 22 | |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 23 | import org.apache.karaf.shell.commands.Argument; |
| 24 | import org.apache.karaf.shell.commands.Command; |
Brian O'Connor | a0d3100 | 2014-12-01 13:43:18 -0800 | [diff] [blame] | 25 | import org.apache.karaf.shell.commands.Option; |
Brian O'Connor | 5b9dfdc | 2015-02-12 09:51:11 -0800 | [diff] [blame] | 26 | import org.onlab.packet.Ethernet; |
| 27 | import org.onlab.packet.MacAddress; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 28 | import org.onosproject.cli.AbstractShellCommand; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 29 | import org.onosproject.net.ConnectPoint; |
| 30 | import org.onosproject.net.DeviceId; |
| 31 | import org.onosproject.net.PortNumber; |
| 32 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 33 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 34 | import org.onosproject.net.flow.TrafficSelector; |
| 35 | import org.onosproject.net.flow.TrafficTreatment; |
| 36 | import org.onosproject.net.intent.Intent; |
| 37 | import org.onosproject.net.intent.IntentEvent; |
| 38 | import org.onosproject.net.intent.IntentEvent.Type; |
| 39 | import org.onosproject.net.intent.IntentListener; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 40 | import org.onosproject.net.intent.IntentService; |
Brian O'Connor | 5b9dfdc | 2015-02-12 09:51:11 -0800 | [diff] [blame] | 41 | import org.onosproject.net.intent.Key; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 42 | import org.onosproject.net.intent.PointToPointIntent; |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 43 | |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 44 | import com.google.common.collect.Lists; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 45 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 46 | import static org.onosproject.net.DeviceId.deviceId; |
| 47 | import static org.onosproject.net.PortNumber.portNumber; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 48 | |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 49 | /** |
Charles M.C. Chan | 6f5bdc6 | 2015-04-22 01:21:09 +0800 | [diff] [blame^] | 50 | * Installs bulk point-to-point connectivity intents between given ingress/egress devices. |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 51 | */ |
| 52 | @Command(scope = "onos", name = "push-test-intents", |
| 53 | description = "Installs random intents to test throughput") |
| 54 | public class IntentPushTestCommand extends AbstractShellCommand |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 55 | implements IntentListener { |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 56 | |
| 57 | @Argument(index = 0, name = "ingressDevice", |
| 58 | description = "Ingress Device/Port Description", |
| 59 | required = true, multiValued = false) |
| 60 | String ingressDeviceString = null; |
| 61 | |
| 62 | @Argument(index = 1, name = "egressDevice", |
| 63 | description = "Egress Device/Port Description", |
| 64 | required = true, multiValued = false) |
| 65 | String egressDeviceString = null; |
| 66 | |
Brian O'Connor | 5b9dfdc | 2015-02-12 09:51:11 -0800 | [diff] [blame] | 67 | @Argument(index = 2, name = "numberOfIntents", |
| 68 | description = "Number of intents to install/withdraw", |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 69 | required = true, multiValued = false) |
Brian O'Connor | 5b9dfdc | 2015-02-12 09:51:11 -0800 | [diff] [blame] | 70 | String numberOfIntents = null; |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 71 | |
Brian O'Connor | 5b9dfdc | 2015-02-12 09:51:11 -0800 | [diff] [blame] | 72 | @Argument(index = 3, name = "keyOffset", |
| 73 | description = "Starting point for first key (default: 1)", |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 74 | required = false, multiValued = false) |
Brian O'Connor | 5b9dfdc | 2015-02-12 09:51:11 -0800 | [diff] [blame] | 75 | String keyOffsetStr = null; |
Brian O'Connor | a0d3100 | 2014-12-01 13:43:18 -0800 | [diff] [blame] | 76 | |
| 77 | @Option(name = "-i", aliases = "--install", |
| 78 | description = "Install intents", |
| 79 | required = false, multiValued = false) |
| 80 | private boolean installOnly = false; |
| 81 | |
| 82 | @Option(name = "-w", aliases = "--withdraw", |
| 83 | description = "Withdraw intents", |
| 84 | required = false, multiValued = false) |
| 85 | private boolean withdrawOnly = false; |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 86 | |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 87 | private IntentService service; |
| 88 | private CountDownLatch latch; |
Yuta HIGUCHI | ad49299 | 2014-12-03 14:13:54 -0800 | [diff] [blame] | 89 | private volatile long start, end; |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 90 | private int count; |
Brian O'Connor | 5b9dfdc | 2015-02-12 09:51:11 -0800 | [diff] [blame] | 91 | private int keyOffset; |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 92 | private boolean add; |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 93 | |
| 94 | @Override |
| 95 | protected void execute() { |
| 96 | service = get(IntentService.class); |
| 97 | |
Brian O'Connor | 6741a5f | 2014-11-19 20:40:11 -0800 | [diff] [blame] | 98 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 99 | DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString)); |
| 100 | PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString)); |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 101 | ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber); |
| 102 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 103 | DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString)); |
| 104 | PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString)); |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 105 | ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber); |
| 106 | |
Brian O'Connor | 5b9dfdc | 2015-02-12 09:51:11 -0800 | [diff] [blame] | 107 | count = Integer.parseInt(numberOfIntents); |
| 108 | keyOffset = (keyOffsetStr != null) ? Integer.parseInt(keyOffsetStr) : 1; |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 109 | |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 110 | service.addListener(this); |
| 111 | |
Brian O'Connor | 5b9dfdc | 2015-02-12 09:51:11 -0800 | [diff] [blame] | 112 | List<Intent> operations = generateIntents(ingress, egress); |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 113 | |
Brian O'Connor | a0d3100 | 2014-12-01 13:43:18 -0800 | [diff] [blame] | 114 | boolean both = !(installOnly ^ withdrawOnly); |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 115 | |
Brian O'Connor | a0d3100 | 2014-12-01 13:43:18 -0800 | [diff] [blame] | 116 | if (installOnly || both) { |
| 117 | add = true; |
Brian O'Connor | a0d3100 | 2014-12-01 13:43:18 -0800 | [diff] [blame] | 118 | submitIntents(operations); |
| 119 | } |
| 120 | |
| 121 | if (withdrawOnly || both) { |
Brian O'Connor | a0d3100 | 2014-12-01 13:43:18 -0800 | [diff] [blame] | 122 | add = false; |
Brian O'Connor | a0d3100 | 2014-12-01 13:43:18 -0800 | [diff] [blame] | 123 | submitIntents(operations); |
| 124 | } |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 125 | |
| 126 | service.removeListener(this); |
| 127 | } |
| 128 | |
Brian O'Connor | 5b9dfdc | 2015-02-12 09:51:11 -0800 | [diff] [blame] | 129 | private List<Intent> generateIntents(ConnectPoint ingress, ConnectPoint egress) { |
| 130 | TrafficSelector.Builder selectorBldr = DefaultTrafficSelector.builder() |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 131 | .matchEthType(Ethernet.TYPE_IPV4); |
Brian O'Connor | 6b52813 | 2015-03-10 16:39:52 -0700 | [diff] [blame] | 132 | TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment(); |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 133 | |
Brian O'Connor | 5b9dfdc | 2015-02-12 09:51:11 -0800 | [diff] [blame] | 134 | List<Intent> intents = Lists.newArrayList(); |
| 135 | for (int i = 0; i < count; i++) { |
| 136 | TrafficSelector selector = selectorBldr |
| 137 | .matchEthSrc(MacAddress.valueOf(i + keyOffset)) |
| 138 | .build(); |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 139 | intents.add(PointToPointIntent.builder() |
| 140 | .appId(appId()) |
| 141 | .key(Key.of(i + keyOffset, appId())) |
| 142 | .selector(selector) |
| 143 | .treatment(treatment) |
| 144 | .ingressPoint(ingress) |
| 145 | .egressPoint(egress) |
| 146 | .build()); |
| 147 | |
Thomas Vachuska | e4b6bb2 | 2014-11-25 17:09:43 -0800 | [diff] [blame] | 148 | |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 149 | } |
Thomas Vachuska | e4b6bb2 | 2014-11-25 17:09:43 -0800 | [diff] [blame] | 150 | return intents; |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Brian O'Connor | 5b9dfdc | 2015-02-12 09:51:11 -0800 | [diff] [blame] | 153 | private void submitIntents(List<Intent> intents) { |
| 154 | latch = new CountDownLatch(count); |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 155 | start = System.currentTimeMillis(); |
Brian O'Connor | 5b9dfdc | 2015-02-12 09:51:11 -0800 | [diff] [blame] | 156 | for (Intent intent : intents) { |
| 157 | if (add) { |
| 158 | service.submit(intent); |
| 159 | } else { |
| 160 | service.withdraw(intent); |
Thomas Vachuska | e4b6bb2 | 2014-11-25 17:09:43 -0800 | [diff] [blame] | 161 | } |
| 162 | } |
Thomas Vachuska | e4b6bb2 | 2014-11-25 17:09:43 -0800 | [diff] [blame] | 163 | |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 164 | try { |
Brian O'Connor | 7d405fe | 2015-02-24 11:38:16 -0800 | [diff] [blame] | 165 | if (latch.await(500 + count * 30, TimeUnit.MILLISECONDS)) { |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 166 | printResults(count); |
| 167 | } else { |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 168 | print("Failure: %d intents not installed", latch.getCount()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 169 | } |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 170 | } catch (InterruptedException e) { |
| 171 | print(e.toString()); |
| 172 | } |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | private void printResults(int count) { |
| 176 | long delta = end - start; |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 177 | String text = add ? "install" : "withdraw"; |
| 178 | print("Time to %s %d intents: %d ms", text, count, delta); |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Extracts the port number portion of the ConnectPoint. |
| 183 | * |
| 184 | * @param deviceString string representing the device/port |
| 185 | * @return port number as a string, empty string if the port is not found |
| 186 | */ |
| 187 | private String getPortNumber(String deviceString) { |
| 188 | int slash = deviceString.indexOf('/'); |
| 189 | if (slash <= 0) { |
| 190 | return ""; |
| 191 | } |
| 192 | return deviceString.substring(slash + 1, deviceString.length()); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Extracts the device ID portion of the ConnectPoint. |
| 197 | * |
| 198 | * @param deviceString string representing the device/port |
| 199 | * @return device ID string |
| 200 | */ |
| 201 | private String getDeviceId(String deviceString) { |
| 202 | int slash = deviceString.indexOf('/'); |
| 203 | if (slash <= 0) { |
| 204 | return ""; |
| 205 | } |
| 206 | return deviceString.substring(0, slash); |
| 207 | } |
| 208 | |
Brian O'Connor | ae3e733 | 2014-12-02 16:02:49 -0800 | [diff] [blame] | 209 | private static final EnumSet<IntentEvent.Type> IGNORE_EVENT |
| 210 | = EnumSet.of(Type.INSTALL_REQ, Type.WITHDRAW_REQ); |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 211 | @Override |
Yuta HIGUCHI | ad49299 | 2014-12-03 14:13:54 -0800 | [diff] [blame] | 212 | public synchronized void event(IntentEvent event) { |
Brian O'Connor | 5b9dfdc | 2015-02-12 09:51:11 -0800 | [diff] [blame] | 213 | if (!appId().equals(event.subject().appId())) { |
Yuta HIGUCHI | 87695b8 | 2014-12-03 15:19:35 -0800 | [diff] [blame] | 214 | // not my event, ignore |
| 215 | return; |
| 216 | } |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 217 | Type expected = add ? Type.INSTALLED : Type.WITHDRAWN; |
| 218 | if (event.type() == expected) { |
Yuta HIGUCHI | ad49299 | 2014-12-03 14:13:54 -0800 | [diff] [blame] | 219 | end = Math.max(end, event.time()); |
Brian O'Connor | ea9021e | 2014-10-10 23:12:02 -0500 | [diff] [blame] | 220 | if (latch != null) { |
Yuta HIGUCHI | ad49299 | 2014-12-03 14:13:54 -0800 | [diff] [blame] | 221 | if (latch.getCount() == 0) { |
| 222 | log.warn("Latch was already 0 before counting down?"); |
| 223 | } |
Brian O'Connor | ea9021e | 2014-10-10 23:12:02 -0500 | [diff] [blame] | 224 | latch.countDown(); |
| 225 | } else { |
| 226 | log.warn("install event latch is null"); |
| 227 | } |
Brian O'Connor | ae3e733 | 2014-12-02 16:02:49 -0800 | [diff] [blame] | 228 | } else if (IGNORE_EVENT.contains(event.type())) { |
Brian O'Connor | 8016f34 | 2015-02-24 17:00:39 -0800 | [diff] [blame] | 229 | log.debug("Unexpected intent event: {}", event); |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | } |