blob: 5eb592a68f2305f720c7996d398391e12850ee65 [file] [log] [blame]
Brian O'Connor7368cd02014-11-19 22:44:46 -08001/*
2 * Copyright 2014 Open Networking Laboratory
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.cli.net;
Brian O'Connor7368cd02014-11-19 22:44:46 -080017
18import com.google.common.collect.Lists;
19import org.apache.karaf.shell.commands.Argument;
20import org.apache.karaf.shell.commands.Command;
Brian O'Connorabafb502014-12-02 22:26:20 -080021import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.core.ApplicationId;
23import org.onosproject.core.CoreService;
24import org.onosproject.net.Host;
25import org.onosproject.net.flow.DefaultTrafficSelector;
26import org.onosproject.net.flow.DefaultTrafficTreatment;
27import org.onosproject.net.flow.TrafficSelector;
28import org.onosproject.net.flow.TrafficTreatment;
29import 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
34import java.util.Collection;
35import java.util.Collections;
36import java.util.List;
37
38/**
39 * Installs point-to-point connectivity intents.
40 */
41@Command(scope = "onos", name = "push-random-intents",
42 description = "Installs random intents to test throughput")
43public class RandomIntentCommand extends AbstractShellCommand {
44
45 @Argument(index = 0, name = "count",
46 description = "Number of intents to push",
47 required = true, multiValued = false)
48 String countString = null;
49
50 private IntentService service;
51 private HostService hostService;
52 private int count;
53
54 @Override
55 protected void execute() {
56 service = get(IntentService.class);
57 hostService = get(HostService.class);
58
59 count = Integer.parseInt(countString);
60
61 if (count > 0) {
62 Collection<Intent> intents = generateIntents();
63 submitIntents(intents);
64 } else {
65 withdrawIntents();
66 }
67 }
68
69 private Collection<Intent> generateIntents() {
Brian O'Connor6b528132015-03-10 16:39:52 -070070 TrafficSelector selector = DefaultTrafficSelector.emptySelector();
71 TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
Brian O'Connor7368cd02014-11-19 22:44:46 -080072
73 List<Host> hosts = Lists.newArrayList(hostService.getHosts());
74 List<Intent> fullMesh = Lists.newArrayList();
75 for (int i = 0; i < hosts.size(); i++) {
76 for (int j = i + 1; j < hosts.size(); j++) {
77 fullMesh.add(new HostToHostIntent(appId(),
78 hosts.get(i).id(),
79 hosts.get(j).id(),
80 selector, treatment));
81 }
82 }
83 Collections.shuffle(fullMesh);
84 return fullMesh.subList(0, Math.min(count, fullMesh.size()));
85 }
86
87 private void submitIntents(Collection<Intent> intents) {
Brian O'Connor7368cd02014-11-19 22:44:46 -080088 for (Intent intent : intents) {
Brian O'Connor03406a42015-02-03 17:28:57 -080089 service.submit(intent);
Brian O'Connor7368cd02014-11-19 22:44:46 -080090 }
Brian O'Connor7368cd02014-11-19 22:44:46 -080091 print("Submitted %d host to host intents.", intents.size());
92 }
93
94 private void withdrawIntents() {
Brian O'Connor7368cd02014-11-19 22:44:46 -080095 for (Intent intent : service.getIntents()) {
96 if (appId().equals(intent.appId())) {
Brian O'Connor03406a42015-02-03 17:28:57 -080097 service.withdraw(intent);
Brian O'Connor7368cd02014-11-19 22:44:46 -080098 }
99 }
Brian O'Connor7368cd02014-11-19 22:44:46 -0800100 print("Withdrew all randomly generated host to host intents.");
101 }
102
103 @Override
104 protected ApplicationId appId() {
Brian O'Connorabafb502014-12-02 22:26:20 -0800105 return get(CoreService.class).registerApplication("org.onosproject.cli-random");
Brian O'Connor7368cd02014-11-19 22:44:46 -0800106 }
107}