blob: cdfc8fef90835b7184df4205431e33df12c8ffdb [file] [log] [blame]
Brian O'Connor7368cd02014-11-19 22:44:46 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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
Ray Milkeyd84f89b2018-08-17 14:54:17 -070022import org.apache.karaf.shell.api.action.Argument;
23import org.apache.karaf.shell.api.action.Command;
Ray Milkey0068fd02018-10-11 15:45:39 -070024import org.apache.karaf.shell.api.action.Completion;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070025import org.apache.karaf.shell.api.action.lifecycle.Service;
Ray Milkey0068fd02018-10-11 15:45:39 -070026import org.apache.karaf.shell.support.completers.NullCompleter;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.cli.AbstractShellCommand;
28import org.onosproject.core.ApplicationId;
29import org.onosproject.core.CoreService;
30import org.onosproject.net.Host;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.net.host.HostService;
32import org.onosproject.net.intent.HostToHostIntent;
33import org.onosproject.net.intent.Intent;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import org.onosproject.net.intent.IntentService;
Brian O'Connor7368cd02014-11-19 22:44:46 -080035
Ray Milkeyebc5d222015-03-18 15:45:36 -070036import com.google.common.collect.Lists;
Brian O'Connor7368cd02014-11-19 22:44:46 -080037
38/**
Luca Prete6c0ed242016-10-15 13:18:13 +020039 * Installs bulk host-to-host intents between hosts of the network.
Brian O'Connor7368cd02014-11-19 22:44:46 -080040 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070041@Service
Brian O'Connor7368cd02014-11-19 22:44:46 -080042@Command(scope = "onos", name = "push-random-intents",
Luca Prete6c0ed242016-10-15 13:18:13 +020043 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'Connor7368cd02014-11-19 22:44:46 -080050public class RandomIntentCommand extends AbstractShellCommand {
51
52 @Argument(index = 0, name = "count",
Luca Prete6c0ed242016-10-15 13:18:13 +020053 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'Connor7368cd02014-11-19 22:44:46 -080056 required = true, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070057 @Completion(NullCompleter.class)
Brian O'Connor7368cd02014-11-19 22:44:46 -080058 String countString = null;
59
60 private IntentService service;
61 private HostService hostService;
62 private int count;
63
64 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070065 protected void doExecute() {
Brian O'Connor7368cd02014-11-19 22:44:46 -080066 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'Connor7368cd02014-11-19 22:44:46 -080080 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 Milkeyebc5d222015-03-18 15:45:36 -070084 fullMesh.add(HostToHostIntent.builder()
85 .appId(appId())
86 .one(hosts.get(i).id())
87 .two(hosts.get(j).id())
88 .build());
89
Brian O'Connor7368cd02014-11-19 22:44:46 -080090 }
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'Connor7368cd02014-11-19 22:44:46 -080097 for (Intent intent : intents) {
Brian O'Connor03406a42015-02-03 17:28:57 -080098 service.submit(intent);
Brian O'Connor7368cd02014-11-19 22:44:46 -080099 }
Brian O'Connor7368cd02014-11-19 22:44:46 -0800100 print("Submitted %d host to host intents.", intents.size());
101 }
102
103 private void withdrawIntents() {
Brian O'Connor7368cd02014-11-19 22:44:46 -0800104 for (Intent intent : service.getIntents()) {
105 if (appId().equals(intent.appId())) {
Brian O'Connor03406a42015-02-03 17:28:57 -0800106 service.withdraw(intent);
Brian O'Connor7368cd02014-11-19 22:44:46 -0800107 }
108 }
Brian O'Connor7368cd02014-11-19 22:44:46 -0800109 print("Withdrew all randomly generated host to host intents.");
110 }
111
112 @Override
113 protected ApplicationId appId() {
Brian O'Connorabafb502014-12-02 22:26:20 -0800114 return get(CoreService.class).registerApplication("org.onosproject.cli-random");
Brian O'Connor7368cd02014-11-19 22:44:46 -0800115 }
116}