alshabib | ab98466 | 2014-12-04 18:56:18 -0800 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
alshabib | ab98466 | 2014-12-04 18:56:18 -0800 | [diff] [blame] | 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.sdnip; |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 17 | |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 18 | import com.google.common.collect.Sets; |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 19 | import com.google.common.util.concurrent.MoreExecutors; |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | import org.onlab.junit.TestUtils; |
| 23 | import org.onlab.junit.TestUtils.TestUtilsException; |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 24 | import org.onlab.packet.Ethernet; |
| 25 | import org.onlab.packet.Ip4Address; |
| 26 | import org.onlab.packet.Ip4Prefix; |
| 27 | import org.onlab.packet.IpAddress; |
| 28 | import org.onlab.packet.IpPrefix; |
| 29 | import org.onlab.packet.MacAddress; |
| 30 | import org.onlab.packet.VlanId; |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 31 | import org.onosproject.TestApplicationId; |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame^] | 32 | import org.onosproject.cluster.LeadershipServiceAdapter; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 33 | import org.onosproject.core.ApplicationId; |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame^] | 34 | import org.onosproject.core.CoreServiceAdapter; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 35 | import org.onosproject.incubator.net.intf.Interface; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 36 | import org.onosproject.net.ConnectPoint; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 37 | import org.onosproject.net.DeviceId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 38 | import org.onosproject.net.PortNumber; |
| 39 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 40 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 41 | import org.onosproject.net.flow.TrafficSelector; |
| 42 | import org.onosproject.net.flow.TrafficTreatment; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 43 | import org.onosproject.net.host.InterfaceIpAddress; |
| 44 | import org.onosproject.net.intent.AbstractIntentTest; |
| 45 | import org.onosproject.net.intent.Intent; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 46 | import org.onosproject.net.intent.IntentService; |
| 47 | import org.onosproject.net.intent.IntentState; |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame^] | 48 | import org.onosproject.net.intent.IntentUtils; |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 49 | import org.onosproject.net.intent.Key; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 50 | import org.onosproject.net.intent.MultiPointToSinglePointIntent; |
Jonathan Hart | 2da1e60 | 2015-02-18 19:09:24 -0800 | [diff] [blame] | 51 | import org.onosproject.routing.RouteEntry; |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 52 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 53 | import java.util.Collections; |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 54 | import java.util.HashSet; |
| 55 | import java.util.Set; |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame^] | 56 | import java.util.concurrent.ExecutorService; |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 57 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 58 | import static org.easymock.EasyMock.createMock; |
| 59 | import static org.easymock.EasyMock.expect; |
| 60 | import static org.easymock.EasyMock.replay; |
| 61 | import static org.easymock.EasyMock.reset; |
| 62 | import static org.easymock.EasyMock.verify; |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 63 | import static org.hamcrest.Matchers.is; |
Jonathan Hart | cb726fc | 2015-02-13 16:26:22 -0800 | [diff] [blame] | 64 | import static org.junit.Assert.assertFalse; |
| 65 | import static org.junit.Assert.assertThat; |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 66 | |
| 67 | /** |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 68 | * This class tests the intent synchronization function in the |
| 69 | * IntentSynchronizer class. |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 70 | */ |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 71 | public class IntentSyncTest extends AbstractIntentTest { |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 72 | |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 73 | private IntentService intentService; |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 74 | |
| 75 | private static final ConnectPoint SW1_ETH1 = new ConnectPoint( |
| 76 | DeviceId.deviceId("of:0000000000000001"), |
| 77 | PortNumber.portNumber(1)); |
| 78 | |
| 79 | private static final ConnectPoint SW2_ETH1 = new ConnectPoint( |
| 80 | DeviceId.deviceId("of:0000000000000002"), |
| 81 | PortNumber.portNumber(1)); |
| 82 | |
| 83 | private static final ConnectPoint SW3_ETH1 = new ConnectPoint( |
| 84 | DeviceId.deviceId("of:0000000000000003"), |
| 85 | PortNumber.portNumber(1)); |
| 86 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 87 | private static final ConnectPoint SW4_ETH1 = new ConnectPoint( |
| 88 | DeviceId.deviceId("of:0000000000000004"), |
| 89 | PortNumber.portNumber(1)); |
| 90 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 91 | private IntentSynchronizer intentSynchronizer; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 92 | private final Set<Interface> interfaces = Sets.newHashSet(); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 93 | |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame^] | 94 | private static final ApplicationId APPID = |
| 95 | TestApplicationId.create("intent-sync-test"); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 96 | |
| 97 | @Before |
| 98 | public void setUp() throws Exception { |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 99 | super.setUp(); |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 100 | |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 101 | setUpInterfaceService(); |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 102 | |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 103 | intentService = createMock(IntentService.class); |
| 104 | |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame^] | 105 | intentSynchronizer = new TestIntentSynchronizer(); |
| 106 | |
| 107 | intentSynchronizer.coreService = new TestCoreService(); |
| 108 | intentSynchronizer.leadershipService = new TestLeadershipService(); |
| 109 | intentSynchronizer.intentService = intentService; |
| 110 | |
| 111 | intentSynchronizer.activate(); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Sets up InterfaceService. |
| 116 | */ |
| 117 | private void setUpInterfaceService() { |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 118 | Set<InterfaceIpAddress> interfaceIpAddresses1 = Sets.newHashSet(); |
| 119 | interfaceIpAddresses1.add(new InterfaceIpAddress( |
| 120 | IpAddress.valueOf("192.168.10.101"), |
| 121 | IpPrefix.valueOf("192.168.10.0/24"))); |
| 122 | Interface sw1Eth1 = new Interface(SW1_ETH1, |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 123 | interfaceIpAddresses1, MacAddress.valueOf("00:00:00:00:00:01"), |
| 124 | VlanId.NONE); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 125 | interfaces.add(sw1Eth1); |
| 126 | |
| 127 | Set<InterfaceIpAddress> interfaceIpAddresses2 = Sets.newHashSet(); |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 128 | interfaceIpAddresses2.add( |
| 129 | new InterfaceIpAddress(IpAddress.valueOf("192.168.20.101"), |
| 130 | IpPrefix.valueOf("192.168.20.0/24"))); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 131 | Interface sw2Eth1 = new Interface(SW2_ETH1, |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 132 | interfaceIpAddresses2, MacAddress.valueOf("00:00:00:00:00:02"), |
| 133 | VlanId.NONE); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 134 | interfaces.add(sw2Eth1); |
| 135 | |
| 136 | Set<InterfaceIpAddress> interfaceIpAddresses3 = Sets.newHashSet(); |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 137 | interfaceIpAddresses3.add( |
| 138 | new InterfaceIpAddress(IpAddress.valueOf("192.168.30.101"), |
| 139 | IpPrefix.valueOf("192.168.30.0/24"))); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 140 | Interface sw3Eth1 = new Interface(SW3_ETH1, |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 141 | interfaceIpAddresses3, MacAddress.valueOf("00:00:00:00:00:03"), |
| 142 | VlanId.NONE); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 143 | interfaces.add(sw3Eth1); |
| 144 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 145 | InterfaceIpAddress interfaceIpAddress4 = |
| 146 | new InterfaceIpAddress(IpAddress.valueOf("192.168.40.101"), |
| 147 | IpPrefix.valueOf("192.168.40.0/24")); |
| 148 | Interface sw4Eth1 = new Interface(SW4_ETH1, |
| 149 | Sets.newHashSet(interfaceIpAddress4), |
| 150 | MacAddress.valueOf("00:00:00:00:00:04"), |
| 151 | VlanId.vlanId((short) 1)); |
| 152 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 153 | interfaces.add(sw4Eth1); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /** |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 157 | * Tests the synchronization behavior of intent synchronizer. We set up |
| 158 | * a discrepancy between the intent service state and the intent |
| 159 | * synchronizer's state and ensure that this is reconciled correctly. |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 160 | * |
| 161 | * @throws TestUtilsException |
| 162 | */ |
| 163 | @Test |
| 164 | public void testIntentSync() throws TestUtilsException { |
| 165 | |
| 166 | // |
| 167 | // Construct routes and intents. |
| 168 | // This test simulates the following cases during the master change |
| 169 | // time interval: |
| 170 | // 1. RouteEntry1 did not change and the intent also did not change. |
| 171 | // 2. RouteEntry2 was deleted, but the intent was not deleted. |
| 172 | // 3. RouteEntry3 was newly added, and the intent was also submitted. |
| 173 | // 4. RouteEntry4 was updated to RouteEntry4Update, and the intent was |
| 174 | // also updated to a new one. |
| 175 | // 5. RouteEntry5 did not change, but its intent id changed. |
| 176 | // 6. RouteEntry6 was newly added, but the intent was not submitted. |
| 177 | // |
| 178 | RouteEntry routeEntry1 = new RouteEntry( |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 179 | Ip4Prefix.valueOf("1.1.1.0/24"), |
| 180 | Ip4Address.valueOf("192.168.10.1")); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 181 | |
| 182 | RouteEntry routeEntry2 = new RouteEntry( |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 183 | Ip4Prefix.valueOf("2.2.2.0/24"), |
| 184 | Ip4Address.valueOf("192.168.20.1")); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 185 | |
| 186 | RouteEntry routeEntry3 = new RouteEntry( |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 187 | Ip4Prefix.valueOf("3.3.3.0/24"), |
| 188 | Ip4Address.valueOf("192.168.30.1")); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 189 | |
| 190 | RouteEntry routeEntry4 = new RouteEntry( |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 191 | Ip4Prefix.valueOf("4.4.4.0/24"), |
| 192 | Ip4Address.valueOf("192.168.30.1")); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 193 | |
| 194 | RouteEntry routeEntry4Update = new RouteEntry( |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 195 | Ip4Prefix.valueOf("4.4.4.0/24"), |
| 196 | Ip4Address.valueOf("192.168.20.1")); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 197 | |
| 198 | RouteEntry routeEntry5 = new RouteEntry( |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 199 | Ip4Prefix.valueOf("5.5.5.0/24"), |
| 200 | Ip4Address.valueOf("192.168.10.1")); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 201 | |
| 202 | RouteEntry routeEntry6 = new RouteEntry( |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 203 | Ip4Prefix.valueOf("6.6.6.0/24"), |
| 204 | Ip4Address.valueOf("192.168.10.1")); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 205 | |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 206 | RouteEntry routeEntry7 = new RouteEntry( |
Pavlin Radoslavov | 6b57073 | 2014-11-06 13:16:45 -0800 | [diff] [blame] | 207 | Ip4Prefix.valueOf("7.7.7.0/24"), |
| 208 | Ip4Address.valueOf("192.168.10.1")); |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 209 | |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 210 | MultiPointToSinglePointIntent intent1 = intentBuilder( |
| 211 | routeEntry1.prefix(), "00:00:00:00:00:01", SW1_ETH1); |
| 212 | MultiPointToSinglePointIntent intent2 = intentBuilder( |
| 213 | routeEntry2.prefix(), "00:00:00:00:00:02", SW2_ETH1); |
| 214 | MultiPointToSinglePointIntent intent3 = intentBuilder( |
| 215 | routeEntry3.prefix(), "00:00:00:00:00:03", SW3_ETH1); |
| 216 | MultiPointToSinglePointIntent intent4 = intentBuilder( |
| 217 | routeEntry4.prefix(), "00:00:00:00:00:03", SW3_ETH1); |
| 218 | MultiPointToSinglePointIntent intent4Update = intentBuilder( |
| 219 | routeEntry4Update.prefix(), "00:00:00:00:00:02", SW2_ETH1); |
| 220 | MultiPointToSinglePointIntent intent5 = intentBuilder( |
| 221 | routeEntry5.prefix(), "00:00:00:00:00:01", SW1_ETH1); |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 222 | MultiPointToSinglePointIntent intent7 = intentBuilder( |
| 223 | routeEntry7.prefix(), "00:00:00:00:00:01", SW1_ETH1); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 224 | |
| 225 | // Compose a intent, which is equal to intent5 but the id is different. |
| 226 | MultiPointToSinglePointIntent intent5New = |
| 227 | staticIntentBuilder(intent5, routeEntry5, "00:00:00:00:00:01"); |
Ray Milkey | 4fd3ceb | 2015-12-10 14:43:08 -0800 | [diff] [blame] | 228 | assertThat(IntentUtils.intentsAreEqual(intent5, intent5New), is(true)); |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 229 | assertFalse(intent5.equals(intent5New)); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 230 | |
| 231 | MultiPointToSinglePointIntent intent6 = intentBuilder( |
| 232 | routeEntry6.prefix(), "00:00:00:00:00:01", SW1_ETH1); |
| 233 | |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 234 | // Set up expectation |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 235 | Set<Intent> intents = new HashSet<>(); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 236 | intents.add(intent1); |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 237 | expect(intentService.getIntentState(intent1.key())) |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 238 | .andReturn(IntentState.INSTALLED).anyTimes(); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 239 | intents.add(intent2); |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 240 | expect(intentService.getIntentState(intent2.key())) |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 241 | .andReturn(IntentState.INSTALLED).anyTimes(); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 242 | intents.add(intent4); |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 243 | expect(intentService.getIntentState(intent4.key())) |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 244 | .andReturn(IntentState.INSTALLED).anyTimes(); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 245 | intents.add(intent5); |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 246 | expect(intentService.getIntentState(intent5.key())) |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 247 | .andReturn(IntentState.INSTALLED).anyTimes(); |
| 248 | intents.add(intent7); |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 249 | expect(intentService.getIntentState(intent7.key())) |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 250 | .andReturn(IntentState.WITHDRAWING).anyTimes(); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 251 | expect(intentService.getIntents()).andReturn(intents).anyTimes(); |
| 252 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 253 | // These are the operations that should be done to the intentService |
| 254 | // during synchronization |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 255 | intentService.withdraw(intent2); |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 256 | intentService.submit(intent3); |
| 257 | intentService.submit(intent4Update); |
| 258 | intentService.submit(intent6); |
| 259 | intentService.submit(intent7); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 260 | replay(intentService); |
| 261 | |
| 262 | // Start the test |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 263 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 264 | // Simulate some input from the clients. The intent synchronizer has not |
| 265 | // gained the global leadership yet, but it will remember this input for |
| 266 | // when it does. |
| 267 | intentSynchronizer.submit(intent1); |
| 268 | intentSynchronizer.submit(intent2); |
| 269 | intentSynchronizer.withdraw(intent2); |
| 270 | intentSynchronizer.submit(intent3); |
| 271 | intentSynchronizer.submit(intent4); |
| 272 | intentSynchronizer.submit(intent4Update); |
| 273 | intentSynchronizer.submit(intent5); |
| 274 | intentSynchronizer.submit(intent6); |
| 275 | intentSynchronizer.submit(intent7); |
| 276 | |
| 277 | // Give the leadership to the intent synchronizer. It will now attempt |
| 278 | // to synchronize the intents in the store with the intents it has |
| 279 | // recorded based on the earlier user input. |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame^] | 280 | intentSynchronizer.modifyPrimary(true); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 281 | |
| 282 | verify(intentService); |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Tests the behavior of the submit API, both when the synchronizer has |
| 287 | * leadership and when it does not. |
| 288 | */ |
| 289 | @Test |
| 290 | public void testSubmit() { |
| 291 | IpPrefix prefix = Ip4Prefix.valueOf("1.1.1.0/24"); |
| 292 | Intent intent = intentBuilder(prefix, "00:00:00:00:00:01", SW1_ETH1); |
| 293 | |
| 294 | // Set up expectations |
| 295 | intentService.submit(intent); |
| 296 | expect(intentService.getIntents()).andReturn(Collections.emptyList()) |
| 297 | .anyTimes(); |
| 298 | replay(intentService); |
| 299 | |
| 300 | // Give the intent synchronizer leadership so it will submit intents |
| 301 | // to the intent service |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame^] | 302 | intentSynchronizer.modifyPrimary(true); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 303 | |
| 304 | // Test the submit |
| 305 | intentSynchronizer.submit(intent); |
| 306 | |
| 307 | verify(intentService); |
| 308 | |
| 309 | // Now we'll remove leadership from the intent synchronizer and verify |
| 310 | // that it does not submit any intents to the intent service when we |
| 311 | // call the submit API |
| 312 | reset(intentService); |
| 313 | replay(intentService); |
| 314 | |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame^] | 315 | intentSynchronizer.modifyPrimary(false); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 316 | |
| 317 | intentSynchronizer.submit(intent); |
| 318 | |
| 319 | verify(intentService); |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Tests the behavior of the withdraw API, both when the synchronizer has |
| 324 | * leadership and when it does not. |
| 325 | */ |
| 326 | @Test |
| 327 | public void testWithdraw() { |
| 328 | IpPrefix prefix = Ip4Prefix.valueOf("1.1.1.0/24"); |
| 329 | Intent intent = intentBuilder(prefix, "00:00:00:00:00:01", SW1_ETH1); |
| 330 | |
| 331 | // Submit an intent first so we can withdraw it later |
| 332 | intentService.submit(intent); |
| 333 | intentService.withdraw(intent); |
| 334 | expect(intentService.getIntents()).andReturn(Collections.emptyList()) |
| 335 | .anyTimes(); |
| 336 | replay(intentService); |
| 337 | |
| 338 | // Give the intent synchronizer leadership so it will submit intents |
| 339 | // to the intent service |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame^] | 340 | intentSynchronizer.modifyPrimary(true); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 341 | |
| 342 | // Test the submit then withdraw |
| 343 | intentSynchronizer.submit(intent); |
| 344 | intentSynchronizer.withdraw(intent); |
| 345 | |
| 346 | verify(intentService); |
| 347 | |
| 348 | // Now we'll remove leadership from the intent synchronizer and verify |
| 349 | // that it does not withdraw any intents to the intent service when we |
| 350 | // call the withdraw API |
| 351 | reset(intentService); |
| 352 | replay(intentService); |
| 353 | |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame^] | 354 | intentSynchronizer.modifyPrimary(false); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 355 | |
| 356 | intentSynchronizer.submit(intent); |
| 357 | intentSynchronizer.withdraw(intent); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 358 | |
| 359 | verify(intentService); |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * MultiPointToSinglePointIntent builder. |
| 364 | * |
| 365 | * @param ipPrefix the ipPrefix to match |
| 366 | * @param nextHopMacAddress to which the destination MAC address in packet |
| 367 | * should be rewritten |
| 368 | * @param egressPoint to which packets should be sent |
| 369 | * @return the constructed MultiPointToSinglePointIntent |
| 370 | */ |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 371 | private MultiPointToSinglePointIntent intentBuilder(IpPrefix ipPrefix, |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 372 | String nextHopMacAddress, ConnectPoint egressPoint) { |
| 373 | |
| 374 | TrafficSelector.Builder selectorBuilder = |
| 375 | DefaultTrafficSelector.builder(); |
Pavlin Radoslavov | 87dd930 | 2015-03-10 13:53:24 -0700 | [diff] [blame] | 376 | if (ipPrefix.isIp4()) { |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 377 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV4); |
Pavlin Radoslavov | a853709 | 2015-02-23 10:15:20 -0800 | [diff] [blame] | 378 | selectorBuilder.matchIPDst(ipPrefix); |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 379 | } else { |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 380 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV6); |
Pavlin Radoslavov | a853709 | 2015-02-23 10:15:20 -0800 | [diff] [blame] | 381 | selectorBuilder.matchIPv6Dst(ipPrefix); |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 382 | } |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 383 | |
| 384 | TrafficTreatment.Builder treatmentBuilder = |
| 385 | DefaultTrafficTreatment.builder(); |
| 386 | treatmentBuilder.setEthDst(MacAddress.valueOf(nextHopMacAddress)); |
| 387 | |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 388 | Set<ConnectPoint> ingressPoints = new HashSet<>(); |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 389 | for (Interface intf : interfaces) { |
| 390 | if (!intf.connectPoint().equals(egressPoint)) { |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 391 | ConnectPoint srcPort = intf.connectPoint(); |
| 392 | ingressPoints.add(srcPort); |
| 393 | } |
| 394 | } |
| 395 | MultiPointToSinglePointIntent intent = |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 396 | MultiPointToSinglePointIntent.builder() |
| 397 | .appId(APPID) |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 398 | .key(Key.of(ipPrefix.toString(), APPID)) |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 399 | .selector(selectorBuilder.build()) |
| 400 | .treatment(treatmentBuilder.build()) |
| 401 | .ingressPoints(ingressPoints) |
| 402 | .egressPoint(egressPoint) |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 403 | .constraints(SdnIpFib.CONSTRAINTS) |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 404 | .build(); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 405 | return intent; |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * A static MultiPointToSinglePointIntent builder, the returned intent is |
| 410 | * equal to the input intent except that the id is different. |
| 411 | * |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 412 | * @param intent the intent to be used for building a new intent |
| 413 | * @param routeEntry the relative routeEntry of the intent |
| 414 | * @return the newly constructed MultiPointToSinglePointIntent |
| 415 | * @throws TestUtilsException |
| 416 | */ |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 417 | private MultiPointToSinglePointIntent staticIntentBuilder( |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 418 | MultiPointToSinglePointIntent intent, RouteEntry routeEntry, |
| 419 | String nextHopMacAddress) throws TestUtilsException { |
| 420 | |
| 421 | // Use a different egress ConnectPoint with that in intent |
| 422 | // to generate a different id |
| 423 | MultiPointToSinglePointIntent intentNew = intentBuilder( |
| 424 | routeEntry.prefix(), nextHopMacAddress, SW2_ETH1); |
| 425 | TestUtils.setField(intentNew, "egressPoint", intent.egressPoint()); |
| 426 | TestUtils.setField(intentNew, |
| 427 | "ingressPoints", intent.ingressPoints()); |
| 428 | return intentNew; |
| 429 | } |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame^] | 430 | |
| 431 | private class TestIntentSynchronizer extends IntentSynchronizer { |
| 432 | @Override |
| 433 | protected ExecutorService createExecutor() { |
| 434 | return MoreExecutors.newDirectExecutorService(); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | private class TestCoreService extends CoreServiceAdapter { |
| 439 | @Override |
| 440 | public ApplicationId registerApplication(String name) { |
| 441 | return APPID; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | private class TestLeadershipService extends LeadershipServiceAdapter { |
| 446 | |
| 447 | } |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 448 | } |