alshabib | ab98466 | 2014-12-04 18:56:18 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
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 | 470ed4f | 2017-01-31 16:52:28 -0800 | [diff] [blame] | 16 | |
| 17 | package org.onosproject.intentsync; |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 18 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 19 | import com.google.common.util.concurrent.MoreExecutors; |
Jonathan Hart | 470ed4f | 2017-01-31 16:52:28 -0800 | [diff] [blame] | 20 | import org.easymock.EasyMock; |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 23 | import org.onlab.packet.Ethernet; |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 24 | import org.onlab.packet.Ip4Prefix; |
| 25 | import org.onlab.packet.IpAddress; |
| 26 | import org.onlab.packet.IpPrefix; |
| 27 | import org.onlab.packet.MacAddress; |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 28 | import org.onosproject.TestApplicationId; |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 29 | import org.onosproject.cluster.ClusterServiceAdapter; |
| 30 | import org.onosproject.cluster.ControllerNode; |
| 31 | import org.onosproject.cluster.DefaultControllerNode; |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame] | 32 | import org.onosproject.cluster.LeadershipServiceAdapter; |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 33 | import org.onosproject.cluster.NodeId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 34 | import org.onosproject.core.ApplicationId; |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame] | 35 | import org.onosproject.core.CoreServiceAdapter; |
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.intent.AbstractIntentTest; |
| 44 | import org.onosproject.net.intent.Intent; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 45 | import org.onosproject.net.intent.IntentService; |
| 46 | import org.onosproject.net.intent.IntentState; |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 47 | import org.onosproject.net.intent.Key; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 48 | import org.onosproject.net.intent.MultiPointToSinglePointIntent; |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 49 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 50 | import java.util.Collections; |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 51 | import java.util.HashSet; |
| 52 | import java.util.Set; |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame] | 53 | import java.util.concurrent.ExecutorService; |
Jonathan Hart | 552e31f | 2015-02-06 11:11:59 -0800 | [diff] [blame] | 54 | |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 55 | /** |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 56 | * This class tests the intent synchronization function in the |
| 57 | * IntentSynchronizer class. |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 58 | */ |
Jonathan Hart | 6af92be | 2016-01-05 20:52:25 -0800 | [diff] [blame] | 59 | public class IntentSynchronizerTest extends AbstractIntentTest { |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 60 | |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 61 | private IntentService intentService; |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 62 | |
| 63 | private static final ConnectPoint SW1_ETH1 = new ConnectPoint( |
| 64 | DeviceId.deviceId("of:0000000000000001"), |
| 65 | PortNumber.portNumber(1)); |
| 66 | |
| 67 | private static final ConnectPoint SW2_ETH1 = new ConnectPoint( |
| 68 | DeviceId.deviceId("of:0000000000000002"), |
| 69 | PortNumber.portNumber(1)); |
| 70 | |
| 71 | private static final ConnectPoint SW3_ETH1 = new ConnectPoint( |
| 72 | DeviceId.deviceId("of:0000000000000003"), |
| 73 | PortNumber.portNumber(1)); |
| 74 | |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 75 | private static final ConnectPoint SW4_ETH1 = new ConnectPoint( |
| 76 | DeviceId.deviceId("of:0000000000000004"), |
| 77 | PortNumber.portNumber(1)); |
| 78 | |
Pavlin Radoslavov | a071b1e | 2014-11-17 13:37:57 -0800 | [diff] [blame] | 79 | private IntentSynchronizer intentSynchronizer; |
Jonathan Hart | de6e203 | 2016-04-15 13:28:02 -0700 | [diff] [blame] | 80 | private final Set<ConnectPoint> connectPoints = new HashSet<>(); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 81 | |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame] | 82 | private static final ApplicationId APPID = |
| 83 | TestApplicationId.create("intent-sync-test"); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 84 | |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 85 | private static final ControllerNode LOCAL_NODE = |
| 86 | new DefaultControllerNode(new NodeId("foo"), IpAddress.valueOf("127.0.0.1")); |
| 87 | |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 88 | @Before |
| 89 | public void setUp() throws Exception { |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 90 | super.setUp(); |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 91 | |
Jonathan Hart | de6e203 | 2016-04-15 13:28:02 -0700 | [diff] [blame] | 92 | setUpConnectPoints(); |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 93 | |
Jonathan Hart | 470ed4f | 2017-01-31 16:52:28 -0800 | [diff] [blame] | 94 | intentService = EasyMock.createMock(IntentService.class); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 95 | |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame] | 96 | intentSynchronizer = new TestIntentSynchronizer(); |
| 97 | |
| 98 | intentSynchronizer.coreService = new TestCoreService(); |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 99 | intentSynchronizer.clusterService = new TestClusterService(); |
Jonathan Hart | de6e203 | 2016-04-15 13:28:02 -0700 | [diff] [blame] | 100 | intentSynchronizer.leadershipService = new LeadershipServiceAdapter(); |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame] | 101 | intentSynchronizer.intentService = intentService; |
| 102 | |
| 103 | intentSynchronizer.activate(); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /** |
Jonathan Hart | de6e203 | 2016-04-15 13:28:02 -0700 | [diff] [blame] | 107 | * Sets up connect points. |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 108 | */ |
Jonathan Hart | de6e203 | 2016-04-15 13:28:02 -0700 | [diff] [blame] | 109 | private void setUpConnectPoints() { |
| 110 | connectPoints.add(SW1_ETH1); |
| 111 | connectPoints.add(SW2_ETH1); |
| 112 | connectPoints.add(SW3_ETH1); |
| 113 | connectPoints.add(SW4_ETH1); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /** |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 117 | * Tests the synchronization behavior of intent synchronizer. We set up |
| 118 | * a discrepancy between the intent service state and the intent |
| 119 | * synchronizer's state and ensure that this is reconciled correctly. |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 120 | */ |
| 121 | @Test |
Jonathan Hart | de6e203 | 2016-04-15 13:28:02 -0700 | [diff] [blame] | 122 | public void testIntentSync() { |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 123 | |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 124 | // Construct routes and intents. |
| 125 | // This test simulates the following cases during the master change |
| 126 | // time interval: |
Jonathan Hart | de6e203 | 2016-04-15 13:28:02 -0700 | [diff] [blame] | 127 | // 1. intent1 did not change and the intent also did not change. |
| 128 | // 2. intent2 was deleted, but the intent was not deleted. |
| 129 | // 3. intent3 was newly added, and the intent was also submitted. |
| 130 | // 4. intent4 was updated to RouteEntry4Update, and the intent was |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 131 | // also updated to a new one. |
Jonathan Hart | de6e203 | 2016-04-15 13:28:02 -0700 | [diff] [blame] | 132 | // 5. intent5 did not change, but its intent id changed. |
| 133 | // 6. intent6 was newly added, but the intent was not submitted. |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 134 | |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 135 | MultiPointToSinglePointIntent intent1 = intentBuilder( |
Jonathan Hart | de6e203 | 2016-04-15 13:28:02 -0700 | [diff] [blame] | 136 | Ip4Prefix.valueOf("1.1.1.0/24"), "00:00:00:00:00:01", SW1_ETH1); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 137 | MultiPointToSinglePointIntent intent2 = intentBuilder( |
Jonathan Hart | de6e203 | 2016-04-15 13:28:02 -0700 | [diff] [blame] | 138 | Ip4Prefix.valueOf("2.2.2.0/24"), "00:00:00:00:00:02", SW2_ETH1); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 139 | MultiPointToSinglePointIntent intent3 = intentBuilder( |
Jonathan Hart | de6e203 | 2016-04-15 13:28:02 -0700 | [diff] [blame] | 140 | Ip4Prefix.valueOf("3.3.3.0/24"), "00:00:00:00:00:03", SW3_ETH1); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 141 | MultiPointToSinglePointIntent intent4 = intentBuilder( |
Jonathan Hart | de6e203 | 2016-04-15 13:28:02 -0700 | [diff] [blame] | 142 | Ip4Prefix.valueOf("4.4.4.0/24"), "00:00:00:00:00:03", SW3_ETH1); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 143 | MultiPointToSinglePointIntent intent4Update = intentBuilder( |
Jonathan Hart | de6e203 | 2016-04-15 13:28:02 -0700 | [diff] [blame] | 144 | Ip4Prefix.valueOf("4.4.4.0/24"), "00:00:00:00:00:02", SW2_ETH1); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 145 | MultiPointToSinglePointIntent intent5 = intentBuilder( |
Jonathan Hart | de6e203 | 2016-04-15 13:28:02 -0700 | [diff] [blame] | 146 | Ip4Prefix.valueOf("5.5.5.0/24"), "00:00:00:00:00:01", SW1_ETH1); |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 147 | MultiPointToSinglePointIntent intent7 = intentBuilder( |
Jonathan Hart | de6e203 | 2016-04-15 13:28:02 -0700 | [diff] [blame] | 148 | Ip4Prefix.valueOf("7.7.7.0/24"), "00:00:00:00:00:01", SW1_ETH1); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 149 | |
| 150 | MultiPointToSinglePointIntent intent6 = intentBuilder( |
Jonathan Hart | de6e203 | 2016-04-15 13:28:02 -0700 | [diff] [blame] | 151 | Ip4Prefix.valueOf("6.6.6.0/24"), "00:00:00:00:00:01", SW1_ETH1); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 152 | |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 153 | // Set up expectation |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame] | 154 | Set<Intent> intents = new HashSet<>(); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 155 | intents.add(intent1); |
Jonathan Hart | 470ed4f | 2017-01-31 16:52:28 -0800 | [diff] [blame] | 156 | EasyMock.expect(intentService.getIntentState(intent1.key())) |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 157 | .andReturn(IntentState.INSTALLED).anyTimes(); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 158 | intents.add(intent2); |
Jonathan Hart | 470ed4f | 2017-01-31 16:52:28 -0800 | [diff] [blame] | 159 | EasyMock.expect(intentService.getIntentState(intent2.key())) |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 160 | .andReturn(IntentState.INSTALLED).anyTimes(); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 161 | intents.add(intent4); |
Jonathan Hart | 470ed4f | 2017-01-31 16:52:28 -0800 | [diff] [blame] | 162 | EasyMock.expect(intentService.getIntentState(intent4.key())) |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 163 | .andReturn(IntentState.INSTALLED).anyTimes(); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 164 | intents.add(intent5); |
Jonathan Hart | 470ed4f | 2017-01-31 16:52:28 -0800 | [diff] [blame] | 165 | EasyMock.expect(intentService.getIntentState(intent5.key())) |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 166 | .andReturn(IntentState.INSTALLED).anyTimes(); |
| 167 | intents.add(intent7); |
Jonathan Hart | 470ed4f | 2017-01-31 16:52:28 -0800 | [diff] [blame] | 168 | EasyMock.expect(intentService.getIntentState(intent7.key())) |
Jonathan Hart | ec2df01 | 2014-10-23 16:40:24 -0700 | [diff] [blame] | 169 | .andReturn(IntentState.WITHDRAWING).anyTimes(); |
Jonathan Hart | 470ed4f | 2017-01-31 16:52:28 -0800 | [diff] [blame] | 170 | EasyMock.expect(intentService.getIntents()).andReturn(intents).anyTimes(); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 171 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 172 | // These are the operations that should be done to the intentService |
| 173 | // during synchronization |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 174 | intentService.withdraw(intent2); |
Brian O'Connor | 03406a4 | 2015-02-03 17:28:57 -0800 | [diff] [blame] | 175 | intentService.submit(intent3); |
| 176 | intentService.submit(intent4Update); |
| 177 | intentService.submit(intent6); |
| 178 | intentService.submit(intent7); |
Jonathan Hart | 470ed4f | 2017-01-31 16:52:28 -0800 | [diff] [blame] | 179 | EasyMock.replay(intentService); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 180 | |
| 181 | // Start the test |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 182 | |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 183 | // Simulate some input from the clients. The intent synchronizer has not |
| 184 | // gained the global leadership yet, but it will remember this input for |
| 185 | // when it does. |
| 186 | intentSynchronizer.submit(intent1); |
| 187 | intentSynchronizer.submit(intent2); |
| 188 | intentSynchronizer.withdraw(intent2); |
| 189 | intentSynchronizer.submit(intent3); |
| 190 | intentSynchronizer.submit(intent4); |
| 191 | intentSynchronizer.submit(intent4Update); |
| 192 | intentSynchronizer.submit(intent5); |
| 193 | intentSynchronizer.submit(intent6); |
| 194 | intentSynchronizer.submit(intent7); |
| 195 | |
| 196 | // Give the leadership to the intent synchronizer. It will now attempt |
| 197 | // to synchronize the intents in the store with the intents it has |
| 198 | // recorded based on the earlier user input. |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame] | 199 | intentSynchronizer.modifyPrimary(true); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 200 | |
Jonathan Hart | 470ed4f | 2017-01-31 16:52:28 -0800 | [diff] [blame] | 201 | EasyMock.verify(intentService); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Tests the behavior of the submit API, both when the synchronizer has |
| 206 | * leadership and when it does not. |
| 207 | */ |
| 208 | @Test |
| 209 | public void testSubmit() { |
| 210 | IpPrefix prefix = Ip4Prefix.valueOf("1.1.1.0/24"); |
| 211 | Intent intent = intentBuilder(prefix, "00:00:00:00:00:01", SW1_ETH1); |
| 212 | |
| 213 | // Set up expectations |
| 214 | intentService.submit(intent); |
Jonathan Hart | 470ed4f | 2017-01-31 16:52:28 -0800 | [diff] [blame] | 215 | EasyMock.expect(intentService.getIntents()).andReturn(Collections.emptyList()) |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 216 | .anyTimes(); |
Jonathan Hart | 470ed4f | 2017-01-31 16:52:28 -0800 | [diff] [blame] | 217 | EasyMock.replay(intentService); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 218 | |
| 219 | // Give the intent synchronizer leadership so it will submit intents |
| 220 | // to the intent service |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame] | 221 | intentSynchronizer.modifyPrimary(true); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 222 | |
| 223 | // Test the submit |
| 224 | intentSynchronizer.submit(intent); |
| 225 | |
Jonathan Hart | 470ed4f | 2017-01-31 16:52:28 -0800 | [diff] [blame] | 226 | EasyMock.verify(intentService); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 227 | |
| 228 | // Now we'll remove leadership from the intent synchronizer and verify |
| 229 | // that it does not submit any intents to the intent service when we |
| 230 | // call the submit API |
Jonathan Hart | 470ed4f | 2017-01-31 16:52:28 -0800 | [diff] [blame] | 231 | EasyMock.reset(intentService); |
| 232 | EasyMock.replay(intentService); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 233 | |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame] | 234 | intentSynchronizer.modifyPrimary(false); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 235 | |
| 236 | intentSynchronizer.submit(intent); |
| 237 | |
Jonathan Hart | 470ed4f | 2017-01-31 16:52:28 -0800 | [diff] [blame] | 238 | EasyMock.verify(intentService); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Tests the behavior of the withdraw API, both when the synchronizer has |
| 243 | * leadership and when it does not. |
| 244 | */ |
| 245 | @Test |
| 246 | public void testWithdraw() { |
| 247 | IpPrefix prefix = Ip4Prefix.valueOf("1.1.1.0/24"); |
| 248 | Intent intent = intentBuilder(prefix, "00:00:00:00:00:01", SW1_ETH1); |
| 249 | |
| 250 | // Submit an intent first so we can withdraw it later |
| 251 | intentService.submit(intent); |
| 252 | intentService.withdraw(intent); |
Jonathan Hart | 470ed4f | 2017-01-31 16:52:28 -0800 | [diff] [blame] | 253 | EasyMock.expect(intentService.getIntents()).andReturn(Collections.emptyList()) |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 254 | .anyTimes(); |
Jonathan Hart | 470ed4f | 2017-01-31 16:52:28 -0800 | [diff] [blame] | 255 | EasyMock.replay(intentService); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 256 | |
| 257 | // Give the intent synchronizer leadership so it will submit intents |
| 258 | // to the intent service |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame] | 259 | intentSynchronizer.modifyPrimary(true); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 260 | |
| 261 | // Test the submit then withdraw |
| 262 | intentSynchronizer.submit(intent); |
| 263 | intentSynchronizer.withdraw(intent); |
| 264 | |
Jonathan Hart | 470ed4f | 2017-01-31 16:52:28 -0800 | [diff] [blame] | 265 | EasyMock.verify(intentService); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 266 | |
| 267 | // Now we'll remove leadership from the intent synchronizer and verify |
| 268 | // that it does not withdraw any intents to the intent service when we |
| 269 | // call the withdraw API |
Jonathan Hart | 470ed4f | 2017-01-31 16:52:28 -0800 | [diff] [blame] | 270 | EasyMock.reset(intentService); |
| 271 | EasyMock.replay(intentService); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 272 | |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame] | 273 | intentSynchronizer.modifyPrimary(false); |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 274 | |
| 275 | intentSynchronizer.submit(intent); |
| 276 | intentSynchronizer.withdraw(intent); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 277 | |
Jonathan Hart | 470ed4f | 2017-01-31 16:52:28 -0800 | [diff] [blame] | 278 | EasyMock.verify(intentService); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | /** |
| 282 | * MultiPointToSinglePointIntent builder. |
| 283 | * |
| 284 | * @param ipPrefix the ipPrefix to match |
| 285 | * @param nextHopMacAddress to which the destination MAC address in packet |
| 286 | * should be rewritten |
| 287 | * @param egressPoint to which packets should be sent |
| 288 | * @return the constructed MultiPointToSinglePointIntent |
| 289 | */ |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 290 | private MultiPointToSinglePointIntent intentBuilder(IpPrefix ipPrefix, |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 291 | String nextHopMacAddress, ConnectPoint egressPoint) { |
| 292 | |
| 293 | TrafficSelector.Builder selectorBuilder = |
| 294 | DefaultTrafficSelector.builder(); |
Pavlin Radoslavov | 87dd930 | 2015-03-10 13:53:24 -0700 | [diff] [blame] | 295 | if (ipPrefix.isIp4()) { |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 296 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV4); |
Pavlin Radoslavov | a853709 | 2015-02-23 10:15:20 -0800 | [diff] [blame] | 297 | selectorBuilder.matchIPDst(ipPrefix); |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 298 | } else { |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 299 | selectorBuilder.matchEthType(Ethernet.TYPE_IPV6); |
Pavlin Radoslavov | a853709 | 2015-02-23 10:15:20 -0800 | [diff] [blame] | 300 | selectorBuilder.matchIPv6Dst(ipPrefix); |
Pavlin Radoslavov | 3a0a52e | 2015-01-06 17:41:37 -0800 | [diff] [blame] | 301 | } |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 302 | |
| 303 | TrafficTreatment.Builder treatmentBuilder = |
| 304 | DefaultTrafficTreatment.builder(); |
| 305 | treatmentBuilder.setEthDst(MacAddress.valueOf(nextHopMacAddress)); |
| 306 | |
Jonathan Hart | de6e203 | 2016-04-15 13:28:02 -0700 | [diff] [blame] | 307 | Set<ConnectPoint> ingressPoints = new HashSet<>(connectPoints); |
| 308 | ingressPoints.remove(egressPoint); |
| 309 | |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 310 | MultiPointToSinglePointIntent intent = |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 311 | MultiPointToSinglePointIntent.builder() |
| 312 | .appId(APPID) |
Jonathan Hart | 9a426f8 | 2015-09-03 15:43:13 +0200 | [diff] [blame] | 313 | .key(Key.of(ipPrefix.toString(), APPID)) |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 314 | .selector(selectorBuilder.build()) |
| 315 | .treatment(treatmentBuilder.build()) |
| 316 | .ingressPoints(ingressPoints) |
| 317 | .egressPoint(egressPoint) |
| 318 | .build(); |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 319 | return intent; |
| 320 | } |
| 321 | |
Jonathan Hart | 365335e | 2015-12-10 11:09:53 -0800 | [diff] [blame] | 322 | private class TestIntentSynchronizer extends IntentSynchronizer { |
| 323 | @Override |
| 324 | protected ExecutorService createExecutor() { |
| 325 | return MoreExecutors.newDirectExecutorService(); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | private class TestCoreService extends CoreServiceAdapter { |
| 330 | @Override |
| 331 | public ApplicationId registerApplication(String name) { |
| 332 | return APPID; |
| 333 | } |
| 334 | } |
| 335 | |
Madan Jampani | 620f70d | 2016-01-30 22:22:47 -0800 | [diff] [blame] | 336 | private class TestClusterService extends ClusterServiceAdapter { |
| 337 | @Override |
| 338 | public ControllerNode getLocalNode() { |
| 339 | return LOCAL_NODE; |
| 340 | } |
| 341 | } |
Pingping | f5d9093 | 2014-10-27 10:50:04 -0700 | [diff] [blame] | 342 | } |