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