blob: 5d3a2770e1d4fced22e626170b409659731ca8d0 [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;
24import org.apache.karaf.shell.api.action.lifecycle.Service;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.cli.AbstractShellCommand;
26import org.onosproject.core.ApplicationId;
27import org.onosproject.core.CoreService;
28import org.onosproject.net.Host;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.net.host.HostService;
30import org.onosproject.net.intent.HostToHostIntent;
31import org.onosproject.net.intent.Intent;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.intent.IntentService;
Brian O'Connor7368cd02014-11-19 22:44:46 -080033
Ray Milkeyebc5d222015-03-18 15:45:36 -070034import com.google.common.collect.Lists;
Brian O'Connor7368cd02014-11-19 22:44:46 -080035
36/**
Luca Prete6c0ed242016-10-15 13:18:13 +020037 * Installs bulk host-to-host intents between hosts of the network.
Brian O'Connor7368cd02014-11-19 22:44:46 -080038 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070039@Service
Brian O'Connor7368cd02014-11-19 22:44:46 -080040@Command(scope = "onos", name = "push-random-intents",
Luca Prete6c0ed242016-10-15 13:18:13 +020041 description = "It installs random intents to test throughput. The " +
42 "maximum number of intents is determined by the number of " +
43 "hosts in the network. The command will push an intent for " +
44 "each unordered pair of hosts. The maximum intent number " +
45 "will be n(n-1)/2, where n represents the number of hosts " +
46 "present")
47
Brian O'Connor7368cd02014-11-19 22:44:46 -080048public class RandomIntentCommand extends AbstractShellCommand {
49
50 @Argument(index = 0, name = "count",
Luca Prete6c0ed242016-10-15 13:18:13 +020051 description = "Max number of intents to push. The value will " +
52 "not be taken into account if it exceeds the maximum " +
53 "number of intents the command can push",
Brian O'Connor7368cd02014-11-19 22:44:46 -080054 required = true, multiValued = false)
55 String countString = null;
56
57 private IntentService service;
58 private HostService hostService;
59 private int count;
60
61 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070062 protected void doExecute() {
Brian O'Connor7368cd02014-11-19 22:44:46 -080063 service = get(IntentService.class);
64 hostService = get(HostService.class);
65
66 count = Integer.parseInt(countString);
67
68 if (count > 0) {
69 Collection<Intent> intents = generateIntents();
70 submitIntents(intents);
71 } else {
72 withdrawIntents();
73 }
74 }
75
76 private Collection<Intent> generateIntents() {
Brian O'Connor7368cd02014-11-19 22:44:46 -080077 List<Host> hosts = Lists.newArrayList(hostService.getHosts());
78 List<Intent> fullMesh = Lists.newArrayList();
79 for (int i = 0; i < hosts.size(); i++) {
80 for (int j = i + 1; j < hosts.size(); j++) {
Ray Milkeyebc5d222015-03-18 15:45:36 -070081 fullMesh.add(HostToHostIntent.builder()
82 .appId(appId())
83 .one(hosts.get(i).id())
84 .two(hosts.get(j).id())
85 .build());
86
Brian O'Connor7368cd02014-11-19 22:44:46 -080087 }
88 }
89 Collections.shuffle(fullMesh);
90 return fullMesh.subList(0, Math.min(count, fullMesh.size()));
91 }
92
93 private void submitIntents(Collection<Intent> intents) {
Brian O'Connor7368cd02014-11-19 22:44:46 -080094 for (Intent intent : intents) {
Brian O'Connor03406a42015-02-03 17:28:57 -080095 service.submit(intent);
Brian O'Connor7368cd02014-11-19 22:44:46 -080096 }
Brian O'Connor7368cd02014-11-19 22:44:46 -080097 print("Submitted %d host to host intents.", intents.size());
98 }
99
100 private void withdrawIntents() {
Brian O'Connor7368cd02014-11-19 22:44:46 -0800101 for (Intent intent : service.getIntents()) {
102 if (appId().equals(intent.appId())) {
Brian O'Connor03406a42015-02-03 17:28:57 -0800103 service.withdraw(intent);
Brian O'Connor7368cd02014-11-19 22:44:46 -0800104 }
105 }
Brian O'Connor7368cd02014-11-19 22:44:46 -0800106 print("Withdrew all randomly generated host to host intents.");
107 }
108
109 @Override
110 protected ApplicationId appId() {
Brian O'Connorabafb502014-12-02 22:26:20 -0800111 return get(CoreService.class).registerApplication("org.onosproject.cli-random");
Brian O'Connor7368cd02014-11-19 22:44:46 -0800112 }
113}