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