Brian O'Connor | eba4e34 | 2015-04-30 22:50:13 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Brian O'Connor | eba4e34 | 2015-04-30 22:50:13 -0700 | [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 | */ |
| 16 | package org.onosproject.net.intent.impl; |
| 17 | |
| 18 | import com.google.common.collect.Sets; |
| 19 | import org.junit.After; |
| 20 | import org.junit.Before; |
Pier Luigi | e6caf68 | 2017-01-26 15:25:09 -0800 | [diff] [blame] | 21 | import org.junit.Ignore; |
Brian O'Connor | eba4e34 | 2015-04-30 22:50:13 -0700 | [diff] [blame] | 22 | import org.junit.Test; |
| 23 | import org.onosproject.cfg.ComponentConfigAdapter; |
Thomas Vachuska | 2048c1f | 2017-05-10 19:32:22 -0700 | [diff] [blame] | 24 | import org.onosproject.net.intent.AbstractIntentTest; |
Brian O'Connor | eba4e34 | 2015-04-30 22:50:13 -0700 | [diff] [blame] | 25 | import org.onosproject.net.intent.Intent; |
| 26 | import org.onosproject.net.intent.IntentData; |
| 27 | import org.onosproject.net.intent.IntentEvent; |
| 28 | import org.onosproject.net.intent.IntentService; |
| 29 | import org.onosproject.net.intent.IntentStore; |
| 30 | import org.onosproject.net.intent.IntentStoreDelegate; |
Brian O'Connor | eba4e34 | 2015-04-30 22:50:13 -0700 | [diff] [blame] | 31 | import org.onosproject.store.Timestamp; |
Thomas Vachuska | c97aa61 | 2015-06-23 16:00:18 -0700 | [diff] [blame] | 32 | import org.onosproject.store.trivial.SimpleIntentStore; |
| 33 | import org.onosproject.store.trivial.SystemClockTimestamp; |
Brian O'Connor | eba4e34 | 2015-04-30 22:50:13 -0700 | [diff] [blame] | 34 | |
| 35 | import static org.easymock.EasyMock.*; |
| 36 | import static org.junit.Assert.assertTrue; |
| 37 | import static org.onosproject.net.intent.IntentState.*; |
| 38 | import static org.onosproject.net.intent.IntentTestsMocks.MockIntent; |
| 39 | |
| 40 | /** |
| 41 | * Test intent cleanup using Mocks. |
| 42 | * FIXME remove this or IntentCleanupTest |
| 43 | */ |
Thomas Vachuska | 2048c1f | 2017-05-10 19:32:22 -0700 | [diff] [blame] | 44 | public class IntentCleanupTestMock extends AbstractIntentTest { |
Brian O'Connor | eba4e34 | 2015-04-30 22:50:13 -0700 | [diff] [blame] | 45 | |
| 46 | private IntentCleanup cleanup; |
| 47 | private IntentService service; |
| 48 | private IntentStore store; |
Brian O'Connor | eba4e34 | 2015-04-30 22:50:13 -0700 | [diff] [blame] | 49 | |
| 50 | @Before |
| 51 | public void setUp() { |
| 52 | service = createMock(IntentService.class); |
| 53 | store = new SimpleIntentStore(); |
| 54 | cleanup = new IntentCleanup(); |
Brian O'Connor | eba4e34 | 2015-04-30 22:50:13 -0700 | [diff] [blame] | 55 | |
Brian O'Connor | eba4e34 | 2015-04-30 22:50:13 -0700 | [diff] [blame] | 56 | cleanup.cfgService = new ComponentConfigAdapter(); |
| 57 | cleanup.service = service; |
| 58 | cleanup.store = store; |
| 59 | cleanup.period = 1000; |
| 60 | cleanup.retryThreshold = 3; |
Brian O'Connor | eba4e34 | 2015-04-30 22:50:13 -0700 | [diff] [blame] | 61 | |
| 62 | assertTrue("store should be empty", |
| 63 | Sets.newHashSet(cleanup.store.getIntents()).isEmpty()); |
| 64 | |
Thomas Vachuska | 2048c1f | 2017-05-10 19:32:22 -0700 | [diff] [blame] | 65 | super.setUp(); |
Brian O'Connor | eba4e34 | 2015-04-30 22:50:13 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | @After |
| 69 | public void tearDown() { |
Thomas Vachuska | 2048c1f | 2017-05-10 19:32:22 -0700 | [diff] [blame] | 70 | super.tearDown(); |
Brian O'Connor | eba4e34 | 2015-04-30 22:50:13 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Trigger resubmit of intent in CORRUPT during periodic poll. |
| 75 | */ |
Brian O'Connor | eba4e34 | 2015-04-30 22:50:13 -0700 | [diff] [blame] | 76 | @Test |
| 77 | public void corruptPoll() { |
| 78 | IntentStoreDelegate mockDelegate = new IntentStoreDelegate() { |
| 79 | @Override |
| 80 | public void process(IntentData intentData) { |
| 81 | intentData.setState(CORRUPT); |
| 82 | store.write(intentData); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public void notify(IntentEvent event) {} |
| 87 | }; |
| 88 | store.setDelegate(mockDelegate); |
| 89 | |
| 90 | Intent intent = new MockIntent(1L); |
| 91 | Timestamp version = new SystemClockTimestamp(1L); |
| 92 | IntentData data = new IntentData(intent, INSTALL_REQ, version); |
| 93 | store.addPending(data); |
| 94 | |
| 95 | service.submit(intent); |
| 96 | expectLastCall().once(); |
| 97 | replay(service); |
| 98 | |
Yi Tseng | 8013c4f | 2017-11-02 16:56:56 -0700 | [diff] [blame^] | 99 | cleanup.run(); |
| 100 | |
Brian O'Connor | eba4e34 | 2015-04-30 22:50:13 -0700 | [diff] [blame] | 101 | verify(service); |
| 102 | reset(service); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Trigger resubmit of intent in INSTALL_REQ for too long. |
| 107 | */ |
| 108 | @Test |
| 109 | public void pendingPoll() { |
| 110 | IntentStoreDelegate mockDelegate = new IntentStoreDelegate() { |
| 111 | @Override |
| 112 | public void process(IntentData intentData) {} |
| 113 | |
| 114 | @Override |
| 115 | public void notify(IntentEvent event) { |
| 116 | cleanup.event(event); |
| 117 | } |
| 118 | }; |
| 119 | store.setDelegate(mockDelegate); |
| 120 | |
| 121 | Intent intent = new MockIntent(1L); |
| 122 | Timestamp version = new SystemClockTimestamp(1L); |
| 123 | IntentData data = new IntentData(intent, INSTALL_REQ, version); |
| 124 | store.addPending(data); |
| 125 | |
Brian O'Connor | 3822430 | 2016-08-02 22:03:01 -0700 | [diff] [blame] | 126 | service.addPending(data); |
Brian O'Connor | eba4e34 | 2015-04-30 22:50:13 -0700 | [diff] [blame] | 127 | expectLastCall().once(); |
| 128 | replay(service); |
| 129 | |
| 130 | cleanup.run(); |
| 131 | verify(service); |
| 132 | reset(service); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Trigger resubmit of intent in INSTALLING for too long. |
| 137 | */ |
| 138 | @Test |
Pier Luigi | e6caf68 | 2017-01-26 15:25:09 -0800 | [diff] [blame] | 139 | @Ignore("The implementation is dependent on the SimpleStore") |
Brian O'Connor | eba4e34 | 2015-04-30 22:50:13 -0700 | [diff] [blame] | 140 | public void installingPoll() { |
| 141 | IntentStoreDelegate mockDelegate = new IntentStoreDelegate() { |
| 142 | @Override |
| 143 | public void process(IntentData intentData) { |
| 144 | intentData.setState(INSTALLING); |
| 145 | store.write(intentData); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public void notify(IntentEvent event) { |
| 150 | cleanup.event(event); |
| 151 | } |
| 152 | }; |
| 153 | store.setDelegate(mockDelegate); |
| 154 | |
| 155 | Intent intent = new MockIntent(1L); |
| 156 | Timestamp version = new SystemClockTimestamp(1L); |
| 157 | IntentData data = new IntentData(intent, INSTALL_REQ, version); |
| 158 | store.addPending(data); |
| 159 | |
Brian O'Connor | 3822430 | 2016-08-02 22:03:01 -0700 | [diff] [blame] | 160 | service.addPending(data); |
Brian O'Connor | eba4e34 | 2015-04-30 22:50:13 -0700 | [diff] [blame] | 161 | expectLastCall().once(); |
| 162 | replay(service); |
| 163 | |
| 164 | cleanup.run(); |
| 165 | verify(service); |
| 166 | reset(service); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Only submit one of two intents because one is too new. |
| 171 | */ |
| 172 | @Test |
| 173 | public void skipPoll() { |
| 174 | IntentStoreDelegate mockDelegate = new IntentStoreDelegate() { |
| 175 | @Override |
| 176 | public void process(IntentData intentData) { |
| 177 | intentData.setState(CORRUPT); |
| 178 | store.write(intentData); |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public void notify(IntentEvent event) {} |
| 183 | }; |
| 184 | store.setDelegate(mockDelegate); |
| 185 | |
| 186 | Intent intent = new MockIntent(1L); |
| 187 | IntentData data = new IntentData(intent, INSTALL_REQ, null); |
| 188 | store.addPending(data); |
| 189 | |
| 190 | Intent intent2 = new MockIntent(2L); |
| 191 | Timestamp version = new SystemClockTimestamp(1L); |
| 192 | data = new IntentData(intent2, INSTALL_REQ, version); |
| 193 | store.addPending(data); |
| 194 | |
Yi Tseng | 8013c4f | 2017-11-02 16:56:56 -0700 | [diff] [blame^] | 195 | service.submit(intent); |
| 196 | expectLastCall().once(); |
| 197 | |
Brian O'Connor | eba4e34 | 2015-04-30 22:50:13 -0700 | [diff] [blame] | 198 | service.submit(intent2); |
| 199 | expectLastCall().once(); |
| 200 | replay(service); |
| 201 | |
| 202 | cleanup.run(); |
| 203 | verify(service); |
| 204 | reset(service); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Verify resubmit in response to CORRUPT event. |
| 209 | */ |
| 210 | @Test |
| 211 | public void corruptEvent() { |
| 212 | IntentStoreDelegate mockDelegate = new IntentStoreDelegate() { |
| 213 | @Override |
| 214 | public void process(IntentData intentData) { |
| 215 | intentData.setState(CORRUPT); |
| 216 | store.write(intentData); |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public void notify(IntentEvent event) { |
| 221 | cleanup.event(event); |
| 222 | } |
| 223 | }; |
| 224 | store.setDelegate(mockDelegate); |
| 225 | |
| 226 | |
| 227 | Intent intent = new MockIntent(1L); |
| 228 | IntentData data = new IntentData(intent, INSTALL_REQ, null); |
| 229 | |
| 230 | service.submit(intent); |
| 231 | expectLastCall().once(); |
| 232 | replay(service); |
| 233 | |
| 234 | store.addPending(data); |
| 235 | |
| 236 | verify(service); |
| 237 | reset(service); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Intent should not be retried because threshold is reached. |
| 242 | */ |
| 243 | @Test |
| 244 | public void corruptEventThreshold() { |
| 245 | IntentStoreDelegate mockDelegate = new IntentStoreDelegate() { |
| 246 | @Override |
| 247 | public void process(IntentData intentData) { |
| 248 | intentData.setState(CORRUPT); |
| 249 | intentData.setErrorCount(cleanup.retryThreshold); |
| 250 | store.write(intentData); |
| 251 | } |
| 252 | |
| 253 | @Override |
| 254 | public void notify(IntentEvent event) { |
| 255 | cleanup.event(event); |
| 256 | } |
| 257 | }; |
| 258 | store.setDelegate(mockDelegate); |
| 259 | |
| 260 | |
| 261 | Intent intent = new MockIntent(1L); |
| 262 | IntentData data = new IntentData(intent, INSTALL_REQ, null); |
| 263 | |
| 264 | replay(service); |
| 265 | |
| 266 | store.addPending(data); |
| 267 | |
| 268 | verify(service); |
| 269 | reset(service); |
| 270 | } |
| 271 | } |