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