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