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 | |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 18 | import com.google.common.collect.ArrayListMultimap; |
Thomas Vachuska | e4b6bb2 | 2014-11-25 17:09:43 -0800 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.commands.Argument; |
| 21 | import org.apache.karaf.shell.commands.Command; |
Brian O'Connor | a0d3100 | 2014-12-01 13:43:18 -0800 | [diff] [blame] | 22 | import org.apache.karaf.shell.commands.Option; |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 23 | import org.onlab.onos.cli.AbstractShellCommand; |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 24 | import org.onlab.onos.core.ApplicationId; |
| 25 | import org.onlab.onos.core.CoreService; |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 26 | import org.onlab.onos.net.ConnectPoint; |
| 27 | import org.onlab.onos.net.DeviceId; |
| 28 | import org.onlab.onos.net.PortNumber; |
| 29 | import org.onlab.onos.net.flow.DefaultTrafficSelector; |
| 30 | import org.onlab.onos.net.flow.DefaultTrafficTreatment; |
| 31 | import org.onlab.onos.net.flow.TrafficSelector; |
| 32 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 33 | import org.onlab.onos.net.intent.Intent; |
| 34 | import org.onlab.onos.net.intent.IntentEvent; |
| 35 | import org.onlab.onos.net.intent.IntentEvent.Type; |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 36 | import org.onlab.onos.net.intent.IntentListener; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 37 | import org.onlab.onos.net.intent.IntentOperations; |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 38 | import org.onlab.onos.net.intent.IntentService; |
| 39 | import org.onlab.onos.net.intent.PointToPointIntent; |
| 40 | import org.onlab.packet.Ethernet; |
| 41 | import org.onlab.packet.MacAddress; |
| 42 | |
Thomas Vachuska | e4b6bb2 | 2014-11-25 17:09:43 -0800 | [diff] [blame] | 43 | import java.util.List; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 44 | import java.util.concurrent.CountDownLatch; |
| 45 | import java.util.concurrent.TimeUnit; |
| 46 | |
| 47 | import static org.onlab.onos.net.DeviceId.deviceId; |
| 48 | import static org.onlab.onos.net.PortNumber.portNumber; |
| 49 | |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 50 | /** |
| 51 | * Installs point-to-point connectivity intents. |
| 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 | |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 68 | |
| 69 | @Argument(index = 2, name = "Intents per appId", |
| 70 | description = "Number of intents per appId", |
| 71 | required = true, multiValued = false) |
| 72 | String intentsPerAppId = null; |
| 73 | |
| 74 | @Argument(index = 3, name = "apps", |
| 75 | description = "Number of appIds", |
| 76 | required = false, multiValued = false) |
| 77 | String appIds = null; |
| 78 | |
Brian O'Connor | a0d3100 | 2014-12-01 13:43:18 -0800 | [diff] [blame] | 79 | @Argument(index = 4, name = "appIdBase", |
| 80 | description = "Base Value for Application IDs", |
| 81 | required = false, multiValued = false) |
| 82 | String appIdBaseStr = null; |
| 83 | |
| 84 | @Option(name = "-i", aliases = "--install", |
| 85 | description = "Install intents", |
| 86 | required = false, multiValued = false) |
| 87 | private boolean installOnly = false; |
| 88 | |
| 89 | @Option(name = "-w", aliases = "--withdraw", |
| 90 | description = "Withdraw intents", |
| 91 | required = false, multiValued = false) |
| 92 | private boolean withdrawOnly = false; |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 93 | |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 94 | private IntentService service; |
| 95 | private CountDownLatch latch; |
| 96 | private long start, end; |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 97 | private int apps; |
| 98 | private int intentsPerApp; |
Brian O'Connor | a0d3100 | 2014-12-01 13:43:18 -0800 | [diff] [blame] | 99 | private int appIdBase; |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 100 | private int count; |
| 101 | private boolean add; |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 102 | |
| 103 | @Override |
| 104 | protected void execute() { |
| 105 | service = get(IntentService.class); |
| 106 | |
Brian O'Connor | 6741a5f | 2014-11-19 20:40:11 -0800 | [diff] [blame] | 107 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 108 | DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString)); |
| 109 | PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString)); |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 110 | ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber); |
| 111 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 112 | DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString)); |
| 113 | PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString)); |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 114 | ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber); |
| 115 | |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 116 | apps = appIds != null ? Integer.parseInt(appIds) : 1; |
| 117 | intentsPerApp = Integer.parseInt(intentsPerAppId); |
Brian O'Connor | a0d3100 | 2014-12-01 13:43:18 -0800 | [diff] [blame] | 118 | appIdBase = appIdBaseStr != null ? Integer.parseInt(appIdBaseStr) : 1; |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 119 | |
| 120 | count = intentsPerApp * apps; |
| 121 | |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 122 | service.addListener(this); |
| 123 | |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 124 | ArrayListMultimap<Integer, Intent> operations = generateIntents(ingress, egress); |
| 125 | |
Brian O'Connor | a0d3100 | 2014-12-01 13:43:18 -0800 | [diff] [blame] | 126 | boolean both = !(installOnly ^ withdrawOnly); |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 127 | |
Brian O'Connor | a0d3100 | 2014-12-01 13:43:18 -0800 | [diff] [blame] | 128 | if (installOnly || both) { |
| 129 | add = true; |
| 130 | latch = new CountDownLatch(count); |
| 131 | submitIntents(operations); |
| 132 | } |
| 133 | |
| 134 | if (withdrawOnly || both) { |
| 135 | if (withdrawOnly && !both) { |
| 136 | print("This should fail for now..."); |
| 137 | } |
| 138 | add = false; |
| 139 | latch = new CountDownLatch(count); |
| 140 | submitIntents(operations); |
| 141 | } |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 142 | |
| 143 | service.removeListener(this); |
| 144 | } |
| 145 | |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 146 | private ArrayListMultimap<Integer, Intent> generateIntents(ConnectPoint ingress, ConnectPoint egress) { |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 147 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder() |
| 148 | .matchEthType(Ethernet.TYPE_IPV4); |
| 149 | TrafficTreatment treatment = DefaultTrafficTreatment.builder().build(); |
| 150 | |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 151 | ArrayListMultimap<Integer, Intent> intents = ArrayListMultimap.create(); |
Brian O'Connor | a0d3100 | 2014-12-01 13:43:18 -0800 | [diff] [blame] | 152 | for (int app = 0; app < apps; app++) { |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 153 | for (int i = 1; i <= intentsPerApp; i++) { |
| 154 | TrafficSelector s = selector |
| 155 | .matchEthSrc(MacAddress.valueOf(i)) |
| 156 | .build(); |
Brian O'Connor | a0d3100 | 2014-12-01 13:43:18 -0800 | [diff] [blame] | 157 | intents.put(app, new PointToPointIntent(appId(app), s, treatment, |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 158 | ingress, egress)); |
Thomas Vachuska | e4b6bb2 | 2014-11-25 17:09:43 -0800 | [diff] [blame] | 159 | |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 160 | } |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 161 | } |
Thomas Vachuska | e4b6bb2 | 2014-11-25 17:09:43 -0800 | [diff] [blame] | 162 | return intents; |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 165 | private void submitIntents(ArrayListMultimap<Integer, Intent> intents) { |
| 166 | List<IntentOperations> opList = Lists.newArrayList(); |
| 167 | for (Integer app : intents.keySet()) { |
| 168 | IntentOperations.Builder builder = IntentOperations.builder(appId(app)); |
| 169 | for (Intent intent : intents.get(app)) { |
| 170 | if (add) { |
| 171 | builder.addSubmitOperation(intent); |
| 172 | } else { |
| 173 | builder.addWithdrawOperation(intent.id()); |
| 174 | } |
Thomas Vachuska | e4b6bb2 | 2014-11-25 17:09:43 -0800 | [diff] [blame] | 175 | } |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 176 | opList.add(builder.build()); |
Thomas Vachuska | e4b6bb2 | 2014-11-25 17:09:43 -0800 | [diff] [blame] | 177 | } |
Thomas Vachuska | e4b6bb2 | 2014-11-25 17:09:43 -0800 | [diff] [blame] | 178 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 179 | start = System.currentTimeMillis(); |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 180 | opList.forEach(ops -> service.execute(ops)); |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 181 | try { |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 182 | if (latch.await(100 + count * 200, TimeUnit.MILLISECONDS)) { |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 183 | printResults(count); |
| 184 | } else { |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 185 | print("Failure: %d intents not installed", latch.getCount()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 186 | } |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 187 | } catch (InterruptedException e) { |
| 188 | print(e.toString()); |
| 189 | } |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | private void printResults(int count) { |
| 193 | long delta = end - start; |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 194 | String text = add ? "install" : "withdraw"; |
| 195 | print("Time to %s %d intents: %d ms", text, count, delta); |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 198 | |
| 199 | /** |
| 200 | * Returns application ID for the CLI. |
| 201 | * |
alshabib | a9819bf | 2014-11-30 18:15:52 -0800 | [diff] [blame] | 202 | * @param id application id |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 203 | * @return command-line application identifier |
| 204 | */ |
| 205 | protected ApplicationId appId(Integer id) { |
Brian O'Connor | a0d3100 | 2014-12-01 13:43:18 -0800 | [diff] [blame] | 206 | return get(CoreService.class).registerApplication("org.onlab.onos.cli-" |
| 207 | + (id + appIdBase)); |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame] | 208 | } |
| 209 | |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 210 | /** |
| 211 | * Extracts the port number portion of the ConnectPoint. |
| 212 | * |
| 213 | * @param deviceString string representing the device/port |
| 214 | * @return port number as a string, empty string if the port is not found |
| 215 | */ |
| 216 | private String getPortNumber(String deviceString) { |
| 217 | int slash = deviceString.indexOf('/'); |
| 218 | if (slash <= 0) { |
| 219 | return ""; |
| 220 | } |
| 221 | return deviceString.substring(slash + 1, deviceString.length()); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Extracts the device ID portion of the ConnectPoint. |
| 226 | * |
| 227 | * @param deviceString string representing the device/port |
| 228 | * @return device ID string |
| 229 | */ |
| 230 | private String getDeviceId(String deviceString) { |
| 231 | int slash = deviceString.indexOf('/'); |
| 232 | if (slash <= 0) { |
| 233 | return ""; |
| 234 | } |
| 235 | return deviceString.substring(0, slash); |
| 236 | } |
| 237 | |
| 238 | @Override |
| 239 | public void event(IntentEvent event) { |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 240 | Type expected = add ? Type.INSTALLED : Type.WITHDRAWN; |
| 241 | if (event.type() == expected) { |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 242 | end = event.time(); |
Brian O'Connor | ea9021e | 2014-10-10 23:12:02 -0500 | [diff] [blame] | 243 | if (latch != null) { |
| 244 | latch.countDown(); |
| 245 | } else { |
| 246 | log.warn("install event latch is null"); |
| 247 | } |
Yuta HIGUCHI | 963c656 | 2014-11-24 18:59:54 -0800 | [diff] [blame] | 248 | } else if (event.type() != Type.SUBMITTED) { |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 249 | log.info("Unexpected intent event: {}", event); |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | } |