blob: 202326b38350eedf1c7526e1b515368d6ee2b5f4 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska7d693f52014-10-21 19:17:57 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuska7d693f52014-10-21 19:17:57 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska7d693f52014-10-21 19:17:57 -070015 */
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070016package org.onlab.onos.cli.net;
17
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070018import org.apache.karaf.shell.commands.Argument;
19import org.apache.karaf.shell.commands.Command;
20import org.onlab.onos.cli.AbstractShellCommand;
21import org.onlab.onos.net.ConnectPoint;
22import org.onlab.onos.net.DeviceId;
23import org.onlab.onos.net.PortNumber;
24import org.onlab.onos.net.flow.DefaultTrafficSelector;
25import org.onlab.onos.net.flow.DefaultTrafficTreatment;
26import org.onlab.onos.net.flow.TrafficSelector;
27import org.onlab.onos.net.flow.TrafficTreatment;
28import org.onlab.onos.net.intent.Intent;
29import org.onlab.onos.net.intent.IntentEvent;
30import org.onlab.onos.net.intent.IntentEvent.Type;
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070031import org.onlab.onos.net.intent.IntentListener;
32import org.onlab.onos.net.intent.IntentService;
33import org.onlab.onos.net.intent.PointToPointIntent;
34import org.onlab.packet.Ethernet;
35import org.onlab.packet.MacAddress;
36
Thomas Vachuskab97cf282014-10-20 23:31:12 -070037import java.util.concurrent.CountDownLatch;
38import java.util.concurrent.TimeUnit;
39
40import static org.onlab.onos.net.DeviceId.deviceId;
41import static org.onlab.onos.net.PortNumber.portNumber;
42
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070043/**
44 * Installs point-to-point connectivity intents.
45 */
46@Command(scope = "onos", name = "push-test-intents",
47 description = "Installs random intents to test throughput")
48public class IntentPushTestCommand extends AbstractShellCommand
Thomas Vachuskab97cf282014-10-20 23:31:12 -070049 implements IntentListener {
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070050
51 @Argument(index = 0, name = "ingressDevice",
52 description = "Ingress Device/Port Description",
53 required = true, multiValued = false)
54 String ingressDeviceString = null;
55
56 @Argument(index = 1, name = "egressDevice",
57 description = "Egress Device/Port Description",
58 required = true, multiValued = false)
59 String egressDeviceString = null;
60
61 @Argument(index = 2, name = "count",
Thomas Vachuskab97cf282014-10-20 23:31:12 -070062 description = "Number of intents to push",
63 required = true, multiValued = false)
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070064 String countString = null;
65
66
67 private static long id = 0x7870001;
68
69 private IntentService service;
70 private CountDownLatch latch;
71 private long start, end;
72
73 @Override
74 protected void execute() {
75 service = get(IntentService.class);
76
Thomas Vachuskab97cf282014-10-20 23:31:12 -070077 DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString));
78 PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070079 ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber);
80
Thomas Vachuskab97cf282014-10-20 23:31:12 -070081 DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString));
82 PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070083 ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber);
84
85 TrafficSelector.Builder selector = DefaultTrafficSelector.builder()
86 .matchEthType(Ethernet.TYPE_IPV4);
87 TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
88
89 int count = Integer.parseInt(countString);
90
91 service.addListener(this);
Brian O'Connorea9021e2014-10-10 23:12:02 -050092 latch = new CountDownLatch(count);
93
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070094 start = System.currentTimeMillis();
95 for (int i = 0; i < count; i++) {
96 TrafficSelector s = selector
Thomas Vachuskab97cf282014-10-20 23:31:12 -070097 .matchEthSrc(MacAddress.valueOf(i))
98 .build();
99 Intent intent = new PointToPointIntent(appId(), s, treatment,
100 ingress, egress);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700101 service.submit(intent);
102 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700103 try {
alshabib7911a052014-10-16 17:49:37 -0700104 if (latch.await(10, TimeUnit.SECONDS)) {
105 printResults(count);
106 } else {
107 print("I FAIL MISERABLY -> %d", latch.getCount());
108 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700109 } catch (InterruptedException e) {
110 print(e.toString());
111 }
alshabib7911a052014-10-16 17:49:37 -0700112
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700113 service.removeListener(this);
114 }
115
116 private void printResults(int count) {
117 long delta = end - start;
118 print("Time to install %d intents: %d ms", count, delta);
119 }
120
121 /**
122 * Extracts the port number portion of the ConnectPoint.
123 *
124 * @param deviceString string representing the device/port
125 * @return port number as a string, empty string if the port is not found
126 */
127 private String getPortNumber(String deviceString) {
128 int slash = deviceString.indexOf('/');
129 if (slash <= 0) {
130 return "";
131 }
132 return deviceString.substring(slash + 1, deviceString.length());
133 }
134
135 /**
136 * Extracts the device ID portion of the ConnectPoint.
137 *
138 * @param deviceString string representing the device/port
139 * @return device ID string
140 */
141 private String getDeviceId(String deviceString) {
142 int slash = deviceString.indexOf('/');
143 if (slash <= 0) {
144 return "";
145 }
146 return deviceString.substring(0, slash);
147 }
148
149 @Override
150 public void event(IntentEvent event) {
151 if (event.type() == Type.INSTALLED) {
152 end = event.time();
Brian O'Connorea9021e2014-10-10 23:12:02 -0500153 if (latch != null) {
154 latch.countDown();
155 } else {
156 log.warn("install event latch is null");
157 }
alshabib7911a052014-10-16 17:49:37 -0700158 } else {
159 log.info("I FAIL -> {}", event);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700160 }
161 }
162}