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