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 | |
jaegonkim | cbe1c5e | 2018-05-20 15:11:18 +0900 | [diff] [blame] | 126 | service.addPending(isA(IntentData.class)); |
| 127 | |
Brian O'Connor | eba4e34 | 2015-04-30 22:50:13 -0700 | [diff] [blame] | 128 | expectLastCall().once(); |
| 129 | replay(service); |
| 130 | |
| 131 | cleanup.run(); |
| 132 | verify(service); |
| 133 | reset(service); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Trigger resubmit of intent in INSTALLING for too long. |
| 138 | */ |
| 139 | @Test |
Pier Luigi | e6caf68 | 2017-01-26 15:25:09 -0800 | [diff] [blame] | 140 | @Ignore("The implementation is dependent on the SimpleStore") |
Brian O'Connor | eba4e34 | 2015-04-30 22:50:13 -0700 | [diff] [blame] | 141 | public void installingPoll() { |
| 142 | IntentStoreDelegate mockDelegate = new IntentStoreDelegate() { |
| 143 | @Override |
| 144 | public void process(IntentData intentData) { |
| 145 | intentData.setState(INSTALLING); |
| 146 | store.write(intentData); |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public void notify(IntentEvent event) { |
| 151 | cleanup.event(event); |
| 152 | } |
| 153 | }; |
| 154 | store.setDelegate(mockDelegate); |
| 155 | |
| 156 | Intent intent = new MockIntent(1L); |
| 157 | Timestamp version = new SystemClockTimestamp(1L); |
| 158 | IntentData data = new IntentData(intent, INSTALL_REQ, version); |
| 159 | store.addPending(data); |
| 160 | |
Brian O'Connor | 3822430 | 2016-08-02 22:03:01 -0700 | [diff] [blame] | 161 | service.addPending(data); |
Brian O'Connor | eba4e34 | 2015-04-30 22:50:13 -0700 | [diff] [blame] | 162 | expectLastCall().once(); |
| 163 | replay(service); |
| 164 | |
| 165 | cleanup.run(); |
| 166 | verify(service); |
| 167 | reset(service); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Only submit one of two intents because one is too new. |
| 172 | */ |
| 173 | @Test |
| 174 | public void skipPoll() { |
| 175 | IntentStoreDelegate mockDelegate = new IntentStoreDelegate() { |
| 176 | @Override |
| 177 | public void process(IntentData intentData) { |
| 178 | intentData.setState(CORRUPT); |
| 179 | store.write(intentData); |
| 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public void notify(IntentEvent event) {} |
| 184 | }; |
| 185 | store.setDelegate(mockDelegate); |
| 186 | |
| 187 | Intent intent = new MockIntent(1L); |
| 188 | IntentData data = new IntentData(intent, INSTALL_REQ, null); |
| 189 | store.addPending(data); |
| 190 | |
| 191 | Intent intent2 = new MockIntent(2L); |
| 192 | Timestamp version = new SystemClockTimestamp(1L); |
| 193 | data = new IntentData(intent2, INSTALL_REQ, version); |
| 194 | store.addPending(data); |
| 195 | |
Yi Tseng | 8013c4f | 2017-11-02 16:56:56 -0700 | [diff] [blame] | 196 | service.submit(intent); |
| 197 | expectLastCall().once(); |
| 198 | |
Brian O'Connor | eba4e34 | 2015-04-30 22:50:13 -0700 | [diff] [blame] | 199 | service.submit(intent2); |
| 200 | expectLastCall().once(); |
| 201 | replay(service); |
| 202 | |
| 203 | cleanup.run(); |
| 204 | verify(service); |
| 205 | reset(service); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Verify resubmit in response to CORRUPT event. |
| 210 | */ |
| 211 | @Test |
| 212 | public void corruptEvent() { |
| 213 | IntentStoreDelegate mockDelegate = new IntentStoreDelegate() { |
| 214 | @Override |
| 215 | public void process(IntentData intentData) { |
| 216 | intentData.setState(CORRUPT); |
| 217 | store.write(intentData); |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public void notify(IntentEvent event) { |
| 222 | cleanup.event(event); |
| 223 | } |
| 224 | }; |
| 225 | store.setDelegate(mockDelegate); |
| 226 | |
| 227 | |
| 228 | Intent intent = new MockIntent(1L); |
| 229 | IntentData data = new IntentData(intent, INSTALL_REQ, null); |
| 230 | |
| 231 | service.submit(intent); |
| 232 | expectLastCall().once(); |
| 233 | replay(service); |
| 234 | |
| 235 | store.addPending(data); |
| 236 | |
| 237 | verify(service); |
| 238 | reset(service); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Intent should not be retried because threshold is reached. |
| 243 | */ |
| 244 | @Test |
| 245 | public void corruptEventThreshold() { |
| 246 | IntentStoreDelegate mockDelegate = new IntentStoreDelegate() { |
| 247 | @Override |
| 248 | public void process(IntentData intentData) { |
| 249 | intentData.setState(CORRUPT); |
| 250 | intentData.setErrorCount(cleanup.retryThreshold); |
| 251 | store.write(intentData); |
| 252 | } |
| 253 | |
| 254 | @Override |
| 255 | public void notify(IntentEvent event) { |
| 256 | cleanup.event(event); |
| 257 | } |
| 258 | }; |
| 259 | store.setDelegate(mockDelegate); |
| 260 | |
| 261 | |
| 262 | Intent intent = new MockIntent(1L); |
| 263 | IntentData data = new IntentData(intent, INSTALL_REQ, null); |
| 264 | |
| 265 | replay(service); |
| 266 | |
| 267 | store.addPending(data); |
| 268 | |
| 269 | verify(service); |
| 270 | reset(service); |
| 271 | } |
| 272 | } |