Ray Milkey | 3595823 | 2015-07-29 11:19:28 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Ray Milkey | 3595823 | 2015-07-29 11:19:28 -0700 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.store.intent.impl; |
| 17 | |
| 18 | import java.util.LinkedList; |
| 19 | import java.util.List; |
| 20 | import java.util.stream.IntStream; |
| 21 | |
| 22 | import org.junit.After; |
| 23 | import org.junit.Before; |
| 24 | import org.junit.Test; |
| 25 | import org.onosproject.cluster.ClusterServiceAdapter; |
| 26 | import org.onosproject.core.IdGenerator; |
| 27 | import org.onosproject.net.intent.HostToHostIntent; |
| 28 | import org.onosproject.net.intent.Intent; |
| 29 | import org.onosproject.net.intent.IntentData; |
| 30 | import org.onosproject.net.intent.IntentState; |
| 31 | import org.onosproject.net.intent.IntentTestsMocks; |
| 32 | import org.onosproject.net.intent.MockIdGenerator; |
Madan Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame^] | 33 | import org.onosproject.net.intent.WorkPartitionServiceAdapter; |
Ray Milkey | 3595823 | 2015-07-29 11:19:28 -0700 | [diff] [blame] | 34 | import org.onosproject.store.service.TestStorageService; |
| 35 | |
| 36 | import static org.hamcrest.Matchers.is; |
| 37 | import static org.hamcrest.Matchers.nullValue; |
| 38 | import static org.junit.Assert.assertThat; |
| 39 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 40 | import static org.onosproject.net.NetTestTools.hid; |
| 41 | |
| 42 | /** |
| 43 | * Gossip Intent Store test using database adapter. |
| 44 | */ |
| 45 | public 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 Jampani | 3b8101a | 2016-09-15 13:22:01 -0700 | [diff] [blame^] | 55 | intentStore.partitionService = new WorkPartitionServiceAdapter(); |
Ray Milkey | 3595823 | 2015-07-29 11:19:28 -0700 | [diff] [blame] | 56 | 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 | } |