blob: ac7f043d2456d9bbb511d1bef8bd03380bc23c7a [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;
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070024import org.onosproject.net.intent.AbstractIntentTest;
Brian O'Connoreba4e342015-04-30 22:50:13 -070025import 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;
Brian O'Connoreba4e342015-04-30 22:50:13 -070031import 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 */
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070044public class IntentCleanupTestMock extends AbstractIntentTest {
Brian O'Connoreba4e342015-04-30 22:50:13 -070045
46 private IntentCleanup cleanup;
47 private IntentService service;
48 private IntentStore store;
Brian O'Connoreba4e342015-04-30 22:50:13 -070049
50 @Before
51 public void setUp() {
52 service = createMock(IntentService.class);
53 store = new SimpleIntentStore();
54 cleanup = new IntentCleanup();
Brian O'Connoreba4e342015-04-30 22:50:13 -070055
56 service.addListener(cleanup);
57 expectLastCall().once();
58 replay(service);
59
60 cleanup.cfgService = new ComponentConfigAdapter();
61 cleanup.service = service;
62 cleanup.store = store;
63 cleanup.period = 1000;
64 cleanup.retryThreshold = 3;
65 cleanup.activate();
66
67 verify(service);
68 reset(service);
69
70 assertTrue("store should be empty",
71 Sets.newHashSet(cleanup.store.getIntents()).isEmpty());
72
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070073 super.setUp();
Brian O'Connoreba4e342015-04-30 22:50:13 -070074 }
75
76 @After
77 public void tearDown() {
78 service.removeListener(cleanup);
79 expectLastCall().once();
80 replay(service);
81
82 cleanup.deactivate();
83
84 verify(service);
85 reset(service);
86
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070087 super.tearDown();
Brian O'Connoreba4e342015-04-30 22:50:13 -070088 }
89
90 /**
91 * Trigger resubmit of intent in CORRUPT during periodic poll.
92 */
Brian O'Connoreba4e342015-04-30 22:50:13 -070093 @Test
94 public void corruptPoll() {
95 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
96 @Override
97 public void process(IntentData intentData) {
98 intentData.setState(CORRUPT);
99 store.write(intentData);
100 }
101
102 @Override
103 public void notify(IntentEvent event) {}
104 };
105 store.setDelegate(mockDelegate);
106
107 Intent intent = new MockIntent(1L);
108 Timestamp version = new SystemClockTimestamp(1L);
109 IntentData data = new IntentData(intent, INSTALL_REQ, version);
110 store.addPending(data);
111
112 service.submit(intent);
113 expectLastCall().once();
114 replay(service);
115
Jihwan Kimfc1bf342016-11-12 00:37:07 +0900116 synchronized (service) {
Jihwan Kima19e1b72016-11-12 11:39:38 +0900117 cleanup.run();
Jihwan Kimfc1bf342016-11-12 00:37:07 +0900118 }
Brian O'Connoreba4e342015-04-30 22:50:13 -0700119 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
Brian O'Connor38224302016-08-02 22:03:01 -0700144 service.addPending(data);
Brian O'Connoreba4e342015-04-30 22:50:13 -0700145 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
Pier Luigie6caf682017-01-26 15:25:09 -0800157 @Ignore("The implementation is dependent on the SimpleStore")
Brian O'Connoreba4e342015-04-30 22:50:13 -0700158 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
Brian O'Connor38224302016-08-02 22:03:01 -0700178 service.addPending(data);
Brian O'Connoreba4e342015-04-30 22:50:13 -0700179 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}