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