Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present 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 | |
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; |
| 27 | import org.onosproject.net.PortNumber; |
| 28 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 29 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 30 | import org.onosproject.net.flow.TrafficSelector; |
| 31 | import org.onosproject.net.flow.TrafficTreatment; |
| 32 | import org.onosproject.net.intent.Intent; |
| 33 | import org.onosproject.net.intent.IntentEvent; |
| 34 | import org.onosproject.net.intent.IntentEvent.Type; |
| 35 | import org.onosproject.net.intent.IntentListener; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 36 | import org.onosproject.net.intent.IntentService; |
Brian O'Connor | 5b9dfdc | 2015-02-12 09:51:11 -0800 | [diff] [blame] | 37 | import org.onosproject.net.intent.Key; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 38 | import org.onosproject.net.intent.PointToPointIntent; |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 39 | |
suibin zhang | 4d2b490 | 2016-01-20 09:15:56 -0800 | [diff] [blame] | 40 | import java.util.ArrayList; |
| 41 | import java.util.EnumSet; |
| 42 | import java.util.List; |
| 43 | import java.util.concurrent.CountDownLatch; |
| 44 | import java.util.concurrent.TimeUnit; |
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; |
suibin zhang | 4d2b490 | 2016-01-20 09:15:56 -0800 | [diff] [blame] | 93 | List<Key> keysForInstall = new ArrayList<>(); |
| 94 | List<Key> keysForWithdraw = new ArrayList<>(); |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 95 | |
| 96 | @Override |
| 97 | protected void execute() { |
| 98 | service = get(IntentService.class); |
| 99 | |
Brian O'Connor | 6741a5f | 2014-11-19 20:40:11 -0800 | [diff] [blame] | 100 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 101 | DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString)); |
| 102 | PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString)); |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 103 | ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber); |
| 104 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 105 | DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString)); |
| 106 | PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString)); |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 107 | ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber); |
| 108 | |
Brian O'Connor | 5b9dfdc | 2015-02-12 09:51:11 -0800 | [diff] [blame] | 109 | count = Integer.parseInt(numberOfIntents); |
| 110 | keyOffset = (keyOffsetStr != null) ? Integer.parseInt(keyOffsetStr) : 1; |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 111 | |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 112 | service.addListener(this); |
| 113 | |
Brian O'Connor | 5b9dfdc | 2015-02-12 09:51:11 -0800 | [diff] [blame] | 114 | List<Intent> operations = generateIntents(ingress, egress); |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 115 | |
Brian O'Connor | a0d3100 | 2014-12-01 13:43:18 -0800 | [diff] [blame] | 116 | boolean both = !(installOnly ^ withdrawOnly); |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 117 | |
Brian O'Connor | a0d3100 | 2014-12-01 13:43:18 -0800 | [diff] [blame] | 118 | if (installOnly || both) { |
| 119 | add = true; |
Brian O'Connor | a0d3100 | 2014-12-01 13:43:18 -0800 | [diff] [blame] | 120 | submitIntents(operations); |
| 121 | } |
| 122 | |
| 123 | if (withdrawOnly || both) { |
Brian O'Connor | a0d3100 | 2014-12-01 13:43:18 -0800 | [diff] [blame] | 124 | add = false; |
Brian O'Connor | a0d3100 | 2014-12-01 13:43:18 -0800 | [diff] [blame] | 125 | submitIntents(operations); |
| 126 | } |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 127 | |
| 128 | service.removeListener(this); |
| 129 | } |
| 130 | |
Brian O'Connor | 5b9dfdc | 2015-02-12 09:51:11 -0800 | [diff] [blame] | 131 | private List<Intent> generateIntents(ConnectPoint ingress, ConnectPoint egress) { |
| 132 | TrafficSelector.Builder selectorBldr = DefaultTrafficSelector.builder() |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 133 | .matchEthType(Ethernet.TYPE_IPV4); |
Brian O'Connor | 6b52813 | 2015-03-10 16:39:52 -0700 | [diff] [blame] | 134 | TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment(); |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 135 | |
Brian O'Connor | 5b9dfdc | 2015-02-12 09:51:11 -0800 | [diff] [blame] | 136 | List<Intent> intents = Lists.newArrayList(); |
| 137 | for (int i = 0; i < count; i++) { |
| 138 | TrafficSelector selector = selectorBldr |
| 139 | .matchEthSrc(MacAddress.valueOf(i + keyOffset)) |
| 140 | .build(); |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 141 | intents.add(PointToPointIntent.builder() |
| 142 | .appId(appId()) |
| 143 | .key(Key.of(i + keyOffset, appId())) |
| 144 | .selector(selector) |
| 145 | .treatment(treatment) |
| 146 | .ingressPoint(ingress) |
| 147 | .egressPoint(egress) |
| 148 | .build()); |
suibin zhang | 4d2b490 | 2016-01-20 09:15:56 -0800 | [diff] [blame] | 149 | keysForInstall.add(Key.of(i + keyOffset, appId())); |
| 150 | keysForWithdraw.add(Key.of(i + keyOffset, appId())); |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 151 | } |
Thomas Vachuska | e4b6bb2 | 2014-11-25 17:09:43 -0800 | [diff] [blame] | 152 | return intents; |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Brian O'Connor | 5b9dfdc | 2015-02-12 09:51:11 -0800 | [diff] [blame] | 155 | private void submitIntents(List<Intent> intents) { |
| 156 | latch = new CountDownLatch(count); |
suibin zhang | 4d2b490 | 2016-01-20 09:15:56 -0800 | [diff] [blame] | 157 | log.info("CountDownLatch is set with count of {}", count); |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 158 | start = System.currentTimeMillis(); |
Brian O'Connor | 5b9dfdc | 2015-02-12 09:51:11 -0800 | [diff] [blame] | 159 | for (Intent intent : intents) { |
| 160 | if (add) { |
| 161 | service.submit(intent); |
| 162 | } else { |
| 163 | service.withdraw(intent); |
Thomas Vachuska | e4b6bb2 | 2014-11-25 17:09:43 -0800 | [diff] [blame] | 164 | } |
| 165 | } |
Thomas Vachuska | e4b6bb2 | 2014-11-25 17:09:43 -0800 | [diff] [blame] | 166 | |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 167 | try { |
suibin zhang | 4d2b490 | 2016-01-20 09:15:56 -0800 | [diff] [blame] | 168 | if (latch.await(1000 + count * 30, TimeUnit.MILLISECONDS)) { |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 169 | printResults(count); |
| 170 | } else { |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 171 | print("Failure: %d intents not installed", latch.getCount()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 172 | } |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 173 | } catch (InterruptedException e) { |
| 174 | print(e.toString()); |
| 175 | } |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | private void printResults(int count) { |
| 179 | long delta = end - start; |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 180 | String text = add ? "install" : "withdraw"; |
| 181 | print("Time to %s %d intents: %d ms", text, count, delta); |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Extracts the port number portion of the ConnectPoint. |
| 186 | * |
| 187 | * @param deviceString string representing the device/port |
| 188 | * @return port number as a string, empty string if the port is not found |
| 189 | */ |
| 190 | private String getPortNumber(String deviceString) { |
| 191 | int slash = deviceString.indexOf('/'); |
| 192 | if (slash <= 0) { |
| 193 | return ""; |
| 194 | } |
| 195 | return deviceString.substring(slash + 1, deviceString.length()); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Extracts the device ID portion of the ConnectPoint. |
| 200 | * |
| 201 | * @param deviceString string representing the device/port |
| 202 | * @return device ID string |
| 203 | */ |
| 204 | private String getDeviceId(String deviceString) { |
| 205 | int slash = deviceString.indexOf('/'); |
| 206 | if (slash <= 0) { |
| 207 | return ""; |
| 208 | } |
| 209 | return deviceString.substring(0, slash); |
| 210 | } |
| 211 | |
Brian O'Connor | ae3e733 | 2014-12-02 16:02:49 -0800 | [diff] [blame] | 212 | private static final EnumSet<IntentEvent.Type> IGNORE_EVENT |
| 213 | = EnumSet.of(Type.INSTALL_REQ, Type.WITHDRAW_REQ); |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 214 | @Override |
Yuta HIGUCHI | ad49299 | 2014-12-03 14:13:54 -0800 | [diff] [blame] | 215 | public synchronized void event(IntentEvent event) { |
Brian O'Connor | 5b9dfdc | 2015-02-12 09:51:11 -0800 | [diff] [blame] | 216 | if (!appId().equals(event.subject().appId())) { |
Yuta HIGUCHI | 87695b8 | 2014-12-03 15:19:35 -0800 | [diff] [blame] | 217 | // not my event, ignore |
| 218 | return; |
| 219 | } |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 220 | Type expected = add ? Type.INSTALLED : Type.WITHDRAWN; |
suibin zhang | 4d2b490 | 2016-01-20 09:15:56 -0800 | [diff] [blame] | 221 | List keylist = add ? keysForInstall : keysForWithdraw; |
| 222 | log.debug("Event generated: {}", event); |
| 223 | if (event.type() == expected && keylist.contains(event.subject().key())) { |
Yuta HIGUCHI | ad49299 | 2014-12-03 14:13:54 -0800 | [diff] [blame] | 224 | end = Math.max(end, event.time()); |
suibin zhang | 4d2b490 | 2016-01-20 09:15:56 -0800 | [diff] [blame] | 225 | keylist.remove(event.subject().key()); |
Brian O'Connor | ea9021e | 2014-10-10 23:12:02 -0500 | [diff] [blame] | 226 | if (latch != null) { |
Yuta HIGUCHI | ad49299 | 2014-12-03 14:13:54 -0800 | [diff] [blame] | 227 | if (latch.getCount() == 0) { |
| 228 | log.warn("Latch was already 0 before counting down?"); |
| 229 | } |
Brian O'Connor | ea9021e | 2014-10-10 23:12:02 -0500 | [diff] [blame] | 230 | latch.countDown(); |
suibin zhang | 4d2b490 | 2016-01-20 09:15:56 -0800 | [diff] [blame] | 231 | log.debug("Latch count is {}", latch.getCount()); |
Brian O'Connor | ea9021e | 2014-10-10 23:12:02 -0500 | [diff] [blame] | 232 | } else { |
| 233 | log.warn("install event latch is null"); |
| 234 | } |
Brian O'Connor | ae3e733 | 2014-12-02 16:02:49 -0800 | [diff] [blame] | 235 | } else if (IGNORE_EVENT.contains(event.type())) { |
Brian O'Connor | 8016f34 | 2015-02-24 17:00:39 -0800 | [diff] [blame] | 236 | log.debug("Unexpected intent event: {}", event); |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | } |