blob: dcc4188c9a8cefcf53a19e5c0e21703ccda19201 [file] [log] [blame]
Brian O'Connoreba4e342015-04-30 22:50:13 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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
Brian O'Connoreba4e342015-04-30 22:50:13 -070056 cleanup.cfgService = new ComponentConfigAdapter();
57 cleanup.service = service;
58 cleanup.store = store;
59 cleanup.period = 1000;
60 cleanup.retryThreshold = 3;
Brian O'Connoreba4e342015-04-30 22:50:13 -070061
62 assertTrue("store should be empty",
63 Sets.newHashSet(cleanup.store.getIntents()).isEmpty());
64
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070065 super.setUp();
Brian O'Connoreba4e342015-04-30 22:50:13 -070066 }
67
68 @After
69 public void tearDown() {
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070070 super.tearDown();
Brian O'Connoreba4e342015-04-30 22:50:13 -070071 }
72
73 /**
74 * Trigger resubmit of intent in CORRUPT during periodic poll.
75 */
Brian O'Connoreba4e342015-04-30 22:50:13 -070076 @Test
77 public void corruptPoll() {
78 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
79 @Override
80 public void process(IntentData intentData) {
81 intentData.setState(CORRUPT);
82 store.write(intentData);
83 }
84
85 @Override
86 public void notify(IntentEvent event) {}
87 };
88 store.setDelegate(mockDelegate);
89
90 Intent intent = new MockIntent(1L);
91 Timestamp version = new SystemClockTimestamp(1L);
92 IntentData data = new IntentData(intent, INSTALL_REQ, version);
93 store.addPending(data);
94
95 service.submit(intent);
96 expectLastCall().once();
97 replay(service);
98
Yi Tseng8013c4f2017-11-02 16:56:56 -070099 cleanup.run();
100
Brian O'Connoreba4e342015-04-30 22:50:13 -0700101 verify(service);
102 reset(service);
103 }
104
105 /**
106 * Trigger resubmit of intent in INSTALL_REQ for too long.
107 */
108 @Test
109 public void pendingPoll() {
110 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
111 @Override
112 public void process(IntentData intentData) {}
113
114 @Override
115 public void notify(IntentEvent event) {
116 cleanup.event(event);
117 }
118 };
119 store.setDelegate(mockDelegate);
120
121 Intent intent = new MockIntent(1L);
122 Timestamp version = new SystemClockTimestamp(1L);
123 IntentData data = new IntentData(intent, INSTALL_REQ, version);
124 store.addPending(data);
125
Brian O'Connor38224302016-08-02 22:03:01 -0700126 service.addPending(data);
Brian O'Connoreba4e342015-04-30 22:50:13 -0700127 expectLastCall().once();
128 replay(service);
129
130 cleanup.run();
131 verify(service);
132 reset(service);
133 }
134
135 /**
136 * Trigger resubmit of intent in INSTALLING for too long.
137 */
138 @Test
Pier Luigie6caf682017-01-26 15:25:09 -0800139 @Ignore("The implementation is dependent on the SimpleStore")
Brian O'Connoreba4e342015-04-30 22:50:13 -0700140 public void installingPoll() {
141 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
142 @Override
143 public void process(IntentData intentData) {
144 intentData.setState(INSTALLING);
145 store.write(intentData);
146 }
147
148 @Override
149 public void notify(IntentEvent event) {
150 cleanup.event(event);
151 }
152 };
153 store.setDelegate(mockDelegate);
154
155 Intent intent = new MockIntent(1L);
156 Timestamp version = new SystemClockTimestamp(1L);
157 IntentData data = new IntentData(intent, INSTALL_REQ, version);
158 store.addPending(data);
159
Brian O'Connor38224302016-08-02 22:03:01 -0700160 service.addPending(data);
Brian O'Connoreba4e342015-04-30 22:50:13 -0700161 expectLastCall().once();
162 replay(service);
163
164 cleanup.run();
165 verify(service);
166 reset(service);
167 }
168
169 /**
170 * Only submit one of two intents because one is too new.
171 */
172 @Test
173 public void skipPoll() {
174 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
175 @Override
176 public void process(IntentData intentData) {
177 intentData.setState(CORRUPT);
178 store.write(intentData);
179 }
180
181 @Override
182 public void notify(IntentEvent event) {}
183 };
184 store.setDelegate(mockDelegate);
185
186 Intent intent = new MockIntent(1L);
187 IntentData data = new IntentData(intent, INSTALL_REQ, null);
188 store.addPending(data);
189
190 Intent intent2 = new MockIntent(2L);
191 Timestamp version = new SystemClockTimestamp(1L);
192 data = new IntentData(intent2, INSTALL_REQ, version);
193 store.addPending(data);
194
Yi Tseng8013c4f2017-11-02 16:56:56 -0700195 service.submit(intent);
196 expectLastCall().once();
197
Brian O'Connoreba4e342015-04-30 22:50:13 -0700198 service.submit(intent2);
199 expectLastCall().once();
200 replay(service);
201
202 cleanup.run();
203 verify(service);
204 reset(service);
205 }
206
207 /**
208 * Verify resubmit in response to CORRUPT event.
209 */
210 @Test
211 public void corruptEvent() {
212 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
213 @Override
214 public void process(IntentData intentData) {
215 intentData.setState(CORRUPT);
216 store.write(intentData);
217 }
218
219 @Override
220 public void notify(IntentEvent event) {
221 cleanup.event(event);
222 }
223 };
224 store.setDelegate(mockDelegate);
225
226
227 Intent intent = new MockIntent(1L);
228 IntentData data = new IntentData(intent, INSTALL_REQ, null);
229
230 service.submit(intent);
231 expectLastCall().once();
232 replay(service);
233
234 store.addPending(data);
235
236 verify(service);
237 reset(service);
238 }
239
240 /**
241 * Intent should not be retried because threshold is reached.
242 */
243 @Test
244 public void corruptEventThreshold() {
245 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
246 @Override
247 public void process(IntentData intentData) {
248 intentData.setState(CORRUPT);
249 intentData.setErrorCount(cleanup.retryThreshold);
250 store.write(intentData);
251 }
252
253 @Override
254 public void notify(IntentEvent event) {
255 cleanup.event(event);
256 }
257 };
258 store.setDelegate(mockDelegate);
259
260
261 Intent intent = new MockIntent(1L);
262 IntentData data = new IntentData(intent, INSTALL_REQ, null);
263
264 replay(service);
265
266 store.addPending(data);
267
268 verify(service);
269 reset(service);
270 }
271}