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; |
| 22 | import org.onlab.onos.cli.AbstractShellCommand; |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame^] | 23 | import org.onlab.onos.core.ApplicationId; |
| 24 | import org.onlab.onos.core.CoreService; |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 25 | import org.onlab.onos.net.ConnectPoint; |
| 26 | import org.onlab.onos.net.DeviceId; |
| 27 | import org.onlab.onos.net.PortNumber; |
| 28 | import org.onlab.onos.net.flow.DefaultTrafficSelector; |
| 29 | import org.onlab.onos.net.flow.DefaultTrafficTreatment; |
| 30 | import org.onlab.onos.net.flow.TrafficSelector; |
| 31 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 32 | import org.onlab.onos.net.intent.Intent; |
| 33 | import org.onlab.onos.net.intent.IntentEvent; |
| 34 | import org.onlab.onos.net.intent.IntentEvent.Type; |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 35 | import org.onlab.onos.net.intent.IntentListener; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 36 | import org.onlab.onos.net.intent.IntentOperations; |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 37 | import org.onlab.onos.net.intent.IntentService; |
| 38 | import org.onlab.onos.net.intent.PointToPointIntent; |
| 39 | import org.onlab.packet.Ethernet; |
| 40 | import org.onlab.packet.MacAddress; |
| 41 | |
Thomas Vachuska | e4b6bb2 | 2014-11-25 17:09:43 -0800 | [diff] [blame] | 42 | import java.util.List; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 43 | import java.util.concurrent.CountDownLatch; |
| 44 | import java.util.concurrent.TimeUnit; |
| 45 | |
| 46 | import static org.onlab.onos.net.DeviceId.deviceId; |
| 47 | import static org.onlab.onos.net.PortNumber.portNumber; |
| 48 | |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 49 | /** |
| 50 | * Installs point-to-point connectivity intents. |
| 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 | |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame^] | 67 | |
| 68 | @Argument(index = 2, name = "Intents per appId", |
| 69 | description = "Number of intents per appId", |
| 70 | required = true, multiValued = false) |
| 71 | String intentsPerAppId = null; |
| 72 | |
| 73 | @Argument(index = 3, name = "apps", |
| 74 | description = "Number of appIds", |
| 75 | required = false, multiValued = false) |
| 76 | String appIds = null; |
| 77 | |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 78 | |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 79 | private IntentService service; |
| 80 | private CountDownLatch latch; |
| 81 | private long start, end; |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame^] | 82 | private int apps; |
| 83 | private int intentsPerApp; |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 84 | private int count; |
| 85 | private boolean add; |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 86 | |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame^] | 87 | |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 88 | @Override |
| 89 | protected void execute() { |
| 90 | service = get(IntentService.class); |
| 91 | |
Brian O'Connor | 6741a5f | 2014-11-19 20:40:11 -0800 | [diff] [blame] | 92 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 93 | DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString)); |
| 94 | PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString)); |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 95 | ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber); |
| 96 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 97 | DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString)); |
| 98 | PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString)); |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 99 | ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber); |
| 100 | |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame^] | 101 | apps = appIds != null ? Integer.parseInt(appIds) : 1; |
| 102 | intentsPerApp = Integer.parseInt(intentsPerAppId); |
| 103 | |
| 104 | count = intentsPerApp * apps; |
| 105 | |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 106 | |
| 107 | service.addListener(this); |
| 108 | |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame^] | 109 | ArrayListMultimap<Integer, Intent> operations = generateIntents(ingress, egress); |
| 110 | |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 111 | add = true; |
| 112 | latch = new CountDownLatch(count); |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 113 | submitIntents(operations); |
| 114 | |
| 115 | add = false; |
| 116 | latch = new CountDownLatch(count); |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 117 | submitIntents(operations); |
| 118 | |
| 119 | service.removeListener(this); |
| 120 | } |
| 121 | |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame^] | 122 | private ArrayListMultimap<Integer, Intent> generateIntents(ConnectPoint ingress, ConnectPoint egress) { |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 123 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder() |
| 124 | .matchEthType(Ethernet.TYPE_IPV4); |
| 125 | TrafficTreatment treatment = DefaultTrafficTreatment.builder().build(); |
| 126 | |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame^] | 127 | ArrayListMultimap<Integer, Intent> intents = ArrayListMultimap.create(); |
| 128 | for (int app = 1; app <= apps; app++) { |
| 129 | for (int i = 1; i <= intentsPerApp; i++) { |
| 130 | TrafficSelector s = selector |
| 131 | .matchEthSrc(MacAddress.valueOf(i)) |
| 132 | .build(); |
| 133 | intents.put(app, new PointToPointIntent(appId(), s, treatment, |
| 134 | ingress, egress)); |
Thomas Vachuska | e4b6bb2 | 2014-11-25 17:09:43 -0800 | [diff] [blame] | 135 | |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame^] | 136 | } |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 137 | } |
Thomas Vachuska | e4b6bb2 | 2014-11-25 17:09:43 -0800 | [diff] [blame] | 138 | return intents; |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame^] | 141 | private void submitIntents(ArrayListMultimap<Integer, Intent> intents) { |
| 142 | List<IntentOperations> opList = Lists.newArrayList(); |
| 143 | for (Integer app : intents.keySet()) { |
| 144 | IntentOperations.Builder builder = IntentOperations.builder(appId(app)); |
| 145 | for (Intent intent : intents.get(app)) { |
| 146 | if (add) { |
| 147 | builder.addSubmitOperation(intent); |
| 148 | } else { |
| 149 | builder.addWithdrawOperation(intent.id()); |
| 150 | } |
Thomas Vachuska | e4b6bb2 | 2014-11-25 17:09:43 -0800 | [diff] [blame] | 151 | } |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame^] | 152 | opList.add(builder.build()); |
Thomas Vachuska | e4b6bb2 | 2014-11-25 17:09:43 -0800 | [diff] [blame] | 153 | } |
Thomas Vachuska | e4b6bb2 | 2014-11-25 17:09:43 -0800 | [diff] [blame] | 154 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 155 | start = System.currentTimeMillis(); |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame^] | 156 | opList.forEach(ops -> service.execute(ops)); |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 157 | try { |
Yuta HIGUCHI | 82e5326 | 2014-11-27 10:28:51 -0800 | [diff] [blame] | 158 | if (latch.await(100 + count * 200, TimeUnit.MILLISECONDS)) { |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 159 | printResults(count); |
| 160 | } else { |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 161 | print("Failure: %d intents not installed", latch.getCount()); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 162 | } |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 163 | } catch (InterruptedException e) { |
| 164 | print(e.toString()); |
| 165 | } |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | private void printResults(int count) { |
| 169 | long delta = end - start; |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 170 | String text = add ? "install" : "withdraw"; |
| 171 | print("Time to %s %d intents: %d ms", text, count, delta); |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Yuta HIGUCHI | c2bf3d8 | 2014-11-28 18:50:41 -0800 | [diff] [blame^] | 174 | |
| 175 | /** |
| 176 | * Returns application ID for the CLI. |
| 177 | * |
| 178 | * @return command-line application identifier |
| 179 | */ |
| 180 | protected ApplicationId appId(Integer id) { |
| 181 | return get(CoreService.class).registerApplication("org.onlab.onos.cli-" + id); |
| 182 | } |
| 183 | |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 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 | |
| 212 | @Override |
| 213 | public void event(IntentEvent event) { |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 214 | Type expected = add ? Type.INSTALLED : Type.WITHDRAWN; |
| 215 | if (event.type() == expected) { |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 216 | end = event.time(); |
Brian O'Connor | ea9021e | 2014-10-10 23:12:02 -0500 | [diff] [blame] | 217 | if (latch != null) { |
| 218 | latch.countDown(); |
| 219 | } else { |
| 220 | log.warn("install event latch is null"); |
| 221 | } |
Yuta HIGUCHI | 963c656 | 2014-11-24 18:59:54 -0800 | [diff] [blame] | 222 | } else if (event.type() != Type.SUBMITTED) { |
Brian O'Connor | 510132a | 2014-11-19 14:09:20 -0800 | [diff] [blame] | 223 | log.info("Unexpected intent event: {}", event); |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | } |