alshabib | ab98466 | 2014-12-04 18:56:18 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
alshabib | ab98466 | 2014-12-04 18:56:18 -0800 | [diff] [blame] | 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'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.cli.net; |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 17 | |
Ray Milkey | 9f87e51 | 2016-01-05 10:00:22 -0800 | [diff] [blame] | 18 | import java.util.ArrayList; |
| 19 | import java.util.concurrent.CountDownLatch; |
| 20 | import java.util.concurrent.TimeUnit; |
| 21 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 22 | import org.apache.commons.lang.math.RandomUtils; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 23 | import org.apache.karaf.shell.api.action.Argument; |
| 24 | import org.apache.karaf.shell.api.action.Command; |
| 25 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 26 | import org.onlab.packet.MacAddress; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 27 | import org.onosproject.cli.AbstractShellCommand; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 28 | import org.onosproject.core.ApplicationId; |
| 29 | import org.onosproject.core.CoreService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import org.onosproject.net.Device; |
| 31 | import org.onosproject.net.PortNumber; |
| 32 | import org.onosproject.net.device.DeviceService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 33 | import org.onosproject.net.flow.DefaultFlowRule; |
| 34 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 35 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
Yuta HIGUCHI | 160732b | 2017-05-22 16:36:43 -0700 | [diff] [blame] | 36 | import org.onosproject.net.flow.FlowEntry.FlowEntryState; |
Ray Milkey | d13a37b | 2015-06-12 11:55:17 -0700 | [diff] [blame] | 37 | import org.onosproject.net.flow.FlowRule; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 38 | import org.onosproject.net.flow.FlowRuleOperations; |
| 39 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 40 | import org.onosproject.net.flow.FlowRuleService; |
| 41 | import org.onosproject.net.flow.TrafficSelector; |
| 42 | import org.onosproject.net.flow.TrafficTreatment; |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 43 | |
Ray Milkey | 9f87e51 | 2016-01-05 10:00:22 -0800 | [diff] [blame] | 44 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 45 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 46 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 47 | import com.google.common.base.Stopwatch; |
Yuta HIGUCHI | 160732b | 2017-05-22 16:36:43 -0700 | [diff] [blame] | 48 | import com.google.common.collect.Iterables; |
Ray Milkey | 9f87e51 | 2016-01-05 10:00:22 -0800 | [diff] [blame] | 49 | import com.google.common.collect.Lists; |
Yuta HIGUCHI | 160732b | 2017-05-22 16:36:43 -0700 | [diff] [blame] | 50 | import com.google.common.collect.Streams; |
Ray Milkey | 9f87e51 | 2016-01-05 10:00:22 -0800 | [diff] [blame] | 51 | |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 52 | /** |
Charles M.C. Chan | 6f5bdc6 | 2015-04-22 01:21:09 +0800 | [diff] [blame] | 53 | * Installs bulk flows. |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 54 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 55 | @Service |
suibin zhang | cb7f0d1 | 2015-08-10 11:14:15 -0700 | [diff] [blame] | 56 | @Command(scope = "onos", name = "add-test-flows", |
Ray Milkey | 82895d7 | 2015-01-22 17:06:00 -0800 | [diff] [blame] | 57 | description = "Installs a number of test flow rules - for testing only") |
suibin zhang | cb7f0d1 | 2015-08-10 11:14:15 -0700 | [diff] [blame] | 58 | public class AddTestFlowsCommand extends AbstractShellCommand { |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 59 | |
Carmelo Cascone | c9b39d6 | 2017-08-24 13:35:02 +0200 | [diff] [blame] | 60 | private static final int MAX_OUT_PORT = 254; |
Deepa Vaddireddy | 2f4883d | 2016-06-07 22:51:01 +0530 | [diff] [blame] | 61 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 62 | private CountDownLatch latch; |
| 63 | |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 64 | @Argument(index = 0, name = "flowPerDevice", description = "Number of flows to add per device", |
| 65 | required = true, multiValued = false) |
| 66 | String flows = null; |
| 67 | |
alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 68 | @Argument(index = 1, name = "numOfRuns", description = "Number of iterations", |
| 69 | required = true, multiValued = false) |
| 70 | String numOfRuns = null; |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 71 | |
| 72 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 73 | protected void doExecute() { |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 74 | FlowRuleService flowService = get(FlowRuleService.class); |
| 75 | DeviceService deviceService = get(DeviceService.class); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 76 | CoreService coreService = get(CoreService.class); |
| 77 | |
| 78 | ApplicationId appId = coreService.registerApplication("onos.test.flow.installer"); |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 79 | |
| 80 | int flowsPerDevice = Integer.parseInt(flows); |
alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 81 | int num = Integer.parseInt(numOfRuns); |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 82 | |
alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 83 | ArrayList<Long> results = Lists.newArrayList(); |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 84 | Iterable<Device> devices = deviceService.getDevices(); |
| 85 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
Deepa Vaddireddy | 2f4883d | 2016-06-07 22:51:01 +0530 | [diff] [blame] | 86 | .setOutput(PortNumber.portNumber(RandomUtils.nextInt(MAX_OUT_PORT))).build(); |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 87 | TrafficSelector.Builder sbuilder; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 88 | FlowRuleOperations.Builder rules = FlowRuleOperations.builder(); |
| 89 | FlowRuleOperations.Builder remove = FlowRuleOperations.builder(); |
| 90 | |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 91 | for (Device d : devices) { |
Ray Milkey | 3717e60 | 2018-02-01 13:49:47 -0800 | [diff] [blame] | 92 | for (long i = 0; i < flowsPerDevice; i++) { |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 93 | sbuilder = DefaultTrafficSelector.builder(); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 94 | |
| 95 | sbuilder.matchEthSrc(MacAddress.valueOf(RandomUtils.nextInt() * i)) |
| 96 | .matchEthDst(MacAddress.valueOf((Integer.MAX_VALUE - i) * RandomUtils.nextInt())); |
| 97 | |
| 98 | |
Carolina Fernandez | fb4b04a | 2016-12-24 14:48:16 +0100 | [diff] [blame] | 99 | int randomPriority = RandomUtils.nextInt( |
| 100 | FlowRule.MAX_PRIORITY - FlowRule.MIN_PRIORITY + 1) + FlowRule.MIN_PRIORITY; |
Ray Milkey | d13a37b | 2015-06-12 11:55:17 -0700 | [diff] [blame] | 101 | |
| 102 | FlowRule addRule = DefaultFlowRule.builder() |
| 103 | .forDevice(d.id()) |
| 104 | .withSelector(sbuilder.build()) |
| 105 | .withTreatment(treatment) |
| 106 | .withPriority(randomPriority) |
| 107 | .fromApp(appId) |
| 108 | .makeTemporary(10) |
| 109 | .build(); |
| 110 | FlowRule removeRule = DefaultFlowRule.builder() |
| 111 | .forDevice(d.id()) |
| 112 | .withSelector(sbuilder.build()) |
| 113 | .withTreatment(treatment) |
| 114 | .withPriority(randomPriority) |
| 115 | .fromApp(appId) |
| 116 | .makeTemporary(10) |
| 117 | .build(); |
| 118 | |
| 119 | rules.add(addRule); |
| 120 | remove.remove(removeRule); |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 121 | |
| 122 | } |
| 123 | } |
Yuta HIGUCHI | 160732b | 2017-05-22 16:36:43 -0700 | [diff] [blame] | 124 | // close stages |
| 125 | rules.newStage(); |
| 126 | remove.newStage(); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 127 | |
alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 128 | for (int i = 0; i < num; i++) { |
Yuta HIGUCHI | 160732b | 2017-05-22 16:36:43 -0700 | [diff] [blame] | 129 | printProgress("Run %d:", i); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 130 | latch = new CountDownLatch(2); |
Yuta HIGUCHI | 160732b | 2017-05-22 16:36:43 -0700 | [diff] [blame] | 131 | final CountDownLatch addSuccess = new CountDownLatch(1); |
| 132 | printProgress("..batch add request"); |
| 133 | Stopwatch add = Stopwatch.createStarted(); |
| 134 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 135 | flowService.apply(rules.build(new FlowRuleOperationsContext() { |
| 136 | |
| 137 | private final Stopwatch timer = Stopwatch.createStarted(); |
| 138 | |
| 139 | @Override |
| 140 | public void onSuccess(FlowRuleOperations ops) { |
| 141 | |
| 142 | timer.stop(); |
Yuta HIGUCHI | 160732b | 2017-05-22 16:36:43 -0700 | [diff] [blame] | 143 | printProgress("..add success"); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 144 | results.add(timer.elapsed(TimeUnit.MILLISECONDS)); |
| 145 | if (results.size() == num) { |
| 146 | if (outputJson()) { |
| 147 | print("%s", json(new ObjectMapper(), true, results)); |
| 148 | } else { |
| 149 | printTime(true, results); |
| 150 | } |
| 151 | } |
| 152 | latch.countDown(); |
Yuta HIGUCHI | 160732b | 2017-05-22 16:36:43 -0700 | [diff] [blame] | 153 | addSuccess.countDown(); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 154 | } |
| 155 | })); |
| 156 | |
Yuta HIGUCHI | 160732b | 2017-05-22 16:36:43 -0700 | [diff] [blame] | 157 | try { |
| 158 | addSuccess.await(); |
| 159 | // wait until all flows reaches ADDED state |
| 160 | while (!Streams.stream(flowService.getFlowEntriesById(appId)) |
| 161 | .allMatch(fr -> fr.state() == FlowEntryState.ADDED)) { |
| 162 | Thread.sleep(100); |
| 163 | } |
| 164 | add.stop(); |
| 165 | printProgress("..completed %d ± 100 ms", add.elapsed(TimeUnit.MILLISECONDS)); |
| 166 | } catch (InterruptedException e1) { |
| 167 | printProgress("Interrupted"); |
| 168 | Thread.currentThread().interrupt(); |
| 169 | } |
| 170 | |
| 171 | printProgress("..cleaning up"); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 172 | flowService.apply(remove.build(new FlowRuleOperationsContext() { |
| 173 | @Override |
| 174 | public void onSuccess(FlowRuleOperations ops) { |
| 175 | latch.countDown(); |
| 176 | } |
| 177 | })); |
Yuta HIGUCHI | 160732b | 2017-05-22 16:36:43 -0700 | [diff] [blame] | 178 | |
alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 179 | try { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 180 | latch.await(); |
Yuta HIGUCHI | 160732b | 2017-05-22 16:36:43 -0700 | [diff] [blame] | 181 | while (!Iterables.isEmpty(flowService.getFlowEntriesById(appId))) { |
| 182 | Thread.sleep(500); |
| 183 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 184 | } catch (InterruptedException e) { |
Yuta HIGUCHI | 160732b | 2017-05-22 16:36:43 -0700 | [diff] [blame] | 185 | printProgress("Interrupted."); |
| 186 | Thread.currentThread().interrupt(); |
alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 187 | } |
Yuta HIGUCHI | 160732b | 2017-05-22 16:36:43 -0700 | [diff] [blame] | 188 | } |
| 189 | if (outputJson()) { |
| 190 | print("%s", json(new ObjectMapper(), true, results)); |
| 191 | } else { |
| 192 | printTime(true, results); |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 193 | } |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 194 | } |
| 195 | |
alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 196 | private Object json(ObjectMapper mapper, boolean isSuccess, ArrayList<Long> elapsed) { |
| 197 | ObjectNode result = mapper.createObjectNode(); |
| 198 | result.put("Success", isSuccess); |
| 199 | ArrayNode node = result.putArray("elapsed-time"); |
| 200 | for (Long v : elapsed) { |
| 201 | node.add(v); |
| 202 | } |
| 203 | return result; |
| 204 | } |
| 205 | |
Yuta HIGUCHI | 160732b | 2017-05-22 16:36:43 -0700 | [diff] [blame] | 206 | private void printProgress(String format, Object... args) { |
| 207 | if (!outputJson()) { |
| 208 | print(format, args); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | |
alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 213 | private void printTime(boolean isSuccess, ArrayList<Long> elapsed) { |
| 214 | print("Run is %s.", isSuccess ? "success" : "failure"); |
| 215 | for (int i = 0; i < elapsed.size(); i++) { |
Yuta HIGUCHI | 160732b | 2017-05-22 16:36:43 -0700 | [diff] [blame] | 216 | print(" Run %s : %s ms", i, elapsed.get(i)); |
alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 217 | } |
| 218 | } |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 219 | } |