Brian O'Connor | 7368cd0 | 2014-11-19 22:44:46 -0800 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 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 | /** |
Charles M.C. Chan | 6f5bdc6 | 2015-04-22 01:21:09 +0800 | [diff] [blame] | 36 | * Installs bulk point-to-point connectivity intents between random ingress/egress devices. |
Brian O'Connor | 7368cd0 | 2014-11-19 22:44:46 -0800 | [diff] [blame] | 37 | */ |
| 38 | @Command(scope = "onos", name = "push-random-intents", |
| 39 | description = "Installs random intents to test throughput") |
| 40 | public class RandomIntentCommand extends AbstractShellCommand { |
| 41 | |
| 42 | @Argument(index = 0, name = "count", |
| 43 | description = "Number of intents to push", |
| 44 | required = true, multiValued = false) |
| 45 | String countString = null; |
| 46 | |
| 47 | private IntentService service; |
| 48 | private HostService hostService; |
| 49 | private int count; |
| 50 | |
| 51 | @Override |
| 52 | protected void execute() { |
| 53 | service = get(IntentService.class); |
| 54 | hostService = get(HostService.class); |
| 55 | |
| 56 | count = Integer.parseInt(countString); |
| 57 | |
| 58 | if (count > 0) { |
| 59 | Collection<Intent> intents = generateIntents(); |
| 60 | submitIntents(intents); |
| 61 | } else { |
| 62 | withdrawIntents(); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | private Collection<Intent> generateIntents() { |
Brian O'Connor | 7368cd0 | 2014-11-19 22:44:46 -0800 | [diff] [blame] | 67 | List<Host> hosts = Lists.newArrayList(hostService.getHosts()); |
| 68 | List<Intent> fullMesh = Lists.newArrayList(); |
| 69 | for (int i = 0; i < hosts.size(); i++) { |
| 70 | for (int j = i + 1; j < hosts.size(); j++) { |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 71 | fullMesh.add(HostToHostIntent.builder() |
| 72 | .appId(appId()) |
| 73 | .one(hosts.get(i).id()) |
| 74 | .two(hosts.get(j).id()) |
| 75 | .build()); |
| 76 | |
Brian O'Connor | 7368cd0 | 2014-11-19 22:44:46 -0800 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | Collections.shuffle(fullMesh); |
| 80 | return fullMesh.subList(0, Math.min(count, fullMesh.size())); |
| 81 | } |
| 82 | |
| 83 | private void submitIntents(Collection<Intent> intents) { |
Brian O'Connor | 7368cd0 | 2014-11-19 22:44:46 -0800 | [diff] [blame] | 84 | for (Intent intent : intents) { |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 85 | service.submit(intent); |
Brian O'Connor | 7368cd0 | 2014-11-19 22:44:46 -0800 | [diff] [blame] | 86 | } |
Brian O'Connor | 7368cd0 | 2014-11-19 22:44:46 -0800 | [diff] [blame] | 87 | print("Submitted %d host to host intents.", intents.size()); |
| 88 | } |
| 89 | |
| 90 | private void withdrawIntents() { |
Brian O'Connor | 7368cd0 | 2014-11-19 22:44:46 -0800 | [diff] [blame] | 91 | for (Intent intent : service.getIntents()) { |
| 92 | if (appId().equals(intent.appId())) { |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 93 | service.withdraw(intent); |
Brian O'Connor | 7368cd0 | 2014-11-19 22:44:46 -0800 | [diff] [blame] | 94 | } |
| 95 | } |
Brian O'Connor | 7368cd0 | 2014-11-19 22:44:46 -0800 | [diff] [blame] | 96 | print("Withdrew all randomly generated host to host intents."); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | protected ApplicationId appId() { |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 101 | return get(CoreService.class).registerApplication("org.onosproject.cli-random"); |
Brian O'Connor | 7368cd0 | 2014-11-19 22:44:46 -0800 | [diff] [blame] | 102 | } |
| 103 | } |