alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 1 | |
alshabib | ab98466 | 2014-12-04 18:56:18 -0800 | [diff] [blame] | 2 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 3 | * Copyright 2014-2015 Open Networking Laboratory |
alshabib | ab98466 | 2014-12-04 18:56:18 -0800 | [diff] [blame] | 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 17 | package org.onosproject.cli.net; |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 18 | |
Ray Milkey | 9f87e51 | 2016-01-05 10:00:22 -0800 | [diff] [blame] | 19 | import java.util.ArrayList; |
| 20 | import java.util.concurrent.CountDownLatch; |
| 21 | import java.util.concurrent.TimeUnit; |
| 22 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 23 | import org.apache.commons.lang.math.RandomUtils; |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 24 | import org.apache.karaf.shell.commands.Argument; |
| 25 | import org.apache.karaf.shell.commands.Command; |
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; |
Ray Milkey | d13a37b | 2015-06-12 11:55:17 -0700 | [diff] [blame] | 36 | import org.onosproject.net.flow.FlowRule; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 37 | import org.onosproject.net.flow.FlowRuleOperations; |
| 38 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 39 | import org.onosproject.net.flow.FlowRuleService; |
| 40 | import org.onosproject.net.flow.TrafficSelector; |
| 41 | import org.onosproject.net.flow.TrafficTreatment; |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 42 | |
Ray Milkey | 9f87e51 | 2016-01-05 10:00:22 -0800 | [diff] [blame] | 43 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 44 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 45 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 46 | import com.google.common.base.Stopwatch; |
| 47 | import com.google.common.collect.Lists; |
| 48 | |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 49 | /** |
Charles M.C. Chan | 6f5bdc6 | 2015-04-22 01:21:09 +0800 | [diff] [blame] | 50 | * Installs bulk flows. |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 51 | */ |
suibin zhang | cb7f0d1 | 2015-08-10 11:14:15 -0700 | [diff] [blame] | 52 | @Command(scope = "onos", name = "add-test-flows", |
Ray Milkey | 82895d7 | 2015-01-22 17:06:00 -0800 | [diff] [blame] | 53 | description = "Installs a number of test flow rules - for testing only") |
suibin zhang | cb7f0d1 | 2015-08-10 11:14:15 -0700 | [diff] [blame] | 54 | public class AddTestFlowsCommand extends AbstractShellCommand { |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 55 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 56 | private CountDownLatch latch; |
| 57 | |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 58 | @Argument(index = 0, name = "flowPerDevice", description = "Number of flows to add per device", |
| 59 | required = true, multiValued = false) |
| 60 | String flows = null; |
| 61 | |
alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 62 | @Argument(index = 1, name = "numOfRuns", description = "Number of iterations", |
| 63 | required = true, multiValued = false) |
| 64 | String numOfRuns = null; |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 65 | |
| 66 | @Override |
Ray Milkey | aef4585 | 2016-01-11 17:13:19 -0800 | [diff] [blame] | 67 | @java.lang.SuppressWarnings("squid:S1148") |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 68 | protected void execute() { |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 69 | FlowRuleService flowService = get(FlowRuleService.class); |
| 70 | DeviceService deviceService = get(DeviceService.class); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 71 | CoreService coreService = get(CoreService.class); |
| 72 | |
| 73 | ApplicationId appId = coreService.registerApplication("onos.test.flow.installer"); |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 74 | |
| 75 | int flowsPerDevice = Integer.parseInt(flows); |
alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 76 | int num = Integer.parseInt(numOfRuns); |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 77 | |
alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 78 | ArrayList<Long> results = Lists.newArrayList(); |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 79 | Iterable<Device> devices = deviceService.getDevices(); |
| 80 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 81 | .setOutput(PortNumber.portNumber(RandomUtils.nextInt())).build(); |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 82 | TrafficSelector.Builder sbuilder; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 83 | FlowRuleOperations.Builder rules = FlowRuleOperations.builder(); |
| 84 | FlowRuleOperations.Builder remove = FlowRuleOperations.builder(); |
| 85 | |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 86 | for (Device d : devices) { |
| 87 | for (int i = 0; i < flowsPerDevice; i++) { |
| 88 | sbuilder = DefaultTrafficSelector.builder(); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 89 | |
| 90 | sbuilder.matchEthSrc(MacAddress.valueOf(RandomUtils.nextInt() * i)) |
| 91 | .matchEthDst(MacAddress.valueOf((Integer.MAX_VALUE - i) * RandomUtils.nextInt())); |
| 92 | |
| 93 | |
| 94 | int randomPriority = RandomUtils.nextInt(); |
Ray Milkey | d13a37b | 2015-06-12 11:55:17 -0700 | [diff] [blame] | 95 | |
| 96 | FlowRule addRule = DefaultFlowRule.builder() |
| 97 | .forDevice(d.id()) |
| 98 | .withSelector(sbuilder.build()) |
| 99 | .withTreatment(treatment) |
| 100 | .withPriority(randomPriority) |
| 101 | .fromApp(appId) |
| 102 | .makeTemporary(10) |
| 103 | .build(); |
| 104 | FlowRule removeRule = DefaultFlowRule.builder() |
| 105 | .forDevice(d.id()) |
| 106 | .withSelector(sbuilder.build()) |
| 107 | .withTreatment(treatment) |
| 108 | .withPriority(randomPriority) |
| 109 | .fromApp(appId) |
| 110 | .makeTemporary(10) |
| 111 | .build(); |
| 112 | |
| 113 | rules.add(addRule); |
| 114 | remove.remove(removeRule); |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 115 | |
| 116 | } |
| 117 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 118 | |
alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 119 | for (int i = 0; i < num; i++) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 120 | latch = new CountDownLatch(2); |
| 121 | flowService.apply(rules.build(new FlowRuleOperationsContext() { |
| 122 | |
| 123 | private final Stopwatch timer = Stopwatch.createStarted(); |
| 124 | |
| 125 | @Override |
| 126 | public void onSuccess(FlowRuleOperations ops) { |
| 127 | |
| 128 | timer.stop(); |
| 129 | results.add(timer.elapsed(TimeUnit.MILLISECONDS)); |
| 130 | if (results.size() == num) { |
| 131 | if (outputJson()) { |
| 132 | print("%s", json(new ObjectMapper(), true, results)); |
| 133 | } else { |
| 134 | printTime(true, results); |
| 135 | } |
| 136 | } |
| 137 | latch.countDown(); |
| 138 | } |
| 139 | })); |
| 140 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 141 | flowService.apply(remove.build(new FlowRuleOperationsContext() { |
| 142 | @Override |
| 143 | public void onSuccess(FlowRuleOperations ops) { |
| 144 | latch.countDown(); |
| 145 | } |
| 146 | })); |
alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 147 | try { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 148 | latch.await(); |
| 149 | } catch (InterruptedException e) { |
alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 150 | e.printStackTrace(); |
| 151 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 152 | |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 153 | } |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 154 | } |
| 155 | |
alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 156 | private Object json(ObjectMapper mapper, boolean isSuccess, ArrayList<Long> elapsed) { |
| 157 | ObjectNode result = mapper.createObjectNode(); |
| 158 | result.put("Success", isSuccess); |
| 159 | ArrayNode node = result.putArray("elapsed-time"); |
| 160 | for (Long v : elapsed) { |
| 161 | node.add(v); |
| 162 | } |
| 163 | return result; |
| 164 | } |
| 165 | |
| 166 | private void printTime(boolean isSuccess, ArrayList<Long> elapsed) { |
| 167 | print("Run is %s.", isSuccess ? "success" : "failure"); |
| 168 | for (int i = 0; i < elapsed.size(); i++) { |
| 169 | print(" Run %s : %s", i, elapsed.get(i)); |
| 170 | } |
| 171 | } |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 172 | } |