blob: 15ee24e6d38a4a63183fc5ab07ff98fa1f435ab9 [file] [log] [blame]
Brian O'Connoreba4e342015-04-30 22:50:13 -07001/*
2 * Copyright 2015 Open Networking Laboratory
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 */
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 */
95 @Test
96 public void corruptPoll() {
97 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
98 @Override
99 public void process(IntentData intentData) {
100 intentData.setState(CORRUPT);
101 store.write(intentData);
102 }
103
104 @Override
105 public void notify(IntentEvent event) {}
106 };
107 store.setDelegate(mockDelegate);
108
109 Intent intent = new MockIntent(1L);
110 Timestamp version = new SystemClockTimestamp(1L);
111 IntentData data = new IntentData(intent, INSTALL_REQ, version);
112 store.addPending(data);
113
114 service.submit(intent);
115 expectLastCall().once();
116 replay(service);
117
118 cleanup.run(); //FIXME broken?
119 verify(service);
120 reset(service);
121 }
122
123 /**
124 * Trigger resubmit of intent in INSTALL_REQ for too long.
125 */
126 @Test
127 public void pendingPoll() {
128 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
129 @Override
130 public void process(IntentData intentData) {}
131
132 @Override
133 public void notify(IntentEvent event) {
134 cleanup.event(event);
135 }
136 };
137 store.setDelegate(mockDelegate);
138
139 Intent intent = new MockIntent(1L);
140 Timestamp version = new SystemClockTimestamp(1L);
141 IntentData data = new IntentData(intent, INSTALL_REQ, version);
142 store.addPending(data);
143
144 service.submit(intent);
145 expectLastCall().once();
146 replay(service);
147
148 cleanup.run();
149 verify(service);
150 reset(service);
151 }
152
153 /**
154 * Trigger resubmit of intent in INSTALLING for too long.
155 */
156 @Test
157 public void installingPoll() {
158 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
159 @Override
160 public void process(IntentData intentData) {
161 intentData.setState(INSTALLING);
162 store.write(intentData);
163 }
164
165 @Override
166 public void notify(IntentEvent event) {
167 cleanup.event(event);
168 }
169 };
170 store.setDelegate(mockDelegate);
171
172 Intent intent = new MockIntent(1L);
173 Timestamp version = new SystemClockTimestamp(1L);
174 IntentData data = new IntentData(intent, INSTALL_REQ, version);
175 store.addPending(data);
176
177 service.submit(intent);
178 expectLastCall().once();
179 replay(service);
180
181 cleanup.run();
182 verify(service);
183 reset(service);
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 service.submit(intent2);
213 expectLastCall().once();
214 replay(service);
215
216 cleanup.run();
217 verify(service);
218 reset(service);
219 }
220
221 /**
222 * Verify resubmit in response to CORRUPT event.
223 */
224 @Test
225 public void corruptEvent() {
226 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
227 @Override
228 public void process(IntentData intentData) {
229 intentData.setState(CORRUPT);
230 store.write(intentData);
231 }
232
233 @Override
234 public void notify(IntentEvent event) {
235 cleanup.event(event);
236 }
237 };
238 store.setDelegate(mockDelegate);
239
240
241 Intent intent = new MockIntent(1L);
242 IntentData data = new IntentData(intent, INSTALL_REQ, null);
243
244 service.submit(intent);
245 expectLastCall().once();
246 replay(service);
247
248 store.addPending(data);
249
250 verify(service);
251 reset(service);
252 }
253
254 /**
255 * Intent should not be retried because threshold is reached.
256 */
257 @Test
258 public void corruptEventThreshold() {
259 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
260 @Override
261 public void process(IntentData intentData) {
262 intentData.setState(CORRUPT);
263 intentData.setErrorCount(cleanup.retryThreshold);
264 store.write(intentData);
265 }
266
267 @Override
268 public void notify(IntentEvent event) {
269 cleanup.event(event);
270 }
271 };
272 store.setDelegate(mockDelegate);
273
274
275 Intent intent = new MockIntent(1L);
276 IntentData data = new IntentData(intent, INSTALL_REQ, null);
277
278 replay(service);
279
280 store.addPending(data);
281
282 verify(service);
283 reset(service);
284 }
285}