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