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