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; |
| 22 | import com.google.common.collect.Lists; |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 23 | import com.google.common.collect.Sets; |
| 24 | import org.apache.karaf.shell.commands.Argument; |
| 25 | import org.apache.karaf.shell.commands.Command; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import org.onosproject.cli.AbstractShellCommand; |
| 27 | import org.onosproject.net.Device; |
| 28 | import org.onosproject.net.PortNumber; |
| 29 | import org.onosproject.net.device.DeviceService; |
| 30 | import org.onosproject.net.flow.CompletedBatchOperation; |
| 31 | import org.onosproject.net.flow.DefaultFlowRule; |
| 32 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 33 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 34 | import org.onosproject.net.flow.FlowRuleBatchEntry; |
| 35 | import org.onosproject.net.flow.FlowRuleBatchOperation; |
| 36 | import org.onosproject.net.flow.FlowRuleService; |
| 37 | import org.onosproject.net.flow.TrafficSelector; |
| 38 | import org.onosproject.net.flow.TrafficTreatment; |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 39 | import org.onlab.packet.MacAddress; |
| 40 | |
alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 41 | import java.util.ArrayList; |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 42 | import java.util.Set; |
| 43 | import java.util.concurrent.ExecutionException; |
| 44 | import java.util.concurrent.Future; |
| 45 | |
| 46 | /** |
| 47 | * Installs many many flows. |
| 48 | */ |
| 49 | @Command(scope = "onos", name = "add-flows", |
| 50 | description = "Installs a flow rules") |
| 51 | public class AddFlowsCommand extends AbstractShellCommand { |
| 52 | |
| 53 | @Argument(index = 0, name = "flowPerDevice", description = "Number of flows to add per device", |
| 54 | required = true, multiValued = false) |
| 55 | String flows = null; |
| 56 | |
alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 57 | @Argument(index = 1, name = "numOfRuns", description = "Number of iterations", |
| 58 | required = true, multiValued = false) |
| 59 | String numOfRuns = null; |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 60 | |
| 61 | @Override |
| 62 | protected void execute() { |
| 63 | |
| 64 | FlowRuleService flowService = get(FlowRuleService.class); |
| 65 | DeviceService deviceService = get(DeviceService.class); |
| 66 | |
| 67 | int flowsPerDevice = Integer.parseInt(flows); |
alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 68 | int num = Integer.parseInt(numOfRuns); |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 69 | |
alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 70 | ArrayList<Long> results = Lists.newArrayList(); |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 71 | Iterable<Device> devices = deviceService.getDevices(); |
| 72 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 73 | .setOutput(PortNumber.portNumber(1)).build(); |
| 74 | TrafficSelector.Builder sbuilder; |
| 75 | Set<FlowRuleBatchEntry> rules = Sets.newHashSet(); |
| 76 | Set<FlowRuleBatchEntry> remove = Sets.newHashSet(); |
| 77 | for (Device d : devices) { |
| 78 | for (int i = 0; i < flowsPerDevice; i++) { |
| 79 | sbuilder = DefaultTrafficSelector.builder(); |
| 80 | sbuilder.matchEthSrc(MacAddress.valueOf(i)) |
| 81 | .matchEthDst(MacAddress.valueOf(Integer.MAX_VALUE - i)); |
| 82 | rules.add(new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.ADD, |
| 83 | new DefaultFlowRule(d.id(), sbuilder.build(), treatment, |
| 84 | 100, (long) 0, 10, false))); |
| 85 | remove.add(new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.REMOVE, |
| 86 | new DefaultFlowRule(d.id(), sbuilder.build(), treatment, |
| 87 | 100, (long) 0, 10, false))); |
| 88 | |
| 89 | } |
| 90 | } |
alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 91 | boolean isSuccess = true; |
| 92 | for (int i = 0; i < num; i++) { |
| 93 | long startTime = System.currentTimeMillis(); |
| 94 | Future<CompletedBatchOperation> op = flowService.applyBatch( |
| 95 | new FlowRuleBatchOperation(rules)); |
| 96 | try { |
| 97 | isSuccess &= op.get().isSuccess(); |
| 98 | } catch (InterruptedException e) { |
| 99 | e.printStackTrace(); |
| 100 | } catch (ExecutionException e) { |
| 101 | e.printStackTrace(); |
| 102 | } |
| 103 | long endTime = System.currentTimeMillis(); |
| 104 | results.add(endTime - startTime); |
| 105 | flowService.applyBatch( |
| 106 | new FlowRuleBatchOperation(remove)); |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 107 | } |
alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 108 | if (outputJson()) { |
| 109 | print("%s", json(new ObjectMapper(), isSuccess, results)); |
| 110 | } else { |
| 111 | printTime(isSuccess, results); |
| 112 | } |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 113 | |
alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 114 | |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 115 | |
| 116 | } |
| 117 | |
alshabib | 3460da1 | 2014-10-30 17:26:49 +0100 | [diff] [blame] | 118 | private Object json(ObjectMapper mapper, boolean isSuccess, ArrayList<Long> elapsed) { |
| 119 | ObjectNode result = mapper.createObjectNode(); |
| 120 | result.put("Success", isSuccess); |
| 121 | ArrayNode node = result.putArray("elapsed-time"); |
| 122 | for (Long v : elapsed) { |
| 123 | node.add(v); |
| 124 | } |
| 125 | return result; |
| 126 | } |
| 127 | |
| 128 | private void printTime(boolean isSuccess, ArrayList<Long> elapsed) { |
| 129 | print("Run is %s.", isSuccess ? "success" : "failure"); |
| 130 | for (int i = 0; i < elapsed.size(); i++) { |
| 131 | print(" Run %s : %s", i, elapsed.get(i)); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | |
alshabib | 5afcbd7 | 2014-10-30 16:28:40 +0100 | [diff] [blame] | 136 | } |