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