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