blob: ba88e787f799f993cb2cc4e4d7c128e777ce07fe [file] [log] [blame]
Ray Milkey35958232015-07-29 11:19:28 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Ray Milkey35958232015-07-29 11:19:28 -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.store.intent.impl;
17
18import java.util.LinkedList;
19import java.util.List;
Aaron Kruglikov37210412016-12-06 12:55:57 -080020import java.util.Set;
Ray Milkey35958232015-07-29 11:19:28 -070021import java.util.stream.IntStream;
22
23import org.junit.After;
24import org.junit.Before;
25import org.junit.Test;
Aaron Kruglikov37210412016-12-06 12:55:57 -080026import org.onosproject.cfg.ComponentConfigService;
27import org.onosproject.cfg.ConfigProperty;
Ray Milkey35958232015-07-29 11:19:28 -070028import org.onosproject.cluster.ClusterServiceAdapter;
29import org.onosproject.core.IdGenerator;
30import org.onosproject.net.intent.HostToHostIntent;
31import org.onosproject.net.intent.Intent;
32import org.onosproject.net.intent.IntentData;
33import org.onosproject.net.intent.IntentState;
34import org.onosproject.net.intent.IntentTestsMocks;
35import org.onosproject.net.intent.MockIdGenerator;
Madan Jampani3b8101a2016-09-15 13:22:01 -070036import org.onosproject.net.intent.WorkPartitionServiceAdapter;
Ray Milkey35958232015-07-29 11:19:28 -070037import org.onosproject.store.service.TestStorageService;
38
39import static org.hamcrest.Matchers.is;
40import static org.hamcrest.Matchers.nullValue;
41import static org.junit.Assert.assertThat;
42import static org.onosproject.net.NetTestTools.APP_ID;
43import static org.onosproject.net.NetTestTools.hid;
44
45/**
46 * Gossip Intent Store test using database adapter.
47 */
48public class GossipIntentStoreTest {
49
50 private GossipIntentStore intentStore;
51 private IdGenerator idGenerator;
52 private HostToHostIntent.Builder builder1;
53
54 @Before
55 public void setUp() {
56 intentStore = new GossipIntentStore();
57 intentStore.storageService = new TestStorageService();
Madan Jampani3b8101a2016-09-15 13:22:01 -070058 intentStore.partitionService = new WorkPartitionServiceAdapter();
Ray Milkey35958232015-07-29 11:19:28 -070059 intentStore.clusterService = new ClusterServiceAdapter();
60 idGenerator = new MockIdGenerator();
61 Intent.bindIdGenerator(idGenerator);
62 builder1 = HostToHostIntent
63 .builder()
64 .one(hid("12:34:56:78:91:ab/1"))
65 .two(hid("12:34:56:78:91:ac/1"))
66 .appId(APP_ID);
Aaron Kruglikov37210412016-12-06 12:55:57 -080067 intentStore.configService = new MockComponentConfigService();
68 intentStore.activate(null);
Ray Milkey35958232015-07-29 11:19:28 -070069 }
70
71 @After
72 public void cleanUp() {
73 intentStore.deactivate();
74 Intent.unbindIdGenerator(idGenerator);
75 }
76
77 /**
78 * Generates a list of test intent data.
79 *
80 * @param count how many intent data objects are needed
81 * @return list of intent data
82 */
83 private List<IntentData> generateIntentList(int count) {
84 LinkedList<IntentData> intents = new LinkedList<>();
85 IntStream.rangeClosed(1, count)
86 .forEach(i ->
87 intents.add(
88 new IntentData(
89 builder1
90 .priority(i)
91 .build(),
92 IntentState.INSTALLED,
93 new IntentTestsMocks.MockTimestamp(12))));
94 return intents;
95 }
96
97 /**
98 * Tests the intent count APIs.
99 */
100 @Test
101 public void testGetIntentCount() {
102 assertThat(intentStore.getIntentCount(), is(0L));
103
104 generateIntentList(5).forEach(intentStore::write);
105
106 assertThat(intentStore.getIntentCount(), is(5L));
107 }
108
109 /**
110 * Tests the batch add API.
111 */
112 @Test
113 public void testBatchAdd() {
114 assertThat(intentStore.getIntentCount(), is(0L));
115
116 List<IntentData> intents = generateIntentList(5);
117
118 intentStore.batchWrite(intents);
119 assertThat(intentStore.getIntentCount(), is(5L));
120 }
121
122
123 /**
124 * Tests adding and withdrawing an Intent.
125 */
126 @Test
127 public void testAddAndWithdrawIntent() {
128 // build and install one intent
129 Intent intent = builder1.build();
130 IntentData installed = new IntentData(
131 intent,
132 IntentState.INSTALLED,
133 new IntentTestsMocks.MockTimestamp(12));
134 intentStore.write(installed);
135
136 // check that the intent count includes the new one
137 assertThat(intentStore.getIntentCount(), is(1L));
138
139 // check that the getIntents() API returns the new intent
140 intentStore.getIntents()
141 .forEach(item -> assertThat(item, is(intent)));
142
143 // check that the getInstallableIntents() API returns the new intent
144 intentStore.getInstallableIntents(intent.key())
145 .forEach(item -> assertThat(item, is(intent)));
146
147 // check that the getIntent() API can find the new intent
148 Intent queried = intentStore.getIntent(intent.key());
149 assertThat(queried, is(intent));
150
151 // check that the state of the new intent is correct
152 IntentState state = intentStore.getIntentState(intent.key());
153 assertThat(state, is(IntentState.INSTALLED));
154
155 // check that the getIntentData() API returns the proper value for the
156 // new intent
157 IntentData dataByQuery = intentStore.getIntentData(intent.key());
158 assertThat(dataByQuery, is(installed));
159
160 // check that the getIntentData() API returns the new intent when given
161 // a time stamp to look for
162 Iterable<IntentData> dataIteratorByTime = intentStore.getIntentData(true, 10L);
163 assertThat(dataIteratorByTime.iterator().hasNext(), is(true));
164 dataIteratorByTime.forEach(
165 data -> assertThat(data, is(installed))
166 );
167
168 // check that the getIntentData() API returns the new intent when asked to
169 // find all intents
170 Iterable<IntentData> dataIteratorAll = intentStore.getIntentData(false, 0L);
171 assertThat(dataIteratorAll.iterator().hasNext(), is(true));
172 dataIteratorAll.forEach(
173 data -> assertThat(data, is(installed))
174 );
175
176 // now purge the intent that was created
177 IntentData purge = new IntentData(
178 intent,
179 IntentState.PURGE_REQ,
180 new IntentTestsMocks.MockTimestamp(12));
181 intentStore.write(purge);
182
183 // check that no intents are left
184 assertThat(intentStore.getIntentCount(), is(0L));
185
186 // check that a getIntent() operation on the key of the purged intent
187 // returns null
188 Intent queriedAfterWithdrawal = intentStore.getIntent(intent.key());
189 assertThat(queriedAfterWithdrawal, nullValue());
190 }
191
192 /**
193 * Tests the operation of the APIs for the pending map.
194 */
195 @Test
196 public void testPending() {
197 // crete a new intent and add it as pending
198 Intent intent = builder1.build();
199 IntentData installed = new IntentData(
200 intent,
201 IntentState.INSTALLED,
202 new IntentTestsMocks.MockTimestamp(11));
203 intentStore.addPending(installed);
204
205 // check that the getPending() API returns the new pending intent
206 Iterable<Intent> pendingIntentIteratorAll = intentStore.getPending();
207 assertThat(pendingIntentIteratorAll.iterator().hasNext(), is(true));
208 pendingIntentIteratorAll.forEach(
209 data -> assertThat(data, is(intent))
210 );
211
212 // check that the getPendingData() API returns the IntentData for the
213 // new pending intent
214 Iterable<IntentData> pendingDataIteratorAll = intentStore.getPendingData();
215 assertThat(pendingDataIteratorAll.iterator().hasNext(), is(true));
216 pendingDataIteratorAll.forEach(
217 data -> assertThat(data, is(installed))
218 );
219
220 // check that the new pending intent is returned by the getPendingData()
221 // API when a time stamp is provided
222 Iterable<IntentData> pendingDataIteratorSelected =
223 intentStore.getPendingData(true, 10L);
224 assertThat(pendingDataIteratorSelected.iterator().hasNext(), is(true));
225 pendingDataIteratorSelected.forEach(
226 data -> assertThat(data, is(installed))
227 );
228
229 // check that the new pending intent is returned by the getPendingData()
230 // API when a time stamp is provided
231 Iterable<IntentData> pendingDataIteratorAllFromTimestamp =
232 intentStore.getPendingData(false, 0L);
233 assertThat(pendingDataIteratorAllFromTimestamp.iterator().hasNext(), is(true));
234 pendingDataIteratorSelected.forEach(
235 data -> assertThat(data, is(installed))
236 );
237 }
Aaron Kruglikov37210412016-12-06 12:55:57 -0800238
239 private class MockComponentConfigService implements ComponentConfigService {
240
241 public MockComponentConfigService() {
242
243 }
244
245 @Override
246 public Set<String> getComponentNames() {
247 return null;
248 }
249
250 @Override
251 public void registerProperties(Class<?> componentClass) {
252
253 }
254
255 @Override
256 public void unregisterProperties(Class<?> componentClass, boolean clear) {
257
258 }
259
260 @Override
261 public Set<ConfigProperty> getProperties(String componentName) {
262 return null;
263 }
264
265 @Override
266 public void setProperty(String componentName, String name, String value) {
267
268 }
269
270 @Override
271 public void preSetProperty(String componentName, String name, String value) {
272
273 }
274
275 @Override
276 public void unsetProperty(String componentName, String name) {
277
278 }
279 }
Ray Milkey35958232015-07-29 11:19:28 -0700280}