blob: 3c21343b90ff4c601cb219ee8ccc1f13f37c0699 [file] [log] [blame]
alshabib5afcbd72014-10-30 16:28:40 +01001/*
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 */
19package org.onlab.onos.cli.net;
20
21import com.google.common.collect.Sets;
22import org.apache.karaf.shell.commands.Argument;
23import org.apache.karaf.shell.commands.Command;
24import org.onlab.onos.cli.AbstractShellCommand;
25import org.onlab.onos.net.Device;
26import org.onlab.onos.net.PortNumber;
27import org.onlab.onos.net.device.DeviceService;
28import org.onlab.onos.net.flow.CompletedBatchOperation;
29import org.onlab.onos.net.flow.DefaultFlowRule;
30import org.onlab.onos.net.flow.DefaultTrafficSelector;
31import org.onlab.onos.net.flow.DefaultTrafficTreatment;
32import org.onlab.onos.net.flow.FlowRuleBatchEntry;
33import org.onlab.onos.net.flow.FlowRuleBatchOperation;
34import org.onlab.onos.net.flow.FlowRuleService;
35import org.onlab.onos.net.flow.TrafficSelector;
36import org.onlab.onos.net.flow.TrafficTreatment;
37import org.onlab.packet.MacAddress;
38
39import java.util.Set;
40import java.util.concurrent.ExecutionException;
41import 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")
48public 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}