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