blob: 8f61ddb9bd475a3add6c09aef120244c69366e92 [file] [log] [blame]
kishoredb79dc02016-03-07 16:24:55 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
kishoredb79dc02016-03-07 16:24:55 +05303 *
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 */
16package org.onosproject.routing.impl;
17
Jonathan Hartf04b7d92016-03-29 09:39:11 -070018import com.google.common.collect.Sets;
kishoredb79dc02016-03-07 16:24:55 +053019import org.easymock.EasyMock;
20import org.junit.Before;
21import org.junit.Test;
22import org.onlab.packet.EthType;
kishoredb79dc02016-03-07 16:24:55 +053023import org.onlab.packet.IpAddress;
24import org.onlab.packet.IpPrefix;
25import org.onlab.packet.MacAddress;
26import org.onlab.packet.VlanId;
27import org.onosproject.core.ApplicationId;
28import org.onosproject.core.CoreService;
29import org.onosproject.core.CoreServiceAdapter;
30import org.onosproject.incubator.net.intf.Interface;
kishore71a27532016-03-16 20:23:49 +053031import org.onosproject.incubator.net.intf.InterfaceEvent;
32import org.onosproject.incubator.net.intf.InterfaceListener;
kishoredb79dc02016-03-07 16:24:55 +053033import org.onosproject.incubator.net.intf.InterfaceService;
kishore71a27532016-03-16 20:23:49 +053034import org.onosproject.incubator.net.intf.InterfaceServiceAdapter;
kishoredb79dc02016-03-07 16:24:55 +053035import org.onosproject.mastership.MastershipService;
36import org.onosproject.mastership.MastershipServiceAdapter;
37import org.onosproject.net.ConnectPoint;
38import org.onosproject.net.Device;
39import org.onosproject.net.DeviceId;
40import org.onosproject.net.NetTestTools;
41import org.onosproject.net.PortNumber;
42import org.onosproject.net.config.Config;
43import org.onosproject.net.config.NetworkConfigEvent;
44import org.onosproject.net.config.NetworkConfigEvent.Type;
45import org.onosproject.net.config.NetworkConfigListener;
46import org.onosproject.net.config.NetworkConfigService;
47import org.onosproject.net.config.NetworkConfigServiceAdapter;
48import org.onosproject.net.device.DeviceEvent;
49import org.onosproject.net.device.DeviceListener;
50import org.onosproject.net.device.DeviceService;
51import org.onosproject.net.device.DeviceServiceAdapter;
52import org.onosproject.net.flow.DefaultTrafficSelector;
53import org.onosproject.net.flow.DefaultTrafficTreatment;
54import org.onosproject.net.flow.TrafficSelector;
55import org.onosproject.net.flow.TrafficTreatment;
56import org.onosproject.net.flowobjective.DefaultForwardingObjective;
57import org.onosproject.net.flowobjective.DefaultNextObjective;
58import org.onosproject.net.flowobjective.FlowObjectiveService;
59import org.onosproject.net.flowobjective.ForwardingObjective;
60import org.onosproject.net.flowobjective.NextObjective;
kishoredb79dc02016-03-07 16:24:55 +053061import org.onosproject.net.host.HostService;
kishoredb79dc02016-03-07 16:24:55 +053062import org.onosproject.net.host.InterfaceIpAddress;
kishoredb79dc02016-03-07 16:24:55 +053063import org.onosproject.routing.RoutingService;
64import org.onosproject.routing.config.RouterConfig;
kishoredb79dc02016-03-07 16:24:55 +053065
Jonathan Hartf04b7d92016-03-29 09:39:11 -070066import java.util.ArrayList;
67import java.util.HashSet;
68import java.util.List;
69import java.util.Objects;
70import java.util.Set;
71
72import static org.easymock.EasyMock.createMock;
73import static org.easymock.EasyMock.createNiceMock;
74import static org.easymock.EasyMock.expect;
75import static org.easymock.EasyMock.expectLastCall;
76import static org.easymock.EasyMock.replay;
77import static org.easymock.EasyMock.verify;
kishoredb79dc02016-03-07 16:24:55 +053078
79/**
80 * UnitTests for ControlPlaneRedirectManager.
81 */
Jonathan Hartf04b7d92016-03-29 09:39:11 -070082public class ControlPlaneRedirectManagerTest {
kishoredb79dc02016-03-07 16:24:55 +053083
kishoredb79dc02016-03-07 16:24:55 +053084 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 Hartf04b7d92016-03-29 09:39:11 -070094 private static final DeviceId DEVICE_ID = DeviceId.deviceId("of:0000000000000001");
95
96 private ConnectPoint controlPlaneConnectPoint = new ConnectPoint(DEVICE_ID,
kishoredb79dc02016-03-07 16:24:55 +053097 PortNumber.portNumber(1));
98
Jonathan Hartf04b7d92016-03-29 09:39:11 -070099 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,
kishoredb79dc02016-03-07 16:24:55 +0530103 PortNumber.portNumber(2));
104
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700105 private static final ConnectPoint SW1_ETH3 = new ConnectPoint(DEVICE_ID,
kishoredb79dc02016-03-07 16:24:55 +0530106 PortNumber.portNumber(3));
kishoredb79dc02016-03-07 16:24:55 +0530107
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700108 private ControlPlaneRedirectManager controlPlaneRedirectManager = new ControlPlaneRedirectManager();
kishoredb79dc02016-03-07 16:24:55 +0530109 private RouterConfig routerConfig = new TestRouterConfig();
110 private NetworkConfigListener networkConfigListener;
111 private DeviceListener deviceListener;
112 private MastershipService mastershipService = new InternalMastershipServiceTest();
kishore71a27532016-03-16 20:23:49 +0530113 private InterfaceListener interfaceListener;
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700114
kishoredb79dc02016-03-07 16:24:55 +0530115 @Before
kishore71a27532016-03-16 20:23:49 +0530116 public void setUp() {
kishoredb79dc02016-03-07 16:24:55 +0530117 networkConfigListener = createMock(NetworkConfigListener.class);
kishoredb79dc02016-03-07 16:24:55 +0530118 deviceService = new TestDeviceService();
119 deviceListener = createMock(DeviceListener.class);
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700120
kishore71a27532016-03-16 20:23:49 +0530121 interfaceListener = createMock(InterfaceListener.class);
kishoredb79dc02016-03-07 16:24:55 +0530122 deviceService.addListener(deviceListener);
123 setUpInterfaceService();
kishore71a27532016-03-16 20:23:49 +0530124 interfaceService = new InternalInterfaceService();
125 interfaceService.addListener(interfaceListener);
kishoredb79dc02016-03-07 16:24:55 +0530126 networkConfigService = new TestNetworkConfigService();
127 networkConfigService.addListener(networkConfigListener);
128 flowObjectiveService = createMock(FlowObjectiveService.class);
129 setUpFlowObjectiveService();
kishoredb79dc02016-03-07 16:24:55 +0530130 controlPlaneRedirectManager.coreService = coreService;
131 controlPlaneRedirectManager.flowObjectiveService = flowObjectiveService;
132 controlPlaneRedirectManager.networkConfigService = networkConfigService;
133 controlPlaneRedirectManager.interfaceService = interfaceService;
134 controlPlaneRedirectManager.deviceService = deviceService;
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700135 controlPlaneRedirectManager.hostService = createNiceMock(HostService.class);
kishoredb79dc02016-03-07 16:24:55 +0530136 controlPlaneRedirectManager.mastershipService = mastershipService;
137 controlPlaneRedirectManager.activate();
138 verify(flowObjectiveService);
139 }
kishore71a27532016-03-16 20:23:49 +0530140
kishoredb79dc02016-03-07 16:24:55 +0530141 /**
kishore71a27532016-03-16 20:23:49 +0530142 * Tests adding new Device to a openflow router.
143 */
144 @Test
145 public void testAddDevice() {
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700146 ConnectPoint sw1eth4 = new ConnectPoint(DEVICE_ID, PortNumber.portNumber(4));
kishore71a27532016-03-16 20:23:49 +0530147 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 Hartf04b7d92016-03-29 09:39:11 -0700165 ConnectPoint sw1eth4 = new ConnectPoint(DEVICE_ID, PortNumber.portNumber(4));
kishore71a27532016-03-16 20:23:49 +0530166 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 Hartf04b7d92016-03-29 09:39:11 -0700186 ConnectPoint sw1eth4 = new ConnectPoint(DEVICE_ID, PortNumber.portNumber(4));
kishore71a27532016-03-16 20:23:49 +0530187 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 Hartf04b7d92016-03-29 09:39:11 -0700200 interfaceListener.event(new InterfaceEvent(InterfaceEvent.Type.INTERFACE_ADDED, sw1Eth4, 500L));
kishore71a27532016-03-16 20:23:49 +0530201 verify(flowObjectiveService);
202 }
203
204 @Test
205 public void testRemoveInterface() {
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700206 ConnectPoint sw1eth4 = new ConnectPoint(DEVICE_ID, PortNumber.portNumber(4));
kishore71a27532016-03-16 20:23:49 +0530207 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 Hartf04b7d92016-03-29 09:39:11 -0700218 interfaceListener.event(new InterfaceEvent(InterfaceEvent.Type.INTERFACE_REMOVED, sw1Eth4, 500L));
kishore71a27532016-03-16 20:23:49 +0530219 verify(flowObjectiveService);
220 }
221
222 /**
223 * Setup flow Configuration for all configured Interfaces.
kishoredb79dc02016-03-07 16:24:55 +0530224 *
225 **/
226 private void setUpFlowObjectiveService() {
kishoredb79dc02016-03-07 16:24:55 +0530227 expect(flowObjectiveService.allocateNextId()).andReturn(1).anyTimes();
kishoredb79dc02016-03-07 16:24:55 +0530228 for (Interface intf : interfaceService.getInterfaces()) {
kishore71a27532016-03-16 20:23:49 +0530229 setUpInterfaceConfiguration(intf, true);
kishoredb79dc02016-03-07 16:24:55 +0530230 }
kishoredb79dc02016-03-07 16:24:55 +0530231 replay(flowObjectiveService);
232 }
233
234 /**
kishore71a27532016-03-16 20:23:49 +0530235 * Setting up flowobjective expectations for basic forwarding and ospf.
kishoredb79dc02016-03-07 16:24:55 +0530236 **/
kishore71a27532016-03-16 20:23:49 +0530237 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.
kishoredb79dc02016-03-07 16:24:55 +0530283 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
kishore71a27532016-03-16 20:23:49 +0530287 modifyNextObjective(deviceId, controlPlanePort, VlanId.vlanId((short) 4094), true, install);
kishoredb79dc02016-03-07 16:24:55 +0530288 flowObjectiveService.forward(controlPlaneConnectPoint.deviceId(),
kishore71a27532016-03-16 20:23:49 +0530289 buildForwardingObjective(toSelector, null, 1, install));
kishoredb79dc02016-03-07 16:24:55 +0530290 expectLastCall().once();
291 }
292
293 /**
kishore71a27532016-03-16 20:23:49 +0530294 * Setup expectations on flowObjectiveService.next for NextObjective.
kishoredb79dc02016-03-07 16:24:55 +0530295 *
296 **/
kishore71a27532016-03-16 20:23:49 +0530297 private int modifyNextObjective(DeviceId deviceId, PortNumber portNumber, VlanId vlanId, boolean popVlan,
298 boolean modifyFlag) {
kishoredb79dc02016-03-07 16:24:55 +0530299 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
kishoredb79dc02016-03-07 16:24:55 +0530308 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder();
309 metabuilder.matchVlanId(vlanId);
310
311 nextObjBuilder.withMeta(metabuilder.build());
312 nextObjBuilder.addTreatment(ttBuilder.build());
kishore71a27532016-03-16 20:23:49 +0530313 if (modifyFlag) {
314 flowObjectiveService.next(deviceId, nextObjBuilder.add());
315 expectLastCall().once();
316 } else {
317 flowObjectiveService.next(deviceId, nextObjBuilder.remove());
318 expectLastCall().once();
319 }
kishoredb79dc02016-03-07 16:24:55 +0530320 return 1;
321 }
322
323 /**
kishore71a27532016-03-16 20:23:49 +0530324 * Setup Interface expectation for all Testcases.
kishoredb79dc02016-03-07 16:24:55 +0530325 **/
326 private void setUpInterfaceService() {
kishoredb79dc02016-03-07 16:24:55 +0530327 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
kishoredb79dc02016-03-07 16:24:55 +0530348 }
349
kishoredb79dc02016-03-07 16:24:55 +0530350 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
kishoredb79dc02016-03-07 16:24:55 +0530365 private class TestCoreService extends CoreServiceAdapter {
kishore71a27532016-03-16 20:23:49 +0530366
kishoredb79dc02016-03-07 16:24:55 +0530367 @Override
368 public ApplicationId getAppId(String name) {
369 return APPID;
370 }
371
372 @Override
373 public ApplicationId registerApplication(String name) {
kishoredb79dc02016-03-07 16:24:55 +0530374 return new TestApplicationId(name);
375 }
376
377 }
378
379 private class TestDeviceService extends DeviceServiceAdapter {
380
381 @Override
382 public boolean isAvailable(DeviceId deviceId) {
kishoredb79dc02016-03-07 16:24:55 +0530383 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) {
kishoredb79dc02016-03-07 16:24:55 +0530392 ControlPlaneRedirectManagerTest.this.deviceListener = listener;
393 }
394
395 }
396
kishoredb79dc02016-03-07 16:24:55 +0530397 private class TestRouterConfig extends RouterConfig {
398
399 @Override
400 public ConnectPoint getControlPlaneConnectPoint() {
kishoredb79dc02016-03-07 16:24:55 +0530401 return controlPlaneConnectPoint;
402 }
403
404 @Override
405 public boolean getOspfEnabled() {
kishoredb79dc02016-03-07 16:24:55 +0530406 return true;
407 }
408
409 @Override
410 public List<String> getInterfaces() {
kishoredb79dc02016-03-07 16:24:55 +0530411 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) {
kishoredb79dc02016-03-07 16:24:55 +0530424 ControlPlaneRedirectManagerTest.this.networkConfigListener = listener;
425 }
426
427 @Override
428 public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) {
kishoredb79dc02016-03-07 16:24:55 +0530429 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;
kishore71a27532016-03-16 20:23:49 +0530485 }
kishoredb79dc02016-03-07 16:24:55 +0530486 } 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) {
kishoredb79dc02016-03-07 16:24:55 +0530497 boolean flag = deviceId.equals(controlPlaneConnectPoint.deviceId());
498 return flag;
499 }
500
501 }
kishore71a27532016-03-16 20:23:49 +0530502
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 }
kishoredb79dc02016-03-07 16:24:55 +0530542}