blob: c5f094130fd4a2669a642093c00f1549f9e92c1c [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.IntentServiceAdapter;
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.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 */
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070043public class IntentCleanupTest extends AbstractIntentTest {
Brian O'Connoreba4e342015-04-30 22:50:13 -070044
45 private IntentCleanup cleanup;
46 private MockIntentService service;
47 private IntentStore store;
Brian O'Connoreba4e342015-04-30 22:50:13 -070048
49 private static class MockIntentService extends IntentServiceAdapter {
50
51 private int submitCounter = 0;
Brian O'Connor38224302016-08-02 22:03:01 -070052 private int pendingCounter = 0;
Brian O'Connoreba4e342015-04-30 22:50:13 -070053
54 @Override
55 public void submit(Intent intent) {
56 submitCounter++;
57 }
58
Brian O'Connor38224302016-08-02 22:03:01 -070059 @Override
60 public void addPending(IntentData intentData) {
61 pendingCounter++;
62 }
63
Brian O'Connoreba4e342015-04-30 22:50:13 -070064 public int submitCounter() {
65 return submitCounter;
66 }
Brian O'Connor38224302016-08-02 22:03:01 -070067
68 public int pendingCounter() {
69 return pendingCounter;
70 }
71
Brian O'Connoreba4e342015-04-30 22:50:13 -070072 }
73
74 @Before
75 public void setUp() {
76 service = new MockIntentService();
77 store = new SimpleIntentStore();
78 cleanup = new IntentCleanup();
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070079
80 super.setUp();
Brian O'Connoreba4e342015-04-30 22:50:13 -070081
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());
Brian O'Connoreba4e342015-04-30 22:50:13 -070091 }
92
93 @After
94 public void tearDown() {
95 cleanup.deactivate();
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070096 super.tearDown();
Brian O'Connoreba4e342015-04-30 22:50:13 -070097 }
98
99 /**
100 * Trigger resubmit of intent in CORRUPT during periodic poll.
101 */
102 @Test
103 public void corruptPoll() {
104 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
105 @Override
106 public void process(IntentData intentData) {
107 intentData.setState(CORRUPT);
108 store.write(intentData);
109 }
110
111 @Override
112 public void notify(IntentEvent event) {}
113 };
114 store.setDelegate(mockDelegate);
115
116 Intent intent = new MockIntent(1L);
117 Timestamp version = new SystemClockTimestamp(1L);
118 IntentData data = new IntentData(intent, INSTALL_REQ, version);
119 store.addPending(data);
120
121 cleanup.run(); //FIXME broken?
122 assertEquals("Expect number of submits incorrect",
123 1, service.submitCounter());
124 }
125
126 /**
127 * Trigger resubmit of intent in INSTALL_REQ for too long.
128 */
129 @Test
130 public void pendingPoll() {
131 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
132 @Override
133 public void process(IntentData intentData) {}
134
135 @Override
136 public void notify(IntentEvent event) {
137 cleanup.event(event);
138 }
139 };
140 store.setDelegate(mockDelegate);
141
142 Intent intent = new MockIntent(1L);
143 Timestamp version = new SystemClockTimestamp(1L);
144 IntentData data = new IntentData(intent, INSTALL_REQ, version);
145 store.addPending(data);
146
147 cleanup.run();
148 assertEquals("Expect number of submits incorrect",
Brian O'Connor38224302016-08-02 22:03:01 -0700149 1, service.pendingCounter());
Brian O'Connoreba4e342015-04-30 22:50:13 -0700150
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
178 cleanup.run();
179 assertEquals("Expect number of submits incorrect",
Brian O'Connor38224302016-08-02 22:03:01 -0700180 1, service.pendingCounter());
Brian O'Connoreba4e342015-04-30 22:50:13 -0700181
182 }
183
184 /**
185 * Only submit one of two intents because one is too new.
186 */
187 @Test
188 public void skipPoll() {
189 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
190 @Override
191 public void process(IntentData intentData) {
192 intentData.setState(CORRUPT);
193 store.write(intentData);
194 }
195
196 @Override
197 public void notify(IntentEvent event) {}
198 };
199 store.setDelegate(mockDelegate);
200
201 Intent intent = new MockIntent(1L);
202 IntentData data = new IntentData(intent, INSTALL_REQ, null);
203 store.addPending(data);
204
205 Intent intent2 = new MockIntent(2L);
206 Timestamp version = new SystemClockTimestamp(1L);
207 data = new IntentData(intent2, INSTALL_REQ, version);
208 store.addPending(data);
209
210 cleanup.run();
211 assertEquals("Expect number of submits incorrect",
212 1, service.submitCounter());
213 }
214
215 /**
216 * Verify resubmit in response to CORRUPT event.
217 */
218 @Test
219 public void corruptEvent() {
220 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
221 @Override
222 public void process(IntentData intentData) {
223 intentData.setState(CORRUPT);
224 store.write(intentData);
225 }
226
227 @Override
228 public void notify(IntentEvent event) {
229 cleanup.event(event);
230 }
231 };
232 store.setDelegate(mockDelegate);
233
234
235 Intent intent = new MockIntent(1L);
236 IntentData data = new IntentData(intent, INSTALL_REQ, null);
237
238 store.addPending(data);
239 assertEquals("Expect number of submits incorrect",
240 1, service.submitCounter());
241 }
242
243 /**
244 * Intent should not be retried because threshold is reached.
245 */
246 @Test
247 public void corruptEventThreshold() {
248 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
249 @Override
250 public void process(IntentData intentData) {
251 intentData.setState(CORRUPT);
252 intentData.setErrorCount(cleanup.retryThreshold);
253 store.write(intentData);
254 }
255
256 @Override
257 public void notify(IntentEvent event) {
258 cleanup.event(event);
259 }
260 };
261 store.setDelegate(mockDelegate);
262
263 Intent intent = new MockIntent(1L);
264 IntentData data = new IntentData(intent, INSTALL_REQ, null);
265
266 store.addPending(data);
267 assertEquals("Expect number of submits incorrect",
268 0, service.submitCounter());
269 }
270}