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