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