blob: 40efded2655d6edfadedfbb49acbcdf56927e2c3 [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;
Pier Luigie6caf682017-01-26 15:25:09 -080021import org.junit.Ignore;
Brian O'Connoreba4e342015-04-30 22:50:13 -070022import org.junit.Test;
23import org.onosproject.cfg.ComponentConfigAdapter;
24import org.onosproject.core.IdGenerator;
25import org.onosproject.net.intent.Intent;
26import org.onosproject.net.intent.IntentData;
27import org.onosproject.net.intent.IntentEvent;
28import org.onosproject.net.intent.IntentService;
29import org.onosproject.net.intent.IntentStore;
30import org.onosproject.net.intent.IntentStoreDelegate;
31import org.onosproject.net.intent.MockIdGenerator;
32import org.onosproject.store.Timestamp;
Thomas Vachuskac97aa612015-06-23 16:00:18 -070033import org.onosproject.store.trivial.SimpleIntentStore;
34import org.onosproject.store.trivial.SystemClockTimestamp;
Brian O'Connoreba4e342015-04-30 22:50:13 -070035
36import static org.easymock.EasyMock.*;
37import static org.junit.Assert.assertTrue;
38import static org.onosproject.net.intent.IntentState.*;
39import static org.onosproject.net.intent.IntentTestsMocks.MockIntent;
40
41/**
42 * Test intent cleanup using Mocks.
43 * FIXME remove this or IntentCleanupTest
44 */
45public class IntentCleanupTestMock {
46
47 private IntentCleanup cleanup;
48 private IntentService service;
49 private IntentStore store;
50 protected IdGenerator idGenerator; // global or one per test? per test for now.
51
52 @Before
53 public void setUp() {
54 service = createMock(IntentService.class);
55 store = new SimpleIntentStore();
56 cleanup = new IntentCleanup();
57 idGenerator = new MockIdGenerator();
58
59 service.addListener(cleanup);
60 expectLastCall().once();
61 replay(service);
62
63 cleanup.cfgService = new ComponentConfigAdapter();
64 cleanup.service = service;
65 cleanup.store = store;
66 cleanup.period = 1000;
67 cleanup.retryThreshold = 3;
68 cleanup.activate();
69
70 verify(service);
71 reset(service);
72
73 assertTrue("store should be empty",
74 Sets.newHashSet(cleanup.store.getIntents()).isEmpty());
75
Thomas Vachuska23235962017-02-03 11:44:15 -080076 Intent.unbindIdGenerator(idGenerator);
Brian O'Connoreba4e342015-04-30 22:50:13 -070077 Intent.bindIdGenerator(idGenerator);
78 }
79
80 @After
81 public void tearDown() {
82 service.removeListener(cleanup);
83 expectLastCall().once();
84 replay(service);
85
86 cleanup.deactivate();
87
88 verify(service);
89 reset(service);
90
91 Intent.unbindIdGenerator(idGenerator);
92 }
93
94 /**
95 * Trigger resubmit of intent in CORRUPT during periodic poll.
96 */
Brian O'Connoreba4e342015-04-30 22:50:13 -070097 @Test
98 public void corruptPoll() {
99 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
100 @Override
101 public void process(IntentData intentData) {
102 intentData.setState(CORRUPT);
103 store.write(intentData);
104 }
105
106 @Override
107 public void notify(IntentEvent event) {}
108 };
109 store.setDelegate(mockDelegate);
110
111 Intent intent = new MockIntent(1L);
112 Timestamp version = new SystemClockTimestamp(1L);
113 IntentData data = new IntentData(intent, INSTALL_REQ, version);
114 store.addPending(data);
115
116 service.submit(intent);
117 expectLastCall().once();
118 replay(service);
119
Jihwan Kimfc1bf342016-11-12 00:37:07 +0900120 synchronized (service) {
Jihwan Kima19e1b72016-11-12 11:39:38 +0900121 cleanup.run();
Jihwan Kimfc1bf342016-11-12 00:37:07 +0900122 }
Brian O'Connoreba4e342015-04-30 22:50:13 -0700123 verify(service);
124 reset(service);
125 }
126
127 /**
128 * Trigger resubmit of intent in INSTALL_REQ for too long.
129 */
130 @Test
131 public void pendingPoll() {
132 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
133 @Override
134 public void process(IntentData intentData) {}
135
136 @Override
137 public void notify(IntentEvent event) {
138 cleanup.event(event);
139 }
140 };
141 store.setDelegate(mockDelegate);
142
143 Intent intent = new MockIntent(1L);
144 Timestamp version = new SystemClockTimestamp(1L);
145 IntentData data = new IntentData(intent, INSTALL_REQ, version);
146 store.addPending(data);
147
Brian O'Connor38224302016-08-02 22:03:01 -0700148 service.addPending(data);
Brian O'Connoreba4e342015-04-30 22:50:13 -0700149 expectLastCall().once();
150 replay(service);
151
152 cleanup.run();
153 verify(service);
154 reset(service);
155 }
156
157 /**
158 * Trigger resubmit of intent in INSTALLING for too long.
159 */
160 @Test
Pier Luigie6caf682017-01-26 15:25:09 -0800161 @Ignore("The implementation is dependent on the SimpleStore")
Brian O'Connoreba4e342015-04-30 22:50:13 -0700162 public void installingPoll() {
163 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
164 @Override
165 public void process(IntentData intentData) {
166 intentData.setState(INSTALLING);
167 store.write(intentData);
168 }
169
170 @Override
171 public void notify(IntentEvent event) {
172 cleanup.event(event);
173 }
174 };
175 store.setDelegate(mockDelegate);
176
177 Intent intent = new MockIntent(1L);
178 Timestamp version = new SystemClockTimestamp(1L);
179 IntentData data = new IntentData(intent, INSTALL_REQ, version);
180 store.addPending(data);
181
Brian O'Connor38224302016-08-02 22:03:01 -0700182 service.addPending(data);
Brian O'Connoreba4e342015-04-30 22:50:13 -0700183 expectLastCall().once();
184 replay(service);
185
186 cleanup.run();
187 verify(service);
188 reset(service);
189 }
190
191 /**
192 * Only submit one of two intents because one is too new.
193 */
194 @Test
195 public void skipPoll() {
196 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
197 @Override
198 public void process(IntentData intentData) {
199 intentData.setState(CORRUPT);
200 store.write(intentData);
201 }
202
203 @Override
204 public void notify(IntentEvent event) {}
205 };
206 store.setDelegate(mockDelegate);
207
208 Intent intent = new MockIntent(1L);
209 IntentData data = new IntentData(intent, INSTALL_REQ, null);
210 store.addPending(data);
211
212 Intent intent2 = new MockIntent(2L);
213 Timestamp version = new SystemClockTimestamp(1L);
214 data = new IntentData(intent2, INSTALL_REQ, version);
215 store.addPending(data);
216
217 service.submit(intent2);
218 expectLastCall().once();
219 replay(service);
220
221 cleanup.run();
222 verify(service);
223 reset(service);
224 }
225
226 /**
227 * Verify resubmit in response to CORRUPT event.
228 */
229 @Test
230 public void corruptEvent() {
231 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
232 @Override
233 public void process(IntentData intentData) {
234 intentData.setState(CORRUPT);
235 store.write(intentData);
236 }
237
238 @Override
239 public void notify(IntentEvent event) {
240 cleanup.event(event);
241 }
242 };
243 store.setDelegate(mockDelegate);
244
245
246 Intent intent = new MockIntent(1L);
247 IntentData data = new IntentData(intent, INSTALL_REQ, null);
248
249 service.submit(intent);
250 expectLastCall().once();
251 replay(service);
252
253 store.addPending(data);
254
255 verify(service);
256 reset(service);
257 }
258
259 /**
260 * Intent should not be retried because threshold is reached.
261 */
262 @Test
263 public void corruptEventThreshold() {
264 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
265 @Override
266 public void process(IntentData intentData) {
267 intentData.setState(CORRUPT);
268 intentData.setErrorCount(cleanup.retryThreshold);
269 store.write(intentData);
270 }
271
272 @Override
273 public void notify(IntentEvent event) {
274 cleanup.event(event);
275 }
276 };
277 store.setDelegate(mockDelegate);
278
279
280 Intent intent = new MockIntent(1L);
281 IntentData data = new IntentData(intent, INSTALL_REQ, null);
282
283 replay(service);
284
285 store.addPending(data);
286
287 verify(service);
288 reset(service);
289 }
290}