blob: 42ec399463ea3727e6e80914ae2e2a0d5bc31b66 [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();
Thomas Vachuska23235962017-02-03 11:44:15 -080061 Intent.unbindIdGenerator(idGenerator);
Ray Milkey35958232015-07-29 11:19:28 -070062 Intent.bindIdGenerator(idGenerator);
63 builder1 = HostToHostIntent
64 .builder()
65 .one(hid("12:34:56:78:91:ab/1"))
66 .two(hid("12:34:56:78:91:ac/1"))
67 .appId(APP_ID);
Aaron Kruglikov37210412016-12-06 12:55:57 -080068 intentStore.configService = new MockComponentConfigService();
69 intentStore.activate(null);
Ray Milkey35958232015-07-29 11:19:28 -070070 }
71
72 @After
73 public void cleanUp() {
74 intentStore.deactivate();
75 Intent.unbindIdGenerator(idGenerator);
76 }
77
78 /**
79 * Generates a list of test intent data.
80 *
81 * @param count how many intent data objects are needed
82 * @return list of intent data
83 */
84 private List<IntentData> generateIntentList(int count) {
85 LinkedList<IntentData> intents = new LinkedList<>();
86 IntStream.rangeClosed(1, count)
87 .forEach(i ->
88 intents.add(
89 new IntentData(
90 builder1
91 .priority(i)
92 .build(),
93 IntentState.INSTALLED,
94 new IntentTestsMocks.MockTimestamp(12))));
95 return intents;
96 }
97
98 /**
99 * Tests the intent count APIs.
100 */
101 @Test
102 public void testGetIntentCount() {
103 assertThat(intentStore.getIntentCount(), is(0L));
104
105 generateIntentList(5).forEach(intentStore::write);
106
107 assertThat(intentStore.getIntentCount(), is(5L));
108 }
109
110 /**
111 * Tests the batch add API.
112 */
113 @Test
114 public void testBatchAdd() {
115 assertThat(intentStore.getIntentCount(), is(0L));
116
117 List<IntentData> intents = generateIntentList(5);
118
119 intentStore.batchWrite(intents);
120 assertThat(intentStore.getIntentCount(), is(5L));
121 }
122
123
124 /**
125 * Tests adding and withdrawing an Intent.
126 */
127 @Test
128 public void testAddAndWithdrawIntent() {
129 // build and install one intent
130 Intent intent = builder1.build();
131 IntentData installed = new IntentData(
132 intent,
133 IntentState.INSTALLED,
134 new IntentTestsMocks.MockTimestamp(12));
135 intentStore.write(installed);
136
137 // check that the intent count includes the new one
138 assertThat(intentStore.getIntentCount(), is(1L));
139
140 // check that the getIntents() API returns the new intent
141 intentStore.getIntents()
142 .forEach(item -> assertThat(item, is(intent)));
143
144 // check that the getInstallableIntents() API returns the new intent
145 intentStore.getInstallableIntents(intent.key())
146 .forEach(item -> assertThat(item, is(intent)));
147
148 // check that the getIntent() API can find the new intent
149 Intent queried = intentStore.getIntent(intent.key());
150 assertThat(queried, is(intent));
151
152 // check that the state of the new intent is correct
153 IntentState state = intentStore.getIntentState(intent.key());
154 assertThat(state, is(IntentState.INSTALLED));
155
156 // check that the getIntentData() API returns the proper value for the
157 // new intent
158 IntentData dataByQuery = intentStore.getIntentData(intent.key());
159 assertThat(dataByQuery, is(installed));
160
161 // check that the getIntentData() API returns the new intent when given
162 // a time stamp to look for
163 Iterable<IntentData> dataIteratorByTime = intentStore.getIntentData(true, 10L);
164 assertThat(dataIteratorByTime.iterator().hasNext(), is(true));
165 dataIteratorByTime.forEach(
166 data -> assertThat(data, is(installed))
167 );
168
169 // check that the getIntentData() API returns the new intent when asked to
170 // find all intents
171 Iterable<IntentData> dataIteratorAll = intentStore.getIntentData(false, 0L);
172 assertThat(dataIteratorAll.iterator().hasNext(), is(true));
173 dataIteratorAll.forEach(
174 data -> assertThat(data, is(installed))
175 );
176
177 // now purge the intent that was created
178 IntentData purge = new IntentData(
179 intent,
180 IntentState.PURGE_REQ,
181 new IntentTestsMocks.MockTimestamp(12));
182 intentStore.write(purge);
183
184 // check that no intents are left
185 assertThat(intentStore.getIntentCount(), is(0L));
186
187 // check that a getIntent() operation on the key of the purged intent
188 // returns null
189 Intent queriedAfterWithdrawal = intentStore.getIntent(intent.key());
190 assertThat(queriedAfterWithdrawal, nullValue());
191 }
192
193 /**
194 * Tests the operation of the APIs for the pending map.
195 */
196 @Test
197 public void testPending() {
198 // crete a new intent and add it as pending
199 Intent intent = builder1.build();
200 IntentData installed = new IntentData(
201 intent,
202 IntentState.INSTALLED,
203 new IntentTestsMocks.MockTimestamp(11));
204 intentStore.addPending(installed);
205
206 // check that the getPending() API returns the new pending intent
207 Iterable<Intent> pendingIntentIteratorAll = intentStore.getPending();
208 assertThat(pendingIntentIteratorAll.iterator().hasNext(), is(true));
209 pendingIntentIteratorAll.forEach(
210 data -> assertThat(data, is(intent))
211 );
212
213 // check that the getPendingData() API returns the IntentData for the
214 // new pending intent
215 Iterable<IntentData> pendingDataIteratorAll = intentStore.getPendingData();
216 assertThat(pendingDataIteratorAll.iterator().hasNext(), is(true));
217 pendingDataIteratorAll.forEach(
218 data -> assertThat(data, is(installed))
219 );
220
221 // check that the new pending intent is returned by the getPendingData()
222 // API when a time stamp is provided
223 Iterable<IntentData> pendingDataIteratorSelected =
224 intentStore.getPendingData(true, 10L);
225 assertThat(pendingDataIteratorSelected.iterator().hasNext(), is(true));
226 pendingDataIteratorSelected.forEach(
227 data -> assertThat(data, is(installed))
228 );
229
230 // check that the new pending intent is returned by the getPendingData()
231 // API when a time stamp is provided
232 Iterable<IntentData> pendingDataIteratorAllFromTimestamp =
233 intentStore.getPendingData(false, 0L);
234 assertThat(pendingDataIteratorAllFromTimestamp.iterator().hasNext(), is(true));
235 pendingDataIteratorSelected.forEach(
236 data -> assertThat(data, is(installed))
237 );
238 }
Aaron Kruglikov37210412016-12-06 12:55:57 -0800239
240 private class MockComponentConfigService implements ComponentConfigService {
241
242 public MockComponentConfigService() {
243
244 }
245
246 @Override
247 public Set<String> getComponentNames() {
248 return null;
249 }
250
251 @Override
252 public void registerProperties(Class<?> componentClass) {
253
254 }
255
256 @Override
257 public void unregisterProperties(Class<?> componentClass, boolean clear) {
258
259 }
260
261 @Override
262 public Set<ConfigProperty> getProperties(String componentName) {
263 return null;
264 }
265
266 @Override
267 public void setProperty(String componentName, String name, String value) {
268
269 }
270
271 @Override
272 public void preSetProperty(String componentName, String name, String value) {
273
274 }
275
276 @Override
277 public void unsetProperty(String componentName, String name) {
278
279 }
280 }
Ray Milkey35958232015-07-29 11:19:28 -0700281}