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.IntentServiceAdapter; |
| 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.junit.Assert.assertEquals; |
| 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. |
| 43 | */ |
| 44 | public class IntentCleanupTest { |
| 45 | |
| 46 | private IntentCleanup cleanup; |
| 47 | private MockIntentService service; |
| 48 | private IntentStore store; |
| 49 | protected IdGenerator idGenerator; // global or one per test? per test for now. |
| 50 | |
| 51 | private static class MockIntentService extends IntentServiceAdapter { |
| 52 | |
| 53 | private int submitCounter = 0; |
Brian O'Connor | 3822430 | 2016-08-02 22:03:01 -0700 | [diff] [blame] | 54 | private int pendingCounter = 0; |
Brian O'Connor | eba4e34 | 2015-04-30 22:50:13 -0700 | [diff] [blame] | 55 | |
| 56 | @Override |
| 57 | public void submit(Intent intent) { |
| 58 | submitCounter++; |
| 59 | } |
| 60 | |
Brian O'Connor | 3822430 | 2016-08-02 22:03:01 -0700 | [diff] [blame] | 61 | @Override |
| 62 | public void addPending(IntentData intentData) { |
| 63 | pendingCounter++; |
| 64 | } |
| 65 | |
Brian O'Connor | eba4e34 | 2015-04-30 22:50:13 -0700 | [diff] [blame] | 66 | public int submitCounter() { |
| 67 | return submitCounter; |
| 68 | } |
Brian O'Connor | 3822430 | 2016-08-02 22:03:01 -0700 | [diff] [blame] | 69 | |
| 70 | public int pendingCounter() { |
| 71 | return pendingCounter; |
| 72 | } |
| 73 | |
Brian O'Connor | eba4e34 | 2015-04-30 22:50:13 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | @Before |
| 77 | public void setUp() { |
| 78 | service = new MockIntentService(); |
| 79 | store = new SimpleIntentStore(); |
| 80 | cleanup = new IntentCleanup(); |
| 81 | idGenerator = new MockIdGenerator(); |
| 82 | |
| 83 | cleanup.cfgService = new ComponentConfigAdapter(); |
| 84 | cleanup.service = service; |
| 85 | cleanup.store = store; |
| 86 | cleanup.period = 10; |
| 87 | cleanup.retryThreshold = 3; |
| 88 | cleanup.activate(); |
| 89 | |
| 90 | assertTrue("store should be empty", |
| 91 | Sets.newHashSet(cleanup.store.getIntents()).isEmpty()); |
| 92 | |
Thomas Vachuska | 2323596 | 2017-02-03 11:44:15 -0800 | [diff] [blame] | 93 | Intent.unbindIdGenerator(idGenerator); |
Brian O'Connor | eba4e34 | 2015-04-30 22:50:13 -0700 | [diff] [blame] | 94 | Intent.bindIdGenerator(idGenerator); |
| 95 | } |
| 96 | |
| 97 | @After |
| 98 | public void tearDown() { |
| 99 | cleanup.deactivate(); |
Brian O'Connor | eba4e34 | 2015-04-30 22:50:13 -0700 | [diff] [blame] | 100 | Intent.unbindIdGenerator(idGenerator); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Trigger resubmit of intent in CORRUPT during periodic poll. |
| 105 | */ |
| 106 | @Test |
| 107 | public void corruptPoll() { |
| 108 | IntentStoreDelegate mockDelegate = new IntentStoreDelegate() { |
| 109 | @Override |
| 110 | public void process(IntentData intentData) { |
| 111 | intentData.setState(CORRUPT); |
| 112 | store.write(intentData); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public void notify(IntentEvent event) {} |
| 117 | }; |
| 118 | store.setDelegate(mockDelegate); |
| 119 | |
| 120 | Intent intent = new MockIntent(1L); |
| 121 | Timestamp version = new SystemClockTimestamp(1L); |
| 122 | IntentData data = new IntentData(intent, INSTALL_REQ, version); |
| 123 | store.addPending(data); |
| 124 | |
| 125 | cleanup.run(); //FIXME broken? |
| 126 | assertEquals("Expect number of submits incorrect", |
| 127 | 1, service.submitCounter()); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Trigger resubmit of intent in INSTALL_REQ for too long. |
| 132 | */ |
| 133 | @Test |
| 134 | public void pendingPoll() { |
| 135 | IntentStoreDelegate mockDelegate = new IntentStoreDelegate() { |
| 136 | @Override |
| 137 | public void process(IntentData intentData) {} |
| 138 | |
| 139 | @Override |
| 140 | public void notify(IntentEvent event) { |
| 141 | cleanup.event(event); |
| 142 | } |
| 143 | }; |
| 144 | store.setDelegate(mockDelegate); |
| 145 | |
| 146 | Intent intent = new MockIntent(1L); |
| 147 | Timestamp version = new SystemClockTimestamp(1L); |
| 148 | IntentData data = new IntentData(intent, INSTALL_REQ, version); |
| 149 | store.addPending(data); |
| 150 | |
| 151 | cleanup.run(); |
| 152 | assertEquals("Expect number of submits incorrect", |
Brian O'Connor | 3822430 | 2016-08-02 22:03:01 -0700 | [diff] [blame] | 153 | 1, service.pendingCounter()); |
Brian O'Connor | eba4e34 | 2015-04-30 22:50:13 -0700 | [diff] [blame] | 154 | |
| 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 | |
| 182 | cleanup.run(); |
| 183 | assertEquals("Expect number of submits incorrect", |
Brian O'Connor | 3822430 | 2016-08-02 22:03:01 -0700 | [diff] [blame] | 184 | 1, service.pendingCounter()); |
Brian O'Connor | eba4e34 | 2015-04-30 22:50:13 -0700 | [diff] [blame] | 185 | |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Only submit one of two intents because one is too new. |
| 190 | */ |
| 191 | @Test |
| 192 | public void skipPoll() { |
| 193 | IntentStoreDelegate mockDelegate = new IntentStoreDelegate() { |
| 194 | @Override |
| 195 | public void process(IntentData intentData) { |
| 196 | intentData.setState(CORRUPT); |
| 197 | store.write(intentData); |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public void notify(IntentEvent event) {} |
| 202 | }; |
| 203 | store.setDelegate(mockDelegate); |
| 204 | |
| 205 | Intent intent = new MockIntent(1L); |
| 206 | IntentData data = new IntentData(intent, INSTALL_REQ, null); |
| 207 | store.addPending(data); |
| 208 | |
| 209 | Intent intent2 = new MockIntent(2L); |
| 210 | Timestamp version = new SystemClockTimestamp(1L); |
| 211 | data = new IntentData(intent2, INSTALL_REQ, version); |
| 212 | store.addPending(data); |
| 213 | |
| 214 | cleanup.run(); |
| 215 | assertEquals("Expect number of submits incorrect", |
| 216 | 1, service.submitCounter()); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Verify resubmit in response to CORRUPT event. |
| 221 | */ |
| 222 | @Test |
| 223 | public void corruptEvent() { |
| 224 | IntentStoreDelegate mockDelegate = new IntentStoreDelegate() { |
| 225 | @Override |
| 226 | public void process(IntentData intentData) { |
| 227 | intentData.setState(CORRUPT); |
| 228 | store.write(intentData); |
| 229 | } |
| 230 | |
| 231 | @Override |
| 232 | public void notify(IntentEvent event) { |
| 233 | cleanup.event(event); |
| 234 | } |
| 235 | }; |
| 236 | store.setDelegate(mockDelegate); |
| 237 | |
| 238 | |
| 239 | Intent intent = new MockIntent(1L); |
| 240 | IntentData data = new IntentData(intent, INSTALL_REQ, null); |
| 241 | |
| 242 | store.addPending(data); |
| 243 | assertEquals("Expect number of submits incorrect", |
| 244 | 1, service.submitCounter()); |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Intent should not be retried because threshold is reached. |
| 249 | */ |
| 250 | @Test |
| 251 | public void corruptEventThreshold() { |
| 252 | IntentStoreDelegate mockDelegate = new IntentStoreDelegate() { |
| 253 | @Override |
| 254 | public void process(IntentData intentData) { |
| 255 | intentData.setState(CORRUPT); |
| 256 | intentData.setErrorCount(cleanup.retryThreshold); |
| 257 | store.write(intentData); |
| 258 | } |
| 259 | |
| 260 | @Override |
| 261 | public void notify(IntentEvent event) { |
| 262 | cleanup.event(event); |
| 263 | } |
| 264 | }; |
| 265 | store.setDelegate(mockDelegate); |
| 266 | |
| 267 | Intent intent = new MockIntent(1L); |
| 268 | IntentData data = new IntentData(intent, INSTALL_REQ, null); |
| 269 | |
| 270 | store.addPending(data); |
| 271 | assertEquals("Expect number of submits incorrect", |
| 272 | 0, service.submitCounter()); |
| 273 | } |
| 274 | } |