blob: 65c1c7fa4c1f4f22fbab3369f945162f7ab435b2 [file] [log] [blame]
Thomas Vachuskaa8e74772018-02-26 11:33:35 -08001/*
2 * Copyright 2018-present Open Networking Foundation
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 */
16
17package org.onosproject.routescale;
18
19import com.google.common.collect.ImmutableList;
20import com.google.common.collect.ImmutableSet;
21import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
24import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
26import org.apache.felix.scr.annotations.Service;
27import org.onlab.packet.Ethernet;
28import org.onlab.packet.IpAddress;
29import org.onlab.packet.IpPrefix;
30import org.onlab.packet.MacAddress;
31import org.onosproject.app.ApplicationService;
32import org.onosproject.core.ApplicationId;
33import org.onosproject.net.Device;
34import org.onosproject.net.DeviceId;
35import org.onosproject.net.Host;
36import org.onosproject.net.PortNumber;
37import org.onosproject.net.device.DeviceService;
38import org.onosproject.net.flow.DefaultFlowRule;
39import org.onosproject.net.flow.DefaultTrafficSelector;
40import org.onosproject.net.flow.DefaultTrafficTreatment;
41import org.onosproject.net.flow.FlowRule;
42import org.onosproject.net.flow.FlowRuleOperations;
43import org.onosproject.net.flow.FlowRuleService;
44import org.onosproject.net.flow.TrafficSelector;
45import org.onosproject.net.flow.TrafficTreatment;
46import org.onosproject.net.host.HostAdminService;
47import org.onosproject.routeservice.Route;
48import org.onosproject.routeservice.RouteAdminService;
49import org.slf4j.Logger;
50import org.slf4j.LoggerFactory;
51
52import java.util.List;
53import java.util.Random;
54
55@Component(immediate = true)
56@Service(value = ScaleTestManager.class)
57public class ScaleTestManager {
58
59 private Logger log = LoggerFactory.getLogger(getClass());
60
61 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
62 protected ApplicationService applicationService;
63
64 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
65 protected DeviceService deviceService;
66
67 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
68 protected HostAdminService hostAdminService;
69
70 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
71 protected FlowRuleService flowRuleService;
72
73 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
74 protected RouteAdminService routeAdminService;
75
76 private final Random random = new Random(System.currentTimeMillis());
77
78 private ApplicationId appId;
79
80 @Activate
81 protected void activate() {
82 appId = applicationService.getId("org.onosproject.routescale");
83 log.info("Started");
84 }
85
86 @Deactivate
87 protected void deactivate() {
88 log.info("Stopped");
89 }
90
91 public void createFlows(int flowCount) {
92 for (Device device : deviceService.getAvailableDevices()) {
93 DeviceId id = device.id();
94 FlowRuleOperations.Builder ops = FlowRuleOperations.builder();
95
96 for (int i = 0; i < flowCount; i++) {
97 FlowRule.Builder frb = DefaultFlowRule.builder();
98 frb.fromApp(appId).makePermanent().withPriority(1000 + i);
99 TrafficSelector.Builder tsb = DefaultTrafficSelector.builder();
100 TrafficTreatment.Builder ttb = DefaultTrafficTreatment.builder();
101
102 tsb.matchEthType(Ethernet.TYPE_IPV4);
103 ttb.setEthDst(randomMac()).setEthSrc(randomMac());
104 ttb.setOutput(PortNumber.portNumber(random.nextInt(512)));
105 frb.withSelector(tsb.build()).withTreatment(ttb.build());
106 ops.add(frb.forDevice(id).build());
107 }
108
109 flowRuleService.apply(ops.build());
110
111 }
112 }
113
114 public void createRoutes(int routeCount) {
115 List<Host> hosts = ImmutableList.copyOf(hostAdminService.getHosts());
116 ImmutableSet.Builder<Route> routes = ImmutableSet.builder();
117 for (int i = 0; i < routeCount; i++) {
118 IpPrefix prefix = randomIp().toIpPrefix();
119 IpAddress nextHop = randomIp(hosts);
120 routes.add(new Route(Route.Source.STATIC, prefix, nextHop));
121 }
122 routeAdminService.update(routes.build());
123 }
124
125 private IpAddress randomIp() {
126 byte[] bytes = new byte[4];
127 random.nextBytes(bytes);
128 return IpAddress.valueOf(IpAddress.Version.INET, bytes, 0);
129 }
130
131 private IpAddress randomIp(List<Host> hosts) {
132 Host host = hosts.get(random.nextInt(hosts.size()));
133 return host.ipAddresses().iterator().next();
134 }
135
136 private MacAddress randomMac() {
137 byte[] bytes = new byte[6];
138 random.nextBytes(bytes);
139 return MacAddress.valueOf(bytes);
140 }
141
142}