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