blob: d77b1da884ceb35ffe390102e824e79960bb3184 [file] [log] [blame]
Brian O'Connor7368cd02014-11-19 22:44:46 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Brian O'Connor7368cd02014-11-19 22:44:46 -08003 *
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.cli.net;
Brian O'Connor7368cd02014-11-19 22:44:46 -080017
Ray Milkeyebc5d222015-03-18 15:45:36 -070018import java.util.Collection;
19import java.util.Collections;
20import java.util.List;
21
Brian O'Connor7368cd02014-11-19 22:44:46 -080022import org.apache.karaf.shell.commands.Argument;
23import org.apache.karaf.shell.commands.Command;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.cli.AbstractShellCommand;
25import org.onosproject.core.ApplicationId;
26import org.onosproject.core.CoreService;
27import org.onosproject.net.Host;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.net.host.HostService;
29import org.onosproject.net.intent.HostToHostIntent;
30import org.onosproject.net.intent.Intent;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.net.intent.IntentService;
Brian O'Connor7368cd02014-11-19 22:44:46 -080032
Ray Milkeyebc5d222015-03-18 15:45:36 -070033import com.google.common.collect.Lists;
Brian O'Connor7368cd02014-11-19 22:44:46 -080034
35/**
Luca Prete6c0ed242016-10-15 13:18:13 +020036 * Installs bulk host-to-host intents between hosts of the network.
Brian O'Connor7368cd02014-11-19 22:44:46 -080037 */
38@Command(scope = "onos", name = "push-random-intents",
Luca Prete6c0ed242016-10-15 13:18:13 +020039 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'Connor7368cd02014-11-19 22:44:46 -080046public class RandomIntentCommand extends AbstractShellCommand {
47
48 @Argument(index = 0, name = "count",
Luca Prete6c0ed242016-10-15 13:18:13 +020049 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'Connor7368cd02014-11-19 22:44:46 -080052 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'Connor7368cd02014-11-19 22:44:46 -080075 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 Milkeyebc5d222015-03-18 15:45:36 -070079 fullMesh.add(HostToHostIntent.builder()
80 .appId(appId())
81 .one(hosts.get(i).id())
82 .two(hosts.get(j).id())
83 .build());
84
Brian O'Connor7368cd02014-11-19 22:44:46 -080085 }
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'Connor7368cd02014-11-19 22:44:46 -080092 for (Intent intent : intents) {
Brian O'Connor03406a42015-02-03 17:28:57 -080093 service.submit(intent);
Brian O'Connor7368cd02014-11-19 22:44:46 -080094 }
Brian O'Connor7368cd02014-11-19 22:44:46 -080095 print("Submitted %d host to host intents.", intents.size());
96 }
97
98 private void withdrawIntents() {
Brian O'Connor7368cd02014-11-19 22:44:46 -080099 for (Intent intent : service.getIntents()) {
100 if (appId().equals(intent.appId())) {
Brian O'Connor03406a42015-02-03 17:28:57 -0800101 service.withdraw(intent);
Brian O'Connor7368cd02014-11-19 22:44:46 -0800102 }
103 }
Brian O'Connor7368cd02014-11-19 22:44:46 -0800104 print("Withdrew all randomly generated host to host intents.");
105 }
106
107 @Override
108 protected ApplicationId appId() {
Brian O'Connorabafb502014-12-02 22:26:20 -0800109 return get(CoreService.class).registerApplication("org.onosproject.cli-random");
Brian O'Connor7368cd02014-11-19 22:44:46 -0800110 }
111}