blob: 7f3d8181c3c460df5842556ab328db4f98b9c6cd [file] [log] [blame]
Pier Luigi09220c22017-09-14 22:00:30 +02001/*
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
17package org.onosproject.net.behaviour.trafficcontrol;
18
19import org.junit.Rule;
20import org.junit.Test;
21import org.junit.rules.ExpectedException;
22
23import static org.hamcrest.Matchers.is;
24import static org.hamcrest.Matchers.notNullValue;
25import static org.junit.Assert.*;
26import static org.onosproject.net.behaviour.trafficcontrol.TokenBucket.Action.DROP;
27import static org.onosproject.net.behaviour.trafficcontrol.TokenBucket.Action.DSCP_CLASS;
28import static org.onosproject.net.behaviour.trafficcontrol.TokenBucket.Action.DSCP_PRECEDENCE;
Pier Luigi256d92b2017-11-15 13:22:15 +010029import static org.onosproject.net.behaviour.trafficcontrol.TokenBucket.Type.COMMITTED;
30import static org.onosproject.net.behaviour.trafficcontrol.TokenBucket.Type.EXCESS;
31import static org.onosproject.net.behaviour.trafficcontrol.TokenBucket.Type.PEAK;
Pier Luigi09220c22017-09-14 22:00:30 +020032
33/**
34 * Test class for TokenBucket.
35 */
36public 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 Luigi256d92b2017-11-15 13:22:15 +010056 .withType(COMMITTED)
Pier Luigi09220c22017-09-14 22:00:30 +020057 .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 Luigi256d92b2017-11-15 13:22:15 +010066 // For committed rate
67 assertThat(drop.type(), is(COMMITTED));
Pier Luigi09220c22017-09-14 22:00:30 +020068 }
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 Luigi256d92b2017-11-15 13:22:15 +010076 TokenBucket mark = DefaultTokenBucket.builder()
Pier Luigi09220c22017-09-14 22:00:30 +020077 .withRate(RATE)
78 .withAction(DSCP_PRECEDENCE)
79 .withBurstSize(6 * 1500)
80 .withDscp(DSCP_PREC)
Pier Luigi256d92b2017-11-15 13:22:15 +010081 .withType(EXCESS)
Pier Luigi09220c22017-09-14 22:00:30 +020082 .build();
83 // Not null
Pier Luigi256d92b2017-11-15 13:22:15 +010084 assertThat(mark, notNullValue());
Pier Luigi09220c22017-09-14 22:00:30 +020085 // Rate should be equal to RATE
Pier Luigi256d92b2017-11-15 13:22:15 +010086 assertThat(mark.rate(), is(RATE));
Pier Luigi09220c22017-09-14 22:00:30 +020087 // Burst size should be equal to 6xMTU
Pier Luigi256d92b2017-11-15 13:22:15 +010088 assertThat(mark.burstSize(), is(6 * 1500L));
Pier Luigi09220c22017-09-14 22:00:30 +020089 // Action should increase dscp drop precedence
Pier Luigi256d92b2017-11-15 13:22:15 +010090 assertThat(mark.action(), is(DSCP_PRECEDENCE));
Pier Luigi09220c22017-09-14 22:00:30 +020091 // Dcsp drop precedence should be increased of 2
Pier Luigi256d92b2017-11-15 13:22:15 +010092 assertThat(mark.dscp(), is(DSCP_PREC));
93 // For excess rate
94 assertThat(mark.type(), is(EXCESS));
Pier Luigi09220c22017-09-14 22:00:30 +020095 }
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 Luigi256d92b2017-11-15 13:22:15 +0100103 TokenBucket mark = DefaultTokenBucket.builder()
Pier Luigi09220c22017-09-14 22:00:30 +0200104 .withRate(RATE)
105 .withAction(DSCP_CLASS)
106 .withDscp(DSCP_CL)
Pier Luigi256d92b2017-11-15 13:22:15 +0100107 .withType(PEAK)
Pier Luigi09220c22017-09-14 22:00:30 +0200108 .build();
109 // Not null
Pier Luigi256d92b2017-11-15 13:22:15 +0100110 assertThat(mark, notNullValue());
Pier Luigi09220c22017-09-14 22:00:30 +0200111 // Rate should be equal to RATE
Pier Luigi256d92b2017-11-15 13:22:15 +0100112 assertThat(mark.rate(), is(RATE));
Pier Luigi09220c22017-09-14 22:00:30 +0200113 // Burst size should be equal to 2xMTU
Pier Luigi256d92b2017-11-15 13:22:15 +0100114 assertThat(mark.burstSize(), is(2 * 1500L));
Pier Luigi09220c22017-09-14 22:00:30 +0200115 // Action should be drop
Pier Luigi256d92b2017-11-15 13:22:15 +0100116 assertThat(mark.action(), is(DSCP_CLASS));
Pier Luigi09220c22017-09-14 22:00:30 +0200117 // Dcsp drop precedence should be increased of 2
Pier Luigi256d92b2017-11-15 13:22:15 +0100118 assertThat(mark.dscp(), is(DSCP_CL));
119 // For peak rate
120 assertThat(mark.type(), is(PEAK));
Pier Luigi09220c22017-09-14 22:00:30 +0200121 }
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 Luigi256d92b2017-11-15 13:22:15 +0100137 DefaultTokenBucket.builder()
Pier Luigi09220c22017-09-14 22:00:30 +0200138 .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 Luigi256d92b2017-11-15 13:22:15 +0100156 DefaultTokenBucket.builder()
Pier Luigi09220c22017-09-14 22:00:30 +0200157 .withRate(RATE)
158 .withAction(DSCP_PRECEDENCE)
159 .withDscp(WRONG_DSCP)
Pier Luigi256d92b2017-11-15 13:22:15 +0100160 .withType(COMMITTED)
Pier Luigi09220c22017-09-14 22:00:30 +0200161 .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 Luigi256d92b2017-11-15 13:22:15 +0100173 .withType(COMMITTED)
Pier Luigi09220c22017-09-14 22:00:30 +0200174 .build();
175 // Create a mark token bucket
176 TokenBucket mark = DefaultTokenBucket.builder()
177 .withRate(RATE)
178 .withAction(DSCP_PRECEDENCE)
179 .withDscp(DSCP_PREC)
Pier Luigi256d92b2017-11-15 13:22:15 +0100180 .withType(COMMITTED)
Pier Luigi09220c22017-09-14 22:00:30 +0200181 .build();
182 // Create a copy of the drop token bucket
183 TokenBucket copyDrop = DefaultTokenBucket.builder()
184 .withRate(RATE)
185 .withAction(DROP)
Pier Luigi256d92b2017-11-15 13:22:15 +0100186 .withType(COMMITTED)
Pier Luigi09220c22017-09-14 22:00:30 +0200187 .build();
188 // Verify equality
189 assertEquals(drop, copyDrop);
190 // Verify not equals
191 assertNotEquals(mark, drop);
192 assertNotEquals(mark, copyDrop);
193 }
194
195}