blob: e1a4a4f7873a3e70094e8d55668d26d3671d3919 [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;
20import java.util.stream.IntStream;
21
22import org.junit.After;
23import org.junit.Before;
24import org.junit.Test;
25import org.onosproject.cluster.ClusterServiceAdapter;
26import org.onosproject.core.IdGenerator;
27import org.onosproject.net.intent.HostToHostIntent;
28import org.onosproject.net.intent.Intent;
29import org.onosproject.net.intent.IntentData;
30import org.onosproject.net.intent.IntentState;
31import org.onosproject.net.intent.IntentTestsMocks;
32import org.onosproject.net.intent.MockIdGenerator;
Madan Jampani3b8101a2016-09-15 13:22:01 -070033import org.onosproject.net.intent.WorkPartitionServiceAdapter;
Ray Milkey35958232015-07-29 11:19:28 -070034import org.onosproject.store.service.TestStorageService;
35
36import static org.hamcrest.Matchers.is;
37import static org.hamcrest.Matchers.nullValue;
38import static org.junit.Assert.assertThat;
39import static org.onosproject.net.NetTestTools.APP_ID;
40import static org.onosproject.net.NetTestTools.hid;
41
42/**
43 * Gossip Intent Store test using database adapter.
44 */
45public class GossipIntentStoreTest {
46
47 private GossipIntentStore intentStore;
48 private IdGenerator idGenerator;
49 private HostToHostIntent.Builder builder1;
50
51 @Before
52 public void setUp() {
53 intentStore = new GossipIntentStore();
54 intentStore.storageService = new TestStorageService();
Madan Jampani3b8101a2016-09-15 13:22:01 -070055 intentStore.partitionService = new WorkPartitionServiceAdapter();
Ray Milkey35958232015-07-29 11:19:28 -070056 intentStore.clusterService = new ClusterServiceAdapter();
57 idGenerator = new MockIdGenerator();
58 Intent.bindIdGenerator(idGenerator);
59 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);
64 intentStore.activate();
65 }
66
67 @After
68 public void cleanUp() {
69 intentStore.deactivate();
70 Intent.unbindIdGenerator(idGenerator);
71 }
72
73 /**
74 * Generates a list of test intent data.
75 *
76 * @param count how many intent data objects are needed
77 * @return list of intent data
78 */
79 private List<IntentData> generateIntentList(int count) {
80 LinkedList<IntentData> intents = new LinkedList<>();
81 IntStream.rangeClosed(1, count)
82 .forEach(i ->
83 intents.add(
84 new IntentData(
85 builder1
86 .priority(i)
87 .build(),
88 IntentState.INSTALLED,
89 new IntentTestsMocks.MockTimestamp(12))));
90 return intents;
91 }
92
93 /**
94 * Tests the intent count APIs.
95 */
96 @Test
97 public void testGetIntentCount() {
98 assertThat(intentStore.getIntentCount(), is(0L));
99
100 generateIntentList(5).forEach(intentStore::write);
101
102 assertThat(intentStore.getIntentCount(), is(5L));
103 }
104
105 /**
106 * Tests the batch add API.
107 */
108 @Test
109 public void testBatchAdd() {
110 assertThat(intentStore.getIntentCount(), is(0L));
111
112 List<IntentData> intents = generateIntentList(5);
113
114 intentStore.batchWrite(intents);
115 assertThat(intentStore.getIntentCount(), is(5L));
116 }
117
118
119 /**
120 * Tests adding and withdrawing an Intent.
121 */
122 @Test
123 public void testAddAndWithdrawIntent() {
124 // build and install one intent
125 Intent intent = builder1.build();
126 IntentData installed = new IntentData(
127 intent,
128 IntentState.INSTALLED,
129 new IntentTestsMocks.MockTimestamp(12));
130 intentStore.write(installed);
131
132 // check that the intent count includes the new one
133 assertThat(intentStore.getIntentCount(), is(1L));
134
135 // check that the getIntents() API returns the new intent
136 intentStore.getIntents()
137 .forEach(item -> assertThat(item, is(intent)));
138
139 // check that the getInstallableIntents() API returns the new intent
140 intentStore.getInstallableIntents(intent.key())
141 .forEach(item -> assertThat(item, is(intent)));
142
143 // check that the getIntent() API can find the new intent
144 Intent queried = intentStore.getIntent(intent.key());
145 assertThat(queried, is(intent));
146
147 // check that the state of the new intent is correct
148 IntentState state = intentStore.getIntentState(intent.key());
149 assertThat(state, is(IntentState.INSTALLED));
150
151 // check that the getIntentData() API returns the proper value for the
152 // new intent
153 IntentData dataByQuery = intentStore.getIntentData(intent.key());
154 assertThat(dataByQuery, is(installed));
155
156 // check that the getIntentData() API returns the new intent when given
157 // a time stamp to look for
158 Iterable<IntentData> dataIteratorByTime = intentStore.getIntentData(true, 10L);
159 assertThat(dataIteratorByTime.iterator().hasNext(), is(true));
160 dataIteratorByTime.forEach(
161 data -> assertThat(data, is(installed))
162 );
163
164 // check that the getIntentData() API returns the new intent when asked to
165 // find all intents
166 Iterable<IntentData> dataIteratorAll = intentStore.getIntentData(false, 0L);
167 assertThat(dataIteratorAll.iterator().hasNext(), is(true));
168 dataIteratorAll.forEach(
169 data -> assertThat(data, is(installed))
170 );
171
172 // now purge the intent that was created
173 IntentData purge = new IntentData(
174 intent,
175 IntentState.PURGE_REQ,
176 new IntentTestsMocks.MockTimestamp(12));
177 intentStore.write(purge);
178
179 // check that no intents are left
180 assertThat(intentStore.getIntentCount(), is(0L));
181
182 // check that a getIntent() operation on the key of the purged intent
183 // returns null
184 Intent queriedAfterWithdrawal = intentStore.getIntent(intent.key());
185 assertThat(queriedAfterWithdrawal, nullValue());
186 }
187
188 /**
189 * Tests the operation of the APIs for the pending map.
190 */
191 @Test
192 public void testPending() {
193 // crete a new intent and add it as pending
194 Intent intent = builder1.build();
195 IntentData installed = new IntentData(
196 intent,
197 IntentState.INSTALLED,
198 new IntentTestsMocks.MockTimestamp(11));
199 intentStore.addPending(installed);
200
201 // check that the getPending() API returns the new pending intent
202 Iterable<Intent> pendingIntentIteratorAll = intentStore.getPending();
203 assertThat(pendingIntentIteratorAll.iterator().hasNext(), is(true));
204 pendingIntentIteratorAll.forEach(
205 data -> assertThat(data, is(intent))
206 );
207
208 // check that the getPendingData() API returns the IntentData for the
209 // new pending intent
210 Iterable<IntentData> pendingDataIteratorAll = intentStore.getPendingData();
211 assertThat(pendingDataIteratorAll.iterator().hasNext(), is(true));
212 pendingDataIteratorAll.forEach(
213 data -> assertThat(data, is(installed))
214 );
215
216 // check that the new pending intent is returned by the getPendingData()
217 // API when a time stamp is provided
218 Iterable<IntentData> pendingDataIteratorSelected =
219 intentStore.getPendingData(true, 10L);
220 assertThat(pendingDataIteratorSelected.iterator().hasNext(), is(true));
221 pendingDataIteratorSelected.forEach(
222 data -> assertThat(data, is(installed))
223 );
224
225 // check that the new pending intent is returned by the getPendingData()
226 // API when a time stamp is provided
227 Iterable<IntentData> pendingDataIteratorAllFromTimestamp =
228 intentStore.getPendingData(false, 0L);
229 assertThat(pendingDataIteratorAllFromTimestamp.iterator().hasNext(), is(true));
230 pendingDataIteratorSelected.forEach(
231 data -> assertThat(data, is(installed))
232 );
233 }
234}