Brian O'Connor | 7368cd0 | 2014-11-19 22:44:46 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
| 16 | package org.onlab.onos.cli.net; |
| 17 | |
| 18 | import com.google.common.collect.Lists; |
| 19 | import org.apache.karaf.shell.commands.Argument; |
| 20 | import org.apache.karaf.shell.commands.Command; |
| 21 | import org.onlab.onos.cli.AbstractShellCommand; |
| 22 | import org.onlab.onos.core.ApplicationId; |
| 23 | import org.onlab.onos.core.CoreService; |
| 24 | import org.onlab.onos.net.Host; |
| 25 | import org.onlab.onos.net.flow.DefaultTrafficSelector; |
| 26 | import org.onlab.onos.net.flow.DefaultTrafficTreatment; |
| 27 | import org.onlab.onos.net.flow.TrafficSelector; |
| 28 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 29 | import org.onlab.onos.net.host.HostService; |
| 30 | import org.onlab.onos.net.intent.HostToHostIntent; |
| 31 | import org.onlab.onos.net.intent.Intent; |
| 32 | import org.onlab.onos.net.intent.IntentOperations; |
| 33 | import org.onlab.onos.net.intent.IntentService; |
| 34 | |
| 35 | import java.util.Collection; |
| 36 | import java.util.Collections; |
| 37 | import java.util.List; |
| 38 | |
| 39 | /** |
| 40 | * Installs point-to-point connectivity intents. |
| 41 | */ |
| 42 | @Command(scope = "onos", name = "push-random-intents", |
| 43 | description = "Installs random intents to test throughput") |
| 44 | public class RandomIntentCommand extends AbstractShellCommand { |
| 45 | |
| 46 | @Argument(index = 0, name = "count", |
| 47 | description = "Number of intents to push", |
| 48 | required = true, multiValued = false) |
| 49 | String countString = null; |
| 50 | |
| 51 | private IntentService service; |
| 52 | private HostService hostService; |
| 53 | private int count; |
| 54 | |
| 55 | @Override |
| 56 | protected void execute() { |
| 57 | service = get(IntentService.class); |
| 58 | hostService = get(HostService.class); |
| 59 | |
| 60 | count = Integer.parseInt(countString); |
| 61 | |
| 62 | if (count > 0) { |
| 63 | Collection<Intent> intents = generateIntents(); |
| 64 | submitIntents(intents); |
| 65 | } else { |
| 66 | withdrawIntents(); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | private Collection<Intent> generateIntents() { |
| 71 | TrafficSelector selector = DefaultTrafficSelector.builder().build(); |
| 72 | TrafficTreatment treatment = DefaultTrafficTreatment.builder().build(); |
| 73 | |
| 74 | List<Host> hosts = Lists.newArrayList(hostService.getHosts()); |
| 75 | List<Intent> fullMesh = Lists.newArrayList(); |
| 76 | for (int i = 0; i < hosts.size(); i++) { |
| 77 | for (int j = i + 1; j < hosts.size(); j++) { |
| 78 | fullMesh.add(new HostToHostIntent(appId(), |
| 79 | hosts.get(i).id(), |
| 80 | hosts.get(j).id(), |
| 81 | selector, treatment)); |
| 82 | } |
| 83 | } |
| 84 | Collections.shuffle(fullMesh); |
| 85 | return fullMesh.subList(0, Math.min(count, fullMesh.size())); |
| 86 | } |
| 87 | |
| 88 | private void submitIntents(Collection<Intent> intents) { |
| 89 | IntentOperations.Builder builder = IntentOperations.builder(); |
| 90 | for (Intent intent : intents) { |
| 91 | builder.addSubmitOperation(intent); |
| 92 | } |
| 93 | service.execute(builder.build()); |
| 94 | print("Submitted %d host to host intents.", intents.size()); |
| 95 | } |
| 96 | |
| 97 | private void withdrawIntents() { |
| 98 | IntentOperations.Builder builder = IntentOperations.builder(); |
| 99 | for (Intent intent : service.getIntents()) { |
| 100 | if (appId().equals(intent.appId())) { |
| 101 | builder.addWithdrawOperation(intent.id()); |
| 102 | } |
| 103 | } |
| 104 | service.execute(builder.build()); |
| 105 | print("Withdrew all randomly generated host to host intents."); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | protected ApplicationId appId() { |
| 110 | return get(CoreService.class).registerApplication("org.onlab.onos.cli-random"); |
| 111 | } |
| 112 | } |