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