blob: 149e64be709fee6fd607b5dde7571369b676b9e4 [file] [log] [blame]
Ray Milkey35958232015-07-29 11:19:28 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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;
Yuta HIGUCHI4f8a3772017-05-16 20:23:49 -070024import org.onosproject.cluster.NodeId;
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070025import org.onosproject.net.intent.AbstractIntentTest;
Ray Milkey35958232015-07-29 11:19:28 -070026import org.onosproject.net.intent.HostToHostIntent;
27import org.onosproject.net.intent.Intent;
28import org.onosproject.net.intent.IntentData;
29import org.onosproject.net.intent.IntentState;
30import org.onosproject.net.intent.IntentTestsMocks;
Madan Jampani3b8101a2016-09-15 13:22:01 -070031import org.onosproject.net.intent.WorkPartitionServiceAdapter;
Ray Milkey35958232015-07-29 11:19:28 -070032import org.onosproject.store.service.TestStorageService;
33
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070034import java.util.LinkedList;
35import java.util.List;
36import java.util.Set;
37import java.util.stream.IntStream;
38
Ray Milkey35958232015-07-29 11:19:28 -070039import 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 */
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070048public class GossipIntentStoreTest extends AbstractIntentTest {
Ray Milkey35958232015-07-29 11:19:28 -070049
50 private GossipIntentStore intentStore;
Ray Milkey35958232015-07-29 11:19:28 -070051 private HostToHostIntent.Builder builder1;
52
Yuta HIGUCHI4f8a3772017-05-16 20:23:49 -070053 @Override
Ray Milkey35958232015-07-29 11:19:28 -070054 @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();
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070060 super.setUp();
Ray Milkey35958232015-07-29 11:19:28 -070061 builder1 = HostToHostIntent
62 .builder()
63 .one(hid("12:34:56:78:91:ab/1"))
64 .two(hid("12:34:56:78:91:ac/1"))
65 .appId(APP_ID);
Aaron Kruglikov37210412016-12-06 12:55:57 -080066 intentStore.configService = new MockComponentConfigService();
67 intentStore.activate(null);
Ray Milkey35958232015-07-29 11:19:28 -070068 }
69
Yuta HIGUCHI4f8a3772017-05-16 20:23:49 -070070 @Override
Ray Milkey35958232015-07-29 11:19:28 -070071 @After
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070072 public void tearDown() {
Ray Milkey35958232015-07-29 11:19:28 -070073 intentStore.deactivate();
Thomas Vachuska2048c1f2017-05-10 19:32:22 -070074 super.tearDown();
Ray Milkey35958232015-07-29 11:19:28 -070075 }
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
Yuta HIGUCHI4f8a3772017-05-16 20:23:49 -0700177 IntentData purgeAssigned =
178 IntentData.assign(IntentData.purge(intent),
179 new IntentTestsMocks.MockTimestamp(12),
180 new NodeId("node-id"));
181 intentStore.write(purgeAssigned);
Ray Milkey35958232015-07-29 11:19:28 -0700182
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 }
psnehaf31ac6f2018-08-02 01:16:46 -0400279
280 @Override
281 public ConfigProperty getProperty(String componentName, String attribute) {
282 return null;
283 }
Aaron Kruglikov37210412016-12-06 12:55:57 -0800284 }
Ray Milkey35958232015-07-29 11:19:28 -0700285}