Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 16 | package org.onlab.onos.sdnip; |
| 17 | |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 18 | import static org.easymock.EasyMock.anyObject; |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 19 | import static org.easymock.EasyMock.createMock; |
| 20 | import static org.easymock.EasyMock.expect; |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 21 | import static org.easymock.EasyMock.expectLastCall; |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 22 | import static org.easymock.EasyMock.replay; |
| 23 | import static org.easymock.EasyMock.reset; |
| 24 | import static org.easymock.EasyMock.verify; |
| 25 | import static org.junit.Assert.assertEquals; |
| 26 | import static org.junit.Assert.assertTrue; |
| 27 | |
| 28 | import java.util.HashMap; |
| 29 | import java.util.HashSet; |
| 30 | import java.util.Map; |
| 31 | import java.util.Set; |
| 32 | |
| 33 | import org.junit.Before; |
| 34 | import org.junit.Test; |
Pavlin Radoslavov | d26f57a | 2014-10-23 17:19:45 -0700 | [diff] [blame] | 35 | import org.onlab.junit.TestUtils; |
| 36 | import org.onlab.junit.TestUtils.TestUtilsException; |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 37 | import org.onlab.onos.core.ApplicationId; |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 38 | import org.onlab.onos.net.ConnectPoint; |
| 39 | import org.onlab.onos.net.DefaultHost; |
| 40 | import org.onlab.onos.net.DeviceId; |
| 41 | import org.onlab.onos.net.Host; |
| 42 | import org.onlab.onos.net.HostId; |
| 43 | import org.onlab.onos.net.HostLocation; |
| 44 | import org.onlab.onos.net.PortNumber; |
| 45 | import org.onlab.onos.net.flow.DefaultTrafficSelector; |
| 46 | import org.onlab.onos.net.flow.DefaultTrafficTreatment; |
| 47 | import org.onlab.onos.net.flow.TrafficSelector; |
| 48 | import org.onlab.onos.net.flow.TrafficTreatment; |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 49 | import org.onlab.onos.net.host.HostListener; |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 50 | import org.onlab.onos.net.host.HostService; |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 51 | import org.onlab.onos.net.host.InterfaceIpAddress; |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 52 | import org.onlab.onos.net.intent.IntentService; |
| 53 | import org.onlab.onos.net.intent.MultiPointToSinglePointIntent; |
| 54 | import org.onlab.onos.net.provider.ProviderId; |
| 55 | import org.onlab.onos.sdnip.config.BgpPeer; |
| 56 | import org.onlab.onos.sdnip.config.Interface; |
| 57 | import org.onlab.onos.sdnip.config.SdnIpConfigService; |
| 58 | import org.onlab.packet.Ethernet; |
| 59 | import org.onlab.packet.IpAddress; |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 60 | import org.onlab.packet.Ip4Address; |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 61 | import org.onlab.packet.IpPrefix; |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 62 | import org.onlab.packet.Ip4Prefix; |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 63 | import org.onlab.packet.MacAddress; |
| 64 | import org.onlab.packet.VlanId; |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 65 | |
| 66 | import com.google.common.collect.Sets; |
| 67 | |
| 68 | /** |
| 69 | * This class tests adding a route, updating a route, deleting a route, |
| 70 | * and adding a route whose next hop is the local BGP speaker. |
Pingping | fc58467 | 2014-10-23 10:51:19 -0700 | [diff] [blame] | 71 | * <p/> |
| 72 | * ARP module answers the MAC address synchronously. |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 73 | */ |
| 74 | public class RouterTest { |
| 75 | |
| 76 | private SdnIpConfigService sdnIpConfigService; |
| 77 | private InterfaceService interfaceService; |
| 78 | private IntentService intentService; |
| 79 | private HostService hostService; |
| 80 | |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 81 | private static final ConnectPoint SW1_ETH1 = new ConnectPoint( |
| 82 | DeviceId.deviceId("of:0000000000000001"), |
| 83 | PortNumber.portNumber(1)); |
| 84 | |
| 85 | private static final ConnectPoint SW2_ETH1 = new ConnectPoint( |
| 86 | DeviceId.deviceId("of:0000000000000002"), |
| 87 | PortNumber.portNumber(1)); |
| 88 | |
| 89 | private static final ConnectPoint SW3_ETH1 = new ConnectPoint( |
| 90 | DeviceId.deviceId("of:0000000000000003"), |
| 91 | PortNumber.portNumber(1)); |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 92 | |
| 93 | private static final ApplicationId APPID = new ApplicationId() { |
| 94 | @Override |
| 95 | public short id() { |
| 96 | return 1; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public String name() { |
| 101 | return "SDNIP"; |
| 102 | } |
| 103 | }; |
| 104 | |
| 105 | private Router router; |
| 106 | |
| 107 | @Before |
| 108 | public void setUp() throws Exception { |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 109 | setUpBgpPeers(); |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 110 | |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 111 | setUpInterfaceService(); |
| 112 | setUpHostService(); |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 113 | |
| 114 | intentService = createMock(IntentService.class); |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 115 | |
| 116 | router = new Router(APPID, intentService, |
| 117 | hostService, sdnIpConfigService, interfaceService); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Sets up BGP peers in external networks. |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 122 | */ |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 123 | private void setUpBgpPeers() { |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 124 | |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 125 | Map<IpAddress, BgpPeer> peers = new HashMap<>(); |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 126 | |
| 127 | String peerSw1Eth1 = "192.168.10.1"; |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 128 | peers.put(IpAddress.valueOf(peerSw1Eth1), |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 129 | new BgpPeer("00:00:00:00:00:00:00:01", 1, peerSw1Eth1)); |
| 130 | |
| 131 | // Two BGP peers are connected to switch 2 port 1. |
| 132 | String peer1Sw2Eth1 = "192.168.20.1"; |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 133 | peers.put(IpAddress.valueOf(peer1Sw2Eth1), |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 134 | new BgpPeer("00:00:00:00:00:00:00:02", 1, peer1Sw2Eth1)); |
| 135 | |
| 136 | String peer2Sw2Eth1 = "192.168.20.2"; |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 137 | peers.put(IpAddress.valueOf(peer2Sw2Eth1), |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 138 | new BgpPeer("00:00:00:00:00:00:00:02", 1, peer2Sw2Eth1)); |
| 139 | |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 140 | sdnIpConfigService = createMock(SdnIpConfigService.class); |
| 141 | expect(sdnIpConfigService.getBgpPeers()).andReturn(peers).anyTimes(); |
| 142 | replay(sdnIpConfigService); |
| 143 | |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Sets up logical interfaces, which emulate the configured interfaces |
| 148 | * in SDN-IP application. |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 149 | */ |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 150 | private void setUpInterfaceService() { |
| 151 | interfaceService = createMock(InterfaceService.class); |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 152 | |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 153 | Set<Interface> interfaces = Sets.newHashSet(); |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 154 | |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 155 | InterfaceIpAddress ia1 = |
| 156 | new InterfaceIpAddress(IpAddress.valueOf("192.168.10.101"), |
| 157 | IpPrefix.valueOf("192.168.10.0/24")); |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 158 | Interface sw1Eth1 = new Interface(SW1_ETH1, |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 159 | Sets.newHashSet(ia1), |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 160 | MacAddress.valueOf("00:00:00:00:00:01")); |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 161 | |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 162 | expect(interfaceService.getInterface(SW1_ETH1)).andReturn(sw1Eth1).anyTimes(); |
| 163 | interfaces.add(sw1Eth1); |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 164 | |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 165 | InterfaceIpAddress ia2 = |
| 166 | new InterfaceIpAddress(IpAddress.valueOf("192.168.20.101"), |
| 167 | IpPrefix.valueOf("192.168.20.0/24")); |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 168 | Interface sw2Eth1 = new Interface(SW2_ETH1, |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 169 | Sets.newHashSet(ia2), |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 170 | MacAddress.valueOf("00:00:00:00:00:02")); |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 171 | |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 172 | expect(interfaceService.getInterface(SW2_ETH1)).andReturn(sw2Eth1).anyTimes(); |
| 173 | interfaces.add(sw2Eth1); |
| 174 | |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 175 | InterfaceIpAddress ia3 = |
| 176 | new InterfaceIpAddress(IpAddress.valueOf("192.168.30.101"), |
| 177 | IpPrefix.valueOf("192.168.30.0/24")); |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 178 | Interface sw3Eth1 = new Interface(SW3_ETH1, |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 179 | Sets.newHashSet(ia3), |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 180 | MacAddress.valueOf("00:00:00:00:00:03")); |
| 181 | |
| 182 | expect(interfaceService.getInterface(SW3_ETH1)).andReturn(sw3Eth1).anyTimes(); |
| 183 | interfaces.add(sw3Eth1); |
| 184 | |
| 185 | expect(interfaceService.getInterfaces()).andReturn(interfaces).anyTimes(); |
| 186 | |
| 187 | replay(interfaceService); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Sets up the host service with details of some hosts. |
| 192 | */ |
| 193 | private void setUpHostService() { |
| 194 | hostService = createMock(HostService.class); |
| 195 | |
| 196 | hostService.addListener(anyObject(HostListener.class)); |
| 197 | expectLastCall().anyTimes(); |
| 198 | |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 199 | IpAddress host1Address = IpAddress.valueOf("192.168.10.1"); |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 200 | Host host1 = new DefaultHost(ProviderId.NONE, HostId.NONE, |
| 201 | MacAddress.valueOf("00:00:00:00:00:01"), VlanId.NONE, |
| 202 | new HostLocation(SW1_ETH1, 1), |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 203 | Sets.newHashSet(host1Address)); |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 204 | |
| 205 | expect(hostService.getHostsByIp(host1Address)) |
| 206 | .andReturn(Sets.newHashSet(host1)).anyTimes(); |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 207 | hostService.startMonitoringIp(host1Address); |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 208 | expectLastCall().anyTimes(); |
| 209 | |
| 210 | |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 211 | IpAddress host2Address = IpAddress.valueOf("192.168.20.1"); |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 212 | Host host2 = new DefaultHost(ProviderId.NONE, HostId.NONE, |
| 213 | MacAddress.valueOf("00:00:00:00:00:02"), VlanId.NONE, |
| 214 | new HostLocation(SW2_ETH1, 1), |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 215 | Sets.newHashSet(host2Address)); |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 216 | |
| 217 | expect(hostService.getHostsByIp(host2Address)) |
| 218 | .andReturn(Sets.newHashSet(host2)).anyTimes(); |
Pavlin Radoslavov | 33f228a | 2014-10-27 19:33:16 -0700 | [diff] [blame] | 219 | hostService.startMonitoringIp(host2Address); |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 220 | expectLastCall().anyTimes(); |
| 221 | |
| 222 | |
| 223 | replay(hostService); |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | /** |
| 227 | * This method tests adding a route entry. |
| 228 | */ |
| 229 | @Test |
| 230 | public void testProcessRouteAdd() throws TestUtilsException { |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 231 | // Construct a route entry |
| 232 | RouteEntry routeEntry = new RouteEntry( |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 233 | Ip4Prefix.valueOf("1.1.1.0/24"), |
| 234 | Ip4Address.valueOf("192.168.10.1")); |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 235 | |
| 236 | // Construct a MultiPointToSinglePointIntent intent |
| 237 | TrafficSelector.Builder selectorBuilder = |
| 238 | DefaultTrafficSelector.builder(); |
| 239 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV4).matchIPDst( |
| 240 | routeEntry.prefix()); |
| 241 | |
| 242 | TrafficTreatment.Builder treatmentBuilder = |
| 243 | DefaultTrafficTreatment.builder(); |
| 244 | treatmentBuilder.setEthDst(MacAddress.valueOf("00:00:00:00:00:01")); |
| 245 | |
| 246 | Set<ConnectPoint> ingressPoints = new HashSet<ConnectPoint>(); |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 247 | ingressPoints.add(SW2_ETH1); |
| 248 | ingressPoints.add(SW3_ETH1); |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 249 | |
| 250 | MultiPointToSinglePointIntent intent = |
| 251 | new MultiPointToSinglePointIntent(APPID, |
| 252 | selectorBuilder.build(), treatmentBuilder.build(), |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 253 | ingressPoints, SW1_ETH1); |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 254 | |
| 255 | // Set up test expectation |
| 256 | reset(intentService); |
| 257 | intentService.submit(intent); |
| 258 | replay(intentService); |
| 259 | |
| 260 | // Call the processRouteAdd() method in Router class |
| 261 | router.leaderChanged(true); |
| 262 | TestUtils.setField(router, "isActivatedLeader", true); |
| 263 | router.processRouteAdd(routeEntry); |
| 264 | |
| 265 | // Verify |
| 266 | assertEquals(router.getRoutes().size(), 1); |
| 267 | assertTrue(router.getRoutes().contains(routeEntry)); |
| 268 | assertEquals(router.getPushedRouteIntents().size(), 1); |
| 269 | assertEquals(router.getPushedRouteIntents().iterator().next(), |
| 270 | intent); |
| 271 | verify(intentService); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * This method tests updating a route entry. |
| 276 | * |
| 277 | * @throws TestUtilsException |
| 278 | */ |
| 279 | @Test |
| 280 | public void testRouteUpdate() throws TestUtilsException { |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 281 | // Firstly add a route |
| 282 | testProcessRouteAdd(); |
| 283 | |
| 284 | // Construct the existing route entry |
| 285 | RouteEntry routeEntry = new RouteEntry( |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 286 | Ip4Prefix.valueOf("1.1.1.0/24"), |
| 287 | Ip4Address.valueOf("192.168.10.1")); |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 288 | |
| 289 | // Construct the existing MultiPointToSinglePointIntent intent |
| 290 | TrafficSelector.Builder selectorBuilder = |
| 291 | DefaultTrafficSelector.builder(); |
| 292 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV4).matchIPDst( |
| 293 | routeEntry.prefix()); |
| 294 | |
| 295 | TrafficTreatment.Builder treatmentBuilder = |
| 296 | DefaultTrafficTreatment.builder(); |
| 297 | treatmentBuilder.setEthDst(MacAddress.valueOf("00:00:00:00:00:01")); |
| 298 | |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 299 | Set<ConnectPoint> ingressPoints = new HashSet<ConnectPoint>(); |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 300 | ingressPoints.add(SW2_ETH1); |
| 301 | ingressPoints.add(SW3_ETH1); |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 302 | |
| 303 | MultiPointToSinglePointIntent intent = |
| 304 | new MultiPointToSinglePointIntent(APPID, |
| 305 | selectorBuilder.build(), treatmentBuilder.build(), |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 306 | ingressPoints, SW1_ETH1); |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 307 | |
| 308 | // Start to construct a new route entry and new intent |
| 309 | RouteEntry routeEntryUpdate = new RouteEntry( |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 310 | Ip4Prefix.valueOf("1.1.1.0/24"), |
| 311 | Ip4Address.valueOf("192.168.20.1")); |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 312 | |
| 313 | // Construct a new MultiPointToSinglePointIntent intent |
| 314 | TrafficSelector.Builder selectorBuilderNew = |
| 315 | DefaultTrafficSelector.builder(); |
| 316 | selectorBuilderNew.matchEthType(Ethernet.TYPE_IPV4).matchIPDst( |
| 317 | routeEntryUpdate.prefix()); |
| 318 | |
| 319 | TrafficTreatment.Builder treatmentBuilderNew = |
| 320 | DefaultTrafficTreatment.builder(); |
| 321 | treatmentBuilderNew.setEthDst(MacAddress.valueOf("00:00:00:00:00:02")); |
| 322 | |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 323 | |
| 324 | Set<ConnectPoint> ingressPointsNew = new HashSet<ConnectPoint>(); |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 325 | ingressPointsNew.add(SW1_ETH1); |
| 326 | ingressPointsNew.add(SW3_ETH1); |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 327 | |
| 328 | MultiPointToSinglePointIntent intentNew = |
| 329 | new MultiPointToSinglePointIntent(APPID, |
| 330 | selectorBuilderNew.build(), |
| 331 | treatmentBuilderNew.build(), |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 332 | ingressPointsNew, SW2_ETH1); |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 333 | |
| 334 | // Set up test expectation |
| 335 | reset(intentService); |
| 336 | intentService.withdraw(intent); |
| 337 | intentService.submit(intentNew); |
| 338 | replay(intentService); |
| 339 | |
| 340 | // Call the processRouteAdd() method in Router class |
| 341 | router.leaderChanged(true); |
| 342 | TestUtils.setField(router, "isActivatedLeader", true); |
| 343 | router.processRouteAdd(routeEntryUpdate); |
| 344 | |
| 345 | // Verify |
| 346 | assertEquals(router.getRoutes().size(), 1); |
| 347 | assertTrue(router.getRoutes().contains(routeEntryUpdate)); |
| 348 | assertEquals(router.getPushedRouteIntents().size(), 1); |
| 349 | assertEquals(router.getPushedRouteIntents().iterator().next(), |
| 350 | intentNew); |
| 351 | verify(intentService); |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * This method tests deleting a route entry. |
| 356 | */ |
| 357 | @Test |
| 358 | public void testProcessRouteDelete() throws TestUtilsException { |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 359 | // Firstly add a route |
| 360 | testProcessRouteAdd(); |
| 361 | |
| 362 | // Construct the existing route entry |
| 363 | RouteEntry routeEntry = new RouteEntry( |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 364 | Ip4Prefix.valueOf("1.1.1.0/24"), |
| 365 | Ip4Address.valueOf("192.168.10.1")); |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 366 | |
| 367 | // Construct the existing MultiPointToSinglePointIntent intent |
| 368 | TrafficSelector.Builder selectorBuilder = |
| 369 | DefaultTrafficSelector.builder(); |
| 370 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV4).matchIPDst( |
| 371 | routeEntry.prefix()); |
| 372 | |
| 373 | TrafficTreatment.Builder treatmentBuilder = |
| 374 | DefaultTrafficTreatment.builder(); |
| 375 | treatmentBuilder.setEthDst(MacAddress.valueOf("00:00:00:00:00:01")); |
| 376 | |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 377 | Set<ConnectPoint> ingressPoints = new HashSet<ConnectPoint>(); |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 378 | ingressPoints.add(SW2_ETH1); |
| 379 | ingressPoints.add(SW3_ETH1); |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 380 | |
| 381 | MultiPointToSinglePointIntent intent = |
| 382 | new MultiPointToSinglePointIntent(APPID, |
| 383 | selectorBuilder.build(), treatmentBuilder.build(), |
Jonathan Hart | 31582d1 | 2014-10-22 13:52:41 -0700 | [diff] [blame] | 384 | ingressPoints, SW1_ETH1); |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 385 | |
| 386 | // Set up expectation |
| 387 | reset(intentService); |
| 388 | intentService.withdraw(intent); |
| 389 | replay(intentService); |
| 390 | |
| 391 | // Call route deleting method in Router class |
| 392 | router.leaderChanged(true); |
| 393 | TestUtils.setField(router, "isActivatedLeader", true); |
| 394 | router.processRouteDelete(routeEntry); |
| 395 | |
| 396 | // Verify |
| 397 | assertEquals(router.getRoutes().size(), 0); |
| 398 | assertEquals(router.getPushedRouteIntents().size(), 0); |
| 399 | verify(intentService); |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * This method tests when the next hop of a route is the local BGP speaker. |
| 404 | * |
| 405 | * @throws TestUtilsException |
| 406 | */ |
| 407 | @Test |
| 408 | public void testLocalRouteAdd() throws TestUtilsException { |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 409 | // Construct a route entry, the next hop is the local BGP speaker |
| 410 | RouteEntry routeEntry = new RouteEntry( |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 411 | Ip4Prefix.valueOf("1.1.1.0/24"), |
| 412 | Ip4Address.valueOf("0.0.0.0")); |
Pingping | 3855f31 | 2014-10-22 12:50:37 -0700 | [diff] [blame] | 413 | |
| 414 | // Reset intentService to check whether the submit method is called |
| 415 | reset(intentService); |
| 416 | replay(intentService); |
| 417 | |
| 418 | // Call the processRouteAdd() method in Router class |
| 419 | router.leaderChanged(true); |
| 420 | TestUtils.setField(router, "isActivatedLeader", true); |
| 421 | router.processRouteAdd(routeEntry); |
| 422 | |
| 423 | // Verify |
| 424 | assertEquals(router.getRoutes().size(), 1); |
| 425 | assertTrue(router.getRoutes().contains(routeEntry)); |
| 426 | assertEquals(router.getPushedRouteIntents().size(), 0); |
| 427 | verify(intentService); |
| 428 | } |
| 429 | } |