Brian O'Connor | 7368cd0 | 2014-11-19 22:44:46 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Brian O'Connor | 7368cd0 | 2014-11-19 22:44:46 -0800 | [diff] [blame] | 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 | |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 18 | import java.util.Collection; |
| 19 | import java.util.Collections; |
| 20 | import java.util.List; |
| 21 | |
Brian O'Connor | 7368cd0 | 2014-11-19 22:44:46 -0800 | [diff] [blame] | 22 | import org.apache.karaf.shell.commands.Argument; |
| 23 | import org.apache.karaf.shell.commands.Command; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import org.onosproject.cli.AbstractShellCommand; |
| 25 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.core.CoreService; |
| 27 | import org.onosproject.net.Host; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 28 | import org.onosproject.net.host.HostService; |
| 29 | import org.onosproject.net.intent.HostToHostIntent; |
| 30 | import org.onosproject.net.intent.Intent; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 31 | import org.onosproject.net.intent.IntentService; |
Brian O'Connor | 7368cd0 | 2014-11-19 22:44:46 -0800 | [diff] [blame] | 32 | |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 33 | import com.google.common.collect.Lists; |
Brian O'Connor | 7368cd0 | 2014-11-19 22:44:46 -0800 | [diff] [blame] | 34 | |
| 35 | /** |
Luca Prete | 6c0ed24 | 2016-10-15 13:18:13 +0200 | [diff] [blame] | 36 | * Installs bulk host-to-host intents between hosts of the network. |
Brian O'Connor | 7368cd0 | 2014-11-19 22:44:46 -0800 | [diff] [blame] | 37 | */ |
| 38 | @Command(scope = "onos", name = "push-random-intents", |
Luca Prete | 6c0ed24 | 2016-10-15 13:18:13 +0200 | [diff] [blame] | 39 | description = "It installs random intents to test throughput. The " + |
| 40 | "maximum number of intents is determined by the number of " + |
| 41 | "hosts in the network. The command will push an intent for " + |
| 42 | "each unordered pair of hosts. The maximum intent number " + |
| 43 | "will be n(n-1)/2, where n represents the number of hosts " + |
| 44 | "present") |
| 45 | |
Brian O'Connor | 7368cd0 | 2014-11-19 22:44:46 -0800 | [diff] [blame] | 46 | public class RandomIntentCommand extends AbstractShellCommand { |
| 47 | |
| 48 | @Argument(index = 0, name = "count", |
Luca Prete | 6c0ed24 | 2016-10-15 13:18:13 +0200 | [diff] [blame] | 49 | description = "Max number of intents to push. The value will " + |
| 50 | "not be taken into account if it exceeds the maximum " + |
| 51 | "number of intents the command can push", |
Brian O'Connor | 7368cd0 | 2014-11-19 22:44:46 -0800 | [diff] [blame] | 52 | required = true, multiValued = false) |
| 53 | String countString = null; |
| 54 | |
| 55 | private IntentService service; |
| 56 | private HostService hostService; |
| 57 | private int count; |
| 58 | |
| 59 | @Override |
| 60 | protected void execute() { |
| 61 | service = get(IntentService.class); |
| 62 | hostService = get(HostService.class); |
| 63 | |
| 64 | count = Integer.parseInt(countString); |
| 65 | |
| 66 | if (count > 0) { |
| 67 | Collection<Intent> intents = generateIntents(); |
| 68 | submitIntents(intents); |
| 69 | } else { |
| 70 | withdrawIntents(); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | private Collection<Intent> generateIntents() { |
Brian O'Connor | 7368cd0 | 2014-11-19 22:44:46 -0800 | [diff] [blame] | 75 | List<Host> hosts = Lists.newArrayList(hostService.getHosts()); |
| 76 | List<Intent> fullMesh = Lists.newArrayList(); |
| 77 | for (int i = 0; i < hosts.size(); i++) { |
| 78 | for (int j = i + 1; j < hosts.size(); j++) { |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 79 | fullMesh.add(HostToHostIntent.builder() |
| 80 | .appId(appId()) |
| 81 | .one(hosts.get(i).id()) |
| 82 | .two(hosts.get(j).id()) |
| 83 | .build()); |
| 84 | |
Brian O'Connor | 7368cd0 | 2014-11-19 22:44:46 -0800 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | Collections.shuffle(fullMesh); |
| 88 | return fullMesh.subList(0, Math.min(count, fullMesh.size())); |
| 89 | } |
| 90 | |
| 91 | private void submitIntents(Collection<Intent> intents) { |
Brian O'Connor | 7368cd0 | 2014-11-19 22:44:46 -0800 | [diff] [blame] | 92 | for (Intent intent : intents) { |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 93 | service.submit(intent); |
Brian O'Connor | 7368cd0 | 2014-11-19 22:44:46 -0800 | [diff] [blame] | 94 | } |
Brian O'Connor | 7368cd0 | 2014-11-19 22:44:46 -0800 | [diff] [blame] | 95 | print("Submitted %d host to host intents.", intents.size()); |
| 96 | } |
| 97 | |
| 98 | private void withdrawIntents() { |
Brian O'Connor | 7368cd0 | 2014-11-19 22:44:46 -0800 | [diff] [blame] | 99 | for (Intent intent : service.getIntents()) { |
| 100 | if (appId().equals(intent.appId())) { |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 101 | service.withdraw(intent); |
Brian O'Connor | 7368cd0 | 2014-11-19 22:44:46 -0800 | [diff] [blame] | 102 | } |
| 103 | } |
Brian O'Connor | 7368cd0 | 2014-11-19 22:44:46 -0800 | [diff] [blame] | 104 | print("Withdrew all randomly generated host to host intents."); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | protected ApplicationId appId() { |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 109 | return get(CoreService.class).registerApplication("org.onosproject.cli-random"); |
Brian O'Connor | 7368cd0 | 2014-11-19 22:44:46 -0800 | [diff] [blame] | 110 | } |
| 111 | } |