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