Pier Luigi | 09220c2 | 2017-09-14 22:00:30 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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 | |
| 17 | package org.onosproject.net.behaviour.trafficcontrol; |
| 18 | |
| 19 | import org.junit.Rule; |
| 20 | import org.junit.Test; |
| 21 | import org.junit.rules.ExpectedException; |
| 22 | |
| 23 | import static org.hamcrest.Matchers.is; |
| 24 | import static org.hamcrest.Matchers.notNullValue; |
| 25 | import static org.junit.Assert.*; |
| 26 | import static org.onosproject.net.behaviour.trafficcontrol.TokenBucket.Action.DROP; |
| 27 | import static org.onosproject.net.behaviour.trafficcontrol.TokenBucket.Action.DSCP_CLASS; |
| 28 | import static org.onosproject.net.behaviour.trafficcontrol.TokenBucket.Action.DSCP_PRECEDENCE; |
Pier Luigi | 256d92b | 2017-11-15 13:22:15 +0100 | [diff] [blame] | 29 | import static org.onosproject.net.behaviour.trafficcontrol.TokenBucket.Type.COMMITTED; |
| 30 | import static org.onosproject.net.behaviour.trafficcontrol.TokenBucket.Type.EXCESS; |
| 31 | import static org.onosproject.net.behaviour.trafficcontrol.TokenBucket.Type.PEAK; |
Pier Luigi | 09220c2 | 2017-09-14 22:00:30 +0200 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Test class for TokenBucket. |
| 35 | */ |
| 36 | public class TokenBucketTest { |
| 37 | |
| 38 | // Test rate |
| 39 | private static final long RATE = 1; |
| 40 | // Test dscp drop precedence |
| 41 | private static final short DSCP_PREC = 2; |
| 42 | // Test dscp class |
| 43 | private static final short DSCP_CL = 250; |
| 44 | // Test wrong dscp |
| 45 | private static final short WRONG_DSCP = -1; |
| 46 | |
| 47 | /** |
| 48 | * Test creation of a drop token bucket. |
| 49 | */ |
| 50 | @Test |
| 51 | public void testDropCreation() { |
| 52 | // Create a drop token bucket |
| 53 | TokenBucket drop = DefaultTokenBucket.builder() |
| 54 | .withRate(RATE) |
| 55 | .withAction(DROP) |
Pier Luigi | 256d92b | 2017-11-15 13:22:15 +0100 | [diff] [blame] | 56 | .withType(COMMITTED) |
Pier Luigi | 09220c2 | 2017-09-14 22:00:30 +0200 | [diff] [blame] | 57 | .build(); |
| 58 | // Not null |
| 59 | assertThat(drop, notNullValue()); |
| 60 | // Rate should be equal to RATE |
| 61 | assertThat(drop.rate(), is(RATE)); |
| 62 | // Burst size should be equal to 2xMTU |
| 63 | assertThat(drop.burstSize(), is(2 * 1500L)); |
| 64 | // Action should be drop |
| 65 | assertThat(drop.action(), is(DROP)); |
Pier Luigi | 256d92b | 2017-11-15 13:22:15 +0100 | [diff] [blame] | 66 | // For committed rate |
| 67 | assertThat(drop.type(), is(COMMITTED)); |
Pier Luigi | 09220c2 | 2017-09-14 22:00:30 +0200 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Test creation of a dscp precedence token bucket. |
| 72 | */ |
| 73 | @Test |
| 74 | public void testDscpPrecCreation() { |
| 75 | // Create a dscp precedence token bucket |
Pier Luigi | 256d92b | 2017-11-15 13:22:15 +0100 | [diff] [blame] | 76 | TokenBucket mark = DefaultTokenBucket.builder() |
Pier Luigi | 09220c2 | 2017-09-14 22:00:30 +0200 | [diff] [blame] | 77 | .withRate(RATE) |
| 78 | .withAction(DSCP_PRECEDENCE) |
| 79 | .withBurstSize(6 * 1500) |
| 80 | .withDscp(DSCP_PREC) |
Pier Luigi | 256d92b | 2017-11-15 13:22:15 +0100 | [diff] [blame] | 81 | .withType(EXCESS) |
Pier Luigi | 09220c2 | 2017-09-14 22:00:30 +0200 | [diff] [blame] | 82 | .build(); |
| 83 | // Not null |
Pier Luigi | 256d92b | 2017-11-15 13:22:15 +0100 | [diff] [blame] | 84 | assertThat(mark, notNullValue()); |
Pier Luigi | 09220c2 | 2017-09-14 22:00:30 +0200 | [diff] [blame] | 85 | // Rate should be equal to RATE |
Pier Luigi | 256d92b | 2017-11-15 13:22:15 +0100 | [diff] [blame] | 86 | assertThat(mark.rate(), is(RATE)); |
Pier Luigi | 09220c2 | 2017-09-14 22:00:30 +0200 | [diff] [blame] | 87 | // Burst size should be equal to 6xMTU |
Pier Luigi | 256d92b | 2017-11-15 13:22:15 +0100 | [diff] [blame] | 88 | assertThat(mark.burstSize(), is(6 * 1500L)); |
Pier Luigi | 09220c2 | 2017-09-14 22:00:30 +0200 | [diff] [blame] | 89 | // Action should increase dscp drop precedence |
Pier Luigi | 256d92b | 2017-11-15 13:22:15 +0100 | [diff] [blame] | 90 | assertThat(mark.action(), is(DSCP_PRECEDENCE)); |
Pier Luigi | 09220c2 | 2017-09-14 22:00:30 +0200 | [diff] [blame] | 91 | // Dcsp drop precedence should be increased of 2 |
Pier Luigi | 256d92b | 2017-11-15 13:22:15 +0100 | [diff] [blame] | 92 | assertThat(mark.dscp(), is(DSCP_PREC)); |
| 93 | // For excess rate |
| 94 | assertThat(mark.type(), is(EXCESS)); |
Pier Luigi | 09220c2 | 2017-09-14 22:00:30 +0200 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Test creation of a dscp class token bucket. |
| 99 | */ |
| 100 | @Test |
| 101 | public void testDscpClassCreation() { |
| 102 | // Create a dscp class token bucket |
Pier Luigi | 256d92b | 2017-11-15 13:22:15 +0100 | [diff] [blame] | 103 | TokenBucket mark = DefaultTokenBucket.builder() |
Pier Luigi | 09220c2 | 2017-09-14 22:00:30 +0200 | [diff] [blame] | 104 | .withRate(RATE) |
| 105 | .withAction(DSCP_CLASS) |
| 106 | .withDscp(DSCP_CL) |
Pier Luigi | 256d92b | 2017-11-15 13:22:15 +0100 | [diff] [blame] | 107 | .withType(PEAK) |
Pier Luigi | 09220c2 | 2017-09-14 22:00:30 +0200 | [diff] [blame] | 108 | .build(); |
| 109 | // Not null |
Pier Luigi | 256d92b | 2017-11-15 13:22:15 +0100 | [diff] [blame] | 110 | assertThat(mark, notNullValue()); |
Pier Luigi | 09220c2 | 2017-09-14 22:00:30 +0200 | [diff] [blame] | 111 | // Rate should be equal to RATE |
Pier Luigi | 256d92b | 2017-11-15 13:22:15 +0100 | [diff] [blame] | 112 | assertThat(mark.rate(), is(RATE)); |
Pier Luigi | 09220c2 | 2017-09-14 22:00:30 +0200 | [diff] [blame] | 113 | // Burst size should be equal to 2xMTU |
Pier Luigi | 256d92b | 2017-11-15 13:22:15 +0100 | [diff] [blame] | 114 | assertThat(mark.burstSize(), is(2 * 1500L)); |
Pier Luigi | 09220c2 | 2017-09-14 22:00:30 +0200 | [diff] [blame] | 115 | // Action should be drop |
Pier Luigi | 256d92b | 2017-11-15 13:22:15 +0100 | [diff] [blame] | 116 | assertThat(mark.action(), is(DSCP_CLASS)); |
Pier Luigi | 09220c2 | 2017-09-14 22:00:30 +0200 | [diff] [blame] | 117 | // Dcsp drop precedence should be increased of 2 |
Pier Luigi | 256d92b | 2017-11-15 13:22:15 +0100 | [diff] [blame] | 118 | assertThat(mark.dscp(), is(DSCP_CL)); |
| 119 | // For peak rate |
| 120 | assertThat(mark.type(), is(PEAK)); |
Pier Luigi | 09220c2 | 2017-09-14 22:00:30 +0200 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Exception expected to raise when creating a token bucket with null action. |
| 125 | */ |
| 126 | @Rule |
| 127 | public ExpectedException exceptionNullAction = ExpectedException.none(); |
| 128 | |
| 129 | /** |
| 130 | * Test creation of a token bucket with null action. |
| 131 | */ |
| 132 | @Test |
| 133 | public void testNullActionCreation() { |
| 134 | // Define expected exception |
| 135 | exceptionNullAction.expect(NullPointerException.class); |
| 136 | // Create a token bucket without action |
Pier Luigi | 256d92b | 2017-11-15 13:22:15 +0100 | [diff] [blame] | 137 | DefaultTokenBucket.builder() |
Pier Luigi | 09220c2 | 2017-09-14 22:00:30 +0200 | [diff] [blame] | 138 | .withRate(RATE) |
| 139 | .build(); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Exception expected to raise when creating a token bucket with wrong dscp. |
| 144 | */ |
| 145 | @Rule |
| 146 | public ExpectedException exceptionWrongDscp = ExpectedException.none(); |
| 147 | |
| 148 | /** |
| 149 | * Test creation of a token bucket with wrong dscp. |
| 150 | */ |
| 151 | @Test |
| 152 | public void testWrongDscpCreation() { |
| 153 | // Define expected exception |
| 154 | exceptionWrongDscp.expect(IllegalArgumentException.class); |
| 155 | // Create a token bucket with wrong dscp |
Pier Luigi | 256d92b | 2017-11-15 13:22:15 +0100 | [diff] [blame] | 156 | DefaultTokenBucket.builder() |
Pier Luigi | 09220c2 | 2017-09-14 22:00:30 +0200 | [diff] [blame] | 157 | .withRate(RATE) |
| 158 | .withAction(DSCP_PRECEDENCE) |
| 159 | .withDscp(WRONG_DSCP) |
Pier Luigi | 256d92b | 2017-11-15 13:22:15 +0100 | [diff] [blame] | 160 | .withType(COMMITTED) |
Pier Luigi | 09220c2 | 2017-09-14 22:00:30 +0200 | [diff] [blame] | 161 | .build(); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Test equality between policer ids. |
| 166 | */ |
| 167 | @Test |
| 168 | public void testEqualilty() { |
| 169 | // Create a drop token bucket |
| 170 | TokenBucket drop = DefaultTokenBucket.builder() |
| 171 | .withRate(RATE) |
| 172 | .withAction(DROP) |
Pier Luigi | 256d92b | 2017-11-15 13:22:15 +0100 | [diff] [blame] | 173 | .withType(COMMITTED) |
Pier Luigi | 09220c2 | 2017-09-14 22:00:30 +0200 | [diff] [blame] | 174 | .build(); |
| 175 | // Create a mark token bucket |
| 176 | TokenBucket mark = DefaultTokenBucket.builder() |
| 177 | .withRate(RATE) |
| 178 | .withAction(DSCP_PRECEDENCE) |
| 179 | .withDscp(DSCP_PREC) |
Pier Luigi | 256d92b | 2017-11-15 13:22:15 +0100 | [diff] [blame] | 180 | .withType(COMMITTED) |
Pier Luigi | 09220c2 | 2017-09-14 22:00:30 +0200 | [diff] [blame] | 181 | .build(); |
| 182 | // Create a copy of the drop token bucket |
| 183 | TokenBucket copyDrop = DefaultTokenBucket.builder() |
| 184 | .withRate(RATE) |
| 185 | .withAction(DROP) |
Pier Luigi | 256d92b | 2017-11-15 13:22:15 +0100 | [diff] [blame] | 186 | .withType(COMMITTED) |
Pier Luigi | 09220c2 | 2017-09-14 22:00:30 +0200 | [diff] [blame] | 187 | .build(); |
| 188 | // Verify equality |
| 189 | assertEquals(drop, copyDrop); |
| 190 | // Verify not equals |
| 191 | assertNotEquals(mark, drop); |
| 192 | assertNotEquals(mark, copyDrop); |
| 193 | } |
| 194 | |
| 195 | } |