blob: 1291f8f11e9190c0e9dbd88779ada347aa9a0420 [file] [log] [blame]
kishoredb79dc02016-03-07 16:24:55 +05301/*
Jonathan Hartf4bd0482017-01-27 15:11:18 -08002 * Copyright 2017-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 */
Jonathan Hartf4bd0482017-01-27 15:11:18 -080016
17package org.onosproject.routing.cpr;
kishoredb79dc02016-03-07 16:24:55 +053018
Jonathan Hartf04b7d92016-03-29 09:39:11 -070019import com.google.common.collect.Sets;
kishoredb79dc02016-03-07 16:24:55 +053020import org.easymock.EasyMock;
21import org.junit.Before;
Jonathan Hartf8cd0522016-10-25 07:09:55 -070022import org.junit.Ignore;
kishoredb79dc02016-03-07 16:24:55 +053023import org.junit.Test;
24import org.onlab.packet.EthType;
kishoredb79dc02016-03-07 16:24:55 +053025import org.onlab.packet.IpAddress;
26import org.onlab.packet.IpPrefix;
27import org.onlab.packet.MacAddress;
28import org.onlab.packet.VlanId;
kishore786b7e42016-05-19 16:25:57 +053029import org.onosproject.app.ApplicationService;
kishoredb79dc02016-03-07 16:24:55 +053030import org.onosproject.core.ApplicationId;
31import org.onosproject.core.CoreService;
32import org.onosproject.core.CoreServiceAdapter;
33import org.onosproject.incubator.net.intf.Interface;
kishore71a27532016-03-16 20:23:49 +053034import org.onosproject.incubator.net.intf.InterfaceEvent;
35import org.onosproject.incubator.net.intf.InterfaceListener;
kishoredb79dc02016-03-07 16:24:55 +053036import org.onosproject.incubator.net.intf.InterfaceService;
kishore71a27532016-03-16 20:23:49 +053037import org.onosproject.incubator.net.intf.InterfaceServiceAdapter;
kishoredb79dc02016-03-07 16:24:55 +053038import org.onosproject.mastership.MastershipService;
39import org.onosproject.mastership.MastershipServiceAdapter;
40import org.onosproject.net.ConnectPoint;
41import org.onosproject.net.Device;
42import org.onosproject.net.DeviceId;
43import org.onosproject.net.NetTestTools;
44import org.onosproject.net.PortNumber;
45import org.onosproject.net.config.Config;
46import org.onosproject.net.config.NetworkConfigEvent;
47import org.onosproject.net.config.NetworkConfigEvent.Type;
48import org.onosproject.net.config.NetworkConfigListener;
49import org.onosproject.net.config.NetworkConfigService;
50import org.onosproject.net.config.NetworkConfigServiceAdapter;
51import org.onosproject.net.device.DeviceEvent;
52import org.onosproject.net.device.DeviceListener;
53import org.onosproject.net.device.DeviceService;
54import org.onosproject.net.device.DeviceServiceAdapter;
55import org.onosproject.net.flow.DefaultTrafficSelector;
56import org.onosproject.net.flow.DefaultTrafficTreatment;
57import org.onosproject.net.flow.TrafficSelector;
58import org.onosproject.net.flow.TrafficTreatment;
59import org.onosproject.net.flowobjective.DefaultForwardingObjective;
60import org.onosproject.net.flowobjective.DefaultNextObjective;
61import org.onosproject.net.flowobjective.FlowObjectiveService;
62import org.onosproject.net.flowobjective.ForwardingObjective;
63import org.onosproject.net.flowobjective.NextObjective;
kishoredb79dc02016-03-07 16:24:55 +053064import org.onosproject.net.host.HostService;
kishoredb79dc02016-03-07 16:24:55 +053065import org.onosproject.net.host.InterfaceIpAddress;
kishoredb79dc02016-03-07 16:24:55 +053066import org.onosproject.routing.RoutingService;
67import org.onosproject.routing.config.RouterConfig;
kishoredb79dc02016-03-07 16:24:55 +053068
Jonathan Hartf04b7d92016-03-29 09:39:11 -070069import java.util.ArrayList;
70import java.util.HashSet;
71import java.util.List;
72import java.util.Objects;
73import java.util.Set;
74
75import static org.easymock.EasyMock.createMock;
76import static org.easymock.EasyMock.createNiceMock;
77import static org.easymock.EasyMock.expect;
78import static org.easymock.EasyMock.expectLastCall;
79import static org.easymock.EasyMock.replay;
80import static org.easymock.EasyMock.verify;
Vinayak Tejankar3a409c62017-01-12 02:20:53 +053081import static org.onlab.packet.ICMP6.NEIGHBOR_ADVERTISEMENT;
82import static org.onlab.packet.ICMP6.NEIGHBOR_SOLICITATION;
Jonathan Hartf4bd0482017-01-27 15:11:18 -080083import static org.onosproject.routing.cpr.ControlPlaneRedirectManager.ACL_PRIORITY;
84import static org.onosproject.routing.cpr.ControlPlaneRedirectManager.buildArpSelector;
85import static org.onosproject.routing.cpr.ControlPlaneRedirectManager.buildIPDstSelector;
86import static org.onosproject.routing.cpr.ControlPlaneRedirectManager.buildIPSrcSelector;
87import static org.onosproject.routing.cpr.ControlPlaneRedirectManager.buildNdpSelector;
kishoredb79dc02016-03-07 16:24:55 +053088
89/**
90 * UnitTests for ControlPlaneRedirectManager.
91 */
Jonathan Hartf8cd0522016-10-25 07:09:55 -070092@Ignore("Too many dependencies on internal implementation, too hard to maintain")
Jonathan Hartf04b7d92016-03-29 09:39:11 -070093public class ControlPlaneRedirectManagerTest {
kishoredb79dc02016-03-07 16:24:55 +053094
kishoredb79dc02016-03-07 16:24:55 +053095 private DeviceService deviceService;
96 private FlowObjectiveService flowObjectiveService;
97 private NetworkConfigService networkConfigService;
98 private final Set<Interface> interfaces = Sets.newHashSet();
99 static Device dev3 = NetTestTools.device("0000000000000001");
100 private static final int OSPF_IP_PROTO = 0x59;
101 private CoreService coreService = new TestCoreService();
102 private InterfaceService interfaceService;
kishore786b7e42016-05-19 16:25:57 +0530103 private static final ApplicationId APPID = TestApplicationId.create("org.onosproject.vrouter");
kishoredb79dc02016-03-07 16:24:55 +0530104
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700105 private static final DeviceId DEVICE_ID = DeviceId.deviceId("of:0000000000000001");
106
107 private ConnectPoint controlPlaneConnectPoint = new ConnectPoint(DEVICE_ID,
kishoredb79dc02016-03-07 16:24:55 +0530108 PortNumber.portNumber(1));
109
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700110 private static final ConnectPoint SW1_ETH1 = new ConnectPoint(DEVICE_ID,
111 PortNumber.portNumber(1));
112
113 private static final ConnectPoint SW1_ETH2 = new ConnectPoint(DEVICE_ID,
kishoredb79dc02016-03-07 16:24:55 +0530114 PortNumber.portNumber(2));
115
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700116 private static final ConnectPoint SW1_ETH3 = new ConnectPoint(DEVICE_ID,
kishoredb79dc02016-03-07 16:24:55 +0530117 PortNumber.portNumber(3));
kishoredb79dc02016-03-07 16:24:55 +0530118
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700119 private ControlPlaneRedirectManager controlPlaneRedirectManager = new ControlPlaneRedirectManager();
kishoredb79dc02016-03-07 16:24:55 +0530120 private RouterConfig routerConfig = new TestRouterConfig();
121 private NetworkConfigListener networkConfigListener;
122 private DeviceListener deviceListener;
123 private MastershipService mastershipService = new InternalMastershipServiceTest();
kishore71a27532016-03-16 20:23:49 +0530124 private InterfaceListener interfaceListener;
kishore786b7e42016-05-19 16:25:57 +0530125 private ApplicationService applicationService;
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700126
kishoredb79dc02016-03-07 16:24:55 +0530127 @Before
kishore71a27532016-03-16 20:23:49 +0530128 public void setUp() {
kishoredb79dc02016-03-07 16:24:55 +0530129 networkConfigListener = createMock(NetworkConfigListener.class);
kishoredb79dc02016-03-07 16:24:55 +0530130 deviceService = new TestDeviceService();
131 deviceListener = createMock(DeviceListener.class);
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700132
kishore71a27532016-03-16 20:23:49 +0530133 interfaceListener = createMock(InterfaceListener.class);
kishoredb79dc02016-03-07 16:24:55 +0530134 deviceService.addListener(deviceListener);
135 setUpInterfaceService();
kishore71a27532016-03-16 20:23:49 +0530136 interfaceService = new InternalInterfaceService();
137 interfaceService.addListener(interfaceListener);
kishoredb79dc02016-03-07 16:24:55 +0530138 networkConfigService = new TestNetworkConfigService();
139 networkConfigService.addListener(networkConfigListener);
140 flowObjectiveService = createMock(FlowObjectiveService.class);
kishore786b7e42016-05-19 16:25:57 +0530141 applicationService = createNiceMock(ApplicationService.class);
142 replay(applicationService);
kishoredb79dc02016-03-07 16:24:55 +0530143 setUpFlowObjectiveService();
kishoredb79dc02016-03-07 16:24:55 +0530144 controlPlaneRedirectManager.coreService = coreService;
145 controlPlaneRedirectManager.flowObjectiveService = flowObjectiveService;
146 controlPlaneRedirectManager.networkConfigService = networkConfigService;
147 controlPlaneRedirectManager.interfaceService = interfaceService;
148 controlPlaneRedirectManager.deviceService = deviceService;
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700149 controlPlaneRedirectManager.hostService = createNiceMock(HostService.class);
kishoredb79dc02016-03-07 16:24:55 +0530150 controlPlaneRedirectManager.mastershipService = mastershipService;
kishore786b7e42016-05-19 16:25:57 +0530151 controlPlaneRedirectManager.applicationService = applicationService;
kishoredb79dc02016-03-07 16:24:55 +0530152 controlPlaneRedirectManager.activate();
153 verify(flowObjectiveService);
154 }
kishore71a27532016-03-16 20:23:49 +0530155
kishoredb79dc02016-03-07 16:24:55 +0530156 /**
kishore71a27532016-03-16 20:23:49 +0530157 * Tests adding new Device to a openflow router.
158 */
159 @Test
160 public void testAddDevice() {
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700161 ConnectPoint sw1eth4 = new ConnectPoint(DEVICE_ID, PortNumber.portNumber(4));
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530162 List<InterfaceIpAddress> interfaceIpAddresses = new ArrayList<>();
163 interfaceIpAddresses.add(
164 new InterfaceIpAddress(IpAddress.valueOf("192.168.40.101"), IpPrefix.valueOf("192.168.40.0/24"))
165 );
166 interfaceIpAddresses.add(
167 new InterfaceIpAddress(IpAddress.valueOf("2000::ff"), IpPrefix.valueOf("2000::ff/120"))
168 );
kishore71a27532016-03-16 20:23:49 +0530169
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530170 Interface sw1Eth4 = new Interface(sw1eth4.deviceId().toString(), sw1eth4, interfaceIpAddresses,
kishore71a27532016-03-16 20:23:49 +0530171 MacAddress.valueOf("00:00:00:00:00:04"), VlanId.NONE);
172 interfaces.add(sw1Eth4);
173 EasyMock.reset(flowObjectiveService);
174 setUpFlowObjectiveService();
175 deviceListener.event(new DeviceEvent(DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED, dev3));
176 verify(flowObjectiveService);
177 }
178
179 /**
180 * Tests adding while updating the networkConfig.
181 */
182 @Test
183 public void testUpdateNetworkConfig() {
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700184 ConnectPoint sw1eth4 = new ConnectPoint(DEVICE_ID, PortNumber.portNumber(4));
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530185 List<InterfaceIpAddress> interfaceIpAddresses = new ArrayList<>();
186 interfaceIpAddresses.add(
187 new InterfaceIpAddress(IpAddress.valueOf("192.168.40.101"), IpPrefix.valueOf("192.168.40.0/24"))
188 );
189 interfaceIpAddresses.add(
190 new InterfaceIpAddress(IpAddress.valueOf("2000::ff"), IpPrefix.valueOf("2000::ff/120"))
191 );
kishore71a27532016-03-16 20:23:49 +0530192
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530193 Interface sw1Eth4 = new Interface(sw1eth4.deviceId().toString(), sw1eth4, interfaceIpAddresses,
kishore71a27532016-03-16 20:23:49 +0530194 MacAddress.valueOf("00:00:00:00:00:04"), VlanId.NONE);
195 interfaces.add(sw1Eth4);
196 EasyMock.reset(flowObjectiveService);
197 setUpFlowObjectiveService();
198 networkConfigListener
199 .event(new NetworkConfigEvent(Type.CONFIG_UPDATED, dev3, RoutingService.ROUTER_CONFIG_CLASS));
200 networkConfigService.addListener(networkConfigListener);
201 verify(flowObjectiveService);
202 }
203
204 /**
205 * Tests adding while updating the networkConfig.
206 */
207 @Test
208 public void testAddInterface() {
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700209 ConnectPoint sw1eth4 = new ConnectPoint(DEVICE_ID, PortNumber.portNumber(4));
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530210 List<InterfaceIpAddress> interfaceIpAddresses = new ArrayList<>();
211 interfaceIpAddresses.add(
212 new InterfaceIpAddress(IpAddress.valueOf("192.168.40.101"), IpPrefix.valueOf("192.168.40.0/24"))
213 );
214 interfaceIpAddresses.add(
215 new InterfaceIpAddress(IpAddress.valueOf("2000::ff"), IpPrefix.valueOf("2000::ff/120"))
216 );
kishore71a27532016-03-16 20:23:49 +0530217
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530218 Interface sw1Eth4 = new Interface(sw1eth4.deviceId().toString(), sw1eth4, interfaceIpAddresses,
kishore71a27532016-03-16 20:23:49 +0530219 MacAddress.valueOf("00:00:00:00:00:04"), VlanId.NONE);
220 interfaces.add(sw1Eth4);
221
222 EasyMock.reset(flowObjectiveService);
223 expect(flowObjectiveService.allocateNextId()).andReturn(1).anyTimes();
224
225 setUpInterfaceConfiguration(sw1Eth4, true);
226 replay(flowObjectiveService);
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700227 interfaceListener.event(new InterfaceEvent(InterfaceEvent.Type.INTERFACE_ADDED, sw1Eth4, 500L));
kishore71a27532016-03-16 20:23:49 +0530228 verify(flowObjectiveService);
229 }
230
231 @Test
232 public void testRemoveInterface() {
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700233 ConnectPoint sw1eth4 = new ConnectPoint(DEVICE_ID, PortNumber.portNumber(4));
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530234 List<InterfaceIpAddress> interfaceIpAddresses = new ArrayList<>();
235 interfaceIpAddresses.add(
236 new InterfaceIpAddress(IpAddress.valueOf("192.168.40.101"), IpPrefix.valueOf("192.168.40.0/24"))
237 );
238 interfaceIpAddresses.add(
239 new InterfaceIpAddress(IpAddress.valueOf("2000::ff"), IpPrefix.valueOf("2000::ff/120"))
240 );
kishore71a27532016-03-16 20:23:49 +0530241
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530242 Interface sw1Eth4 = new Interface(sw1eth4.deviceId().toString(), sw1eth4, interfaceIpAddresses,
kishore71a27532016-03-16 20:23:49 +0530243 MacAddress.valueOf("00:00:00:00:00:04"), VlanId.NONE);
244 EasyMock.reset(flowObjectiveService);
245 expect(flowObjectiveService.allocateNextId()).andReturn(1).anyTimes();
246
247 setUpInterfaceConfiguration(sw1Eth4, false);
248 replay(flowObjectiveService);
Jonathan Hartf04b7d92016-03-29 09:39:11 -0700249 interfaceListener.event(new InterfaceEvent(InterfaceEvent.Type.INTERFACE_REMOVED, sw1Eth4, 500L));
kishore71a27532016-03-16 20:23:49 +0530250 verify(flowObjectiveService);
251 }
252
253 /**
254 * Setup flow Configuration for all configured Interfaces.
kishoredb79dc02016-03-07 16:24:55 +0530255 *
256 **/
257 private void setUpFlowObjectiveService() {
kishoredb79dc02016-03-07 16:24:55 +0530258 expect(flowObjectiveService.allocateNextId()).andReturn(1).anyTimes();
kishoredb79dc02016-03-07 16:24:55 +0530259 for (Interface intf : interfaceService.getInterfaces()) {
kishore71a27532016-03-16 20:23:49 +0530260 setUpInterfaceConfiguration(intf, true);
kishoredb79dc02016-03-07 16:24:55 +0530261 }
kishoredb79dc02016-03-07 16:24:55 +0530262 replay(flowObjectiveService);
263 }
264
265 /**
kishore71a27532016-03-16 20:23:49 +0530266 * Setting up flowobjective expectations for basic forwarding and ospf.
kishoredb79dc02016-03-07 16:24:55 +0530267 **/
kishore71a27532016-03-16 20:23:49 +0530268 private void setUpInterfaceConfiguration(Interface intf, boolean install) {
269 DeviceId deviceId = controlPlaneConnectPoint.deviceId();
270 PortNumber controlPlanePort = controlPlaneConnectPoint.port();
271
272 for (InterfaceIpAddress ip : intf.ipAddresses()) {
273 int cpNextId, intfNextId;
274
275 cpNextId = modifyNextObjective(deviceId, controlPlanePort,
Jonathan Hartf4bd0482017-01-27 15:11:18 -0800276 VlanId.vlanId(ControlPlaneRedirectManager.ASSIGNED_VLAN), true, install);
kishore71a27532016-03-16 20:23:49 +0530277 intfNextId = modifyNextObjective(deviceId, intf.connectPoint().port(),
Jonathan Hartf4bd0482017-01-27 15:11:18 -0800278 VlanId.vlanId(ControlPlaneRedirectManager.ASSIGNED_VLAN), true, install);
kishore71a27532016-03-16 20:23:49 +0530279
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530280 // IP to router
281 TrafficSelector toSelector = buildIPDstSelector(
282 ip.ipAddress().toIpPrefix(),
283 intf.connectPoint().port(),
284 null,
285 intf.mac(),
286 intf.vlan());
kishore71a27532016-03-16 20:23:49 +0530287
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530288 flowObjectiveService.forward(deviceId, buildForwardingObjective(toSelector, null,
289 cpNextId, install, ACL_PRIORITY));
kishore71a27532016-03-16 20:23:49 +0530290 expectLastCall().once();
kishore71a27532016-03-16 20:23:49 +0530291
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530292 // IP from router
293 TrafficSelector fromSelector = buildIPSrcSelector(
294 ip.ipAddress().toIpPrefix(),
295 controlPlanePort,
296 intf.mac(),
297 null,
298 intf.vlan()
299 );
300
301 flowObjectiveService.forward(deviceId, buildForwardingObjective(fromSelector, null,
302 intfNextId, install, ACL_PRIORITY));
kishore71a27532016-03-16 20:23:49 +0530303 expectLastCall().once();
kishore71a27532016-03-16 20:23:49 +0530304
305 TrafficTreatment puntTreatment = DefaultTrafficTreatment.builder().punt().build();
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530306 if (ip.ipAddress().isIp4()) {
307 // ARP to router
308 toSelector = buildArpSelector(
309 intf.connectPoint().port(),
310 intf.vlan(),
311 null,
312 null
313 );
kishore71a27532016-03-16 20:23:49 +0530314
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530315 flowObjectiveService.forward(deviceId, buildForwardingObjective(toSelector, puntTreatment,
316 cpNextId, install, ACL_PRIORITY + 1));
317 expectLastCall().once();
kishore71a27532016-03-16 20:23:49 +0530318
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530319 // ARP from router
320 fromSelector = buildArpSelector(
321 controlPlanePort,
322 intf.vlan(),
323 ip.ipAddress().getIp4Address(),
324 intf.mac()
325 );
326
327 flowObjectiveService.forward(deviceId,
328 buildForwardingObjective(fromSelector, puntTreatment,
329 intfNextId, install, ACL_PRIORITY + 1));
330 expectLastCall().once();
331 } else {
332 // NDP solicitation to router
333 toSelector = buildNdpSelector(
334 intf.connectPoint().port(),
335 intf.vlan(),
336 null,
337 NEIGHBOR_SOLICITATION,
338 null
339 );
340
341 flowObjectiveService.forward(deviceId, buildForwardingObjective(toSelector, puntTreatment,
342 cpNextId, install, ACL_PRIORITY + 1));
343 expectLastCall().once();
344
345 // NDP solicitation from router
346 fromSelector = buildNdpSelector(
347 controlPlanePort,
348 intf.vlan(),
349 ip.ipAddress().toIpPrefix(),
350 NEIGHBOR_SOLICITATION,
351 intf.mac()
352 );
353
354 flowObjectiveService.forward(deviceId,
355 buildForwardingObjective(fromSelector, puntTreatment,
356 intfNextId, install, ACL_PRIORITY + 1));
357
358 // NDP advertisement to router
359 toSelector = buildNdpSelector(
360 intf.connectPoint().port(),
361 intf.vlan(),
362 null,
363 NEIGHBOR_ADVERTISEMENT,
364 null
365 );
366
367 flowObjectiveService.forward(deviceId, buildForwardingObjective(toSelector, puntTreatment,
368 cpNextId, install, ACL_PRIORITY + 1));
369 expectLastCall().once();
370
371 // NDP advertisement from router
372 fromSelector = buildNdpSelector(
373 controlPlanePort,
374 intf.vlan(),
375 ip.ipAddress().toIpPrefix(),
376 NEIGHBOR_ADVERTISEMENT,
377 intf.mac()
378 );
379
380 flowObjectiveService.forward(deviceId,
381 buildForwardingObjective(fromSelector, puntTreatment,
382 intfNextId, install, ACL_PRIORITY + 1));
383 }
kishore71a27532016-03-16 20:23:49 +0530384 }
385 // setting expectations for ospf forwarding.
kishoredb79dc02016-03-07 16:24:55 +0530386 TrafficSelector toSelector = DefaultTrafficSelector.builder().matchInPort(intf.connectPoint().port())
387 .matchEthType(EthType.EtherType.IPV4.ethType().toShort()).matchVlanId(intf.vlan())
388 .matchIPProtocol((byte) OSPF_IP_PROTO).build();
389
kishore71a27532016-03-16 20:23:49 +0530390 modifyNextObjective(deviceId, controlPlanePort, VlanId.vlanId((short) 4094), true, install);
kishoredb79dc02016-03-07 16:24:55 +0530391 flowObjectiveService.forward(controlPlaneConnectPoint.deviceId(),
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530392 buildForwardingObjective(toSelector, null, 1, install, 40001));
kishoredb79dc02016-03-07 16:24:55 +0530393 expectLastCall().once();
394 }
395
396 /**
kishore71a27532016-03-16 20:23:49 +0530397 * Setup expectations on flowObjectiveService.next for NextObjective.
kishoredb79dc02016-03-07 16:24:55 +0530398 *
399 **/
kishore71a27532016-03-16 20:23:49 +0530400 private int modifyNextObjective(DeviceId deviceId, PortNumber portNumber, VlanId vlanId, boolean popVlan,
401 boolean modifyFlag) {
kishoredb79dc02016-03-07 16:24:55 +0530402 NextObjective.Builder nextObjBuilder = DefaultNextObjective.builder().withId(1)
403 .withType(NextObjective.Type.SIMPLE).fromApp(APPID);
404
405 TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder();
406 if (popVlan) {
407 ttBuilder.popVlan();
408 }
409 ttBuilder.setOutput(portNumber);
410
kishoredb79dc02016-03-07 16:24:55 +0530411 TrafficSelector.Builder metabuilder = DefaultTrafficSelector.builder();
412 metabuilder.matchVlanId(vlanId);
413
414 nextObjBuilder.withMeta(metabuilder.build());
415 nextObjBuilder.addTreatment(ttBuilder.build());
kishore71a27532016-03-16 20:23:49 +0530416 if (modifyFlag) {
417 flowObjectiveService.next(deviceId, nextObjBuilder.add());
418 expectLastCall().once();
419 } else {
420 flowObjectiveService.next(deviceId, nextObjBuilder.remove());
421 expectLastCall().once();
422 }
kishoredb79dc02016-03-07 16:24:55 +0530423 return 1;
424 }
425
426 /**
kishore71a27532016-03-16 20:23:49 +0530427 * Setup Interface expectation for all Testcases.
kishoredb79dc02016-03-07 16:24:55 +0530428 **/
429 private void setUpInterfaceService() {
Sho SHIMIZU891162b2016-06-06 16:32:01 -0700430 List<InterfaceIpAddress> interfaceIpAddresses1 = new ArrayList<>();
kishoredb79dc02016-03-07 16:24:55 +0530431 interfaceIpAddresses1
432 .add(new InterfaceIpAddress(IpAddress.valueOf("192.168.10.101"), IpPrefix.valueOf("192.168.10.0/24")));
433 Interface sw1Eth1 = new Interface(SW1_ETH1.deviceId().toString(), SW1_ETH1, interfaceIpAddresses1,
434 MacAddress.valueOf("00:00:00:00:00:01"), VlanId.NONE);
435 interfaces.add(sw1Eth1);
436
Sho SHIMIZU891162b2016-06-06 16:32:01 -0700437 List<InterfaceIpAddress> interfaceIpAddresses2 = new ArrayList<>();
kishoredb79dc02016-03-07 16:24:55 +0530438 interfaceIpAddresses2
439 .add(new InterfaceIpAddress(IpAddress.valueOf("192.168.20.101"), IpPrefix.valueOf("192.168.20.0/24")));
440 Interface sw1Eth2 = new Interface(SW1_ETH1.deviceId().toString(), SW1_ETH2, interfaceIpAddresses2,
441 MacAddress.valueOf("00:00:00:00:00:02"), VlanId.NONE);
442 interfaces.add(sw1Eth2);
443
Sho SHIMIZU891162b2016-06-06 16:32:01 -0700444 List<InterfaceIpAddress> interfaceIpAddresses3 = new ArrayList<>();
kishoredb79dc02016-03-07 16:24:55 +0530445 interfaceIpAddresses3
446 .add(new InterfaceIpAddress(IpAddress.valueOf("192.168.30.101"), IpPrefix.valueOf("192.168.30.0/24")));
447 Interface sw1Eth3 = new Interface(SW1_ETH1.deviceId().toString(), SW1_ETH3, interfaceIpAddresses3,
448 MacAddress.valueOf("00:00:00:00:00:03"), VlanId.NONE);
449 interfaces.add(sw1Eth3);
450
kishoredb79dc02016-03-07 16:24:55 +0530451 }
452
kishoredb79dc02016-03-07 16:24:55 +0530453 private ForwardingObjective buildForwardingObjective(TrafficSelector selector, TrafficTreatment treatment,
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530454 int nextId, boolean add, int priority) {
kishoredb79dc02016-03-07 16:24:55 +0530455 DefaultForwardingObjective.Builder fobBuilder = DefaultForwardingObjective.builder();
456 fobBuilder.withSelector(selector);
457 if (treatment != null) {
458 fobBuilder.withTreatment(treatment);
459 }
460 if (nextId != -1) {
461 fobBuilder.nextStep(nextId);
462 }
Vinayak Tejankar3a409c62017-01-12 02:20:53 +0530463 fobBuilder.fromApp(APPID).withPriority(priority).withFlag(ForwardingObjective.Flag.VERSATILE);
kishoredb79dc02016-03-07 16:24:55 +0530464
465 return add ? fobBuilder.add() : fobBuilder.remove();
466 }
467
kishoredb79dc02016-03-07 16:24:55 +0530468 private class TestCoreService extends CoreServiceAdapter {
kishore71a27532016-03-16 20:23:49 +0530469
kishoredb79dc02016-03-07 16:24:55 +0530470 @Override
471 public ApplicationId getAppId(String name) {
472 return APPID;
473 }
474
475 @Override
476 public ApplicationId registerApplication(String name) {
kishoredb79dc02016-03-07 16:24:55 +0530477 return new TestApplicationId(name);
478 }
479
480 }
481
482 private class TestDeviceService extends DeviceServiceAdapter {
483
484 @Override
485 public boolean isAvailable(DeviceId deviceId) {
kishoredb79dc02016-03-07 16:24:55 +0530486 boolean flag = false;
487 if (deviceId.equals(controlPlaneConnectPoint.deviceId())) {
488 flag = true;
489 }
490 return flag;
491 }
492
493 @Override
494 public void addListener(DeviceListener listener) {
kishoredb79dc02016-03-07 16:24:55 +0530495 ControlPlaneRedirectManagerTest.this.deviceListener = listener;
496 }
497
498 }
499
kishoredb79dc02016-03-07 16:24:55 +0530500 private class TestRouterConfig extends RouterConfig {
501
502 @Override
503 public ConnectPoint getControlPlaneConnectPoint() {
kishoredb79dc02016-03-07 16:24:55 +0530504 return controlPlaneConnectPoint;
505 }
506
507 @Override
508 public boolean getOspfEnabled() {
kishoredb79dc02016-03-07 16:24:55 +0530509 return true;
510 }
511
512 @Override
513 public List<String> getInterfaces() {
kishoredb79dc02016-03-07 16:24:55 +0530514 ArrayList<String> interfaces = new ArrayList<>();
515 interfaces.add("of:0000000000000001");
516 interfaces.add("of:0000000000000001/2");
517 interfaces.add("of:0000000000000001/3");
518 return interfaces;
519 }
520
521 }
522
523 private class TestNetworkConfigService extends NetworkConfigServiceAdapter {
524
525 @Override
526 public void addListener(NetworkConfigListener listener) {
kishoredb79dc02016-03-07 16:24:55 +0530527 ControlPlaneRedirectManagerTest.this.networkConfigListener = listener;
528 }
529
530 @Override
531 public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) {
kishoredb79dc02016-03-07 16:24:55 +0530532 return (C) ControlPlaneRedirectManagerTest.this.routerConfig;
533 }
534
535 }
536
537 private static class TestApplicationId implements ApplicationId {
538
539 private final String name;
540 private final short id;
541
542 public TestApplicationId(String name) {
543 this.name = name;
544 this.id = (short) Objects.hash(name);
545 }
546
547 public static ApplicationId create(String name) {
548 return new TestApplicationId(name);
549 }
550
551 @Override
552 public short id() {
553 return id;
554 }
555
556 @Override
557 public String name() {
558 return name;
559 }
560
561 @Override
562 public int hashCode() {
563 final int prime = 31;
564 int result = 1;
565 result = prime * result + id;
566 result = prime * result + ((name == null) ? 0 : name.hashCode());
567 return result;
568 }
569
570 @Override
571 public boolean equals(Object obj) {
572 if (this == obj) {
573 return true;
574 }
575 if (obj == null) {
576 return false;
577 }
578 if (getClass() != obj.getClass()) {
579 return false;
580 }
581 TestApplicationId other = (TestApplicationId) obj;
582 if (id != other.id) {
583 return false;
584 }
585 if (name == null) {
586 if (other.name != null) {
587 return false;
kishore71a27532016-03-16 20:23:49 +0530588 }
kishoredb79dc02016-03-07 16:24:55 +0530589 } else if (!name.equals(other.name)) {
590 return false;
591 }
592 return true;
593 }
594 }
595
596 private class InternalMastershipServiceTest extends MastershipServiceAdapter {
597
598 @Override
599 public boolean isLocalMaster(DeviceId deviceId) {
kishoredb79dc02016-03-07 16:24:55 +0530600 boolean flag = deviceId.equals(controlPlaneConnectPoint.deviceId());
601 return flag;
602 }
603
604 }
kishore71a27532016-03-16 20:23:49 +0530605
606 private class InternalInterfaceService extends InterfaceServiceAdapter {
607
608 @Override
609 public void addListener(InterfaceListener listener) {
610 ControlPlaneRedirectManagerTest.this.interfaceListener = listener;
611 }
612
613 @Override
614 public Set<Interface> getInterfaces() {
615 return interfaces;
616 }
617
618 @Override
619 public Set<Interface> getInterfacesByPort(ConnectPoint port) {
620 Set<Interface> setIntf = new HashSet<Interface>();
621 for (Interface intf : interfaces) {
622 if (intf.connectPoint().equals(port)) {
623 setIntf.add(intf);
624 }
625 }
626 return setIntf;
627 }
628
629 @Override
630 public Interface getMatchingInterface(IpAddress ip) {
631 Interface intff = null;
632 for (Interface intf : interfaces) {
633 for (InterfaceIpAddress address : intf.ipAddresses()) {
634 if (address.ipAddress().equals(ip)) {
635 intff = intf;
636 break;
637 }
638 }
639 }
640
641 return intff;
642 }
643
644 }
kishoredb79dc02016-03-07 16:24:55 +0530645}