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