Thomas Vachuska | d821c75 | 2018-02-26 11:33:35 -0800 | [diff] [blame^] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.routescale; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.ImmutableSet; |
| 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 26 | import org.apache.felix.scr.annotations.Service; |
| 27 | import org.onlab.packet.Ethernet; |
| 28 | import org.onlab.packet.IpAddress; |
| 29 | import org.onlab.packet.IpPrefix; |
| 30 | import org.onlab.packet.MacAddress; |
| 31 | import org.onosproject.app.ApplicationService; |
| 32 | import org.onosproject.core.ApplicationId; |
| 33 | import org.onosproject.net.Device; |
| 34 | import org.onosproject.net.DeviceId; |
| 35 | import org.onosproject.net.Host; |
| 36 | import org.onosproject.net.PortNumber; |
| 37 | import org.onosproject.net.device.DeviceService; |
| 38 | import org.onosproject.net.flow.DefaultFlowRule; |
| 39 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 40 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 41 | import org.onosproject.net.flow.FlowRule; |
| 42 | import org.onosproject.net.flow.FlowRuleOperations; |
| 43 | import org.onosproject.net.flow.FlowRuleService; |
| 44 | import org.onosproject.net.flow.TrafficSelector; |
| 45 | import org.onosproject.net.flow.TrafficTreatment; |
| 46 | import org.onosproject.net.host.HostAdminService; |
| 47 | import org.onosproject.routeservice.Route; |
| 48 | import org.onosproject.routeservice.RouteAdminService; |
| 49 | import org.slf4j.Logger; |
| 50 | import org.slf4j.LoggerFactory; |
| 51 | |
| 52 | import java.util.List; |
| 53 | import java.util.Random; |
| 54 | |
| 55 | @Component(immediate = true) |
| 56 | @Service(value = ScaleTestManager.class) |
| 57 | public 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 | } |