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