blob: 08e89e77d085f5a769143516a0af27943edeebce [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;
kishore786b7e42016-05-19 16:25:57 +053027import org.onosproject.app.ApplicationService;
kishoredb79dc02016-03-07 16:24:55 +053028import org.onosproject.core.ApplicationId;
29import org.onosproject.core.CoreService;
30import org.onosproject.core.CoreServiceAdapter;
31import org.onosproject.incubator.net.intf.Interface;
kishore71a27532016-03-16 20:23:49 +053032import org.onosproject.incubator.net.intf.InterfaceEvent;
33import org.onosproject.incubator.net.intf.InterfaceListener;
kishoredb79dc02016-03-07 16:24:55 +053034import org.onosproject.incubator.net.intf.InterfaceService;
kishore71a27532016-03-16 20:23:49 +053035import org.onosproject.incubator.net.intf.InterfaceServiceAdapter;
kishoredb79dc02016-03-07 16:24:55 +053036import org.onosproject.mastership.MastershipService;
37import org.onosproject.mastership.MastershipServiceAdapter;
38import org.onosproject.net.ConnectPoint;
39import org.onosproject.net.Device;
40import org.onosproject.net.DeviceId;
41import org.onosproject.net.NetTestTools;
42import org.onosproject.net.PortNumber;
43import org.onosproject.net.config.Config;
44import org.onosproject.net.config.NetworkConfigEvent;
45import org.onosproject.net.config.NetworkConfigEvent.Type;
46import org.onosproject.net.config.NetworkConfigListener;
47import org.onosproject.net.config.NetworkConfigService;
48import org.onosproject.net.config.NetworkConfigServiceAdapter;
49import org.onosproject.net.device.DeviceEvent;
50import org.onosproject.net.device.DeviceListener;
51import org.onosproject.net.device.DeviceService;
52import org.onosproject.net.device.DeviceServiceAdapter;
53import org.onosproject.net.flow.DefaultTrafficSelector;
54import org.onosproject.net.flow.DefaultTrafficTreatment;
55import org.onosproject.net.flow.TrafficSelector;
56import org.onosproject.net.flow.TrafficTreatment;
57import org.onosproject.net.flowobjective.DefaultForwardingObjective;
58import org.onosproject.net.flowobjective.DefaultNextObjective;
59import org.onosproject.net.flowobjective.FlowObjectiveService;
60import org.onosproject.net.flowobjective.ForwardingObjective;
61import org.onosproject.net.flowobjective.NextObjective;
kishoredb79dc02016-03-07 16:24:55 +053062import org.onosproject.net.host.HostService;
kishoredb79dc02016-03-07 16:24:55 +053063import org.onosproject.net.host.InterfaceIpAddress;
kishoredb79dc02016-03-07 16:24:55 +053064import org.onosproject.routing.RoutingService;
65import org.onosproject.routing.config.RouterConfig;
kishoredb79dc02016-03-07 16:24:55 +053066
Jonathan Hartf04b7d92016-03-29 09:39:11 -070067import java.util.ArrayList;
68import java.util.HashSet;
69import java.util.List;
70import java.util.Objects;
71import java.util.Set;
72
73import static org.easymock.EasyMock.createMock;
74import static org.easymock.EasyMock.createNiceMock;
75import static org.easymock.EasyMock.expect;
76import static org.easymock.EasyMock.expectLastCall;
77import static org.easymock.EasyMock.replay;
78import static org.easymock.EasyMock.verify;
kishoredb79dc02016-03-07 16:24:55 +053079
80/**
81 * UnitTests for ControlPlaneRedirectManager.
82 */
Jonathan Hartf04b7d92016-03-29 09:39:11 -070083public class ControlPlaneRedirectManagerTest {
kishoredb79dc02016-03-07 16:24:55 +053084
kishoredb79dc02016-03-07 16:24:55 +053085 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;
kishore786b7e42016-05-19 16:25:57 +053093 private static final ApplicationId APPID = TestApplicationId.create("org.onosproject.vrouter");
kishoredb79dc02016-03-07 16:24:55 +053094
Jonathan Hartf04b7d92016-03-29 09:39:11 -070095 private static final DeviceId DEVICE_ID = DeviceId.deviceId("of:0000000000000001");
96
97 private ConnectPoint controlPlaneConnectPoint = new ConnectPoint(DEVICE_ID,
kishoredb79dc02016-03-07 16:24:55 +053098 PortNumber.portNumber(1));
99
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700100 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,
kishoredb79dc02016-03-07 16:24:55 +0530104 PortNumber.portNumber(2));
105
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700106 private static final ConnectPoint SW1_ETH3 = new ConnectPoint(DEVICE_ID,
kishoredb79dc02016-03-07 16:24:55 +0530107 PortNumber.portNumber(3));
kishoredb79dc02016-03-07 16:24:55 +0530108
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700109 private ControlPlaneRedirectManager controlPlaneRedirectManager = new ControlPlaneRedirectManager();
kishoredb79dc02016-03-07 16:24:55 +0530110 private RouterConfig routerConfig = new TestRouterConfig();
111 private NetworkConfigListener networkConfigListener;
112 private DeviceListener deviceListener;
113 private MastershipService mastershipService = new InternalMastershipServiceTest();
kishore71a27532016-03-16 20:23:49 +0530114 private InterfaceListener interfaceListener;
kishore786b7e42016-05-19 16:25:57 +0530115 private ApplicationService applicationService;
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700116
kishoredb79dc02016-03-07 16:24:55 +0530117 @Before
kishore71a27532016-03-16 20:23:49 +0530118 public void setUp() {
kishoredb79dc02016-03-07 16:24:55 +0530119 networkConfigListener = createMock(NetworkConfigListener.class);
kishoredb79dc02016-03-07 16:24:55 +0530120 deviceService = new TestDeviceService();
121 deviceListener = createMock(DeviceListener.class);
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700122
kishore71a27532016-03-16 20:23:49 +0530123 interfaceListener = createMock(InterfaceListener.class);
kishoredb79dc02016-03-07 16:24:55 +0530124 deviceService.addListener(deviceListener);
125 setUpInterfaceService();
kishore71a27532016-03-16 20:23:49 +0530126 interfaceService = new InternalInterfaceService();
127 interfaceService.addListener(interfaceListener);
kishoredb79dc02016-03-07 16:24:55 +0530128 networkConfigService = new TestNetworkConfigService();
129 networkConfigService.addListener(networkConfigListener);
130 flowObjectiveService = createMock(FlowObjectiveService.class);
kishore786b7e42016-05-19 16:25:57 +0530131 applicationService = createNiceMock(ApplicationService.class);
132 replay(applicationService);
kishoredb79dc02016-03-07 16:24:55 +0530133 setUpFlowObjectiveService();
kishoredb79dc02016-03-07 16:24:55 +0530134 controlPlaneRedirectManager.coreService = coreService;
135 controlPlaneRedirectManager.flowObjectiveService = flowObjectiveService;
136 controlPlaneRedirectManager.networkConfigService = networkConfigService;
137 controlPlaneRedirectManager.interfaceService = interfaceService;
138 controlPlaneRedirectManager.deviceService = deviceService;
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700139 controlPlaneRedirectManager.hostService = createNiceMock(HostService.class);
kishoredb79dc02016-03-07 16:24:55 +0530140 controlPlaneRedirectManager.mastershipService = mastershipService;
kishore786b7e42016-05-19 16:25:57 +0530141 controlPlaneRedirectManager.applicationService = applicationService;
kishoredb79dc02016-03-07 16:24:55 +0530142 controlPlaneRedirectManager.activate();
143 verify(flowObjectiveService);
144 }
kishore71a27532016-03-16 20:23:49 +0530145
kishoredb79dc02016-03-07 16:24:55 +0530146 /**
kishore71a27532016-03-16 20:23:49 +0530147 * Tests adding new Device to a openflow router.
148 */
149 @Test
150 public void testAddDevice() {
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700151 ConnectPoint sw1eth4 = new ConnectPoint(DEVICE_ID, PortNumber.portNumber(4));
Sho SHIMIZU891162b2016-06-06 16:32:01 -0700152 List<InterfaceIpAddress> interfaceIpAddresses4 = new ArrayList<>();
kishore71a27532016-03-16 20:23:49 +0530153 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 Hartf04b7d92016-03-29 09:39:11 -0700170 ConnectPoint sw1eth4 = new ConnectPoint(DEVICE_ID, PortNumber.portNumber(4));
Sho SHIMIZU891162b2016-06-06 16:32:01 -0700171 List<InterfaceIpAddress> interfaceIpAddresses4 = new ArrayList<>();
kishore71a27532016-03-16 20:23:49 +0530172 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 Hartf04b7d92016-03-29 09:39:11 -0700191 ConnectPoint sw1eth4 = new ConnectPoint(DEVICE_ID, PortNumber.portNumber(4));
Sho SHIMIZU891162b2016-06-06 16:32:01 -0700192 List<InterfaceIpAddress> interfaceIpAddresses4 = new ArrayList<>();
kishore71a27532016-03-16 20:23:49 +0530193 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 Hartf04b7d92016-03-29 09:39:11 -0700205 interfaceListener.event(new InterfaceEvent(InterfaceEvent.Type.INTERFACE_ADDED, sw1Eth4, 500L));
kishore71a27532016-03-16 20:23:49 +0530206 verify(flowObjectiveService);
207 }
208
209 @Test
210 public void testRemoveInterface() {
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700211 ConnectPoint sw1eth4 = new ConnectPoint(DEVICE_ID, PortNumber.portNumber(4));
Sho SHIMIZU891162b2016-06-06 16:32:01 -0700212 List<InterfaceIpAddress> interfaceIpAddresses4 = new ArrayList<>();
kishore71a27532016-03-16 20:23:49 +0530213 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 Hartf04b7d92016-03-29 09:39:11 -0700223 interfaceListener.event(new InterfaceEvent(InterfaceEvent.Type.INTERFACE_REMOVED, sw1Eth4, 500L));
kishore71a27532016-03-16 20:23:49 +0530224 verify(flowObjectiveService);
225 }
226
227 /**
228 * Setup flow Configuration for all configured Interfaces.
kishoredb79dc02016-03-07 16:24:55 +0530229 *
230 **/
231 private void setUpFlowObjectiveService() {
kishoredb79dc02016-03-07 16:24:55 +0530232 expect(flowObjectiveService.allocateNextId()).andReturn(1).anyTimes();
kishoredb79dc02016-03-07 16:24:55 +0530233 for (Interface intf : interfaceService.getInterfaces()) {
kishore71a27532016-03-16 20:23:49 +0530234 setUpInterfaceConfiguration(intf, true);
kishoredb79dc02016-03-07 16:24:55 +0530235 }
kishoredb79dc02016-03-07 16:24:55 +0530236 replay(flowObjectiveService);
237 }
238
239 /**
kishore71a27532016-03-16 20:23:49 +0530240 * Setting up flowobjective expectations for basic forwarding and ospf.
kishoredb79dc02016-03-07 16:24:55 +0530241 **/
kishore71a27532016-03-16 20:23:49 +0530242 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.
kishoredb79dc02016-03-07 16:24:55 +0530288 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
kishore71a27532016-03-16 20:23:49 +0530292 modifyNextObjective(deviceId, controlPlanePort, VlanId.vlanId((short) 4094), true, install);
kishoredb79dc02016-03-07 16:24:55 +0530293 flowObjectiveService.forward(controlPlaneConnectPoint.deviceId(),
kishore71a27532016-03-16 20:23:49 +0530294 buildForwardingObjective(toSelector, null, 1, install));
kishoredb79dc02016-03-07 16:24:55 +0530295 expectLastCall().once();
296 }
297
298 /**
kishore71a27532016-03-16 20:23:49 +0530299 * Setup expectations on flowObjectiveService.next for NextObjective.
kishoredb79dc02016-03-07 16:24:55 +0530300 *
301 **/
kishore71a27532016-03-16 20:23:49 +0530302 private int modifyNextObjective(DeviceId deviceId, PortNumber portNumber, VlanId vlanId, boolean popVlan,
303 boolean modifyFlag) {
kishoredb79dc02016-03-07 16:24:55 +0530304 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
kishoredb79dc02016-03-07 16:24:55 +0530313 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder();
314 metabuilder.matchVlanId(vlanId);
315
316 nextObjBuilder.withMeta(metabuilder.build());
317 nextObjBuilder.addTreatment(ttBuilder.build());
kishore71a27532016-03-16 20:23:49 +0530318 if (modifyFlag) {
319 flowObjectiveService.next(deviceId, nextObjBuilder.add());
320 expectLastCall().once();
321 } else {
322 flowObjectiveService.next(deviceId, nextObjBuilder.remove());
323 expectLastCall().once();
324 }
kishoredb79dc02016-03-07 16:24:55 +0530325 return 1;
326 }
327
328 /**
kishore71a27532016-03-16 20:23:49 +0530329 * Setup Interface expectation for all Testcases.
kishoredb79dc02016-03-07 16:24:55 +0530330 **/
331 private void setUpInterfaceService() {
Sho SHIMIZU891162b2016-06-06 16:32:01 -0700332 List<InterfaceIpAddress> interfaceIpAddresses1 = new ArrayList<>();
kishoredb79dc02016-03-07 16:24:55 +0530333 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 SHIMIZU891162b2016-06-06 16:32:01 -0700339 List<InterfaceIpAddress> interfaceIpAddresses2 = new ArrayList<>();
kishoredb79dc02016-03-07 16:24:55 +0530340 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 SHIMIZU891162b2016-06-06 16:32:01 -0700346 List<InterfaceIpAddress> interfaceIpAddresses3 = new ArrayList<>();
kishoredb79dc02016-03-07 16:24:55 +0530347 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
kishoredb79dc02016-03-07 16:24:55 +0530353 }
354
kishoredb79dc02016-03-07 16:24:55 +0530355 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
kishoredb79dc02016-03-07 16:24:55 +0530370 private class TestCoreService extends CoreServiceAdapter {
kishore71a27532016-03-16 20:23:49 +0530371
kishoredb79dc02016-03-07 16:24:55 +0530372 @Override
373 public ApplicationId getAppId(String name) {
374 return APPID;
375 }
376
377 @Override
378 public ApplicationId registerApplication(String name) {
kishoredb79dc02016-03-07 16:24:55 +0530379 return new TestApplicationId(name);
380 }
381
382 }
383
384 private class TestDeviceService extends DeviceServiceAdapter {
385
386 @Override
387 public boolean isAvailable(DeviceId deviceId) {
kishoredb79dc02016-03-07 16:24:55 +0530388 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) {
kishoredb79dc02016-03-07 16:24:55 +0530397 ControlPlaneRedirectManagerTest.this.deviceListener = listener;
398 }
399
400 }
401
kishoredb79dc02016-03-07 16:24:55 +0530402 private class TestRouterConfig extends RouterConfig {
403
404 @Override
405 public ConnectPoint getControlPlaneConnectPoint() {
kishoredb79dc02016-03-07 16:24:55 +0530406 return controlPlaneConnectPoint;
407 }
408
409 @Override
410 public boolean getOspfEnabled() {
kishoredb79dc02016-03-07 16:24:55 +0530411 return true;
412 }
413
414 @Override
415 public List<String> getInterfaces() {
kishoredb79dc02016-03-07 16:24:55 +0530416 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) {
kishoredb79dc02016-03-07 16:24:55 +0530429 ControlPlaneRedirectManagerTest.this.networkConfigListener = listener;
430 }
431
432 @Override
433 public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) {
kishoredb79dc02016-03-07 16:24:55 +0530434 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;
kishore71a27532016-03-16 20:23:49 +0530490 }
kishoredb79dc02016-03-07 16:24:55 +0530491 } 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) {
kishoredb79dc02016-03-07 16:24:55 +0530502 boolean flag = deviceId.equals(controlPlaneConnectPoint.deviceId());
503 return flag;
504 }
505
506 }
kishore71a27532016-03-16 20:23:49 +0530507
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 }
kishoredb79dc02016-03-07 16:24:55 +0530547}