blob: f305563b05b6e124a78efd6ed0536bf2b2e6abf0 [file] [log] [blame]
kishoredb79dc02016-03-07 16:24:55 +05301/*
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 */
16package org.onosproject.routing.impl;
17
18import static org.easymock.EasyMock.createMock;
19import static org.easymock.EasyMock.expect;
20import static org.easymock.EasyMock.expectLastCall;
21import static org.easymock.EasyMock.replay;
22import static org.easymock.EasyMock.verify;
23import static org.slf4j.LoggerFactory.getLogger;
24
25import java.util.ArrayList;
kishore71a27532016-03-16 20:23:49 +053026import java.util.HashSet;
kishoredb79dc02016-03-07 16:24:55 +053027import java.util.List;
28import java.util.Objects;
29import java.util.Set;
30
31import org.easymock.EasyMock;
32import org.junit.Before;
33import org.junit.Test;
34import org.onlab.packet.EthType;
kishoredb79dc02016-03-07 16:24:55 +053035import org.onlab.packet.IpAddress;
36import org.onlab.packet.IpPrefix;
37import org.onlab.packet.MacAddress;
38import org.onlab.packet.VlanId;
39import org.onosproject.core.ApplicationId;
40import org.onosproject.core.CoreService;
41import org.onosproject.core.CoreServiceAdapter;
42import org.onosproject.incubator.net.intf.Interface;
kishore71a27532016-03-16 20:23:49 +053043import org.onosproject.incubator.net.intf.InterfaceEvent;
44import org.onosproject.incubator.net.intf.InterfaceListener;
kishoredb79dc02016-03-07 16:24:55 +053045import org.onosproject.incubator.net.intf.InterfaceService;
kishore71a27532016-03-16 20:23:49 +053046import org.onosproject.incubator.net.intf.InterfaceServiceAdapter;
kishoredb79dc02016-03-07 16:24:55 +053047import org.onosproject.mastership.MastershipService;
48import org.onosproject.mastership.MastershipServiceAdapter;
49import org.onosproject.net.ConnectPoint;
50import org.onosproject.net.Device;
51import org.onosproject.net.DeviceId;
52import org.onosproject.net.NetTestTools;
53import org.onosproject.net.PortNumber;
54import org.onosproject.net.config.Config;
55import org.onosproject.net.config.NetworkConfigEvent;
56import org.onosproject.net.config.NetworkConfigEvent.Type;
57import org.onosproject.net.config.NetworkConfigListener;
58import org.onosproject.net.config.NetworkConfigService;
59import org.onosproject.net.config.NetworkConfigServiceAdapter;
60import org.onosproject.net.device.DeviceEvent;
61import org.onosproject.net.device.DeviceListener;
62import org.onosproject.net.device.DeviceService;
63import org.onosproject.net.device.DeviceServiceAdapter;
64import org.onosproject.net.flow.DefaultTrafficSelector;
65import org.onosproject.net.flow.DefaultTrafficTreatment;
66import org.onosproject.net.flow.TrafficSelector;
67import org.onosproject.net.flow.TrafficTreatment;
68import org.onosproject.net.flowobjective.DefaultForwardingObjective;
69import org.onosproject.net.flowobjective.DefaultNextObjective;
70import org.onosproject.net.flowobjective.FlowObjectiveService;
71import org.onosproject.net.flowobjective.ForwardingObjective;
72import org.onosproject.net.flowobjective.NextObjective;
73import org.onosproject.net.host.HostListener;
74import org.onosproject.net.host.HostService;
75import org.onosproject.net.host.HostServiceAdapter;
76import org.onosproject.net.host.InterfaceIpAddress;
77import org.onosproject.net.intent.AbstractIntentTest;
78import org.onosproject.routing.RoutingService;
79import org.onosproject.routing.config.RouterConfig;
80import org.slf4j.Logger;
81
82import com.google.common.collect.Sets;
83
84/**
85 * UnitTests for ControlPlaneRedirectManager.
86 */
87public class ControlPlaneRedirectManagerTest extends AbstractIntentTest {
88
89 private final Logger log = getLogger(getClass());
kishoredb79dc02016-03-07 16:24:55 +053090 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;
kishore71a27532016-03-16 20:23:49 +0530122 private InterfaceListener interfaceListener;
kishoredb79dc02016-03-07 16:24:55 +0530123 @Override
124 @Before
kishore71a27532016-03-16 20:23:49 +0530125 public void setUp() {
kishoredb79dc02016-03-07 16:24:55 +0530126 networkConfigListener = createMock(NetworkConfigListener.class);
127 hostService = new TestHostService();
128 deviceService = new TestDeviceService();
129 deviceListener = createMock(DeviceListener.class);
130 hostListener = createMock(HostListener.class);
kishore71a27532016-03-16 20:23:49 +0530131 interfaceListener = createMock(InterfaceListener.class);
kishoredb79dc02016-03-07 16:24:55 +0530132 hostService.addListener(hostListener);
133 deviceService.addListener(deviceListener);
134 setUpInterfaceService();
kishore71a27532016-03-16 20:23:49 +0530135 interfaceService = new InternalInterfaceService();
136 interfaceService.addListener(interfaceListener);
kishoredb79dc02016-03-07 16:24:55 +0530137 networkConfigService = new TestNetworkConfigService();
138 networkConfigService.addListener(networkConfigListener);
139 flowObjectiveService = createMock(FlowObjectiveService.class);
140 setUpFlowObjectiveService();
kishoredb79dc02016-03-07 16:24:55 +0530141 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 }
kishore71a27532016-03-16 20:23:49 +0530151
kishoredb79dc02016-03-07 16:24:55 +0530152 /**
kishore71a27532016-03-16 20:23:49 +0530153 * 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.
kishoredb79dc02016-03-07 16:24:55 +0530237 *
238 **/
239 private void setUpFlowObjectiveService() {
kishoredb79dc02016-03-07 16:24:55 +0530240 expect(flowObjectiveService.allocateNextId()).andReturn(1).anyTimes();
kishoredb79dc02016-03-07 16:24:55 +0530241 for (Interface intf : interfaceService.getInterfaces()) {
kishore71a27532016-03-16 20:23:49 +0530242 setUpInterfaceConfiguration(intf, true);
kishoredb79dc02016-03-07 16:24:55 +0530243 }
kishoredb79dc02016-03-07 16:24:55 +0530244 replay(flowObjectiveService);
245 }
246
247 /**
kishore71a27532016-03-16 20:23:49 +0530248 * Setting up flowobjective expectations for basic forwarding and ospf.
kishoredb79dc02016-03-07 16:24:55 +0530249 **/
kishore71a27532016-03-16 20:23:49 +0530250 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.
kishoredb79dc02016-03-07 16:24:55 +0530296 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
kishore71a27532016-03-16 20:23:49 +0530300 modifyNextObjective(deviceId, controlPlanePort, VlanId.vlanId((short) 4094), true, install);
kishoredb79dc02016-03-07 16:24:55 +0530301 flowObjectiveService.forward(controlPlaneConnectPoint.deviceId(),
kishore71a27532016-03-16 20:23:49 +0530302 buildForwardingObjective(toSelector, null, 1, install));
kishoredb79dc02016-03-07 16:24:55 +0530303 expectLastCall().once();
304 }
305
306 /**
kishore71a27532016-03-16 20:23:49 +0530307 * Setup expectations on flowObjectiveService.next for NextObjective.
kishoredb79dc02016-03-07 16:24:55 +0530308 *
309 **/
kishore71a27532016-03-16 20:23:49 +0530310 private int modifyNextObjective(DeviceId deviceId, PortNumber portNumber, VlanId vlanId, boolean popVlan,
311 boolean modifyFlag) {
kishoredb79dc02016-03-07 16:24:55 +0530312 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
kishoredb79dc02016-03-07 16:24:55 +0530321 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder();
322 metabuilder.matchVlanId(vlanId);
323
324 nextObjBuilder.withMeta(metabuilder.build());
325 nextObjBuilder.addTreatment(ttBuilder.build());
kishore71a27532016-03-16 20:23:49 +0530326 if (modifyFlag) {
327 flowObjectiveService.next(deviceId, nextObjBuilder.add());
328 expectLastCall().once();
329 } else {
330 flowObjectiveService.next(deviceId, nextObjBuilder.remove());
331 expectLastCall().once();
332 }
kishoredb79dc02016-03-07 16:24:55 +0530333 return 1;
334 }
335
336 /**
kishore71a27532016-03-16 20:23:49 +0530337 * Setup Interface expectation for all Testcases.
kishoredb79dc02016-03-07 16:24:55 +0530338 **/
339 private void setUpInterfaceService() {
kishoredb79dc02016-03-07 16:24:55 +0530340 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
kishoredb79dc02016-03-07 16:24:55 +0530361 }
362
kishoredb79dc02016-03-07 16:24:55 +0530363 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
kishoredb79dc02016-03-07 16:24:55 +0530378 private class TestCoreService extends CoreServiceAdapter {
kishore71a27532016-03-16 20:23:49 +0530379
kishoredb79dc02016-03-07 16:24:55 +0530380 @Override
381 public ApplicationId getAppId(String name) {
382 return APPID;
383 }
384
385 @Override
386 public ApplicationId registerApplication(String name) {
kishoredb79dc02016-03-07 16:24:55 +0530387 return new TestApplicationId(name);
388 }
389
390 }
391
392 private class TestDeviceService extends DeviceServiceAdapter {
393
394 @Override
395 public boolean isAvailable(DeviceId deviceId) {
kishoredb79dc02016-03-07 16:24:55 +0530396 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) {
kishoredb79dc02016-03-07 16:24:55 +0530405 ControlPlaneRedirectManagerTest.this.deviceListener = listener;
406 }
407
408 }
409
410 private class TestHostService extends HostServiceAdapter {
411
412 @Override
413 public void addListener(HostListener listener) {
kishoredb79dc02016-03-07 16:24:55 +0530414 ControlPlaneRedirectManagerTest.this.hostListener = listener;
415 }
416
417 }
418
419 private class TestRouterConfig extends RouterConfig {
420
421 @Override
422 public ConnectPoint getControlPlaneConnectPoint() {
kishoredb79dc02016-03-07 16:24:55 +0530423 return controlPlaneConnectPoint;
424 }
425
426 @Override
427 public boolean getOspfEnabled() {
kishoredb79dc02016-03-07 16:24:55 +0530428 return true;
429 }
430
431 @Override
432 public List<String> getInterfaces() {
kishoredb79dc02016-03-07 16:24:55 +0530433 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) {
kishoredb79dc02016-03-07 16:24:55 +0530446 ControlPlaneRedirectManagerTest.this.networkConfigListener = listener;
447 }
448
449 @Override
450 public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) {
kishoredb79dc02016-03-07 16:24:55 +0530451 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;
kishore71a27532016-03-16 20:23:49 +0530507 }
kishoredb79dc02016-03-07 16:24:55 +0530508 } 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) {
kishoredb79dc02016-03-07 16:24:55 +0530519 boolean flag = deviceId.equals(controlPlaneConnectPoint.deviceId());
520 return flag;
521 }
522
523 }
kishore71a27532016-03-16 20:23:49 +0530524
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 }
kishoredb79dc02016-03-07 16:24:55 +0530564}