kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
kishore | db79dc0 | 2016-03-07 16:24:55 +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; |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 19 | import org.easymock.EasyMock; |
| 20 | import org.junit.Before; |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame^] | 21 | import org.junit.Ignore; |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 22 | import org.junit.Test; |
| 23 | import org.onlab.packet.EthType; |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 24 | import org.onlab.packet.IpAddress; |
| 25 | import org.onlab.packet.IpPrefix; |
| 26 | import org.onlab.packet.MacAddress; |
| 27 | import org.onlab.packet.VlanId; |
kishore | 786b7e4 | 2016-05-19 16:25:57 +0530 | [diff] [blame] | 28 | import org.onosproject.app.ApplicationService; |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 29 | import org.onosproject.core.ApplicationId; |
| 30 | import org.onosproject.core.CoreService; |
| 31 | import org.onosproject.core.CoreServiceAdapter; |
| 32 | import org.onosproject.incubator.net.intf.Interface; |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 33 | import org.onosproject.incubator.net.intf.InterfaceEvent; |
| 34 | import org.onosproject.incubator.net.intf.InterfaceListener; |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 35 | import org.onosproject.incubator.net.intf.InterfaceService; |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 36 | import org.onosproject.incubator.net.intf.InterfaceServiceAdapter; |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 37 | import org.onosproject.mastership.MastershipService; |
| 38 | import org.onosproject.mastership.MastershipServiceAdapter; |
| 39 | import org.onosproject.net.ConnectPoint; |
| 40 | import org.onosproject.net.Device; |
| 41 | import org.onosproject.net.DeviceId; |
| 42 | import org.onosproject.net.NetTestTools; |
| 43 | import org.onosproject.net.PortNumber; |
| 44 | import org.onosproject.net.config.Config; |
| 45 | import org.onosproject.net.config.NetworkConfigEvent; |
| 46 | import org.onosproject.net.config.NetworkConfigEvent.Type; |
| 47 | import org.onosproject.net.config.NetworkConfigListener; |
| 48 | import org.onosproject.net.config.NetworkConfigService; |
| 49 | import org.onosproject.net.config.NetworkConfigServiceAdapter; |
| 50 | import org.onosproject.net.device.DeviceEvent; |
| 51 | import org.onosproject.net.device.DeviceListener; |
| 52 | import org.onosproject.net.device.DeviceService; |
| 53 | import org.onosproject.net.device.DeviceServiceAdapter; |
| 54 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 55 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 56 | import org.onosproject.net.flow.TrafficSelector; |
| 57 | import org.onosproject.net.flow.TrafficTreatment; |
| 58 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 59 | import org.onosproject.net.flowobjective.DefaultNextObjective; |
| 60 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 61 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 62 | import org.onosproject.net.flowobjective.NextObjective; |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 63 | import org.onosproject.net.host.HostService; |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 64 | import org.onosproject.net.host.InterfaceIpAddress; |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 65 | import org.onosproject.routing.RoutingService; |
| 66 | import org.onosproject.routing.config.RouterConfig; |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 67 | |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 68 | import java.util.ArrayList; |
| 69 | import java.util.HashSet; |
| 70 | import java.util.List; |
| 71 | import java.util.Objects; |
| 72 | import java.util.Set; |
| 73 | |
| 74 | import static org.easymock.EasyMock.createMock; |
| 75 | import static org.easymock.EasyMock.createNiceMock; |
| 76 | import static org.easymock.EasyMock.expect; |
| 77 | import static org.easymock.EasyMock.expectLastCall; |
| 78 | import static org.easymock.EasyMock.replay; |
| 79 | import static org.easymock.EasyMock.verify; |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 80 | import static org.onlab.packet.ICMP6.NEIGHBOR_ADVERTISEMENT; |
| 81 | import static org.onlab.packet.ICMP6.NEIGHBOR_SOLICITATION; |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame^] | 82 | import static org.onosproject.routing.impl.ControlPlaneRedirectManager.ACL_PRIORITY; |
| 83 | import static org.onosproject.routing.impl.ControlPlaneRedirectManager.buildArpSelector; |
| 84 | import static org.onosproject.routing.impl.ControlPlaneRedirectManager.buildIPDstSelector; |
| 85 | import static org.onosproject.routing.impl.ControlPlaneRedirectManager.buildIPSrcSelector; |
| 86 | import static org.onosproject.routing.impl.ControlPlaneRedirectManager.buildNdpSelector; |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 87 | |
| 88 | /** |
| 89 | * UnitTests for ControlPlaneRedirectManager. |
| 90 | */ |
Jonathan Hart | f8cd052 | 2016-10-25 07:09:55 -0700 | [diff] [blame^] | 91 | @Ignore("Too many dependencies on internal implementation, too hard to maintain") |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 92 | public class ControlPlaneRedirectManagerTest { |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 93 | |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 94 | private DeviceService deviceService; |
| 95 | private FlowObjectiveService flowObjectiveService; |
| 96 | private NetworkConfigService networkConfigService; |
| 97 | private final Set<Interface> interfaces = Sets.newHashSet(); |
| 98 | static Device dev3 = NetTestTools.device("0000000000000001"); |
| 99 | private static final int OSPF_IP_PROTO = 0x59; |
| 100 | private CoreService coreService = new TestCoreService(); |
| 101 | private InterfaceService interfaceService; |
kishore | 786b7e4 | 2016-05-19 16:25:57 +0530 | [diff] [blame] | 102 | private static final ApplicationId APPID = TestApplicationId.create("org.onosproject.vrouter"); |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 103 | |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 104 | private static final DeviceId DEVICE_ID = DeviceId.deviceId("of:0000000000000001"); |
| 105 | |
| 106 | private ConnectPoint controlPlaneConnectPoint = new ConnectPoint(DEVICE_ID, |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 107 | PortNumber.portNumber(1)); |
| 108 | |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 109 | private static final ConnectPoint SW1_ETH1 = new ConnectPoint(DEVICE_ID, |
| 110 | PortNumber.portNumber(1)); |
| 111 | |
| 112 | private static final ConnectPoint SW1_ETH2 = new ConnectPoint(DEVICE_ID, |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 113 | PortNumber.portNumber(2)); |
| 114 | |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 115 | private static final ConnectPoint SW1_ETH3 = new ConnectPoint(DEVICE_ID, |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 116 | PortNumber.portNumber(3)); |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 117 | |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 118 | private ControlPlaneRedirectManager controlPlaneRedirectManager = new ControlPlaneRedirectManager(); |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 119 | private RouterConfig routerConfig = new TestRouterConfig(); |
| 120 | private NetworkConfigListener networkConfigListener; |
| 121 | private DeviceListener deviceListener; |
| 122 | private MastershipService mastershipService = new InternalMastershipServiceTest(); |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 123 | private InterfaceListener interfaceListener; |
kishore | 786b7e4 | 2016-05-19 16:25:57 +0530 | [diff] [blame] | 124 | private ApplicationService applicationService; |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 125 | |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 126 | @Before |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 127 | public void setUp() { |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 128 | networkConfigListener = createMock(NetworkConfigListener.class); |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 129 | deviceService = new TestDeviceService(); |
| 130 | deviceListener = createMock(DeviceListener.class); |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 131 | |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 132 | interfaceListener = createMock(InterfaceListener.class); |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 133 | deviceService.addListener(deviceListener); |
| 134 | setUpInterfaceService(); |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 135 | interfaceService = new InternalInterfaceService(); |
| 136 | interfaceService.addListener(interfaceListener); |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 137 | networkConfigService = new TestNetworkConfigService(); |
| 138 | networkConfigService.addListener(networkConfigListener); |
| 139 | flowObjectiveService = createMock(FlowObjectiveService.class); |
kishore | 786b7e4 | 2016-05-19 16:25:57 +0530 | [diff] [blame] | 140 | applicationService = createNiceMock(ApplicationService.class); |
| 141 | replay(applicationService); |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 142 | setUpFlowObjectiveService(); |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 143 | controlPlaneRedirectManager.coreService = coreService; |
| 144 | controlPlaneRedirectManager.flowObjectiveService = flowObjectiveService; |
| 145 | controlPlaneRedirectManager.networkConfigService = networkConfigService; |
| 146 | controlPlaneRedirectManager.interfaceService = interfaceService; |
| 147 | controlPlaneRedirectManager.deviceService = deviceService; |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 148 | controlPlaneRedirectManager.hostService = createNiceMock(HostService.class); |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 149 | controlPlaneRedirectManager.mastershipService = mastershipService; |
kishore | 786b7e4 | 2016-05-19 16:25:57 +0530 | [diff] [blame] | 150 | controlPlaneRedirectManager.applicationService = applicationService; |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 151 | controlPlaneRedirectManager.activate(); |
| 152 | verify(flowObjectiveService); |
| 153 | } |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 154 | |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 155 | /** |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 156 | * Tests adding new Device to a openflow router. |
| 157 | */ |
| 158 | @Test |
| 159 | public void testAddDevice() { |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 160 | ConnectPoint sw1eth4 = new ConnectPoint(DEVICE_ID, PortNumber.portNumber(4)); |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 161 | List<InterfaceIpAddress> interfaceIpAddresses = new ArrayList<>(); |
| 162 | interfaceIpAddresses.add( |
| 163 | new InterfaceIpAddress(IpAddress.valueOf("192.168.40.101"), IpPrefix.valueOf("192.168.40.0/24")) |
| 164 | ); |
| 165 | interfaceIpAddresses.add( |
| 166 | new InterfaceIpAddress(IpAddress.valueOf("2000::ff"), IpPrefix.valueOf("2000::ff/120")) |
| 167 | ); |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 168 | |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 169 | Interface sw1Eth4 = new Interface(sw1eth4.deviceId().toString(), sw1eth4, interfaceIpAddresses, |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 170 | MacAddress.valueOf("00:00:00:00:00:04"), VlanId.NONE); |
| 171 | interfaces.add(sw1Eth4); |
| 172 | EasyMock.reset(flowObjectiveService); |
| 173 | setUpFlowObjectiveService(); |
| 174 | deviceListener.event(new DeviceEvent(DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED, dev3)); |
| 175 | verify(flowObjectiveService); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Tests adding while updating the networkConfig. |
| 180 | */ |
| 181 | @Test |
| 182 | public void testUpdateNetworkConfig() { |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 183 | ConnectPoint sw1eth4 = new ConnectPoint(DEVICE_ID, PortNumber.portNumber(4)); |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 184 | List<InterfaceIpAddress> interfaceIpAddresses = new ArrayList<>(); |
| 185 | interfaceIpAddresses.add( |
| 186 | new InterfaceIpAddress(IpAddress.valueOf("192.168.40.101"), IpPrefix.valueOf("192.168.40.0/24")) |
| 187 | ); |
| 188 | interfaceIpAddresses.add( |
| 189 | new InterfaceIpAddress(IpAddress.valueOf("2000::ff"), IpPrefix.valueOf("2000::ff/120")) |
| 190 | ); |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 191 | |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 192 | Interface sw1Eth4 = new Interface(sw1eth4.deviceId().toString(), sw1eth4, interfaceIpAddresses, |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 193 | MacAddress.valueOf("00:00:00:00:00:04"), VlanId.NONE); |
| 194 | interfaces.add(sw1Eth4); |
| 195 | EasyMock.reset(flowObjectiveService); |
| 196 | setUpFlowObjectiveService(); |
| 197 | networkConfigListener |
| 198 | .event(new NetworkConfigEvent(Type.CONFIG_UPDATED, dev3, RoutingService.ROUTER_CONFIG_CLASS)); |
| 199 | networkConfigService.addListener(networkConfigListener); |
| 200 | verify(flowObjectiveService); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Tests adding while updating the networkConfig. |
| 205 | */ |
| 206 | @Test |
| 207 | public void testAddInterface() { |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 208 | ConnectPoint sw1eth4 = new ConnectPoint(DEVICE_ID, PortNumber.portNumber(4)); |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 209 | List<InterfaceIpAddress> interfaceIpAddresses = new ArrayList<>(); |
| 210 | interfaceIpAddresses.add( |
| 211 | new InterfaceIpAddress(IpAddress.valueOf("192.168.40.101"), IpPrefix.valueOf("192.168.40.0/24")) |
| 212 | ); |
| 213 | interfaceIpAddresses.add( |
| 214 | new InterfaceIpAddress(IpAddress.valueOf("2000::ff"), IpPrefix.valueOf("2000::ff/120")) |
| 215 | ); |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 216 | |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 217 | Interface sw1Eth4 = new Interface(sw1eth4.deviceId().toString(), sw1eth4, interfaceIpAddresses, |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 218 | MacAddress.valueOf("00:00:00:00:00:04"), VlanId.NONE); |
| 219 | interfaces.add(sw1Eth4); |
| 220 | |
| 221 | EasyMock.reset(flowObjectiveService); |
| 222 | expect(flowObjectiveService.allocateNextId()).andReturn(1).anyTimes(); |
| 223 | |
| 224 | setUpInterfaceConfiguration(sw1Eth4, true); |
| 225 | replay(flowObjectiveService); |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 226 | interfaceListener.event(new InterfaceEvent(InterfaceEvent.Type.INTERFACE_ADDED, sw1Eth4, 500L)); |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 227 | verify(flowObjectiveService); |
| 228 | } |
| 229 | |
| 230 | @Test |
| 231 | public void testRemoveInterface() { |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 232 | ConnectPoint sw1eth4 = new ConnectPoint(DEVICE_ID, PortNumber.portNumber(4)); |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 233 | List<InterfaceIpAddress> interfaceIpAddresses = new ArrayList<>(); |
| 234 | interfaceIpAddresses.add( |
| 235 | new InterfaceIpAddress(IpAddress.valueOf("192.168.40.101"), IpPrefix.valueOf("192.168.40.0/24")) |
| 236 | ); |
| 237 | interfaceIpAddresses.add( |
| 238 | new InterfaceIpAddress(IpAddress.valueOf("2000::ff"), IpPrefix.valueOf("2000::ff/120")) |
| 239 | ); |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 240 | |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 241 | Interface sw1Eth4 = new Interface(sw1eth4.deviceId().toString(), sw1eth4, interfaceIpAddresses, |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 242 | MacAddress.valueOf("00:00:00:00:00:04"), VlanId.NONE); |
| 243 | EasyMock.reset(flowObjectiveService); |
| 244 | expect(flowObjectiveService.allocateNextId()).andReturn(1).anyTimes(); |
| 245 | |
| 246 | setUpInterfaceConfiguration(sw1Eth4, false); |
| 247 | replay(flowObjectiveService); |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 248 | interfaceListener.event(new InterfaceEvent(InterfaceEvent.Type.INTERFACE_REMOVED, sw1Eth4, 500L)); |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 249 | verify(flowObjectiveService); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Setup flow Configuration for all configured Interfaces. |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 254 | * |
| 255 | **/ |
| 256 | private void setUpFlowObjectiveService() { |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 257 | expect(flowObjectiveService.allocateNextId()).andReturn(1).anyTimes(); |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 258 | for (Interface intf : interfaceService.getInterfaces()) { |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 259 | setUpInterfaceConfiguration(intf, true); |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 260 | } |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 261 | replay(flowObjectiveService); |
| 262 | } |
| 263 | |
| 264 | /** |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 265 | * Setting up flowobjective expectations for basic forwarding and ospf. |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 266 | **/ |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 267 | private void setUpInterfaceConfiguration(Interface intf, boolean install) { |
| 268 | DeviceId deviceId = controlPlaneConnectPoint.deviceId(); |
| 269 | PortNumber controlPlanePort = controlPlaneConnectPoint.port(); |
| 270 | |
| 271 | for (InterfaceIpAddress ip : intf.ipAddresses()) { |
| 272 | int cpNextId, intfNextId; |
| 273 | |
| 274 | cpNextId = modifyNextObjective(deviceId, controlPlanePort, |
| 275 | VlanId.vlanId(SingleSwitchFibInstaller.ASSIGNED_VLAN), true, install); |
| 276 | intfNextId = modifyNextObjective(deviceId, intf.connectPoint().port(), |
| 277 | VlanId.vlanId(SingleSwitchFibInstaller.ASSIGNED_VLAN), true, install); |
| 278 | |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 279 | // IP to router |
| 280 | TrafficSelector toSelector = buildIPDstSelector( |
| 281 | ip.ipAddress().toIpPrefix(), |
| 282 | intf.connectPoint().port(), |
| 283 | null, |
| 284 | intf.mac(), |
| 285 | intf.vlan()); |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 286 | |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 287 | flowObjectiveService.forward(deviceId, buildForwardingObjective(toSelector, null, |
| 288 | cpNextId, install, ACL_PRIORITY)); |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 289 | expectLastCall().once(); |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 290 | |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 291 | // IP from router |
| 292 | TrafficSelector fromSelector = buildIPSrcSelector( |
| 293 | ip.ipAddress().toIpPrefix(), |
| 294 | controlPlanePort, |
| 295 | intf.mac(), |
| 296 | null, |
| 297 | intf.vlan() |
| 298 | ); |
| 299 | |
| 300 | flowObjectiveService.forward(deviceId, buildForwardingObjective(fromSelector, null, |
| 301 | intfNextId, install, ACL_PRIORITY)); |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 302 | expectLastCall().once(); |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 303 | |
| 304 | TrafficTreatment puntTreatment = DefaultTrafficTreatment.builder().punt().build(); |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 305 | if (ip.ipAddress().isIp4()) { |
| 306 | // ARP to router |
| 307 | toSelector = buildArpSelector( |
| 308 | intf.connectPoint().port(), |
| 309 | intf.vlan(), |
| 310 | null, |
| 311 | null |
| 312 | ); |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 313 | |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 314 | flowObjectiveService.forward(deviceId, buildForwardingObjective(toSelector, puntTreatment, |
| 315 | cpNextId, install, ACL_PRIORITY + 1)); |
| 316 | expectLastCall().once(); |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 317 | |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 318 | // ARP from router |
| 319 | fromSelector = buildArpSelector( |
| 320 | controlPlanePort, |
| 321 | intf.vlan(), |
| 322 | ip.ipAddress().getIp4Address(), |
| 323 | intf.mac() |
| 324 | ); |
| 325 | |
| 326 | flowObjectiveService.forward(deviceId, |
| 327 | buildForwardingObjective(fromSelector, puntTreatment, |
| 328 | intfNextId, install, ACL_PRIORITY + 1)); |
| 329 | expectLastCall().once(); |
| 330 | } else { |
| 331 | // NDP solicitation to router |
| 332 | toSelector = buildNdpSelector( |
| 333 | intf.connectPoint().port(), |
| 334 | intf.vlan(), |
| 335 | null, |
| 336 | NEIGHBOR_SOLICITATION, |
| 337 | null |
| 338 | ); |
| 339 | |
| 340 | flowObjectiveService.forward(deviceId, buildForwardingObjective(toSelector, puntTreatment, |
| 341 | cpNextId, install, ACL_PRIORITY + 1)); |
| 342 | expectLastCall().once(); |
| 343 | |
| 344 | // NDP solicitation from router |
| 345 | fromSelector = buildNdpSelector( |
| 346 | controlPlanePort, |
| 347 | intf.vlan(), |
| 348 | ip.ipAddress().toIpPrefix(), |
| 349 | NEIGHBOR_SOLICITATION, |
| 350 | intf.mac() |
| 351 | ); |
| 352 | |
| 353 | flowObjectiveService.forward(deviceId, |
| 354 | buildForwardingObjective(fromSelector, puntTreatment, |
| 355 | intfNextId, install, ACL_PRIORITY + 1)); |
| 356 | |
| 357 | // NDP advertisement to router |
| 358 | toSelector = buildNdpSelector( |
| 359 | intf.connectPoint().port(), |
| 360 | intf.vlan(), |
| 361 | null, |
| 362 | NEIGHBOR_ADVERTISEMENT, |
| 363 | null |
| 364 | ); |
| 365 | |
| 366 | flowObjectiveService.forward(deviceId, buildForwardingObjective(toSelector, puntTreatment, |
| 367 | cpNextId, install, ACL_PRIORITY + 1)); |
| 368 | expectLastCall().once(); |
| 369 | |
| 370 | // NDP advertisement from router |
| 371 | fromSelector = buildNdpSelector( |
| 372 | controlPlanePort, |
| 373 | intf.vlan(), |
| 374 | ip.ipAddress().toIpPrefix(), |
| 375 | NEIGHBOR_ADVERTISEMENT, |
| 376 | intf.mac() |
| 377 | ); |
| 378 | |
| 379 | flowObjectiveService.forward(deviceId, |
| 380 | buildForwardingObjective(fromSelector, puntTreatment, |
| 381 | intfNextId, install, ACL_PRIORITY + 1)); |
| 382 | } |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 383 | } |
| 384 | // setting expectations for ospf forwarding. |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 385 | TrafficSelector toSelector = DefaultTrafficSelector.builder().matchInPort(intf.connectPoint().port()) |
| 386 | .matchEthType(EthType.EtherType.IPV4.ethType().toShort()).matchVlanId(intf.vlan()) |
| 387 | .matchIPProtocol((byte) OSPF_IP_PROTO).build(); |
| 388 | |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 389 | modifyNextObjective(deviceId, controlPlanePort, VlanId.vlanId((short) 4094), true, install); |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 390 | flowObjectiveService.forward(controlPlaneConnectPoint.deviceId(), |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 391 | buildForwardingObjective(toSelector, null, 1, install, 40001)); |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 392 | expectLastCall().once(); |
| 393 | } |
| 394 | |
| 395 | /** |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 396 | * Setup expectations on flowObjectiveService.next for NextObjective. |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 397 | * |
| 398 | **/ |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 399 | private int modifyNextObjective(DeviceId deviceId, PortNumber portNumber, VlanId vlanId, boolean popVlan, |
| 400 | boolean modifyFlag) { |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 401 | NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder().withId(1) |
| 402 | .withType(NextObjective.Type.SIMPLE).fromApp(APPID); |
| 403 | |
| 404 | TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder(); |
| 405 | if (popVlan) { |
| 406 | ttBuilder.popVlan(); |
| 407 | } |
| 408 | ttBuilder.setOutput(portNumber); |
| 409 | |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 410 | TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder(); |
| 411 | metabuilder.matchVlanId(vlanId); |
| 412 | |
| 413 | nextObjBuilder.withMeta(metabuilder.build()); |
| 414 | nextObjBuilder.addTreatment(ttBuilder.build()); |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 415 | if (modifyFlag) { |
| 416 | flowObjectiveService.next(deviceId, nextObjBuilder.add()); |
| 417 | expectLastCall().once(); |
| 418 | } else { |
| 419 | flowObjectiveService.next(deviceId, nextObjBuilder.remove()); |
| 420 | expectLastCall().once(); |
| 421 | } |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 422 | return 1; |
| 423 | } |
| 424 | |
| 425 | /** |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 426 | * Setup Interface expectation for all Testcases. |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 427 | **/ |
| 428 | private void setUpInterfaceService() { |
Sho SHIMIZU | 891162b | 2016-06-06 16:32:01 -0700 | [diff] [blame] | 429 | List<InterfaceIpAddress> interfaceIpAddresses1 = new ArrayList<>(); |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 430 | interfaceIpAddresses1 |
| 431 | .add(new InterfaceIpAddress(IpAddress.valueOf("192.168.10.101"), IpPrefix.valueOf("192.168.10.0/24"))); |
| 432 | Interface sw1Eth1 = new Interface(SW1_ETH1.deviceId().toString(), SW1_ETH1, interfaceIpAddresses1, |
| 433 | MacAddress.valueOf("00:00:00:00:00:01"), VlanId.NONE); |
| 434 | interfaces.add(sw1Eth1); |
| 435 | |
Sho SHIMIZU | 891162b | 2016-06-06 16:32:01 -0700 | [diff] [blame] | 436 | List<InterfaceIpAddress> interfaceIpAddresses2 = new ArrayList<>(); |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 437 | interfaceIpAddresses2 |
| 438 | .add(new InterfaceIpAddress(IpAddress.valueOf("192.168.20.101"), IpPrefix.valueOf("192.168.20.0/24"))); |
| 439 | Interface sw1Eth2 = new Interface(SW1_ETH1.deviceId().toString(), SW1_ETH2, interfaceIpAddresses2, |
| 440 | MacAddress.valueOf("00:00:00:00:00:02"), VlanId.NONE); |
| 441 | interfaces.add(sw1Eth2); |
| 442 | |
Sho SHIMIZU | 891162b | 2016-06-06 16:32:01 -0700 | [diff] [blame] | 443 | List<InterfaceIpAddress> interfaceIpAddresses3 = new ArrayList<>(); |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 444 | interfaceIpAddresses3 |
| 445 | .add(new InterfaceIpAddress(IpAddress.valueOf("192.168.30.101"), IpPrefix.valueOf("192.168.30.0/24"))); |
| 446 | Interface sw1Eth3 = new Interface(SW1_ETH1.deviceId().toString(), SW1_ETH3, interfaceIpAddresses3, |
| 447 | MacAddress.valueOf("00:00:00:00:00:03"), VlanId.NONE); |
| 448 | interfaces.add(sw1Eth3); |
| 449 | |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 450 | } |
| 451 | |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 452 | private ForwardingObjective buildForwardingObjective(TrafficSelector selector, TrafficTreatment treatment, |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 453 | int nextId, boolean add, int priority) { |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 454 | DefaultForwardingObjective.Builder fobBuilder = DefaultForwardingObjective.builder(); |
| 455 | fobBuilder.withSelector(selector); |
| 456 | if (treatment != null) { |
| 457 | fobBuilder.withTreatment(treatment); |
| 458 | } |
| 459 | if (nextId != -1) { |
| 460 | fobBuilder.nextStep(nextId); |
| 461 | } |
Vinayak Tejankar | 3a409c6 | 2017-01-12 02:20:53 +0530 | [diff] [blame] | 462 | fobBuilder.fromApp(APPID).withPriority(priority).withFlag(ForwardingObjective.Flag.VERSATILE); |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 463 | |
| 464 | return add ? fobBuilder.add() : fobBuilder.remove(); |
| 465 | } |
| 466 | |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 467 | private class TestCoreService extends CoreServiceAdapter { |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 468 | |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 469 | @Override |
| 470 | public ApplicationId getAppId(String name) { |
| 471 | return APPID; |
| 472 | } |
| 473 | |
| 474 | @Override |
| 475 | public ApplicationId registerApplication(String name) { |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 476 | return new TestApplicationId(name); |
| 477 | } |
| 478 | |
| 479 | } |
| 480 | |
| 481 | private class TestDeviceService extends DeviceServiceAdapter { |
| 482 | |
| 483 | @Override |
| 484 | public boolean isAvailable(DeviceId deviceId) { |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 485 | boolean flag = false; |
| 486 | if (deviceId.equals(controlPlaneConnectPoint.deviceId())) { |
| 487 | flag = true; |
| 488 | } |
| 489 | return flag; |
| 490 | } |
| 491 | |
| 492 | @Override |
| 493 | public void addListener(DeviceListener listener) { |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 494 | ControlPlaneRedirectManagerTest.this.deviceListener = listener; |
| 495 | } |
| 496 | |
| 497 | } |
| 498 | |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 499 | private class TestRouterConfig extends RouterConfig { |
| 500 | |
| 501 | @Override |
| 502 | public ConnectPoint getControlPlaneConnectPoint() { |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 503 | return controlPlaneConnectPoint; |
| 504 | } |
| 505 | |
| 506 | @Override |
| 507 | public boolean getOspfEnabled() { |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 508 | return true; |
| 509 | } |
| 510 | |
| 511 | @Override |
| 512 | public List<String> getInterfaces() { |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 513 | ArrayList<String> interfaces = new ArrayList<>(); |
| 514 | interfaces.add("of:0000000000000001"); |
| 515 | interfaces.add("of:0000000000000001/2"); |
| 516 | interfaces.add("of:0000000000000001/3"); |
| 517 | return interfaces; |
| 518 | } |
| 519 | |
| 520 | } |
| 521 | |
| 522 | private class TestNetworkConfigService extends NetworkConfigServiceAdapter { |
| 523 | |
| 524 | @Override |
| 525 | public void addListener(NetworkConfigListener listener) { |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 526 | ControlPlaneRedirectManagerTest.this.networkConfigListener = listener; |
| 527 | } |
| 528 | |
| 529 | @Override |
| 530 | public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) { |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 531 | return (C) ControlPlaneRedirectManagerTest.this.routerConfig; |
| 532 | } |
| 533 | |
| 534 | } |
| 535 | |
| 536 | private static class TestApplicationId implements ApplicationId { |
| 537 | |
| 538 | private final String name; |
| 539 | private final short id; |
| 540 | |
| 541 | public TestApplicationId(String name) { |
| 542 | this.name = name; |
| 543 | this.id = (short) Objects.hash(name); |
| 544 | } |
| 545 | |
| 546 | public static ApplicationId create(String name) { |
| 547 | return new TestApplicationId(name); |
| 548 | } |
| 549 | |
| 550 | @Override |
| 551 | public short id() { |
| 552 | return id; |
| 553 | } |
| 554 | |
| 555 | @Override |
| 556 | public String name() { |
| 557 | return name; |
| 558 | } |
| 559 | |
| 560 | @Override |
| 561 | public int hashCode() { |
| 562 | final int prime = 31; |
| 563 | int result = 1; |
| 564 | result = prime * result + id; |
| 565 | result = prime * result + ((name == null) ? 0 : name.hashCode()); |
| 566 | return result; |
| 567 | } |
| 568 | |
| 569 | @Override |
| 570 | public boolean equals(Object obj) { |
| 571 | if (this == obj) { |
| 572 | return true; |
| 573 | } |
| 574 | if (obj == null) { |
| 575 | return false; |
| 576 | } |
| 577 | if (getClass() != obj.getClass()) { |
| 578 | return false; |
| 579 | } |
| 580 | TestApplicationId other = (TestApplicationId) obj; |
| 581 | if (id != other.id) { |
| 582 | return false; |
| 583 | } |
| 584 | if (name == null) { |
| 585 | if (other.name != null) { |
| 586 | return false; |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 587 | } |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 588 | } else if (!name.equals(other.name)) { |
| 589 | return false; |
| 590 | } |
| 591 | return true; |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | private class InternalMastershipServiceTest extends MastershipServiceAdapter { |
| 596 | |
| 597 | @Override |
| 598 | public boolean isLocalMaster(DeviceId deviceId) { |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 599 | boolean flag = deviceId.equals(controlPlaneConnectPoint.deviceId()); |
| 600 | return flag; |
| 601 | } |
| 602 | |
| 603 | } |
kishore | 71a2753 | 2016-03-16 20:23:49 +0530 | [diff] [blame] | 604 | |
| 605 | private class InternalInterfaceService extends InterfaceServiceAdapter { |
| 606 | |
| 607 | @Override |
| 608 | public void addListener(InterfaceListener listener) { |
| 609 | ControlPlaneRedirectManagerTest.this.interfaceListener = listener; |
| 610 | } |
| 611 | |
| 612 | @Override |
| 613 | public Set<Interface> getInterfaces() { |
| 614 | return interfaces; |
| 615 | } |
| 616 | |
| 617 | @Override |
| 618 | public Set<Interface> getInterfacesByPort(ConnectPoint port) { |
| 619 | Set<Interface> setIntf = new HashSet<Interface>(); |
| 620 | for (Interface intf : interfaces) { |
| 621 | if (intf.connectPoint().equals(port)) { |
| 622 | setIntf.add(intf); |
| 623 | } |
| 624 | } |
| 625 | return setIntf; |
| 626 | } |
| 627 | |
| 628 | @Override |
| 629 | public Interface getMatchingInterface(IpAddress ip) { |
| 630 | Interface intff = null; |
| 631 | for (Interface intf : interfaces) { |
| 632 | for (InterfaceIpAddress address : intf.ipAddresses()) { |
| 633 | if (address.ipAddress().equals(ip)) { |
| 634 | intff = intf; |
| 635 | break; |
| 636 | } |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | return intff; |
| 641 | } |
| 642 | |
| 643 | } |
kishore | db79dc0 | 2016-03-07 16:24:55 +0530 | [diff] [blame] | 644 | } |