blob: 08864c86abe526237b07ccb6fa5ca2a3b1bcfe67 [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;
Jonathan Hartf8cd0522016-10-25 07:09:55 -070021import org.junit.Ignore;
kishoredb79dc02016-03-07 16:24:55 +053022import org.junit.Test;
23import org.onlab.packet.EthType;
kishoredb79dc02016-03-07 16:24:55 +053024import org.onlab.packet.IpAddress;
25import org.onlab.packet.IpPrefix;
26import org.onlab.packet.MacAddress;
27import org.onlab.packet.VlanId;
kishore786b7e42016-05-19 16:25:57 +053028import org.onosproject.app.ApplicationService;
kishoredb79dc02016-03-07 16:24:55 +053029import org.onosproject.core.ApplicationId;
30import org.onosproject.core.CoreService;
31import org.onosproject.core.CoreServiceAdapter;
32import org.onosproject.incubator.net.intf.Interface;
kishore71a27532016-03-16 20:23:49 +053033import org.onosproject.incubator.net.intf.InterfaceEvent;
34import org.onosproject.incubator.net.intf.InterfaceListener;
kishoredb79dc02016-03-07 16:24:55 +053035import org.onosproject.incubator.net.intf.InterfaceService;
kishore71a27532016-03-16 20:23:49 +053036import org.onosproject.incubator.net.intf.InterfaceServiceAdapter;
kishoredb79dc02016-03-07 16:24:55 +053037import org.onosproject.mastership.MastershipService;
38import org.onosproject.mastership.MastershipServiceAdapter;
39import org.onosproject.net.ConnectPoint;
40import org.onosproject.net.Device;
41import org.onosproject.net.DeviceId;
42import org.onosproject.net.NetTestTools;
43import org.onosproject.net.PortNumber;
44import org.onosproject.net.config.Config;
45import org.onosproject.net.config.NetworkConfigEvent;
46import org.onosproject.net.config.NetworkConfigEvent.Type;
47import org.onosproject.net.config.NetworkConfigListener;
48import org.onosproject.net.config.NetworkConfigService;
49import org.onosproject.net.config.NetworkConfigServiceAdapter;
50import org.onosproject.net.device.DeviceEvent;
51import org.onosproject.net.device.DeviceListener;
52import org.onosproject.net.device.DeviceService;
53import org.onosproject.net.device.DeviceServiceAdapter;
54import org.onosproject.net.flow.DefaultTrafficSelector;
55import org.onosproject.net.flow.DefaultTrafficTreatment;
56import org.onosproject.net.flow.TrafficSelector;
57import org.onosproject.net.flow.TrafficTreatment;
58import org.onosproject.net.flowobjective.DefaultForwardingObjective;
59import org.onosproject.net.flowobjective.DefaultNextObjective;
60import org.onosproject.net.flowobjective.FlowObjectiveService;
61import org.onosproject.net.flowobjective.ForwardingObjective;
62import org.onosproject.net.flowobjective.NextObjective;
kishoredb79dc02016-03-07 16:24:55 +053063import org.onosproject.net.host.HostService;
kishoredb79dc02016-03-07 16:24:55 +053064import org.onosproject.net.host.InterfaceIpAddress;
kishoredb79dc02016-03-07 16:24:55 +053065import org.onosproject.routing.RoutingService;
66import org.onosproject.routing.config.RouterConfig;
kishoredb79dc02016-03-07 16:24:55 +053067
Jonathan Hartf04b7d92016-03-29 09:39:11 -070068import java.util.ArrayList;
69import java.util.HashSet;
70import java.util.List;
71import java.util.Objects;
72import java.util.Set;
73
74import static org.easymock.EasyMock.createMock;
75import static org.easymock.EasyMock.createNiceMock;
76import static org.easymock.EasyMock.expect;
77import static org.easymock.EasyMock.expectLastCall;
78import static org.easymock.EasyMock.replay;
79import static org.easymock.EasyMock.verify;
Vinayak Tejankar3a409c62017-01-12 02:20:53 +053080import static org.onlab.packet.ICMP6.NEIGHBOR_ADVERTISEMENT;
81import static org.onlab.packet.ICMP6.NEIGHBOR_SOLICITATION;
Jonathan Hartf8cd0522016-10-25 07:09:55 -070082import static org.onosproject.routing.impl.ControlPlaneRedirectManager.ACL_PRIORITY;
83import static org.onosproject.routing.impl.ControlPlaneRedirectManager.buildArpSelector;
84import static org.onosproject.routing.impl.ControlPlaneRedirectManager.buildIPDstSelector;
85import static org.onosproject.routing.impl.ControlPlaneRedirectManager.buildIPSrcSelector;
86import static org.onosproject.routing.impl.ControlPlaneRedirectManager.buildNdpSelector;
kishoredb79dc02016-03-07 16:24:55 +053087
88/**
89 * UnitTests for ControlPlaneRedirectManager.
90 */
Jonathan Hartf8cd0522016-10-25 07:09:55 -070091@Ignore("Too many dependencies on internal implementation, too hard to maintain")
Jonathan Hartf04b7d92016-03-29 09:39:11 -070092public class ControlPlaneRedirectManagerTest {
kishoredb79dc02016-03-07 16:24:55 +053093
kishoredb79dc02016-03-07 16:24:55 +053094 private DeviceService deviceService;
95 private FlowObjectiveService flowObjectiveService;
96 private NetworkConfigService networkConfigService;
97 private final Set<Interface> interfaces = Sets.newHashSet();
98 static Device dev3 = NetTestTools.device("0000000000000001");
99 private static final int OSPF_IP_PROTO = 0x59;
100 private CoreService coreService = new TestCoreService();
101 private InterfaceService interfaceService;
kishore786b7e42016-05-19 16:25:57 +0530102 private static final ApplicationId APPID = TestApplicationId.create("org.onosproject.vrouter");
kishoredb79dc02016-03-07 16:24:55 +0530103
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700104 private static final DeviceId DEVICE_ID = DeviceId.deviceId("of:0000000000000001");
105
106 private ConnectPoint controlPlaneConnectPoint = new ConnectPoint(DEVICE_ID,
kishoredb79dc02016-03-07 16:24:55 +0530107 PortNumber.portNumber(1));
108
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700109 private static final ConnectPoint SW1_ETH1 = new ConnectPoint(DEVICE_ID,
110 PortNumber.portNumber(1));
111
112 private static final ConnectPoint SW1_ETH2 = new ConnectPoint(DEVICE_ID,
kishoredb79dc02016-03-07 16:24:55 +0530113 PortNumber.portNumber(2));
114
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700115 private static final ConnectPoint SW1_ETH3 = new ConnectPoint(DEVICE_ID,
kishoredb79dc02016-03-07 16:24:55 +0530116 PortNumber.portNumber(3));
kishoredb79dc02016-03-07 16:24:55 +0530117
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700118 private ControlPlaneRedirectManager controlPlaneRedirectManager = new ControlPlaneRedirectManager();
kishoredb79dc02016-03-07 16:24:55 +0530119 private RouterConfig routerConfig = new TestRouterConfig();
120 private NetworkConfigListener networkConfigListener;
121 private DeviceListener deviceListener;
122 private MastershipService mastershipService = new InternalMastershipServiceTest();
kishore71a27532016-03-16 20:23:49 +0530123 private InterfaceListener interfaceListener;
kishore786b7e42016-05-19 16:25:57 +0530124 private ApplicationService applicationService;
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700125
kishoredb79dc02016-03-07 16:24:55 +0530126 @Before
kishore71a27532016-03-16 20:23:49 +0530127 public void setUp() {
kishoredb79dc02016-03-07 16:24:55 +0530128 networkConfigListener = createMock(NetworkConfigListener.class);
kishoredb79dc02016-03-07 16:24:55 +0530129 deviceService = new TestDeviceService();
130 deviceListener = createMock(DeviceListener.class);
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700131
kishore71a27532016-03-16 20:23:49 +0530132 interfaceListener = createMock(InterfaceListener.class);
kishoredb79dc02016-03-07 16:24:55 +0530133 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);
kishore786b7e42016-05-19 16:25:57 +0530140 applicationService = createNiceMock(ApplicationService.class);
141 replay(applicationService);
kishoredb79dc02016-03-07 16:24:55 +0530142 setUpFlowObjectiveService();
kishoredb79dc02016-03-07 16:24:55 +0530143 controlPlaneRedirectManager.coreService = coreService;
144 controlPlaneRedirectManager.flowObjectiveService = flowObjectiveService;
145 controlPlaneRedirectManager.networkConfigService = networkConfigService;
146 controlPlaneRedirectManager.interfaceService = interfaceService;
147 controlPlaneRedirectManager.deviceService = deviceService;
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700148 controlPlaneRedirectManager.hostService = createNiceMock(HostService.class);
kishoredb79dc02016-03-07 16:24:55 +0530149 controlPlaneRedirectManager.mastershipService = mastershipService;
kishore786b7e42016-05-19 16:25:57 +0530150 controlPlaneRedirectManager.applicationService = applicationService;
kishoredb79dc02016-03-07 16:24:55 +0530151 controlPlaneRedirectManager.activate();
152 verify(flowObjectiveService);
153 }
kishore71a27532016-03-16 20:23:49 +0530154
kishoredb79dc02016-03-07 16:24:55 +0530155 /**
kishore71a27532016-03-16 20:23:49 +0530156 * Tests adding new Device to a openflow router.
157 */
158 @Test
159 public void testAddDevice() {
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700160 ConnectPoint sw1eth4 = new ConnectPoint(DEVICE_ID, PortNumber.portNumber(4));
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530161 List<InterfaceIpAddress> interfaceIpAddresses = new ArrayList<>();
162 interfaceIpAddresses.add(
163 new InterfaceIpAddress(IpAddress.valueOf("192.168.40.101"), IpPrefix.valueOf("192.168.40.0/24"))
164 );
165 interfaceIpAddresses.add(
166 new InterfaceIpAddress(IpAddress.valueOf("2000::ff"), IpPrefix.valueOf("2000::ff/120"))
167 );
kishore71a27532016-03-16 20:23:49 +0530168
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530169 Interface sw1Eth4 = new Interface(sw1eth4.deviceId().toString(), sw1eth4, interfaceIpAddresses,
kishore71a27532016-03-16 20:23:49 +0530170 MacAddress.valueOf("00:00:00:00:00:04"), VlanId.NONE);
171 interfaces.add(sw1Eth4);
172 EasyMock.reset(flowObjectiveService);
173 setUpFlowObjectiveService();
174 deviceListener.event(new DeviceEvent(DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED, dev3));
175 verify(flowObjectiveService);
176 }
177
178 /**
179 * Tests adding while updating the networkConfig.
180 */
181 @Test
182 public void testUpdateNetworkConfig() {
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700183 ConnectPoint sw1eth4 = new ConnectPoint(DEVICE_ID, PortNumber.portNumber(4));
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530184 List<InterfaceIpAddress> interfaceIpAddresses = new ArrayList<>();
185 interfaceIpAddresses.add(
186 new InterfaceIpAddress(IpAddress.valueOf("192.168.40.101"), IpPrefix.valueOf("192.168.40.0/24"))
187 );
188 interfaceIpAddresses.add(
189 new InterfaceIpAddress(IpAddress.valueOf("2000::ff"), IpPrefix.valueOf("2000::ff/120"))
190 );
kishore71a27532016-03-16 20:23:49 +0530191
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530192 Interface sw1Eth4 = new Interface(sw1eth4.deviceId().toString(), sw1eth4, interfaceIpAddresses,
kishore71a27532016-03-16 20:23:49 +0530193 MacAddress.valueOf("00:00:00:00:00:04"), VlanId.NONE);
194 interfaces.add(sw1Eth4);
195 EasyMock.reset(flowObjectiveService);
196 setUpFlowObjectiveService();
197 networkConfigListener
198 .event(new NetworkConfigEvent(Type.CONFIG_UPDATED, dev3, RoutingService.ROUTER_CONFIG_CLASS));
199 networkConfigService.addListener(networkConfigListener);
200 verify(flowObjectiveService);
201 }
202
203 /**
204 * Tests adding while updating the networkConfig.
205 */
206 @Test
207 public void testAddInterface() {
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700208 ConnectPoint sw1eth4 = new ConnectPoint(DEVICE_ID, PortNumber.portNumber(4));
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530209 List<InterfaceIpAddress> interfaceIpAddresses = new ArrayList<>();
210 interfaceIpAddresses.add(
211 new InterfaceIpAddress(IpAddress.valueOf("192.168.40.101"), IpPrefix.valueOf("192.168.40.0/24"))
212 );
213 interfaceIpAddresses.add(
214 new InterfaceIpAddress(IpAddress.valueOf("2000::ff"), IpPrefix.valueOf("2000::ff/120"))
215 );
kishore71a27532016-03-16 20:23:49 +0530216
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530217 Interface sw1Eth4 = new Interface(sw1eth4.deviceId().toString(), sw1eth4, interfaceIpAddresses,
kishore71a27532016-03-16 20:23:49 +0530218 MacAddress.valueOf("00:00:00:00:00:04"), VlanId.NONE);
219 interfaces.add(sw1Eth4);
220
221 EasyMock.reset(flowObjectiveService);
222 expect(flowObjectiveService.allocateNextId()).andReturn(1).anyTimes();
223
224 setUpInterfaceConfiguration(sw1Eth4, true);
225 replay(flowObjectiveService);
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700226 interfaceListener.event(new InterfaceEvent(InterfaceEvent.Type.INTERFACE_ADDED, sw1Eth4, 500L));
kishore71a27532016-03-16 20:23:49 +0530227 verify(flowObjectiveService);
228 }
229
230 @Test
231 public void testRemoveInterface() {
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700232 ConnectPoint sw1eth4 = new ConnectPoint(DEVICE_ID, PortNumber.portNumber(4));
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530233 List<InterfaceIpAddress> interfaceIpAddresses = new ArrayList<>();
234 interfaceIpAddresses.add(
235 new InterfaceIpAddress(IpAddress.valueOf("192.168.40.101"), IpPrefix.valueOf("192.168.40.0/24"))
236 );
237 interfaceIpAddresses.add(
238 new InterfaceIpAddress(IpAddress.valueOf("2000::ff"), IpPrefix.valueOf("2000::ff/120"))
239 );
kishore71a27532016-03-16 20:23:49 +0530240
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530241 Interface sw1Eth4 = new Interface(sw1eth4.deviceId().toString(), sw1eth4, interfaceIpAddresses,
kishore71a27532016-03-16 20:23:49 +0530242 MacAddress.valueOf("00:00:00:00:00:04"), VlanId.NONE);
243 EasyMock.reset(flowObjectiveService);
244 expect(flowObjectiveService.allocateNextId()).andReturn(1).anyTimes();
245
246 setUpInterfaceConfiguration(sw1Eth4, false);
247 replay(flowObjectiveService);
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700248 interfaceListener.event(new InterfaceEvent(InterfaceEvent.Type.INTERFACE_REMOVED, sw1Eth4, 500L));
kishore71a27532016-03-16 20:23:49 +0530249 verify(flowObjectiveService);
250 }
251
252 /**
253 * Setup flow Configuration for all configured Interfaces.
kishoredb79dc02016-03-07 16:24:55 +0530254 *
255 **/
256 private void setUpFlowObjectiveService() {
kishoredb79dc02016-03-07 16:24:55 +0530257 expect(flowObjectiveService.allocateNextId()).andReturn(1).anyTimes();
kishoredb79dc02016-03-07 16:24:55 +0530258 for (Interface intf : interfaceService.getInterfaces()) {
kishore71a27532016-03-16 20:23:49 +0530259 setUpInterfaceConfiguration(intf, true);
kishoredb79dc02016-03-07 16:24:55 +0530260 }
kishoredb79dc02016-03-07 16:24:55 +0530261 replay(flowObjectiveService);
262 }
263
264 /**
kishore71a27532016-03-16 20:23:49 +0530265 * Setting up flowobjective expectations for basic forwarding and ospf.
kishoredb79dc02016-03-07 16:24:55 +0530266 **/
kishore71a27532016-03-16 20:23:49 +0530267 private void setUpInterfaceConfiguration(Interface intf, boolean install) {
268 DeviceId deviceId = controlPlaneConnectPoint.deviceId();
269 PortNumber controlPlanePort = controlPlaneConnectPoint.port();
270
271 for (InterfaceIpAddress ip : intf.ipAddresses()) {
272 int cpNextId, intfNextId;
273
274 cpNextId = modifyNextObjective(deviceId, controlPlanePort,
275 VlanId.vlanId(SingleSwitchFibInstaller.ASSIGNED_VLAN), true, install);
276 intfNextId = modifyNextObjective(deviceId, intf.connectPoint().port(),
277 VlanId.vlanId(SingleSwitchFibInstaller.ASSIGNED_VLAN), true, install);
278
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530279 // IP to router
280 TrafficSelector toSelector = buildIPDstSelector(
281 ip.ipAddress().toIpPrefix(),
282 intf.connectPoint().port(),
283 null,
284 intf.mac(),
285 intf.vlan());
kishore71a27532016-03-16 20:23:49 +0530286
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530287 flowObjectiveService.forward(deviceId, buildForwardingObjective(toSelector, null,
288 cpNextId, install, ACL_PRIORITY));
kishore71a27532016-03-16 20:23:49 +0530289 expectLastCall().once();
kishore71a27532016-03-16 20:23:49 +0530290
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530291 // IP from router
292 TrafficSelector fromSelector = buildIPSrcSelector(
293 ip.ipAddress().toIpPrefix(),
294 controlPlanePort,
295 intf.mac(),
296 null,
297 intf.vlan()
298 );
299
300 flowObjectiveService.forward(deviceId, buildForwardingObjective(fromSelector, null,
301 intfNextId, install, ACL_PRIORITY));
kishore71a27532016-03-16 20:23:49 +0530302 expectLastCall().once();
kishore71a27532016-03-16 20:23:49 +0530303
304 TrafficTreatment puntTreatment = DefaultTrafficTreatment.builder().punt().build();
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530305 if (ip.ipAddress().isIp4()) {
306 // ARP to router
307 toSelector = buildArpSelector(
308 intf.connectPoint().port(),
309 intf.vlan(),
310 null,
311 null
312 );
kishore71a27532016-03-16 20:23:49 +0530313
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530314 flowObjectiveService.forward(deviceId, buildForwardingObjective(toSelector, puntTreatment,
315 cpNextId, install, ACL_PRIORITY + 1));
316 expectLastCall().once();
kishore71a27532016-03-16 20:23:49 +0530317
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530318 // ARP from router
319 fromSelector = buildArpSelector(
320 controlPlanePort,
321 intf.vlan(),
322 ip.ipAddress().getIp4Address(),
323 intf.mac()
324 );
325
326 flowObjectiveService.forward(deviceId,
327 buildForwardingObjective(fromSelector, puntTreatment,
328 intfNextId, install, ACL_PRIORITY + 1));
329 expectLastCall().once();
330 } else {
331 // NDP solicitation to router
332 toSelector = buildNdpSelector(
333 intf.connectPoint().port(),
334 intf.vlan(),
335 null,
336 NEIGHBOR_SOLICITATION,
337 null
338 );
339
340 flowObjectiveService.forward(deviceId, buildForwardingObjective(toSelector, puntTreatment,
341 cpNextId, install, ACL_PRIORITY + 1));
342 expectLastCall().once();
343
344 // NDP solicitation from router
345 fromSelector = buildNdpSelector(
346 controlPlanePort,
347 intf.vlan(),
348 ip.ipAddress().toIpPrefix(),
349 NEIGHBOR_SOLICITATION,
350 intf.mac()
351 );
352
353 flowObjectiveService.forward(deviceId,
354 buildForwardingObjective(fromSelector, puntTreatment,
355 intfNextId, install, ACL_PRIORITY + 1));
356
357 // NDP advertisement to router
358 toSelector = buildNdpSelector(
359 intf.connectPoint().port(),
360 intf.vlan(),
361 null,
362 NEIGHBOR_ADVERTISEMENT,
363 null
364 );
365
366 flowObjectiveService.forward(deviceId, buildForwardingObjective(toSelector, puntTreatment,
367 cpNextId, install, ACL_PRIORITY + 1));
368 expectLastCall().once();
369
370 // NDP advertisement from router
371 fromSelector = buildNdpSelector(
372 controlPlanePort,
373 intf.vlan(),
374 ip.ipAddress().toIpPrefix(),
375 NEIGHBOR_ADVERTISEMENT,
376 intf.mac()
377 );
378
379 flowObjectiveService.forward(deviceId,
380 buildForwardingObjective(fromSelector, puntTreatment,
381 intfNextId, install, ACL_PRIORITY + 1));
382 }
kishore71a27532016-03-16 20:23:49 +0530383 }
384 // setting expectations for ospf forwarding.
kishoredb79dc02016-03-07 16:24:55 +0530385 TrafficSelector toSelector = DefaultTrafficSelector.builder().matchInPort(intf.connectPoint().port())
386 .matchEthType(EthType.EtherType.IPV4.ethType().toShort()).matchVlanId(intf.vlan())
387 .matchIPProtocol((byte) OSPF_IP_PROTO).build();
388
kishore71a27532016-03-16 20:23:49 +0530389 modifyNextObjective(deviceId, controlPlanePort, VlanId.vlanId((short) 4094), true, install);
kishoredb79dc02016-03-07 16:24:55 +0530390 flowObjectiveService.forward(controlPlaneConnectPoint.deviceId(),
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530391 buildForwardingObjective(toSelector, null, 1, install, 40001));
kishoredb79dc02016-03-07 16:24:55 +0530392 expectLastCall().once();
393 }
394
395 /**
kishore71a27532016-03-16 20:23:49 +0530396 * Setup expectations on flowObjectiveService.next for NextObjective.
kishoredb79dc02016-03-07 16:24:55 +0530397 *
398 **/
kishore71a27532016-03-16 20:23:49 +0530399 private int modifyNextObjective(DeviceId deviceId, PortNumber portNumber, VlanId vlanId, boolean popVlan,
400 boolean modifyFlag) {
kishoredb79dc02016-03-07 16:24:55 +0530401 NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder().withId(1)
402 .withType(NextObjective.Type.SIMPLE).fromApp(APPID);
403
404 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
405 if (popVlan) {
406 ttBuilder.popVlan();
407 }
408 ttBuilder.setOutput(portNumber);
409
kishoredb79dc02016-03-07 16:24:55 +0530410 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder();
411 metabuilder.matchVlanId(vlanId);
412
413 nextObjBuilder.withMeta(metabuilder.build());
414 nextObjBuilder.addTreatment(ttBuilder.build());
kishore71a27532016-03-16 20:23:49 +0530415 if (modifyFlag) {
416 flowObjectiveService.next(deviceId, nextObjBuilder.add());
417 expectLastCall().once();
418 } else {
419 flowObjectiveService.next(deviceId, nextObjBuilder.remove());
420 expectLastCall().once();
421 }
kishoredb79dc02016-03-07 16:24:55 +0530422 return 1;
423 }
424
425 /**
kishore71a27532016-03-16 20:23:49 +0530426 * Setup Interface expectation for all Testcases.
kishoredb79dc02016-03-07 16:24:55 +0530427 **/
428 private void setUpInterfaceService() {
Sho SHIMIZU891162b2016-06-06 16:32:01 -0700429 List<InterfaceIpAddress> interfaceIpAddresses1 = new ArrayList<>();
kishoredb79dc02016-03-07 16:24:55 +0530430 interfaceIpAddresses1
431 .add(new InterfaceIpAddress(IpAddress.valueOf("192.168.10.101"), IpPrefix.valueOf("192.168.10.0/24")));
432 Interface sw1Eth1 = new Interface(SW1_ETH1.deviceId().toString(), SW1_ETH1, interfaceIpAddresses1,
433 MacAddress.valueOf("00:00:00:00:00:01"), VlanId.NONE);
434 interfaces.add(sw1Eth1);
435
Sho SHIMIZU891162b2016-06-06 16:32:01 -0700436 List<InterfaceIpAddress> interfaceIpAddresses2 = new ArrayList<>();
kishoredb79dc02016-03-07 16:24:55 +0530437 interfaceIpAddresses2
438 .add(new InterfaceIpAddress(IpAddress.valueOf("192.168.20.101"), IpPrefix.valueOf("192.168.20.0/24")));
439 Interface sw1Eth2 = new Interface(SW1_ETH1.deviceId().toString(), SW1_ETH2, interfaceIpAddresses2,
440 MacAddress.valueOf("00:00:00:00:00:02"), VlanId.NONE);
441 interfaces.add(sw1Eth2);
442
Sho SHIMIZU891162b2016-06-06 16:32:01 -0700443 List<InterfaceIpAddress> interfaceIpAddresses3 = new ArrayList<>();
kishoredb79dc02016-03-07 16:24:55 +0530444 interfaceIpAddresses3
445 .add(new InterfaceIpAddress(IpAddress.valueOf("192.168.30.101"), IpPrefix.valueOf("192.168.30.0/24")));
446 Interface sw1Eth3 = new Interface(SW1_ETH1.deviceId().toString(), SW1_ETH3, interfaceIpAddresses3,
447 MacAddress.valueOf("00:00:00:00:00:03"), VlanId.NONE);
448 interfaces.add(sw1Eth3);
449
kishoredb79dc02016-03-07 16:24:55 +0530450 }
451
kishoredb79dc02016-03-07 16:24:55 +0530452 private ForwardingObjective buildForwardingObjective(TrafficSelector selector, TrafficTreatment treatment,
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530453 int nextId, boolean add, int priority) {
kishoredb79dc02016-03-07 16:24:55 +0530454 DefaultForwardingObjective.Builder fobBuilder = DefaultForwardingObjective.builder();
455 fobBuilder.withSelector(selector);
456 if (treatment != null) {
457 fobBuilder.withTreatment(treatment);
458 }
459 if (nextId != -1) {
460 fobBuilder.nextStep(nextId);
461 }
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530462 fobBuilder.fromApp(APPID).withPriority(priority).withFlag(ForwardingObjective.Flag.VERSATILE);
kishoredb79dc02016-03-07 16:24:55 +0530463
464 return add ? fobBuilder.add() : fobBuilder.remove();
465 }
466
kishoredb79dc02016-03-07 16:24:55 +0530467 private class TestCoreService extends CoreServiceAdapter {
kishore71a27532016-03-16 20:23:49 +0530468
kishoredb79dc02016-03-07 16:24:55 +0530469 @Override
470 public ApplicationId getAppId(String name) {
471 return APPID;
472 }
473
474 @Override
475 public ApplicationId registerApplication(String name) {
kishoredb79dc02016-03-07 16:24:55 +0530476 return new TestApplicationId(name);
477 }
478
479 }
480
481 private class TestDeviceService extends DeviceServiceAdapter {
482
483 @Override
484 public boolean isAvailable(DeviceId deviceId) {
kishoredb79dc02016-03-07 16:24:55 +0530485 boolean flag = false;
486 if (deviceId.equals(controlPlaneConnectPoint.deviceId())) {
487 flag = true;
488 }
489 return flag;
490 }
491
492 @Override
493 public void addListener(DeviceListener listener) {
kishoredb79dc02016-03-07 16:24:55 +0530494 ControlPlaneRedirectManagerTest.this.deviceListener = listener;
495 }
496
497 }
498
kishoredb79dc02016-03-07 16:24:55 +0530499 private class TestRouterConfig extends RouterConfig {
500
501 @Override
502 public ConnectPoint getControlPlaneConnectPoint() {
kishoredb79dc02016-03-07 16:24:55 +0530503 return controlPlaneConnectPoint;
504 }
505
506 @Override
507 public boolean getOspfEnabled() {
kishoredb79dc02016-03-07 16:24:55 +0530508 return true;
509 }
510
511 @Override
512 public List<String> getInterfaces() {
kishoredb79dc02016-03-07 16:24:55 +0530513 ArrayList<String> interfaces = new ArrayList<>();
514 interfaces.add("of:0000000000000001");
515 interfaces.add("of:0000000000000001/2");
516 interfaces.add("of:0000000000000001/3");
517 return interfaces;
518 }
519
520 }
521
522 private class TestNetworkConfigService extends NetworkConfigServiceAdapter {
523
524 @Override
525 public void addListener(NetworkConfigListener listener) {
kishoredb79dc02016-03-07 16:24:55 +0530526 ControlPlaneRedirectManagerTest.this.networkConfigListener = listener;
527 }
528
529 @Override
530 public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) {
kishoredb79dc02016-03-07 16:24:55 +0530531 return (C) ControlPlaneRedirectManagerTest.this.routerConfig;
532 }
533
534 }
535
536 private static class TestApplicationId implements ApplicationId {
537
538 private final String name;
539 private final short id;
540
541 public TestApplicationId(String name) {
542 this.name = name;
543 this.id = (short) Objects.hash(name);
544 }
545
546 public static ApplicationId create(String name) {
547 return new TestApplicationId(name);
548 }
549
550 @Override
551 public short id() {
552 return id;
553 }
554
555 @Override
556 public String name() {
557 return name;
558 }
559
560 @Override
561 public int hashCode() {
562 final int prime = 31;
563 int result = 1;
564 result = prime * result + id;
565 result = prime * result + ((name == null) ? 0 : name.hashCode());
566 return result;
567 }
568
569 @Override
570 public boolean equals(Object obj) {
571 if (this == obj) {
572 return true;
573 }
574 if (obj == null) {
575 return false;
576 }
577 if (getClass() != obj.getClass()) {
578 return false;
579 }
580 TestApplicationId other = (TestApplicationId) obj;
581 if (id != other.id) {
582 return false;
583 }
584 if (name == null) {
585 if (other.name != null) {
586 return false;
kishore71a27532016-03-16 20:23:49 +0530587 }
kishoredb79dc02016-03-07 16:24:55 +0530588 } else if (!name.equals(other.name)) {
589 return false;
590 }
591 return true;
592 }
593 }
594
595 private class InternalMastershipServiceTest extends MastershipServiceAdapter {
596
597 @Override
598 public boolean isLocalMaster(DeviceId deviceId) {
kishoredb79dc02016-03-07 16:24:55 +0530599 boolean flag = deviceId.equals(controlPlaneConnectPoint.deviceId());
600 return flag;
601 }
602
603 }
kishore71a27532016-03-16 20:23:49 +0530604
605 private class InternalInterfaceService extends InterfaceServiceAdapter {
606
607 @Override
608 public void addListener(InterfaceListener listener) {
609 ControlPlaneRedirectManagerTest.this.interfaceListener = listener;
610 }
611
612 @Override
613 public Set<Interface> getInterfaces() {
614 return interfaces;
615 }
616
617 @Override
618 public Set<Interface> getInterfacesByPort(ConnectPoint port) {
619 Set<Interface> setIntf = new HashSet<Interface>();
620 for (Interface intf : interfaces) {
621 if (intf.connectPoint().equals(port)) {
622 setIntf.add(intf);
623 }
624 }
625 return setIntf;
626 }
627
628 @Override
629 public Interface getMatchingInterface(IpAddress ip) {
630 Interface intff = null;
631 for (Interface intf : interfaces) {
632 for (InterfaceIpAddress address : intf.ipAddresses()) {
633 if (address.ipAddress().equals(ip)) {
634 intff = intf;
635 break;
636 }
637 }
638 }
639
640 return intff;
641 }
642
643 }
kishoredb79dc02016-03-07 16:24:55 +0530644}