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