blob: 0f6ce67c061888adc235c0bf970b08c905c62df9 [file] [log] [blame]
Brian O'Connoreba4e342015-04-30 22:50:13 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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;
53
54 @Override
55 public void submit(Intent intent) {
56 submitCounter++;
57 }
58
59 public int submitCounter() {
60 return submitCounter;
61 }
62 }
63
64 @Before
65 public void setUp() {
66 service = new MockIntentService();
67 store = new SimpleIntentStore();
68 cleanup = new IntentCleanup();
69 idGenerator = new MockIdGenerator();
70
71 cleanup.cfgService = new ComponentConfigAdapter();
72 cleanup.service = service;
73 cleanup.store = store;
74 cleanup.period = 10;
75 cleanup.retryThreshold = 3;
76 cleanup.activate();
77
78 assertTrue("store should be empty",
79 Sets.newHashSet(cleanup.store.getIntents()).isEmpty());
80
81 Intent.bindIdGenerator(idGenerator);
82 }
83
84 @After
85 public void tearDown() {
86 cleanup.deactivate();
87
88 Intent.unbindIdGenerator(idGenerator);
89 }
90
91 /**
92 * Trigger resubmit of intent in CORRUPT during periodic poll.
93 */
94 @Test
95 public void corruptPoll() {
96 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
97 @Override
98 public void process(IntentData intentData) {
99 intentData.setState(CORRUPT);
100 store.write(intentData);
101 }
102
103 @Override
104 public void notify(IntentEvent event) {}
105 };
106 store.setDelegate(mockDelegate);
107
108 Intent intent = new MockIntent(1L);
109 Timestamp version = new SystemClockTimestamp(1L);
110 IntentData data = new IntentData(intent, INSTALL_REQ, version);
111 store.addPending(data);
112
113 cleanup.run(); //FIXME broken?
114 assertEquals("Expect number of submits incorrect",
115 1, service.submitCounter());
116 }
117
118 /**
119 * Trigger resubmit of intent in INSTALL_REQ for too long.
120 */
121 @Test
122 public void pendingPoll() {
123 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
124 @Override
125 public void process(IntentData intentData) {}
126
127 @Override
128 public void notify(IntentEvent event) {
129 cleanup.event(event);
130 }
131 };
132 store.setDelegate(mockDelegate);
133
134 Intent intent = new MockIntent(1L);
135 Timestamp version = new SystemClockTimestamp(1L);
136 IntentData data = new IntentData(intent, INSTALL_REQ, version);
137 store.addPending(data);
138
139 cleanup.run();
140 assertEquals("Expect number of submits incorrect",
141 1, service.submitCounter());
142
143 }
144
145 /**
146 * Trigger resubmit of intent in INSTALLING for too long.
147 */
148 @Test
149 public void installingPoll() {
150 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
151 @Override
152 public void process(IntentData intentData) {
153 intentData.setState(INSTALLING);
154 store.write(intentData);
155 }
156
157 @Override
158 public void notify(IntentEvent event) {
159 cleanup.event(event);
160 }
161 };
162 store.setDelegate(mockDelegate);
163
164 Intent intent = new MockIntent(1L);
165 Timestamp version = new SystemClockTimestamp(1L);
166 IntentData data = new IntentData(intent, INSTALL_REQ, version);
167 store.addPending(data);
168
169 cleanup.run();
170 assertEquals("Expect number of submits incorrect",
171 1, service.submitCounter());
172
173 }
174
175 /**
176 * Only submit one of two intents because one is too new.
177 */
178 @Test
179 public void skipPoll() {
180 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
181 @Override
182 public void process(IntentData intentData) {
183 intentData.setState(CORRUPT);
184 store.write(intentData);
185 }
186
187 @Override
188 public void notify(IntentEvent event) {}
189 };
190 store.setDelegate(mockDelegate);
191
192 Intent intent = new MockIntent(1L);
193 IntentData data = new IntentData(intent, INSTALL_REQ, null);
194 store.addPending(data);
195
196 Intent intent2 = new MockIntent(2L);
197 Timestamp version = new SystemClockTimestamp(1L);
198 data = new IntentData(intent2, INSTALL_REQ, version);
199 store.addPending(data);
200
201 cleanup.run();
202 assertEquals("Expect number of submits incorrect",
203 1, service.submitCounter());
204 }
205
206 /**
207 * Verify resubmit in response to CORRUPT event.
208 */
209 @Test
210 public void corruptEvent() {
211 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
212 @Override
213 public void process(IntentData intentData) {
214 intentData.setState(CORRUPT);
215 store.write(intentData);
216 }
217
218 @Override
219 public void notify(IntentEvent event) {
220 cleanup.event(event);
221 }
222 };
223 store.setDelegate(mockDelegate);
224
225
226 Intent intent = new MockIntent(1L);
227 IntentData data = new IntentData(intent, INSTALL_REQ, null);
228
229 store.addPending(data);
230 assertEquals("Expect number of submits incorrect",
231 1, service.submitCounter());
232 }
233
234 /**
235 * Intent should not be retried because threshold is reached.
236 */
237 @Test
238 public void corruptEventThreshold() {
239 IntentStoreDelegate mockDelegate = new IntentStoreDelegate() {
240 @Override
241 public void process(IntentData intentData) {
242 intentData.setState(CORRUPT);
243 intentData.setErrorCount(cleanup.retryThreshold);
244 store.write(intentData);
245 }
246
247 @Override
248 public void notify(IntentEvent event) {
249 cleanup.event(event);
250 }
251 };
252 store.setDelegate(mockDelegate);
253
254 Intent intent = new MockIntent(1L);
255 IntentData data = new IntentData(intent, INSTALL_REQ, null);
256
257 store.addPending(data);
258 assertEquals("Expect number of submits incorrect",
259 0, service.submitCounter());
260 }
261}