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