blob: f71e6ac2374af7f2d76eb54dc0ac46953d204105 [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;
32import org.onosproject.net.intent.IntentOperations;
33import org.onosproject.net.intent.IntentService;
Brian O'Connor7368cd02014-11-19 22:44:46 -080034
35import java.util.Collection;
36import java.util.Collections;
37import java.util.List;
38
39/**
40 * Installs point-to-point connectivity intents.
41 */
42@Command(scope = "onos", name = "push-random-intents",
43 description = "Installs random intents to test throughput")
44public class RandomIntentCommand extends AbstractShellCommand {
45
46 @Argument(index = 0, name = "count",
47 description = "Number of intents to push",
48 required = true, multiValued = false)
49 String countString = null;
50
51 private IntentService service;
52 private HostService hostService;
53 private int count;
54
55 @Override
56 protected void execute() {
57 service = get(IntentService.class);
58 hostService = get(HostService.class);
59
60 count = Integer.parseInt(countString);
61
62 if (count > 0) {
63 Collection<Intent> intents = generateIntents();
64 submitIntents(intents);
65 } else {
66 withdrawIntents();
67 }
68 }
69
70 private Collection<Intent> generateIntents() {
71 TrafficSelector selector = DefaultTrafficSelector.builder().build();
72 TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
73
74 List<Host> hosts = Lists.newArrayList(hostService.getHosts());
75 List<Intent> fullMesh = Lists.newArrayList();
76 for (int i = 0; i < hosts.size(); i++) {
77 for (int j = i + 1; j < hosts.size(); j++) {
78 fullMesh.add(new HostToHostIntent(appId(),
79 hosts.get(i).id(),
80 hosts.get(j).id(),
81 selector, treatment));
82 }
83 }
84 Collections.shuffle(fullMesh);
85 return fullMesh.subList(0, Math.min(count, fullMesh.size()));
86 }
87
88 private void submitIntents(Collection<Intent> intents) {
Brian O'Connor72a034c2014-11-26 18:24:23 -080089 IntentOperations.Builder builder = IntentOperations.builder(appId());
Brian O'Connor7368cd02014-11-19 22:44:46 -080090 for (Intent intent : intents) {
91 builder.addSubmitOperation(intent);
92 }
93 service.execute(builder.build());
94 print("Submitted %d host to host intents.", intents.size());
95 }
96
97 private void withdrawIntents() {
Brian O'Connor72a034c2014-11-26 18:24:23 -080098 IntentOperations.Builder builder = IntentOperations.builder(appId());
Brian O'Connor7368cd02014-11-19 22:44:46 -080099 for (Intent intent : service.getIntents()) {
100 if (appId().equals(intent.appId())) {
101 builder.addWithdrawOperation(intent.id());
102 }
103 }
104 service.execute(builder.build());
105 print("Withdrew all randomly generated host to host intents.");
106 }
107
108 @Override
109 protected ApplicationId appId() {
Brian O'Connorabafb502014-12-02 22:26:20 -0800110 return get(CoreService.class).registerApplication("org.onosproject.cli-random");
Brian O'Connor7368cd02014-11-19 22:44:46 -0800111 }
112}