Open Kilda Java Documentation
MeterPoolTest.java
Go to the documentation of this file.
1 /* Copyright 2017 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.floodlight.switchmanager;
17 
18 import static org.junit.Assert.assertEquals;
19 
21 
22 import org.junit.Test;
23 
24 import java.util.HashSet;
25 import java.util.Set;
26 
27 public class MeterPoolTest {
28  private static final SwitchId SWITCH_1_ID = new SwitchId("ff:01");
29  private static final SwitchId SWITCH_2_ID = new SwitchId("ff:02");
30  private static final SwitchId SWITCH_3_ID = new SwitchId("ff:03");
31  private static final String FLOW_1_ID = "first-flow";
32  private static final String FLOW_2_ID = "second-flow";
33  private static final String FLOW_3_ID = "third-flow";
34 
35  private MeterPool meterPool = new MeterPool();
36 
37  @Test
38  public void testMeterPool() {
39  Set<Integer> expected = new HashSet<>();
40 
41  int m1 = meterPool.allocate(SWITCH_1_ID, FLOW_1_ID);
42  int m2 = meterPool.allocate(SWITCH_2_ID, FLOW_1_ID);
43 
44  expected.add(m1);
45  expected.add(m2);
46  assertEquals(expected, meterPool.getMetersByFlow(FLOW_1_ID));
47  expected.clear();
48 
49  int m3 = meterPool.allocate(SWITCH_1_ID, FLOW_2_ID);
50  int m4 = meterPool.allocate(SWITCH_2_ID, FLOW_2_ID);
51  expected.add(m3);
52  expected.add(m4);
53  assertEquals(expected, meterPool.getMetersByFlow(FLOW_2_ID));
54  expected.clear();
55 
56  expected.add(m1);
57  expected.add(m3);
58  assertEquals(expected, meterPool.getMetersBySwitch(SWITCH_1_ID));
59  expected.clear();
60 
61  expected.add(m2);
62  expected.add(m4);
63  assertEquals(expected, meterPool.getMetersBySwitch(SWITCH_2_ID));
64  expected.clear();
65 
66  meterPool.deallocate(SWITCH_1_ID, FLOW_1_ID);
67  meterPool.deallocate(SWITCH_2_ID, FLOW_1_ID);
68 
69  int m5 = meterPool.allocate(SWITCH_1_ID, FLOW_3_ID);
70  int m6 = meterPool.allocate(SWITCH_3_ID, FLOW_3_ID);
71 
72  expected.add(m5);
73  expected.add(m6);
74  assertEquals(expected, meterPool.getMetersByFlow(FLOW_3_ID));
75  expected.clear();
76 
77  expected.add(m3);
78  expected.add(m5);
79  assertEquals(expected, meterPool.getMetersBySwitch(SWITCH_1_ID));
80  expected.clear();
81 
82  assertEquals(m1 + 2, m5);
83  }
84 }
synchronized Set< Integer > getMetersBySwitch(final SwitchId switchId)
Definition: MeterPool.java:45
synchronized Integer deallocate(final SwitchId switchId, final String flowId)
Definition: MeterPool.java:88
synchronized Integer allocate(final SwitchId switchId, final String flowId, Integer meterId)
Definition: MeterPool.java:58
synchronized Set< Integer > getMetersByFlow(final String flowId)
Definition: MeterPool.java:41