Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 19 | package org.onlab.onos.cli.net; |
| 20 | |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.commands.Argument; |
| 22 | import org.apache.karaf.shell.commands.Command; |
| 23 | import org.onlab.onos.cli.AbstractShellCommand; |
| 24 | import org.onlab.onos.net.ConnectPoint; |
| 25 | import org.onlab.onos.net.DeviceId; |
| 26 | import org.onlab.onos.net.PortNumber; |
| 27 | import org.onlab.onos.net.flow.DefaultTrafficSelector; |
| 28 | import org.onlab.onos.net.flow.DefaultTrafficTreatment; |
| 29 | import org.onlab.onos.net.flow.TrafficSelector; |
| 30 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 31 | import org.onlab.onos.net.intent.Intent; |
| 32 | import org.onlab.onos.net.intent.IntentEvent; |
| 33 | import org.onlab.onos.net.intent.IntentEvent.Type; |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 34 | import org.onlab.onos.net.intent.IntentListener; |
| 35 | import org.onlab.onos.net.intent.IntentService; |
| 36 | import org.onlab.onos.net.intent.PointToPointIntent; |
| 37 | import org.onlab.packet.Ethernet; |
| 38 | import org.onlab.packet.MacAddress; |
| 39 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 40 | import java.util.concurrent.CountDownLatch; |
| 41 | import java.util.concurrent.TimeUnit; |
| 42 | |
| 43 | import static org.onlab.onos.net.DeviceId.deviceId; |
| 44 | import static org.onlab.onos.net.PortNumber.portNumber; |
| 45 | |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 46 | /** |
| 47 | * Installs point-to-point connectivity intents. |
| 48 | */ |
| 49 | @Command(scope = "onos", name = "push-test-intents", |
| 50 | description = "Installs random intents to test throughput") |
| 51 | public class IntentPushTestCommand extends AbstractShellCommand |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 52 | implements IntentListener { |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 53 | |
| 54 | @Argument(index = 0, name = "ingressDevice", |
| 55 | description = "Ingress Device/Port Description", |
| 56 | required = true, multiValued = false) |
| 57 | String ingressDeviceString = null; |
| 58 | |
| 59 | @Argument(index = 1, name = "egressDevice", |
| 60 | description = "Egress Device/Port Description", |
| 61 | required = true, multiValued = false) |
| 62 | String egressDeviceString = null; |
| 63 | |
| 64 | @Argument(index = 2, name = "count", |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 65 | description = "Number of intents to push", |
| 66 | required = true, multiValued = false) |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 67 | String countString = null; |
| 68 | |
| 69 | |
| 70 | private static long id = 0x7870001; |
| 71 | |
| 72 | private IntentService service; |
| 73 | private CountDownLatch latch; |
| 74 | private long start, end; |
| 75 | |
| 76 | @Override |
| 77 | protected void execute() { |
| 78 | service = get(IntentService.class); |
| 79 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 80 | DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString)); |
| 81 | PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString)); |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 82 | ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber); |
| 83 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 84 | DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString)); |
| 85 | PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString)); |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 86 | ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber); |
| 87 | |
| 88 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder() |
| 89 | .matchEthType(Ethernet.TYPE_IPV4); |
| 90 | TrafficTreatment treatment = DefaultTrafficTreatment.builder().build(); |
| 91 | |
| 92 | int count = Integer.parseInt(countString); |
| 93 | |
| 94 | service.addListener(this); |
Brian O'Connor | ea9021e | 2014-10-10 23:12:02 -0500 | [diff] [blame] | 95 | latch = new CountDownLatch(count); |
| 96 | |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 97 | start = System.currentTimeMillis(); |
| 98 | for (int i = 0; i < count; i++) { |
| 99 | TrafficSelector s = selector |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 100 | .matchEthSrc(MacAddress.valueOf(i)) |
| 101 | .build(); |
| 102 | Intent intent = new PointToPointIntent(appId(), s, treatment, |
| 103 | ingress, egress); |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 104 | service.submit(intent); |
| 105 | } |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 106 | try { |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 107 | if (latch.await(10, TimeUnit.SECONDS)) { |
| 108 | printResults(count); |
| 109 | } else { |
| 110 | print("I FAIL MISERABLY -> %d", latch.getCount()); |
| 111 | } |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 112 | } catch (InterruptedException e) { |
| 113 | print(e.toString()); |
| 114 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 115 | |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 116 | service.removeListener(this); |
| 117 | } |
| 118 | |
| 119 | private void printResults(int count) { |
| 120 | long delta = end - start; |
| 121 | print("Time to install %d intents: %d ms", count, delta); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Extracts the port number portion of the ConnectPoint. |
| 126 | * |
| 127 | * @param deviceString string representing the device/port |
| 128 | * @return port number as a string, empty string if the port is not found |
| 129 | */ |
| 130 | private String getPortNumber(String deviceString) { |
| 131 | int slash = deviceString.indexOf('/'); |
| 132 | if (slash <= 0) { |
| 133 | return ""; |
| 134 | } |
| 135 | return deviceString.substring(slash + 1, deviceString.length()); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Extracts the device ID portion of the ConnectPoint. |
| 140 | * |
| 141 | * @param deviceString string representing the device/port |
| 142 | * @return device ID string |
| 143 | */ |
| 144 | private String getDeviceId(String deviceString) { |
| 145 | int slash = deviceString.indexOf('/'); |
| 146 | if (slash <= 0) { |
| 147 | return ""; |
| 148 | } |
| 149 | return deviceString.substring(0, slash); |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public void event(IntentEvent event) { |
| 154 | if (event.type() == Type.INSTALLED) { |
| 155 | end = event.time(); |
Brian O'Connor | ea9021e | 2014-10-10 23:12:02 -0500 | [diff] [blame] | 156 | if (latch != null) { |
| 157 | latch.countDown(); |
| 158 | } else { |
| 159 | log.warn("install event latch is null"); |
| 160 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 161 | } else { |
| 162 | log.info("I FAIL -> {}", event); |
Brian O'Connor | 1aa99eb | 2014-10-10 16:18:58 -0700 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | } |