gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 1 | /* |
Jonathan Hart | f4bd048 | 2017-01-27 15:11:18 -0800 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Laboratory |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 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 | */ |
Jonathan Hart | f4bd048 | 2017-01-27 15:11:18 -0800 | [diff] [blame] | 16 | |
| 17 | package org.onosproject.routing.fibinstaller; |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 18 | |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 19 | import com.google.common.collect.Sets; |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 20 | import org.easymock.EasyMock; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.packet.Ethernet; |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 24 | import org.onlab.packet.Ip4Prefix; |
| 25 | import org.onlab.packet.IpAddress; |
| 26 | import org.onlab.packet.IpPrefix; |
| 27 | import org.onlab.packet.MacAddress; |
| 28 | import org.onlab.packet.VlanId; |
| 29 | import org.onosproject.TestApplicationId; |
gaurav | a2e61a5 | 2016-05-05 03:39:31 +0530 | [diff] [blame] | 30 | import org.onosproject.app.ApplicationService; |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 31 | import org.onosproject.cfg.ComponentConfigService; |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 32 | import org.onosproject.core.ApplicationId; |
| 33 | import org.onosproject.core.CoreService; |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 34 | import org.onosproject.incubator.net.intf.Interface; |
gaurav | 164cf6d | 2016-03-25 21:43:04 +0530 | [diff] [blame] | 35 | import org.onosproject.incubator.net.intf.InterfaceListener; |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 36 | import org.onosproject.incubator.net.intf.InterfaceService; |
gaurav | 164cf6d | 2016-03-25 21:43:04 +0530 | [diff] [blame] | 37 | import org.onosproject.incubator.net.intf.InterfaceServiceAdapter; |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 38 | import org.onosproject.incubator.net.routing.ResolvedRoute; |
| 39 | import org.onosproject.incubator.net.routing.RouteEvent; |
| 40 | import org.onosproject.incubator.net.routing.RouteListener; |
| 41 | import org.onosproject.incubator.net.routing.RouteServiceAdapter; |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 42 | import org.onosproject.net.ConnectPoint; |
| 43 | import org.onosproject.net.DeviceId; |
| 44 | import org.onosproject.net.PortNumber; |
Jonathan Hart | 25bd668 | 2016-06-03 10:45:47 -0700 | [diff] [blame] | 45 | import org.onosproject.net.config.NetworkConfigListener; |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 46 | import org.onosproject.net.config.NetworkConfigRegistry; |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 47 | import org.onosproject.net.config.NetworkConfigService; |
| 48 | import org.onosproject.net.device.DeviceListener; |
| 49 | import org.onosproject.net.device.DeviceService; |
| 50 | import org.onosproject.net.device.DeviceServiceAdapter; |
| 51 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 52 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 53 | import org.onosproject.net.flow.TrafficSelector; |
| 54 | import org.onosproject.net.flow.TrafficTreatment; |
| 55 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 56 | import org.onosproject.net.flowobjective.DefaultNextObjective; |
| 57 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 58 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 59 | import org.onosproject.net.flowobjective.NextObjective; |
| 60 | import org.onosproject.net.host.InterfaceIpAddress; |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 61 | import org.onosproject.routing.RoutingService; |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 62 | import org.onosproject.routing.config.RouterConfig; |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 63 | import org.onosproject.routing.config.RoutersConfig; |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 64 | import org.osgi.service.component.ComponentContext; |
| 65 | |
| 66 | import java.util.ArrayList; |
| 67 | import java.util.Collections; |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 68 | import java.util.List; |
| 69 | import java.util.Set; |
| 70 | |
| 71 | import static org.easymock.EasyMock.anyObject; |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 72 | import static org.easymock.EasyMock.anyString; |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 73 | import static org.easymock.EasyMock.createMock; |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 74 | import static org.easymock.EasyMock.createNiceMock; |
| 75 | import static org.easymock.EasyMock.eq; |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 76 | import static org.easymock.EasyMock.expect; |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 77 | import static org.easymock.EasyMock.expectLastCall; |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 78 | import static org.easymock.EasyMock.replay; |
| 79 | import static org.easymock.EasyMock.reset; |
| 80 | import static org.easymock.EasyMock.verify; |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * Unit tests for SingleSwitchFibInstaller. |
| 84 | */ |
Jonathan Hart | e732704 | 2017-02-02 13:11:25 -0800 | [diff] [blame] | 85 | public class FibInstallerTest { |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 86 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 87 | private static final DeviceId DEVICE_ID = DeviceId.deviceId("of:0000000000000001"); |
| 88 | |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 89 | private static final ConnectPoint SW1_ETH1 = new ConnectPoint( |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 90 | DEVICE_ID, PortNumber.portNumber(1)); |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 91 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 92 | private static final ConnectPoint SW1_ETH2 = new ConnectPoint( |
| 93 | DEVICE_ID, PortNumber.portNumber(2)); |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 94 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 95 | private static final int NEXT_ID = 11; |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 96 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 97 | private static final VlanId VLAN1 = VlanId.vlanId((short) 1); |
| 98 | private static final MacAddress MAC1 = MacAddress.valueOf("00:00:00:00:00:01"); |
| 99 | private static final MacAddress MAC2 = MacAddress.valueOf("00:00:00:00:00:02"); |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 100 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 101 | private static final IpPrefix PREFIX1 = Ip4Prefix.valueOf("1.1.1.0/24"); |
| 102 | private static final IpAddress NEXT_HOP1 = IpAddress.valueOf("192.168.10.1"); |
| 103 | private static final IpAddress NEXT_HOP2 = IpAddress.valueOf("192.168.20.1"); |
| 104 | private static final InterfaceIpAddress INTF1 = |
| 105 | InterfaceIpAddress.valueOf("192.168.10.2/24"); |
| 106 | private static final InterfaceIpAddress INTF2 = |
| 107 | InterfaceIpAddress.valueOf("192.168.20.2/24"); |
| 108 | |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 109 | private final Set<Interface> interfaces = Sets.newHashSet(); |
| 110 | private InterfaceService interfaceService; |
| 111 | private NetworkConfigService networkConfigService; |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 112 | private NetworkConfigRegistry networkConfigRegistry; |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 113 | private FlowObjectiveService flowObjectiveService; |
gaurav | a2e61a5 | 2016-05-05 03:39:31 +0530 | [diff] [blame] | 114 | private ApplicationService applicationService; |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 115 | private DeviceService deviceService; |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 116 | private static final ApplicationId APPID = TestApplicationId.create("foo"); |
| 117 | |
| 118 | private RouteListener routeListener; |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 119 | private DeviceListener deviceListener; |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 120 | |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 121 | private RouterConfig routerConfig; |
Jonathan Hart | e732704 | 2017-02-02 13:11:25 -0800 | [diff] [blame] | 122 | private FibInstaller sSfibInstaller; |
gaurav | 164cf6d | 2016-03-25 21:43:04 +0530 | [diff] [blame] | 123 | private InterfaceListener interfaceListener; |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 124 | |
| 125 | @Before |
| 126 | public void setUp() throws Exception { |
Jonathan Hart | e732704 | 2017-02-02 13:11:25 -0800 | [diff] [blame] | 127 | sSfibInstaller = new FibInstaller(); |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 128 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 129 | sSfibInstaller.componentConfigService = createNiceMock(ComponentConfigService.class); |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 130 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 131 | ComponentContext mockContext = createNiceMock(ComponentContext.class); |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 132 | |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 133 | routerConfig = new TestRouterConfig(); |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 134 | interfaceService = createMock(InterfaceService.class); |
| 135 | |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 136 | networkConfigService = createMock(NetworkConfigService.class); |
Jonathan Hart | 25bd668 | 2016-06-03 10:45:47 -0700 | [diff] [blame] | 137 | networkConfigService.addListener(anyObject(NetworkConfigListener.class)); |
| 138 | expectLastCall().anyTimes(); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 139 | networkConfigRegistry = createMock(NetworkConfigRegistry.class); |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 140 | flowObjectiveService = createMock(FlowObjectiveService.class); |
gaurav | a2e61a5 | 2016-05-05 03:39:31 +0530 | [diff] [blame] | 141 | applicationService = createNiceMock(ApplicationService.class); |
| 142 | replay(applicationService); |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 143 | deviceService = new TestDeviceService(); |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 144 | CoreService coreService = createNiceMock(CoreService.class); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 145 | expect(coreService.registerApplication(anyString())).andReturn(APPID).anyTimes(); |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 146 | replay(coreService); |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 147 | |
| 148 | sSfibInstaller.networkConfigService = networkConfigService; |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 149 | sSfibInstaller.networkConfigRegistry = networkConfigRegistry; |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 150 | sSfibInstaller.interfaceService = interfaceService; |
| 151 | sSfibInstaller.flowObjectiveService = flowObjectiveService; |
gaurav | a2e61a5 | 2016-05-05 03:39:31 +0530 | [diff] [blame] | 152 | sSfibInstaller.applicationService = applicationService; |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 153 | sSfibInstaller.coreService = coreService; |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 154 | sSfibInstaller.routeService = new TestRouteService(); |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 155 | sSfibInstaller.deviceService = deviceService; |
| 156 | |
| 157 | setUpNetworkConfigService(); |
| 158 | setUpInterfaceService(); |
| 159 | sSfibInstaller.activate(mockContext); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Sets up InterfaceService. |
| 164 | */ |
| 165 | private void setUpInterfaceService() { |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 166 | interfaceService.addListener(anyObject(InterfaceListener.class)); |
| 167 | expectLastCall().andDelegateTo(new TestInterfaceService()); |
| 168 | |
| 169 | // Interface with no VLAN |
| 170 | Interface sw1Eth1 = new Interface("intf1", SW1_ETH1, |
| 171 | Collections.singletonList(INTF1), MAC1, VlanId.NONE); |
| 172 | expect(interfaceService.getMatchingInterface(NEXT_HOP1)).andReturn(sw1Eth1); |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 173 | interfaces.add(sw1Eth1); |
| 174 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 175 | // Interface with a VLAN |
| 176 | Interface sw2Eth1 = new Interface("intf2", SW1_ETH2, |
| 177 | Collections.singletonList(INTF2), MAC2, VLAN1); |
| 178 | expect(interfaceService.getMatchingInterface(NEXT_HOP2)).andReturn(sw2Eth1); |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 179 | interfaces.add(sw2Eth1); |
| 180 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 181 | expect(interfaceService.getInterfaces()).andReturn(interfaces); |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 182 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 183 | replay(interfaceService); |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /* |
| 187 | * Sets up NetworkConfigService. |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 188 | */ |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 189 | private void setUpNetworkConfigService() { |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 190 | expect(networkConfigService.getConfig( |
| 191 | anyObject(ApplicationId.class), eq(RoutingService.ROUTER_CONFIG_CLASS))). |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 192 | andReturn(routerConfig); |
Jonathan Hart | 249b4cf | 2017-02-03 18:02:58 -0800 | [diff] [blame] | 193 | expect(networkConfigService.getConfig(anyObject(ApplicationId.class), eq(RoutersConfig.class))) |
| 194 | .andReturn(null); |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 195 | replay(networkConfigService); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Sets up FlowObjectiveService. |
| 200 | */ |
| 201 | private void setUpFlowObjectiveService() { |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 202 | expect(flowObjectiveService.allocateNextId()).andReturn(NEXT_ID); |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 203 | replay(flowObjectiveService); |
| 204 | } |
| 205 | |
| 206 | /** |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 207 | * Creates a next objective with the given parameters. |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 208 | * |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 209 | * @param srcMac source MAC address |
| 210 | * @param dstMac destination MAC address |
| 211 | * @param port port number |
| 212 | * @param vlan vlan ID |
| 213 | * @param add whether to create an add objective or remove objective |
| 214 | * @return new next objective |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 215 | */ |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 216 | private NextObjective createNextObjective(MacAddress srcMac, |
| 217 | MacAddress dstMac, |
| 218 | PortNumber port, |
| 219 | VlanId vlan, |
| 220 | boolean add) { |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 221 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder() |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 222 | .setEthSrc(srcMac) |
| 223 | .setEthDst(dstMac); |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 224 | TrafficSelector.Builder metabuilder = null; |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 225 | if (!vlan.equals(VlanId.NONE)) { |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 226 | treatment.pushVlan() |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 227 | .setVlanId(vlan) |
| 228 | .setVlanPcp((byte) 0); |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 229 | } else { |
| 230 | metabuilder = DefaultTrafficSelector.builder(); |
Jonathan Hart | e732704 | 2017-02-02 13:11:25 -0800 | [diff] [blame] | 231 | metabuilder.matchVlanId(VlanId.vlanId(FibInstaller.ASSIGNED_VLAN)); |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 232 | } |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 233 | |
| 234 | treatment.setOutput(port); |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 235 | NextObjective.Builder nextBuilder = DefaultNextObjective.builder() |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 236 | .withId(NEXT_ID) |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 237 | .addTreatment(treatment.build()) |
| 238 | .withType(NextObjective.Type.SIMPLE) |
| 239 | .fromApp(APPID); |
| 240 | if (metabuilder != null) { |
| 241 | nextBuilder.withMeta(metabuilder.build()); |
| 242 | } |
| 243 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 244 | return add ? nextBuilder.add() : nextBuilder.remove(); |
| 245 | } |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 246 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 247 | /** |
| 248 | * Creates a new forwarding objective with the given parameters. |
| 249 | * |
| 250 | * @param prefix IP prefix |
| 251 | * @param add whether to create an add objective or a remove objective |
| 252 | * @return new forwarding objective |
| 253 | */ |
| 254 | private ForwardingObjective createForwardingObjective(IpPrefix prefix, |
| 255 | boolean add) { |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 256 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 257 | .matchEthType(Ethernet.TYPE_IPV4) |
| 258 | .matchIPDst(prefix) |
| 259 | .build(); |
| 260 | |
| 261 | int priority = prefix.prefixLength() * 5 + 100; |
| 262 | ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective.builder() |
| 263 | .fromApp(APPID) |
| 264 | .makePermanent() |
| 265 | .withSelector(selector) |
| 266 | .withPriority(priority) |
| 267 | .withFlag(ForwardingObjective.Flag.SPECIFIC); |
| 268 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 269 | if (add) { |
| 270 | fwdBuilder.nextStep(NEXT_ID); |
| 271 | } else { |
| 272 | fwdBuilder.withTreatment(DefaultTrafficTreatment.builder().build()); |
| 273 | } |
| 274 | |
| 275 | return add ? fwdBuilder.add() : fwdBuilder.remove(); |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Tests adding a route. |
| 280 | * |
| 281 | * We verify that the flowObjectiveService records the correct state and that the |
| 282 | * correct flow is submitted to the flowObjectiveService. |
| 283 | */ |
| 284 | @Test |
| 285 | public void testRouteAdd() { |
Charles Chan | 8fe9f4c | 2016-10-24 16:46:25 -0700 | [diff] [blame] | 286 | ResolvedRoute resolvedRoute = new ResolvedRoute(PREFIX1, NEXT_HOP1, MAC1, SW1_ETH1); |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 287 | |
| 288 | // Create the next objective |
| 289 | NextObjective nextObjective = createNextObjective(MAC1, MAC1, SW1_ETH1.port(), VlanId.NONE, true); |
| 290 | flowObjectiveService.next(DEVICE_ID, nextObjective); |
| 291 | |
| 292 | // Create the flow objective |
| 293 | ForwardingObjective fwd = createForwardingObjective(PREFIX1, true); |
| 294 | flowObjectiveService.forward(DEVICE_ID, fwd); |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 295 | EasyMock.expectLastCall().once(); |
| 296 | setUpFlowObjectiveService(); |
| 297 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 298 | // Send in the add event |
| 299 | RouteEvent routeEvent = new RouteEvent(RouteEvent.Type.ROUTE_ADDED, resolvedRoute); |
| 300 | routeListener.event(routeEvent); |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 301 | verify(flowObjectiveService); |
| 302 | } |
| 303 | |
| 304 | /** |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 305 | * Tests adding a route with to a next hop in a VLAN. |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 306 | * |
| 307 | * We verify that the flowObjectiveService records the correct state and that the |
| 308 | * correct flowObjectiveService is submitted to the flowObjectiveService. |
| 309 | */ |
| 310 | @Test |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 311 | public void testRouteAddWithVlan() { |
Charles Chan | 8fe9f4c | 2016-10-24 16:46:25 -0700 | [diff] [blame] | 312 | ResolvedRoute route = new ResolvedRoute(PREFIX1, NEXT_HOP2, MAC2, SW1_ETH2); |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 313 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 314 | // Create the next objective |
| 315 | NextObjective nextObjective = createNextObjective(MAC2, MAC2, SW1_ETH2.port(), VLAN1, true); |
| 316 | flowObjectiveService.next(DEVICE_ID, nextObjective); |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 317 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 318 | // Create the flow objective |
| 319 | ForwardingObjective fwd = createForwardingObjective(PREFIX1, true); |
| 320 | flowObjectiveService.forward(DEVICE_ID, fwd); |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 321 | EasyMock.expectLastCall().once(); |
| 322 | setUpFlowObjectiveService(); |
| 323 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 324 | // Send in the add event |
| 325 | routeListener.event(new RouteEvent(RouteEvent.Type.ROUTE_ADDED, route)); |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 326 | |
| 327 | verify(flowObjectiveService); |
| 328 | } |
| 329 | |
| 330 | /** |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 331 | * Tests updating a route. |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 332 | * |
| 333 | * We verify that the flowObjectiveService records the correct state and that the |
| 334 | * correct flow is submitted to the flowObjectiveService. |
| 335 | */ |
| 336 | @Test |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 337 | public void testRouteUpdate() { |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 338 | // Firstly add a route |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 339 | testRouteAdd(); |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 340 | reset(flowObjectiveService); |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 341 | |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 342 | ResolvedRoute oldRoute = new ResolvedRoute(PREFIX1, NEXT_HOP1, MAC1, SW1_ETH1); |
Charles Chan | 8fe9f4c | 2016-10-24 16:46:25 -0700 | [diff] [blame] | 343 | ResolvedRoute route = new ResolvedRoute(PREFIX1, NEXT_HOP2, MAC2, SW1_ETH2); |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 344 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 345 | // Create the next objective |
| 346 | NextObjective nextObjective = createNextObjective(MAC2, MAC2, SW1_ETH2.port(), VLAN1, true); |
| 347 | flowObjectiveService.next(DEVICE_ID, nextObjective); |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 348 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 349 | // Create the flow objective |
| 350 | ForwardingObjective fwd = createForwardingObjective(PREFIX1, true); |
| 351 | flowObjectiveService.forward(DEVICE_ID, fwd); |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 352 | EasyMock.expectLastCall().once(); |
| 353 | setUpFlowObjectiveService(); |
| 354 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 355 | // Send in the update event |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 356 | routeListener.event(new RouteEvent(RouteEvent.Type.ROUTE_UPDATED, route, oldRoute)); |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 357 | |
| 358 | verify(flowObjectiveService); |
| 359 | } |
| 360 | |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 361 | /** |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 362 | * Tests deleting a route. |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 363 | * |
| 364 | * We verify that the flowObjectiveService records the correct state and that the |
| 365 | * correct flow is withdrawn from the flowObjectiveService. |
| 366 | */ |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 367 | @Test |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 368 | public void testRouteDelete() { |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 369 | // Firstly add a route |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 370 | testRouteAdd(); |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 371 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 372 | // Construct the existing route |
Charles Chan | e4d1310 | 2016-11-08 15:38:44 -0800 | [diff] [blame] | 373 | ResolvedRoute route = new ResolvedRoute(PREFIX1, NEXT_HOP1, MAC1, SW1_ETH1); |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 374 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 375 | // Create the flow objective |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 376 | reset(flowObjectiveService); |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 377 | ForwardingObjective fwd = createForwardingObjective(PREFIX1, false); |
| 378 | flowObjectiveService.forward(DEVICE_ID, fwd); |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 379 | replay(flowObjectiveService); |
| 380 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 381 | // Send in the delete event |
| 382 | routeListener.event(new RouteEvent(RouteEvent.Type.ROUTE_REMOVED, route)); |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 383 | |
| 384 | verify(flowObjectiveService); |
| 385 | } |
| 386 | |
gaurav | 164cf6d | 2016-03-25 21:43:04 +0530 | [diff] [blame] | 387 | private class TestInterfaceService extends InterfaceServiceAdapter { |
gaurav | 164cf6d | 2016-03-25 21:43:04 +0530 | [diff] [blame] | 388 | @Override |
| 389 | public void addListener(InterfaceListener listener) { |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 390 | interfaceListener = listener; |
gaurav | 164cf6d | 2016-03-25 21:43:04 +0530 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 394 | private class TestRouteService extends RouteServiceAdapter { |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 395 | @Override |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 396 | public void addListener(RouteListener listener) { |
Jonathan Hart | e732704 | 2017-02-02 13:11:25 -0800 | [diff] [blame] | 397 | FibInstallerTest.this.routeListener = listener; |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | |
| 401 | private class TestRouterConfig extends RouterConfig { |
| 402 | |
| 403 | @Override |
| 404 | public List<String> getInterfaces() { |
| 405 | ArrayList<String> interfaces = new ArrayList<>(); |
| 406 | interfaces.add("of:0000000000000001/1"); |
Jonathan Hart | a2eb9ff | 2016-04-13 21:27:06 -0700 | [diff] [blame] | 407 | interfaces.add("of:0000000000000001/2"); |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 408 | return interfaces; |
| 409 | } |
| 410 | |
| 411 | @Override |
| 412 | public ConnectPoint getControlPlaneConnectPoint() { |
| 413 | return SW1_ETH1; |
| 414 | } |
| 415 | |
| 416 | @Override |
| 417 | public boolean getOspfEnabled() { |
| 418 | return true; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | private class TestDeviceService extends DeviceServiceAdapter { |
| 423 | |
| 424 | @Override |
| 425 | public boolean isAvailable(DeviceId deviceId) { |
| 426 | return true; |
| 427 | } |
| 428 | |
| 429 | @Override |
| 430 | public void addListener(DeviceListener listener) { |
Jonathan Hart | e732704 | 2017-02-02 13:11:25 -0800 | [diff] [blame] | 431 | FibInstallerTest.this.deviceListener = listener; |
gaurav | 3678593 | 2016-03-10 17:24:04 +0530 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | } |