Open Kilda Java Documentation
ResourceCacheTest.java
Go to the documentation of this file.
1 /* Copyright 2018 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.pce.cache;
17 
18 import static java.lang.String.format;
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNull;
21 import static org.junit.Assert.assertTrue;
22 
29 
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.Test;
33 
34 import java.util.Arrays;
35 import java.util.Collections;
36 import java.util.HashSet;
37 import java.util.Map;
38 import java.util.Random;
39 import java.util.Set;
40 
41 public class ResourceCacheTest {
42  private static final SwitchId SWITCH_ID = new SwitchId("ff:00");
43  private static final SwitchId SWITCH_ID_2 = new SwitchId("ff:02");
44  private final Flow forwardCreatedFlow = new Flow("created-flow", 0, false, 10L, "description",
45  "timestamp", new SwitchId("ff:03"), new SwitchId("ff:04"), 21, 22, 100,
46  200, 4, 4, new PathInfoData(), FlowState.ALLOCATED);
47  private final Flow reverseCreatedFlow = new Flow("created-flow", 0, false, 10L, "description",
48  "timestamp", new SwitchId("ff:04"), new SwitchId("ff:03"), 22, 21, 200,
49  100, 5, 5, new PathInfoData(), FlowState.ALLOCATED);
50  private ResourceCache resourceCache;
51 
52  @After
53  public void tearDown() throws Exception {
54  resourceCache.clear();
55  }
56 
57  @Before
58  public void setUp() throws Exception {
59  resourceCache = new ResourceCache();
60  }
61 
62  @Test
63  public void allocateAll() throws Exception {
64  // TODO
65  }
66 
67  @Test
68  public void cookiePool() throws Exception {
69  resourceCache.allocateCookie(4);
70 
71  int first = resourceCache.allocateCookie();
72  assertEquals(5, first);
73 
74  int second = resourceCache.allocateCookie();
75  assertEquals(6, second);
76 
77  int third = resourceCache.allocateCookie();
78  assertEquals(7, third);
79 
80  resourceCache.deallocateCookie(second);
81  int fourth = resourceCache.allocateCookie();
82  assertEquals(8, fourth);
83 
84  assertEquals(4, resourceCache.getAllCookies().size());
85 
86  int fifth = resourceCache.allocateCookie();
87  assertEquals(9, fifth);
88  }
89 
90  @Test
91  public void vlanIdPool() throws Exception {
92  resourceCache.allocateVlanId(5);
93 
94  int first = resourceCache.allocateVlanId();
95  assertEquals(6, first);
96 
97  int second = resourceCache.allocateVlanId();
98  assertEquals(7, second);
99 
100  int third = resourceCache.allocateVlanId();
101  assertEquals(8, third);
102 
103  resourceCache.deallocateVlanId(second);
104  int fourth = resourceCache.allocateVlanId();
105  assertEquals(9, fourth);
106 
107  assertEquals(4, resourceCache.getAllVlanIds().size());
108 
109  int fifth = resourceCache.allocateVlanId();
110  assertEquals(10, fifth);
111  }
112 
113  @Test
114  public void meterIdPool() throws Exception {
115  resourceCache.allocateMeterId(SWITCH_ID, 4);
116  int m1 = ResourceCache.MIN_METER_ID;
117 
118  int first = resourceCache.allocateMeterId(SWITCH_ID);
119  assertEquals(m1, first);
120 
121  int second = resourceCache.allocateMeterId(SWITCH_ID);
122  assertEquals(m1 + 1, second);
123 
124  int third = resourceCache.allocateMeterId(SWITCH_ID);
125  assertEquals(m1 + 2, third);
126 
127  resourceCache.deallocateMeterId(SWITCH_ID, second);
128  int fourth = resourceCache.allocateMeterId(SWITCH_ID);
129  assertEquals(m1 + 3, fourth);
130 
131  assertEquals(4, resourceCache.getAllMeterIds(SWITCH_ID).size());
132 
133  int fifth = resourceCache.allocateMeterId(SWITCH_ID);
134  assertEquals(m1 + 4, fifth);
135 
136  assertEquals(5, resourceCache.deallocateMeterId(SWITCH_ID).size());
137  assertEquals(0, resourceCache.getAllMeterIds(SWITCH_ID).size());
138  }
139 
140  @Test(expected = ArrayIndexOutOfBoundsException.class)
141  public void vlanPoolFullTest() {
142  resourceCache.allocateVlanId();
143  int i = ResourceCache.MIN_VLAN_ID;
144  while (i++ <= ResourceCache.MAX_VLAN_ID) {
145  resourceCache.allocateVlanId();
146  }
147  }
148 
149  // (crimi - 2018.04.06 ... Don't do this ... cookie pool is massive
150  //
151  // @Test(expected = ArrayIndexOutOfBoundsException.class)
152  // public void cookiePoolFullTest() {
153  // resourceCache.allocateCookie();
154  // int i = ResourceCache.MIN_COOKIE;
155  // while (i++ <= ResourceCache.MAX_COOKIE) {
156  // resourceCache.allocateCookie();
157  // }
158  // }
159  //
160 
161  @Test(expected = ArrayIndexOutOfBoundsException.class)
162  public void meterIdPoolFullTest() {
163  resourceCache.allocateMeterId(SWITCH_ID);
164  int i = ResourceCache.MIN_METER_ID;
165  while (i++ <= ResourceCache.MAX_METER_ID) {
166  resourceCache.allocateMeterId(SWITCH_ID);
167  }
168  }
169 
170  @Test
171  public void allocateFlow() throws Exception {
172  resourceCache.allocateFlow(new ImmutablePair<>(forwardCreatedFlow, reverseCreatedFlow));
173  resourceCache.allocateFlow(new ImmutablePair<>(forwardCreatedFlow, reverseCreatedFlow));
174 
175  Set<Integer> allocatedCookies = resourceCache.getAllCookies();
176  Set<Integer> allocatedMeterIds = new HashSet<>();
177 
178  allocatedMeterIds.addAll(resourceCache.getAllMeterIds(
180  allocatedMeterIds.addAll(resourceCache.getAllMeterIds(
182 
183  Set<Integer> expectedCookies = new HashSet<>(Arrays.asList(
184  (int) forwardCreatedFlow.getCookie(),
185  (int) reverseCreatedFlow.getCookie()));
186 
187  Set<Integer> expectedVlanIds = new HashSet<>(Arrays.asList(
188  forwardCreatedFlow.getTransitVlan(),
189  reverseCreatedFlow.getTransitVlan()));
190 
191  Set<Integer> expectedMeterIds = new HashSet<>(Arrays.asList(
192  forwardCreatedFlow.getMeterId(),
193  reverseCreatedFlow.getMeterId()));
194 
195  Set<Integer> allocatedVlanIds = resourceCache.getAllVlanIds();
196  assertEquals(expectedCookies, allocatedCookies);
197  assertEquals(expectedVlanIds, allocatedVlanIds);
198  assertEquals(expectedMeterIds, allocatedMeterIds);
199  }
200 
201  @Test
202  public void deallocateFlow() throws Exception {
203  allocateFlow();
204  resourceCache.deallocateFlow(new ImmutablePair<>(forwardCreatedFlow, reverseCreatedFlow));
205  resourceCache.deallocateFlow(new ImmutablePair<>(forwardCreatedFlow, reverseCreatedFlow));
206 
207  Set<Integer> allocatedCookies = resourceCache.getAllCookies();
208  Set<Integer> allocatedVlanIds = resourceCache.getAllVlanIds();
209  Set<Integer> allocatedMeterIds = resourceCache.getAllMeterIds(
211 
212  assertEquals(Collections.emptySet(), allocatedCookies);
213  assertEquals(Collections.emptySet(), allocatedVlanIds);
214  assertEquals(Collections.emptySet(), allocatedMeterIds);
215  }
216 
217  @Test
219  // given
220  Random random = new Random();
221  final SwitchId switchId = new SwitchId(format("%s:%s", SWITCH_ID, Integer.toString(random.nextInt(0X100), 16)));
222 
223  // then
224  assertNull(resourceCache.deallocateMeterId(switchId));
225  }
226 
227  @Test
229  // given
230  Random random = new Random();
231  final SwitchId switchId = new SwitchId(format("%s:%s", SWITCH_ID, Integer.toString(random.nextInt(0X100), 16)));
232 
233  // then
234  assertNull(resourceCache.deallocateMeterId(switchId, forwardCreatedFlow.getMeterId()));
235  }
236 
237  @Test
238  public void getAllMeterIds() {
239  int first = resourceCache.allocateMeterId(SWITCH_ID);
240  assertEquals(ResourceCache.MIN_METER_ID, first);
241 
242  int second = resourceCache.allocateMeterId(SWITCH_ID);
243  assertEquals(ResourceCache.MIN_METER_ID + 1, second);
244 
245  int third = resourceCache.allocateMeterId(SWITCH_ID);
246  assertEquals(ResourceCache.MIN_METER_ID + 2, third);
247 
248  first = resourceCache.allocateMeterId(SWITCH_ID_2);
249  assertEquals(ResourceCache.MIN_METER_ID, first);
250 
251  second = resourceCache.allocateMeterId(SWITCH_ID_2);
252  assertEquals(ResourceCache.MIN_METER_ID + 1, second);
253 
254  Map<SwitchId, Set<Integer>> allMeterIds = resourceCache.getAllMeterIds();
255  assertEquals(2, allMeterIds.size());
256  assertEquals(3, allMeterIds.get(SWITCH_ID).size());
257  assertEquals(2, allMeterIds.get(SWITCH_ID_2).size());
258  }
259 
260 
261  @Test
262  public void earlyMeterIds() {
263  /*
264  * Test that we can add a meter id less than the minimum, assuming the minimum is > 1.
265  */
266  int m1 = ResourceCache.MIN_METER_ID;
267  int first = resourceCache.allocateMeterId(SWITCH_ID);
268  assertEquals(m1, first);
269 
270  resourceCache.allocateMeterId(SWITCH_ID, m1 - 1);
271  assertEquals(2, resourceCache.getAllMeterIds(SWITCH_ID).size());
272  assertTrue(resourceCache.getAllMeterIds(SWITCH_ID).contains(m1 - 1));
273 
274  /*
275  * verify that if we delete all, and then request a new vlan, it starts at min.
276  */
277  resourceCache.deallocateMeterId(SWITCH_ID, m1);
278  resourceCache.deallocateMeterId(SWITCH_ID, m1 - 1);
279  first = resourceCache.allocateMeterId(SWITCH_ID);
280  assertEquals(m1 + 1, first);
281  }
282 
283 
284 }
void deallocateFlow(ImmutablePair< Flow, Flow > flow)
Integer deallocateCookie(Integer cookie)
void allocateFlow(ImmutablePair< Flow, Flow > flow)
synchronized Integer allocateMeterId(SwitchId switchId)
Set< Integer > getAllMeterIds(SwitchId switchId)
synchronized Integer deallocateMeterId(SwitchId switchId, Integer meterId)
Integer deallocateVlanId(Integer vlanId)