blob: 9d297294333011c4a9d846d5c0ae114b69a17edf [file] [log] [blame]
Brian O'Connoreba4e342015-04-30 22:50:13 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Brian O'Connoreba4e342015-04-30 22:50:13 -07003 *
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 */
16package org.onosproject.net.intent.impl;
17
18import com.google.common.collect.Sets;
19import org.junit.After;
20import org.junit.Before;
21import org.junit.Test;
22import org.onosproject.cfg.ComponentConfigAdapter;
23import org.onosproject.core.IdGenerator;
24import org.onosproject.net.intent.Intent;
25import org.onosproject.net.intent.IntentData;
26import org.onosproject.net.intent.IntentEvent;
27import org.onosproject.net.intent.IntentService;
28import org.onosproject.net.intent.IntentStore;
29import org.onosproject.net.intent.IntentStoreDelegate;
30import org.onosproject.net.intent.MockIdGenerator;
31import org.onosproject.store.Timestamp;
Thomas Vachuskac97aa612015-06-23 16:00:18 -070032import org.onosproject.store.trivial.SimpleIntentStore;
33import org.onosproject.store.trivial.SystemClockTimestamp;
Brian O'Connoreba4e342015-04-30 22:50:13 -070034
35import static org.easymock.EasyMock.*;
36import static org.junit.Assert.assertTrue;
37import static org.onosproject.net.intent.IntentState.*;
38import static org.onosproject.net.intent.IntentTestsMocks.MockIntent;
39
40/**
41 * Test intent cleanup using Mocks.
42 * FIXME remove this or IntentCleanupTest
43 */
44public class IntentCleanupTestMock {
45
46 private IntentCleanup cleanup;
47 private IntentService service;
48 private IntentStore store;
49 protected IdGenerator idGenerator; // global or one per test? per test for now.
50
51 @Before
52 public void setUp() {
53 service = createMock(IntentService.class);
54 store = new SimpleIntentStore();
55 cleanup = new IntentCleanup();
56 idGenerator = new MockIdGenerator();
57
58 service.addListener(cleanup);
59 expectLastCall().once();
60 replay(service);
61
62 cleanup.cfgService = new ComponentConfigAdapter();
63 cleanup.service = service;
64 cleanup.store = store;
65 cleanup.period = 1000;
66 cleanup.retryThreshold = 3;
67 cleanup.activate();
68
69 verify(service);
70 reset(service);
71
72 assertTrue("store should be empty",
73 Sets.newHashSet(cleanup.store.getIntents()).isEmpty());
74
75 Intent.bindIdGenerator(idGenerator);
76 }
77
78 @After
79 public void tearDown() {
80 service.removeListener(cleanup);
81 expectLastCall().once();
82 replay(service);
83
84 cleanup.deactivate();
85
86 verify(service);
87 reset(service);
88
89 Intent.unbindIdGenerator(idGenerator);
90 }
91
92 /**
93 * Trigger resubmit of intent in CORRUPT during periodic poll.
94 */
Brian O'Connora91dc192016-10-25 00:44:37 -070095 // FIXME there is a race condition in this test that will trigger a failure very rarely
Brian O'Connoreba4e342015-04-30 22:50:13 -070096 @Test
97 public void corruptPoll() {
98 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
99 @Override
100 public void process(IntentData intentData) {
101 intentData.setState(CORRUPT);
102 store.write(intentData);
103 }
104
105 @Override
106 public void notify(IntentEvent event) {}
107 };
108 store.setDelegate(mockDelegate);
109
110 Intent intent = new MockIntent(1L);
111 Timestamp version = new SystemClockTimestamp(1L);
112 IntentData data = new IntentData(intent, INSTALL_REQ, version);
113 store.addPending(data);
114
115 service.submit(intent);
116 expectLastCall().once();
117 replay(service);
118
119 cleanup.run(); //FIXME broken?
120 verify(service);
121 reset(service);
122 }
123
124 /**
125 * Trigger resubmit of intent in INSTALL_REQ for too long.
126 */
127 @Test
128 public void pendingPoll() {
129 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
130 @Override
131 public void process(IntentData intentData) {}
132
133 @Override
134 public void notify(IntentEvent event) {
135 cleanup.event(event);
136 }
137 };
138 store.setDelegate(mockDelegate);
139
140 Intent intent = new MockIntent(1L);
141 Timestamp version = new SystemClockTimestamp(1L);
142 IntentData data = new IntentData(intent, INSTALL_REQ, version);
143 store.addPending(data);
144
145 service.submit(intent);
146 expectLastCall().once();
147 replay(service);
148
149 cleanup.run();
150 verify(service);
151 reset(service);
152 }
153
154 /**
155 * Trigger resubmit of intent in INSTALLING for too long.
156 */
157 @Test
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 service.submit(intent);
179 expectLastCall().once();
180 replay(service);
181
182 cleanup.run();
183 verify(service);
184 reset(service);
185 }
186
187 /**
188 * Only submit one of two intents because one is too new.
189 */
190 @Test
191 public void skipPoll() {
192 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
193 @Override
194 public void process(IntentData intentData) {
195 intentData.setState(CORRUPT);
196 store.write(intentData);
197 }
198
199 @Override
200 public void notify(IntentEvent event) {}
201 };
202 store.setDelegate(mockDelegate);
203
204 Intent intent = new MockIntent(1L);
205 IntentData data = new IntentData(intent, INSTALL_REQ, null);
206 store.addPending(data);
207
208 Intent intent2 = new MockIntent(2L);
209 Timestamp version = new SystemClockTimestamp(1L);
210 data = new IntentData(intent2, INSTALL_REQ, version);
211 store.addPending(data);
212
213 service.submit(intent2);
214 expectLastCall().once();
215 replay(service);
216
217 cleanup.run();
218 verify(service);
219 reset(service);
220 }
221
222 /**
223 * Verify resubmit in response to CORRUPT event.
224 */
225 @Test
226 public void corruptEvent() {
227 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
228 @Override
229 public void process(IntentData intentData) {
230 intentData.setState(CORRUPT);
231 store.write(intentData);
232 }
233
234 @Override
235 public void notify(IntentEvent event) {
236 cleanup.event(event);
237 }
238 };
239 store.setDelegate(mockDelegate);
240
241
242 Intent intent = new MockIntent(1L);
243 IntentData data = new IntentData(intent, INSTALL_REQ, null);
244
245 service.submit(intent);
246 expectLastCall().once();
247 replay(service);
248
249 store.addPending(data);
250
251 verify(service);
252 reset(service);
253 }
254
255 /**
256 * Intent should not be retried because threshold is reached.
257 */
258 @Test
259 public void corruptEventThreshold() {
260 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
261 @Override
262 public void process(IntentData intentData) {
263 intentData.setState(CORRUPT);
264 intentData.setErrorCount(cleanup.retryThreshold);
265 store.write(intentData);
266 }
267
268 @Override
269 public void notify(IntentEvent event) {
270 cleanup.event(event);
271 }
272 };
273 store.setDelegate(mockDelegate);
274
275
276 Intent intent = new MockIntent(1L);
277 IntentData data = new IntentData(intent, INSTALL_REQ, null);
278
279 replay(service);
280
281 store.addPending(data);
282
283 verify(service);
284 reset(service);
285 }
286}