blob: 101569db9971a45ac9fed8cdd877ecda14c889f4 [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.IntentServiceAdapter;
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.junit.Assert.assertEquals;
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.
42 */
43public class IntentCleanupTest {
44
45 private IntentCleanup cleanup;
46 private MockIntentService service;
47 private IntentStore store;
48 protected IdGenerator idGenerator; // global or one per test? per test for now.
49
50 private static class MockIntentService extends IntentServiceAdapter {
51
52 private int submitCounter = 0;
Brian O'Connor38224302016-08-02 22:03:01 -070053 private int pendingCounter = 0;
Brian O'Connoreba4e342015-04-30 22:50:13 -070054
55 @Override
56 public void submit(Intent intent) {
57 submitCounter++;
58 }
59
Brian O'Connor38224302016-08-02 22:03:01 -070060 @Override
61 public void addPending(IntentData intentData) {
62 pendingCounter++;
63 }
64
Brian O'Connoreba4e342015-04-30 22:50:13 -070065 public int submitCounter() {
66 return submitCounter;
67 }
Brian O'Connor38224302016-08-02 22:03:01 -070068
69 public int pendingCounter() {
70 return pendingCounter;
71 }
72
Brian O'Connoreba4e342015-04-30 22:50:13 -070073 }
74
75 @Before
76 public void setUp() {
77 service = new MockIntentService();
78 store = new SimpleIntentStore();
79 cleanup = new IntentCleanup();
80 idGenerator = new MockIdGenerator();
81
82 cleanup.cfgService = new ComponentConfigAdapter();
83 cleanup.service = service;
84 cleanup.store = store;
85 cleanup.period = 10;
86 cleanup.retryThreshold = 3;
87 cleanup.activate();
88
89 assertTrue("store should be empty",
90 Sets.newHashSet(cleanup.store.getIntents()).isEmpty());
91
Thomas Vachuska23235962017-02-03 11:44:15 -080092 Intent.unbindIdGenerator(idGenerator);
Brian O'Connoreba4e342015-04-30 22:50:13 -070093 Intent.bindIdGenerator(idGenerator);
94 }
95
96 @After
97 public void tearDown() {
98 cleanup.deactivate();
Brian O'Connoreba4e342015-04-30 22:50:13 -070099 Intent.unbindIdGenerator(idGenerator);
100 }
101
102 /**
103 * Trigger resubmit of intent in CORRUPT during periodic poll.
104 */
105 @Test
106 public void corruptPoll() {
107 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
108 @Override
109 public void process(IntentData intentData) {
110 intentData.setState(CORRUPT);
111 store.write(intentData);
112 }
113
114 @Override
115 public void notify(IntentEvent event) {}
116 };
117 store.setDelegate(mockDelegate);
118
119 Intent intent = new MockIntent(1L);
120 Timestamp version = new SystemClockTimestamp(1L);
121 IntentData data = new IntentData(intent, INSTALL_REQ, version);
122 store.addPending(data);
123
124 cleanup.run(); //FIXME broken?
125 assertEquals("Expect number of submits incorrect",
126 1, service.submitCounter());
127 }
128
129 /**
130 * Trigger resubmit of intent in INSTALL_REQ for too long.
131 */
132 @Test
133 public void pendingPoll() {
134 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
135 @Override
136 public void process(IntentData intentData) {}
137
138 @Override
139 public void notify(IntentEvent event) {
140 cleanup.event(event);
141 }
142 };
143 store.setDelegate(mockDelegate);
144
145 Intent intent = new MockIntent(1L);
146 Timestamp version = new SystemClockTimestamp(1L);
147 IntentData data = new IntentData(intent, INSTALL_REQ, version);
148 store.addPending(data);
149
150 cleanup.run();
151 assertEquals("Expect number of submits incorrect",
Brian O'Connor38224302016-08-02 22:03:01 -0700152 1, service.pendingCounter());
Brian O'Connoreba4e342015-04-30 22:50:13 -0700153
154 }
155
156 /**
157 * Trigger resubmit of intent in INSTALLING for too long.
158 */
159 @Test
160 public void installingPoll() {
161 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
162 @Override
163 public void process(IntentData intentData) {
164 intentData.setState(INSTALLING);
165 store.write(intentData);
166 }
167
168 @Override
169 public void notify(IntentEvent event) {
170 cleanup.event(event);
171 }
172 };
173 store.setDelegate(mockDelegate);
174
175 Intent intent = new MockIntent(1L);
176 Timestamp version = new SystemClockTimestamp(1L);
177 IntentData data = new IntentData(intent, INSTALL_REQ, version);
178 store.addPending(data);
179
180 cleanup.run();
181 assertEquals("Expect number of submits incorrect",
Brian O'Connor38224302016-08-02 22:03:01 -0700182 1, service.pendingCounter());
Brian O'Connoreba4e342015-04-30 22:50:13 -0700183
184 }
185
186 /**
187 * Only submit one of two intents because one is too new.
188 */
189 @Test
190 public void skipPoll() {
191 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
192 @Override
193 public void process(IntentData intentData) {
194 intentData.setState(CORRUPT);
195 store.write(intentData);
196 }
197
198 @Override
199 public void notify(IntentEvent event) {}
200 };
201 store.setDelegate(mockDelegate);
202
203 Intent intent = new MockIntent(1L);
204 IntentData data = new IntentData(intent, INSTALL_REQ, null);
205 store.addPending(data);
206
207 Intent intent2 = new MockIntent(2L);
208 Timestamp version = new SystemClockTimestamp(1L);
209 data = new IntentData(intent2, INSTALL_REQ, version);
210 store.addPending(data);
211
212 cleanup.run();
213 assertEquals("Expect number of submits incorrect",
214 1, service.submitCounter());
215 }
216
217 /**
218 * Verify resubmit in response to CORRUPT event.
219 */
220 @Test
221 public void corruptEvent() {
222 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
223 @Override
224 public void process(IntentData intentData) {
225 intentData.setState(CORRUPT);
226 store.write(intentData);
227 }
228
229 @Override
230 public void notify(IntentEvent event) {
231 cleanup.event(event);
232 }
233 };
234 store.setDelegate(mockDelegate);
235
236
237 Intent intent = new MockIntent(1L);
238 IntentData data = new IntentData(intent, INSTALL_REQ, null);
239
240 store.addPending(data);
241 assertEquals("Expect number of submits incorrect",
242 1, service.submitCounter());
243 }
244
245 /**
246 * Intent should not be retried because threshold is reached.
247 */
248 @Test
249 public void corruptEventThreshold() {
250 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
251 @Override
252 public void process(IntentData intentData) {
253 intentData.setState(CORRUPT);
254 intentData.setErrorCount(cleanup.retryThreshold);
255 store.write(intentData);
256 }
257
258 @Override
259 public void notify(IntentEvent event) {
260 cleanup.event(event);
261 }
262 };
263 store.setDelegate(mockDelegate);
264
265 Intent intent = new MockIntent(1L);
266 IntentData data = new IntentData(intent, INSTALL_REQ, null);
267
268 store.addPending(data);
269 assertEquals("Expect number of submits incorrect",
270 0, service.submitCounter());
271 }
272}