blob: 1ccd3aafa1752e8432165a70e777ddc71317e1ee [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
jaegonkimcbe1c5e2018-05-20 15:11:18 +0900126 service.addPending(isA(IntentData.class));
127
Brian O'Connoreba4e342015-04-30 22:50:13 -0700128 expectLastCall().once();
129 replay(service);
130
131 cleanup.run();
132 verify(service);
133 reset(service);
134 }
135
136 /**
137 * Trigger resubmit of intent in INSTALLING for too long.
138 */
139 @Test
Pier Luigie6caf682017-01-26 15:25:09 -0800140 @Ignore("The implementation is dependent on the SimpleStore")
Brian O'Connoreba4e342015-04-30 22:50:13 -0700141 public void installingPoll() {
142 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
143 @Override
144 public void process(IntentData intentData) {
145 intentData.setState(INSTALLING);
146 store.write(intentData);
147 }
148
149 @Override
150 public void notify(IntentEvent event) {
151 cleanup.event(event);
152 }
153 };
154 store.setDelegate(mockDelegate);
155
156 Intent intent = new MockIntent(1L);
157 Timestamp version = new SystemClockTimestamp(1L);
158 IntentData data = new IntentData(intent, INSTALL_REQ, version);
159 store.addPending(data);
160
Brian O'Connor38224302016-08-02 22:03:01 -0700161 service.addPending(data);
Brian O'Connoreba4e342015-04-30 22:50:13 -0700162 expectLastCall().once();
163 replay(service);
164
165 cleanup.run();
166 verify(service);
167 reset(service);
168 }
169
170 /**
171 * Only submit one of two intents because one is too new.
172 */
173 @Test
174 public void skipPoll() {
175 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
176 @Override
177 public void process(IntentData intentData) {
178 intentData.setState(CORRUPT);
179 store.write(intentData);
180 }
181
182 @Override
183 public void notify(IntentEvent event) {}
184 };
185 store.setDelegate(mockDelegate);
186
187 Intent intent = new MockIntent(1L);
188 IntentData data = new IntentData(intent, INSTALL_REQ, null);
189 store.addPending(data);
190
191 Intent intent2 = new MockIntent(2L);
192 Timestamp version = new SystemClockTimestamp(1L);
193 data = new IntentData(intent2, INSTALL_REQ, version);
194 store.addPending(data);
195
Yi Tseng8013c4f2017-11-02 16:56:56 -0700196 service.submit(intent);
197 expectLastCall().once();
198
Brian O'Connoreba4e342015-04-30 22:50:13 -0700199 service.submit(intent2);
200 expectLastCall().once();
201 replay(service);
202
203 cleanup.run();
204 verify(service);
205 reset(service);
206 }
207
208 /**
209 * Verify resubmit in response to CORRUPT event.
210 */
211 @Test
212 public void corruptEvent() {
213 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
214 @Override
215 public void process(IntentData intentData) {
216 intentData.setState(CORRUPT);
217 store.write(intentData);
218 }
219
220 @Override
221 public void notify(IntentEvent event) {
222 cleanup.event(event);
223 }
224 };
225 store.setDelegate(mockDelegate);
226
227
228 Intent intent = new MockIntent(1L);
229 IntentData data = new IntentData(intent, INSTALL_REQ, null);
230
231 service.submit(intent);
232 expectLastCall().once();
233 replay(service);
234
235 store.addPending(data);
236
237 verify(service);
238 reset(service);
239 }
240
241 /**
242 * Intent should not be retried because threshold is reached.
243 */
244 @Test
245 public void corruptEventThreshold() {
246 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
247 @Override
248 public void process(IntentData intentData) {
249 intentData.setState(CORRUPT);
250 intentData.setErrorCount(cleanup.retryThreshold);
251 store.write(intentData);
252 }
253
254 @Override
255 public void notify(IntentEvent event) {
256 cleanup.event(event);
257 }
258 };
259 store.setDelegate(mockDelegate);
260
261
262 Intent intent = new MockIntent(1L);
263 IntentData data = new IntentData(intent, INSTALL_REQ, null);
264
265 replay(service);
266
267 store.addPending(data);
268
269 verify(service);
270 reset(service);
271 }
272}