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