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