blob: fca151213e3f157098533a63f7ca03f1ce9bf256 [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/**
Charles M.C. Chan6f5bdc62015-04-22 01:21:09 +080036 * Installs bulk point-to-point connectivity intents between random ingress/egress devices.
Brian O'Connor7368cd02014-11-19 22:44:46 -080037 */
38@Command(scope = "onos", name = "push-random-intents",
39 description = "Installs random intents to test throughput")
40public 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'Connor7368cd02014-11-19 22:44:46 -080067 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 Milkeyebc5d222015-03-18 15:45:36 -070071 fullMesh.add(HostToHostIntent.builder()
72 .appId(appId())
73 .one(hosts.get(i).id())
74 .two(hosts.get(j).id())
75 .build());
76
Brian O'Connor7368cd02014-11-19 22:44:46 -080077 }
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'Connor7368cd02014-11-19 22:44:46 -080084 for (Intent intent : intents) {
Brian O'Connor03406a42015-02-03 17:28:57 -080085 service.submit(intent);
Brian O'Connor7368cd02014-11-19 22:44:46 -080086 }
Brian O'Connor7368cd02014-11-19 22:44:46 -080087 print("Submitted %d host to host intents.", intents.size());
88 }
89
90 private void withdrawIntents() {
Brian O'Connor7368cd02014-11-19 22:44:46 -080091 for (Intent intent : service.getIntents()) {
92 if (appId().equals(intent.appId())) {
Brian O'Connor03406a42015-02-03 17:28:57 -080093 service.withdraw(intent);
Brian O'Connor7368cd02014-11-19 22:44:46 -080094 }
95 }
Brian O'Connor7368cd02014-11-19 22:44:46 -080096 print("Withdrew all randomly generated host to host intents.");
97 }
98
99 @Override
100 protected ApplicationId appId() {
Brian O'Connorabafb502014-12-02 22:26:20 -0800101 return get(CoreService.class).registerApplication("org.onosproject.cli-random");
Brian O'Connor7368cd02014-11-19 22:44:46 -0800102 }
103}