blob: 6ee67dac271ad2c3c49b5e2ce3a745e46dd34690 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070019package org.onlab.onos.cli.net;
20
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070021import org.apache.karaf.shell.commands.Argument;
22import org.apache.karaf.shell.commands.Command;
23import org.onlab.onos.cli.AbstractShellCommand;
24import org.onlab.onos.net.ConnectPoint;
25import org.onlab.onos.net.DeviceId;
26import org.onlab.onos.net.PortNumber;
27import org.onlab.onos.net.flow.DefaultTrafficSelector;
28import org.onlab.onos.net.flow.DefaultTrafficTreatment;
29import org.onlab.onos.net.flow.TrafficSelector;
30import org.onlab.onos.net.flow.TrafficTreatment;
31import org.onlab.onos.net.intent.Intent;
32import org.onlab.onos.net.intent.IntentEvent;
33import org.onlab.onos.net.intent.IntentEvent.Type;
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070034import org.onlab.onos.net.intent.IntentListener;
35import org.onlab.onos.net.intent.IntentService;
36import org.onlab.onos.net.intent.PointToPointIntent;
37import org.onlab.packet.Ethernet;
38import org.onlab.packet.MacAddress;
39
Thomas Vachuskab97cf282014-10-20 23:31:12 -070040import java.util.concurrent.CountDownLatch;
41import java.util.concurrent.TimeUnit;
42
43import static org.onlab.onos.net.DeviceId.deviceId;
44import static org.onlab.onos.net.PortNumber.portNumber;
45
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070046/**
47 * Installs point-to-point connectivity intents.
48 */
49@Command(scope = "onos", name = "push-test-intents",
50 description = "Installs random intents to test throughput")
51public class IntentPushTestCommand extends AbstractShellCommand
Thomas Vachuskab97cf282014-10-20 23:31:12 -070052 implements IntentListener {
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070053
54 @Argument(index = 0, name = "ingressDevice",
55 description = "Ingress Device/Port Description",
56 required = true, multiValued = false)
57 String ingressDeviceString = null;
58
59 @Argument(index = 1, name = "egressDevice",
60 description = "Egress Device/Port Description",
61 required = true, multiValued = false)
62 String egressDeviceString = null;
63
64 @Argument(index = 2, name = "count",
Thomas Vachuskab97cf282014-10-20 23:31:12 -070065 description = "Number of intents to push",
66 required = true, multiValued = false)
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070067 String countString = null;
68
69
70 private static long id = 0x7870001;
71
72 private IntentService service;
73 private CountDownLatch latch;
74 private long start, end;
75
76 @Override
77 protected void execute() {
78 service = get(IntentService.class);
79
Thomas Vachuskab97cf282014-10-20 23:31:12 -070080 DeviceId ingressDeviceId = deviceId(getDeviceId(ingressDeviceString));
81 PortNumber ingressPortNumber = portNumber(getPortNumber(ingressDeviceString));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070082 ConnectPoint ingress = new ConnectPoint(ingressDeviceId, ingressPortNumber);
83
Thomas Vachuskab97cf282014-10-20 23:31:12 -070084 DeviceId egressDeviceId = deviceId(getDeviceId(egressDeviceString));
85 PortNumber egressPortNumber = portNumber(getPortNumber(egressDeviceString));
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070086 ConnectPoint egress = new ConnectPoint(egressDeviceId, egressPortNumber);
87
88 TrafficSelector.Builder selector = DefaultTrafficSelector.builder()
89 .matchEthType(Ethernet.TYPE_IPV4);
90 TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
91
92 int count = Integer.parseInt(countString);
93
94 service.addListener(this);
Brian O'Connorea9021e2014-10-10 23:12:02 -050095 latch = new CountDownLatch(count);
96
Brian O'Connor1aa99eb2014-10-10 16:18:58 -070097 start = System.currentTimeMillis();
98 for (int i = 0; i < count; i++) {
99 TrafficSelector s = selector
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700100 .matchEthSrc(MacAddress.valueOf(i))
101 .build();
102 Intent intent = new PointToPointIntent(appId(), s, treatment,
103 ingress, egress);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700104 service.submit(intent);
105 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700106 try {
alshabib7911a052014-10-16 17:49:37 -0700107 if (latch.await(10, TimeUnit.SECONDS)) {
108 printResults(count);
109 } else {
110 print("I FAIL MISERABLY -> %d", latch.getCount());
111 }
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700112 } catch (InterruptedException e) {
113 print(e.toString());
114 }
alshabib7911a052014-10-16 17:49:37 -0700115
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700116 service.removeListener(this);
117 }
118
119 private void printResults(int count) {
120 long delta = end - start;
121 print("Time to install %d intents: %d ms", count, delta);
122 }
123
124 /**
125 * Extracts the port number portion of the ConnectPoint.
126 *
127 * @param deviceString string representing the device/port
128 * @return port number as a string, empty string if the port is not found
129 */
130 private String getPortNumber(String deviceString) {
131 int slash = deviceString.indexOf('/');
132 if (slash <= 0) {
133 return "";
134 }
135 return deviceString.substring(slash + 1, deviceString.length());
136 }
137
138 /**
139 * Extracts the device ID portion of the ConnectPoint.
140 *
141 * @param deviceString string representing the device/port
142 * @return device ID string
143 */
144 private String getDeviceId(String deviceString) {
145 int slash = deviceString.indexOf('/');
146 if (slash <= 0) {
147 return "";
148 }
149 return deviceString.substring(0, slash);
150 }
151
152 @Override
153 public void event(IntentEvent event) {
154 if (event.type() == Type.INSTALLED) {
155 end = event.time();
Brian O'Connorea9021e2014-10-10 23:12:02 -0500156 if (latch != null) {
157 latch.countDown();
158 } else {
159 log.warn("install event latch is null");
160 }
alshabib7911a052014-10-16 17:49:37 -0700161 } else {
162 log.info("I FAIL -> {}", event);
Brian O'Connor1aa99eb2014-10-10 16:18:58 -0700163 }
164 }
165}