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