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