blob: 42ef127c1de1faca07949015d786b93a43aa9bdc [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'Connoreba4e342015-04-30 22:50:13 -070095 @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
Jihwan Kimfc1bf342016-11-12 00:37:07 +0900118 synchronized (service) {
Jihwan Kima19e1b72016-11-12 11:39:38 +0900119 cleanup.run();
Jihwan Kimfc1bf342016-11-12 00:37:07 +0900120 }
Brian O'Connoreba4e342015-04-30 22:50:13 -0700121 verify(service);
122 reset(service);
123 }
124
125 /**
126 * Trigger resubmit of intent in INSTALL_REQ for too long.
127 */
128 @Test
129 public void pendingPoll() {
130 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
131 @Override
132 public void process(IntentData intentData) {}
133
134 @Override
135 public void notify(IntentEvent event) {
136 cleanup.event(event);
137 }
138 };
139 store.setDelegate(mockDelegate);
140
141 Intent intent = new MockIntent(1L);
142 Timestamp version = new SystemClockTimestamp(1L);
143 IntentData data = new IntentData(intent, INSTALL_REQ, version);
144 store.addPending(data);
145
Brian O'Connor38224302016-08-02 22:03:01 -0700146 service.addPending(data);
Brian O'Connoreba4e342015-04-30 22:50:13 -0700147 expectLastCall().once();
148 replay(service);
149
150 cleanup.run();
151 verify(service);
152 reset(service);
153 }
154
155 /**
156 * Trigger resubmit of intent in INSTALLING for too long.
157 */
158 @Test
159 public void installingPoll() {
160 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
161 @Override
162 public void process(IntentData intentData) {
163 intentData.setState(INSTALLING);
164 store.write(intentData);
165 }
166
167 @Override
168 public void notify(IntentEvent event) {
169 cleanup.event(event);
170 }
171 };
172 store.setDelegate(mockDelegate);
173
174 Intent intent = new MockIntent(1L);
175 Timestamp version = new SystemClockTimestamp(1L);
176 IntentData data = new IntentData(intent, INSTALL_REQ, version);
177 store.addPending(data);
178
Brian O'Connor38224302016-08-02 22:03:01 -0700179 service.addPending(data);
Brian O'Connoreba4e342015-04-30 22:50:13 -0700180 expectLastCall().once();
181 replay(service);
182
183 cleanup.run();
184 verify(service);
185 reset(service);
186 }
187
188 /**
189 * Only submit one of two intents because one is too new.
190 */
191 @Test
192 public void skipPoll() {
193 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
194 @Override
195 public void process(IntentData intentData) {
196 intentData.setState(CORRUPT);
197 store.write(intentData);
198 }
199
200 @Override
201 public void notify(IntentEvent event) {}
202 };
203 store.setDelegate(mockDelegate);
204
205 Intent intent = new MockIntent(1L);
206 IntentData data = new IntentData(intent, INSTALL_REQ, null);
207 store.addPending(data);
208
209 Intent intent2 = new MockIntent(2L);
210 Timestamp version = new SystemClockTimestamp(1L);
211 data = new IntentData(intent2, INSTALL_REQ, version);
212 store.addPending(data);
213
214 service.submit(intent2);
215 expectLastCall().once();
216 replay(service);
217
218 cleanup.run();
219 verify(service);
220 reset(service);
221 }
222
223 /**
224 * Verify resubmit in response to CORRUPT event.
225 */
226 @Test
227 public void corruptEvent() {
228 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
229 @Override
230 public void process(IntentData intentData) {
231 intentData.setState(CORRUPT);
232 store.write(intentData);
233 }
234
235 @Override
236 public void notify(IntentEvent event) {
237 cleanup.event(event);
238 }
239 };
240 store.setDelegate(mockDelegate);
241
242
243 Intent intent = new MockIntent(1L);
244 IntentData data = new IntentData(intent, INSTALL_REQ, null);
245
246 service.submit(intent);
247 expectLastCall().once();
248 replay(service);
249
250 store.addPending(data);
251
252 verify(service);
253 reset(service);
254 }
255
256 /**
257 * Intent should not be retried because threshold is reached.
258 */
259 @Test
260 public void corruptEventThreshold() {
261 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
262 @Override
263 public void process(IntentData intentData) {
264 intentData.setState(CORRUPT);
265 intentData.setErrorCount(cleanup.retryThreshold);
266 store.write(intentData);
267 }
268
269 @Override
270 public void notify(IntentEvent event) {
271 cleanup.event(event);
272 }
273 };
274 store.setDelegate(mockDelegate);
275
276
277 Intent intent = new MockIntent(1L);
278 IntentData data = new IntentData(intent, INSTALL_REQ, null);
279
280 replay(service);
281
282 store.addPending(data);
283
284 verify(service);
285 reset(service);
286 }
287}