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