Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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.segmentrouting; |
| 18 | |
| 19 | import com.google.common.collect.Maps; |
| 20 | import com.google.common.collect.Sets; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.packet.IpAddress; |
| 24 | import org.onlab.packet.IpPrefix; |
| 25 | import org.onlab.packet.MacAddress; |
| 26 | import org.onlab.packet.VlanId; |
| 27 | import org.onosproject.net.ConnectPoint; |
Charles Chan | f0ae41e | 2017-08-23 13:55:39 -0700 | [diff] [blame^] | 28 | import org.onosproject.net.DefaultHost; |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 29 | import org.onosproject.net.DeviceId; |
| 30 | import org.onosproject.net.Host; |
Charles Chan | f0ae41e | 2017-08-23 13:55:39 -0700 | [diff] [blame^] | 31 | import org.onosproject.net.HostId; |
| 32 | import org.onosproject.net.HostLocation; |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 33 | import org.onosproject.net.config.NetworkConfigRegistryAdapter; |
| 34 | import org.onosproject.net.flow.TrafficTreatment; |
| 35 | import org.onosproject.net.intf.Interface; |
Charles Chan | f0ae41e | 2017-08-23 13:55:39 -0700 | [diff] [blame^] | 36 | import org.onosproject.net.provider.ProviderId; |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 37 | import org.onosproject.routeservice.ResolvedRoute; |
| 38 | import org.onosproject.routeservice.Route; |
| 39 | import org.onosproject.routeservice.RouteEvent; |
| 40 | import org.onosproject.routeservice.RouteInfo; |
| 41 | import org.onosproject.segmentrouting.config.DeviceConfiguration; |
| 42 | |
| 43 | import java.util.Map; |
| 44 | import java.util.Set; |
| 45 | |
| 46 | import static org.junit.Assert.*; |
| 47 | |
| 48 | /** |
| 49 | * Unit test for {@link RouteHandler}. |
| 50 | */ |
| 51 | public class RouteHandlerTest { |
| 52 | private RouteHandler routeHandler; |
| 53 | |
| 54 | // Mocked routing and bridging tables |
| 55 | private static final Map<MockBridgingTableKey, MockBridgingTableValue> BRIDGING_TABLE = |
| 56 | Maps.newConcurrentMap(); |
| 57 | private static final Map<MockRoutingTableKey, MockRoutingTableValue> ROUTING_TABLE = |
| 58 | Maps.newConcurrentMap(); |
| 59 | private static final Map<ConnectPoint, Set<IpPrefix>> SUBNET_TABLE = Maps.newConcurrentMap(); |
| 60 | // Mocked Next Id |
| 61 | private static final Map<Integer, TrafficTreatment> NEXT_TABLE = Maps.newConcurrentMap(); |
| 62 | |
| 63 | private static final IpPrefix P1 = IpPrefix.valueOf("10.0.0.0/24"); |
| 64 | private static final IpAddress N1 = IpAddress.valueOf("10.0.1.254"); |
| 65 | private static final MacAddress M1 = MacAddress.valueOf("00:00:00:00:00:01"); |
| 66 | private static final VlanId V1 = VlanId.vlanId((short) 1); |
| 67 | private static final ConnectPoint CP1 = ConnectPoint.deviceConnectPoint("of:0000000000000001/1"); |
| 68 | private static final Route R1 = new Route(Route.Source.STATIC, P1, N1); |
Charles Chan | f0ae41e | 2017-08-23 13:55:39 -0700 | [diff] [blame^] | 69 | private static final ResolvedRoute RR1 = new ResolvedRoute(R1, M1, V1); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 70 | |
| 71 | private static final IpAddress N2 = IpAddress.valueOf("10.0.2.254"); |
| 72 | private static final MacAddress M2 = MacAddress.valueOf("00:00:00:00:00:02"); |
| 73 | private static final VlanId V2 = VlanId.vlanId((short) 2); |
| 74 | private static final ConnectPoint CP2 = ConnectPoint.deviceConnectPoint("of:0000000000000001/2"); |
| 75 | private static final Route R2 = new Route(Route.Source.STATIC, P1, N2); |
Charles Chan | f0ae41e | 2017-08-23 13:55:39 -0700 | [diff] [blame^] | 76 | private static final ResolvedRoute RR2 = new ResolvedRoute(R2, M2, V2); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 77 | |
| 78 | private static final RouteInfo RI1 = new RouteInfo(P1, RR1, Sets.newHashSet(RR1)); |
| 79 | |
Charles Chan | f0ae41e | 2017-08-23 13:55:39 -0700 | [diff] [blame^] | 80 | private static final Host H1 = new DefaultHost(ProviderId.NONE, HostId.hostId(M1, V1), M1, V1, |
| 81 | Sets.newHashSet(new HostLocation(CP1, 0)), Sets.newHashSet(N1), false); |
| 82 | private static final Host H2 = new DefaultHost(ProviderId.NONE, HostId.hostId(M2, V2), M2, V2, |
| 83 | Sets.newHashSet(new HostLocation(CP2, 0)), Sets.newHashSet(N2), false); |
| 84 | |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 85 | // A set of hosts |
Charles Chan | f0ae41e | 2017-08-23 13:55:39 -0700 | [diff] [blame^] | 86 | private static final Set<Host> HOSTS = Sets.newHashSet(H1, H2); |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 87 | // A set of devices of which we have mastership |
| 88 | private static final Set<DeviceId> LOCAL_DEVICES = Sets.newHashSet(); |
| 89 | // A set of interfaces |
| 90 | private static final Set<Interface> INTERFACES = Sets.newHashSet(); |
| 91 | // A set of routes |
| 92 | private static final Set<RouteInfo> ROUTE_INFOS = Sets.newHashSet(RI1); |
| 93 | |
| 94 | @Before |
| 95 | public void setUp() throws Exception { |
| 96 | // TODO Initialize pairDevice and pairLocalPort config |
| 97 | // ObjectMapper mapper = new ObjectMapper(); |
| 98 | // ConfigApplyDelegate delegate = config -> {}; |
| 99 | // |
| 100 | // SegmentRoutingDeviceConfig dev3Config = new SegmentRoutingDeviceConfig(); |
| 101 | // JsonNode dev3Tree = mapper.createObjectNode(); |
| 102 | // dev3Config.init(DEV3, "host-handler-test", dev3Tree, mapper, delegate); |
| 103 | // dev3Config.setPairDeviceId(DEV4).setPairLocalPort(P9); |
| 104 | // |
| 105 | // SegmentRoutingDeviceConfig dev4Config = new SegmentRoutingDeviceConfig(); |
| 106 | // JsonNode dev4Tree = mapper.createObjectNode(); |
| 107 | // dev4Config.init(DEV4, "host-handler-test", dev4Tree, mapper, delegate); |
| 108 | // dev4Config.setPairDeviceId(DEV3).setPairLocalPort(P9); |
| 109 | |
| 110 | MockNetworkConfigRegistry mockNetworkConfigRegistry = new MockNetworkConfigRegistry(); |
| 111 | // mockNetworkConfigRegistry.applyConfig(dev3Config); |
| 112 | // mockNetworkConfigRegistry.applyConfig(dev4Config); |
| 113 | |
| 114 | // Initialize Segment Routing Manager |
| 115 | SegmentRoutingManager srManager = new MockSegmentRoutingManager(NEXT_TABLE); |
| 116 | srManager.cfgService = new NetworkConfigRegistryAdapter(); |
| 117 | srManager.deviceConfiguration = new DeviceConfiguration(srManager); |
| 118 | srManager.flowObjectiveService = new MockFlowObjectiveService(BRIDGING_TABLE, NEXT_TABLE); |
| 119 | srManager.routingRulePopulator = new MockRoutingRulePopulator(srManager, ROUTING_TABLE); |
| 120 | srManager.defaultRoutingHandler = new MockDefaultRoutingHandler(srManager, SUBNET_TABLE); |
| 121 | srManager.interfaceService = new MockInterfaceService(INTERFACES); |
| 122 | srManager.mastershipService = new MockMastershipService(LOCAL_DEVICES); |
| 123 | srManager.hostService = new MockHostService(HOSTS); |
| 124 | srManager.cfgService = mockNetworkConfigRegistry; |
| 125 | srManager.routeService = new MockRouteService(ROUTE_INFOS); |
| 126 | |
| 127 | routeHandler = new RouteHandler(srManager); |
| 128 | |
| 129 | ROUTING_TABLE.clear(); |
| 130 | BRIDGING_TABLE.clear(); |
| 131 | SUBNET_TABLE.clear(); |
| 132 | } |
| 133 | |
| 134 | @Test |
| 135 | public void init() throws Exception { |
| 136 | routeHandler.init(CP1.deviceId()); |
| 137 | |
| 138 | assertEquals(1, ROUTING_TABLE.size()); |
Charles Chan | f0ae41e | 2017-08-23 13:55:39 -0700 | [diff] [blame^] | 139 | MockRoutingTableValue rtv1 = ROUTING_TABLE.get(new MockRoutingTableKey(CP1.deviceId(), P1)); |
| 140 | assertEquals(M1, rtv1.macAddress); |
| 141 | assertEquals(V1, rtv1.vlanId); |
| 142 | assertEquals(CP1.port(), rtv1.portNumber); |
| 143 | |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 144 | assertEquals(1, SUBNET_TABLE.size()); |
| 145 | assertTrue(SUBNET_TABLE.get(CP1).contains(P1)); |
| 146 | } |
| 147 | |
| 148 | @Test |
| 149 | public void processRouteAdded() throws Exception { |
| 150 | RouteEvent re = new RouteEvent(RouteEvent.Type.ROUTE_ADDED, RR1); |
| 151 | routeHandler.processRouteAdded(re); |
| 152 | |
| 153 | assertEquals(1, ROUTING_TABLE.size()); |
Charles Chan | f0ae41e | 2017-08-23 13:55:39 -0700 | [diff] [blame^] | 154 | MockRoutingTableValue rtv1 = ROUTING_TABLE.get(new MockRoutingTableKey(CP1.deviceId(), P1)); |
| 155 | assertEquals(M1, rtv1.macAddress); |
| 156 | assertEquals(V1, rtv1.vlanId); |
| 157 | assertEquals(CP1.port(), rtv1.portNumber); |
| 158 | |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 159 | assertEquals(1, SUBNET_TABLE.size()); |
| 160 | assertTrue(SUBNET_TABLE.get(CP1).contains(P1)); |
| 161 | } |
| 162 | |
| 163 | @Test |
| 164 | public void processRouteUpdated() throws Exception { |
| 165 | processRouteAdded(); |
| 166 | |
| 167 | RouteEvent re = new RouteEvent(RouteEvent.Type.ROUTE_UPDATED, RR2, RR1); |
| 168 | routeHandler.processRouteUpdated(re); |
| 169 | |
| 170 | assertEquals(1, ROUTING_TABLE.size()); |
Charles Chan | f0ae41e | 2017-08-23 13:55:39 -0700 | [diff] [blame^] | 171 | MockRoutingTableValue rtv2 = ROUTING_TABLE.get(new MockRoutingTableKey(CP1.deviceId(), P1)); |
| 172 | assertEquals(M2, rtv2.macAddress); |
| 173 | assertEquals(V2, rtv2.vlanId); |
| 174 | assertEquals(CP2.port(), rtv2.portNumber); |
| 175 | |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 176 | assertEquals(1, SUBNET_TABLE.size()); |
| 177 | assertTrue(SUBNET_TABLE.get(CP2).contains(P1)); |
| 178 | } |
| 179 | |
| 180 | @Test |
| 181 | public void processRouteRemoved() throws Exception { |
| 182 | processRouteAdded(); |
| 183 | |
| 184 | RouteEvent re = new RouteEvent(RouteEvent.Type.ROUTE_REMOVED, RR1); |
| 185 | routeHandler.processRouteRemoved(re); |
| 186 | |
| 187 | assertEquals(0, ROUTING_TABLE.size()); |
| 188 | assertEquals(0, SUBNET_TABLE.size()); |
| 189 | } |
Charles Chan | 6c62499 | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 190 | } |