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