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 | |
| 49 | import static org.onlab.util.SonarSuppressionConstants.SONAR_PRINT_STACK_TRACE; |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 50 | |
| 51 | /** |
Charles M.C. Chan | 6f5bdc6 | 2015-04-22 01:21:09 +0800 | [diff] [blame] | 52 | * Installs bulk flows. |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 53 | */ |
suibin zhang | cb7f0d1 | 2015-08-10 11:14:15 -0700 | [diff] [blame] | 54 | @Command(scope = "onos", name = "add-test-flows", |
Ray Milkey | 82895d7 | 2015-01-22 17:06:00 -0800 | [diff] [blame] | 55 | description = "Installs a number of test flow rules - for testing only") |
suibin zhang | cb7f0d1 | 2015-08-10 11:14:15 -0700 | [diff] [blame] | 56 | public class AddTestFlowsCommand extends AbstractShellCommand { |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 57 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 58 | private CountDownLatch latch; |
| 59 | |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 60 | @Argument(index = 0, name = "flowPerDevice", description = "Number of flows to add per device", |
| 61 | required = true, multiValued = false) |
| 62 | String flows = null; |
| 63 | |
alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 64 | @Argument(index = 1, name = "numOfRuns", description = "Number of iterations", |
| 65 | required = true, multiValued = false) |
| 66 | String numOfRuns = null; |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 67 | |
| 68 | @Override |
Ray Milkey | 9f87e51 | 2016-01-05 10:00:22 -0800 | [diff] [blame^] | 69 | @java.lang.SuppressWarnings(SONAR_PRINT_STACK_TRACE) |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 70 | protected void execute() { |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 71 | FlowRuleService flowService = get(FlowRuleService.class); |
| 72 | DeviceService deviceService = get(DeviceService.class); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 73 | CoreService coreService = get(CoreService.class); |
| 74 | |
| 75 | ApplicationId appId = coreService.registerApplication("onos.test.flow.installer"); |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 76 | |
| 77 | int flowsPerDevice = Integer.parseInt(flows); |
alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 78 | int num = Integer.parseInt(numOfRuns); |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 79 | |
alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 80 | ArrayList<Long> results = Lists.newArrayList(); |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 81 | Iterable<Device> devices = deviceService.getDevices(); |
| 82 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 83 | .setOutput(PortNumber.portNumber(RandomUtils.nextInt())).build(); |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 84 | TrafficSelector.Builder sbuilder; |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 85 | FlowRuleOperations.Builder rules = FlowRuleOperations.builder(); |
| 86 | FlowRuleOperations.Builder remove = FlowRuleOperations.builder(); |
| 87 | |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 88 | for (Device d : devices) { |
| 89 | for (int i = 0; i < flowsPerDevice; i++) { |
| 90 | sbuilder = DefaultTrafficSelector.builder(); |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 91 | |
| 92 | sbuilder.matchEthSrc(MacAddress.valueOf(RandomUtils.nextInt() * i)) |
| 93 | .matchEthDst(MacAddress.valueOf((Integer.MAX_VALUE - i) * RandomUtils.nextInt())); |
| 94 | |
| 95 | |
| 96 | int randomPriority = RandomUtils.nextInt(); |
Ray Milkey | d13a37b | 2015-06-12 11:55:17 -0700 | [diff] [blame] | 97 | |
| 98 | FlowRule addRule = DefaultFlowRule.builder() |
| 99 | .forDevice(d.id()) |
| 100 | .withSelector(sbuilder.build()) |
| 101 | .withTreatment(treatment) |
| 102 | .withPriority(randomPriority) |
| 103 | .fromApp(appId) |
| 104 | .makeTemporary(10) |
| 105 | .build(); |
| 106 | FlowRule removeRule = DefaultFlowRule.builder() |
| 107 | .forDevice(d.id()) |
| 108 | .withSelector(sbuilder.build()) |
| 109 | .withTreatment(treatment) |
| 110 | .withPriority(randomPriority) |
| 111 | .fromApp(appId) |
| 112 | .makeTemporary(10) |
| 113 | .build(); |
| 114 | |
| 115 | rules.add(addRule); |
| 116 | remove.remove(removeRule); |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 117 | |
| 118 | } |
| 119 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 120 | |
alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 121 | for (int i = 0; i < num; i++) { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 122 | latch = new CountDownLatch(2); |
| 123 | flowService.apply(rules.build(new FlowRuleOperationsContext() { |
| 124 | |
| 125 | private final Stopwatch timer = Stopwatch.createStarted(); |
| 126 | |
| 127 | @Override |
| 128 | public void onSuccess(FlowRuleOperations ops) { |
| 129 | |
| 130 | timer.stop(); |
| 131 | results.add(timer.elapsed(TimeUnit.MILLISECONDS)); |
| 132 | if (results.size() == num) { |
| 133 | if (outputJson()) { |
| 134 | print("%s", json(new ObjectMapper(), true, results)); |
| 135 | } else { |
| 136 | printTime(true, results); |
| 137 | } |
| 138 | } |
| 139 | latch.countDown(); |
| 140 | } |
| 141 | })); |
| 142 | |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 143 | flowService.apply(remove.build(new FlowRuleOperationsContext() { |
| 144 | @Override |
| 145 | public void onSuccess(FlowRuleOperations ops) { |
| 146 | latch.countDown(); |
| 147 | } |
| 148 | })); |
alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 149 | try { |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 150 | latch.await(); |
| 151 | } catch (InterruptedException e) { |
alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 152 | e.printStackTrace(); |
| 153 | } |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 154 | |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 155 | } |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 156 | } |
| 157 | |
alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 158 | private Object json(ObjectMapper mapper, boolean isSuccess, ArrayList<Long> elapsed) { |
| 159 | ObjectNode result = mapper.createObjectNode(); |
| 160 | result.put("Success", isSuccess); |
| 161 | ArrayNode node = result.putArray("elapsed-time"); |
| 162 | for (Long v : elapsed) { |
| 163 | node.add(v); |
| 164 | } |
| 165 | return result; |
| 166 | } |
| 167 | |
| 168 | private void printTime(boolean isSuccess, ArrayList<Long> elapsed) { |
| 169 | print("Run is %s.", isSuccess ? "success" : "failure"); |
| 170 | for (int i = 0; i < elapsed.size(); i++) { |
| 171 | print(" Run %s : %s", i, elapsed.get(i)); |
| 172 | } |
| 173 | } |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 174 | } |