alshabib

added add-flows command

Change-Id: I240da77d78eebd54eeaa5a0864244920278429a2
1 +/*
2 + * Licensed to the Apache Software Foundation (ASF) under one
3 + * or more contributor license agreements. See the NOTICE file
4 + * distributed with this work for additional information
5 + * regarding copyright ownership. The ASF licenses this file
6 + * to you under the Apache License, Version 2.0 (the
7 + * "License"); you may not use this file except in compliance
8 + * with the License. You may obtain a copy of the License at
9 + *
10 + * http://www.apache.org/licenses/LICENSE-2.0
11 + *
12 + * Unless required by applicable law or agreed to in writing,
13 + * software distributed under the License is distributed on an
14 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 + * KIND, either express or implied. See the License for the
16 + * specific language governing permissions and limitations
17 + * under the License.
18 + */
19 +package org.onlab.onos.cli.net;
20 +
21 +import com.google.common.collect.Sets;
22 +import org.apache.karaf.shell.commands.Argument;
23 +import org.apache.karaf.shell.commands.Command;
24 +import org.onlab.onos.cli.AbstractShellCommand;
25 +import org.onlab.onos.net.Device;
26 +import org.onlab.onos.net.PortNumber;
27 +import org.onlab.onos.net.device.DeviceService;
28 +import org.onlab.onos.net.flow.CompletedBatchOperation;
29 +import org.onlab.onos.net.flow.DefaultFlowRule;
30 +import org.onlab.onos.net.flow.DefaultTrafficSelector;
31 +import org.onlab.onos.net.flow.DefaultTrafficTreatment;
32 +import org.onlab.onos.net.flow.FlowRuleBatchEntry;
33 +import org.onlab.onos.net.flow.FlowRuleBatchOperation;
34 +import org.onlab.onos.net.flow.FlowRuleService;
35 +import org.onlab.onos.net.flow.TrafficSelector;
36 +import org.onlab.onos.net.flow.TrafficTreatment;
37 +import org.onlab.packet.MacAddress;
38 +
39 +import java.util.Set;
40 +import java.util.concurrent.ExecutionException;
41 +import java.util.concurrent.Future;
42 +
43 +/**
44 + * Installs many many flows.
45 + */
46 +@Command(scope = "onos", name = "add-flows",
47 + description = "Installs a flow rules")
48 +public class AddFlowsCommand extends AbstractShellCommand {
49 +
50 + @Argument(index = 0, name = "flowPerDevice", description = "Number of flows to add per device",
51 + required = true, multiValued = false)
52 + String flows = null;
53 +
54 +
55 + @Override
56 + protected void execute() {
57 +
58 + FlowRuleService flowService = get(FlowRuleService.class);
59 + DeviceService deviceService = get(DeviceService.class);
60 +
61 + int flowsPerDevice = Integer.parseInt(flows);
62 +
63 + Iterable<Device> devices = deviceService.getDevices();
64 + TrafficTreatment treatment = DefaultTrafficTreatment.builder()
65 + .setOutput(PortNumber.portNumber(1)).build();
66 + TrafficSelector.Builder sbuilder;
67 + Set<FlowRuleBatchEntry> rules = Sets.newHashSet();
68 + Set<FlowRuleBatchEntry> remove = Sets.newHashSet();
69 + for (Device d : devices) {
70 + for (int i = 0; i < flowsPerDevice; i++) {
71 + sbuilder = DefaultTrafficSelector.builder();
72 + sbuilder.matchEthSrc(MacAddress.valueOf(i))
73 + .matchEthDst(MacAddress.valueOf(Integer.MAX_VALUE - i));
74 + rules.add(new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.ADD,
75 + new DefaultFlowRule(d.id(), sbuilder.build(), treatment,
76 + 100, (long) 0, 10, false)));
77 + remove.add(new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.REMOVE,
78 + new DefaultFlowRule(d.id(), sbuilder.build(), treatment,
79 + 100, (long) 0, 10, false)));
80 +
81 + }
82 + }
83 + long startTime = System.currentTimeMillis();
84 + Future<CompletedBatchOperation> op = flowService.applyBatch(
85 + new FlowRuleBatchOperation(rules));
86 + CompletedBatchOperation completed = null;
87 + try {
88 + completed = op.get();
89 + } catch (InterruptedException e) {
90 + e.printStackTrace();
91 + } catch (ExecutionException e) {
92 + e.printStackTrace();
93 + }
94 + long endTime = System.currentTimeMillis();
95 + print("%s AFTER ELAPSED TIME %s", completed.isSuccess() ? "SUCCESS" : "FAILURE",
96 + endTime - startTime);
97 +
98 + flowService.applyBatch(
99 + new FlowRuleBatchOperation(remove));
100 +
101 + }
102 +
103 +}
...@@ -183,6 +183,10 @@ ...@@ -183,6 +183,10 @@
183 </command> 183 </command>
184 184
185 <command> 185 <command>
186 + <action class="org.onlab.onos.cli.net.AddFlowsCommand"/>
187 + </command>
188 +
189 + <command>
186 <action class="org.onlab.onos.cli.net.WipeOutCommand"/> 190 <action class="org.onlab.onos.cli.net.WipeOutCommand"/>
187 </command> 191 </command>
188 </command-bundle> 192 </command-bundle>
......