blob: ba5d7cd79919151ddca70dcda168920f40b7d0dc [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 com.google.common.collect.ImmutableList;
20import org.junit.Rule;
21import org.junit.Test;
22import org.junit.rules.ExpectedException;
23import org.onosproject.TestApplicationId;
24import org.onosproject.core.ApplicationId;
25import org.onosproject.net.DeviceId;
26
27import static org.hamcrest.Matchers.is;
28import static org.junit.Assert.*;
29import static org.onosproject.net.behaviour.trafficcontrol.Policer.Unit.KB_PER_SEC;
30import static org.onosproject.net.behaviour.trafficcontrol.Policer.Unit.MB_PER_SEC;
31import static org.onosproject.net.behaviour.trafficcontrol.TokenBucket.Action.DROP;
32import static org.onosproject.net.behaviour.trafficcontrol.TokenBucket.Action.DSCP_CLASS;
33import static org.onosproject.net.behaviour.trafficcontrol.TokenBucket.Action.DSCP_PRECEDENCE;
Pier Luigi256d92b2017-11-15 13:22:15 +010034import static org.onosproject.net.behaviour.trafficcontrol.TokenBucket.Type.COMMITTED;
35import static org.onosproject.net.behaviour.trafficcontrol.TokenBucket.Type.EXCESS;
36import static org.onosproject.net.behaviour.trafficcontrol.TokenBucket.Type.PEAK;
Pier Luigi09220c22017-09-14 22:00:30 +020037
38/**
39 * Test class for policer implementation.
40 */
41public class PolicerTest {
42
43 // Fake Application Id
44 private static final ApplicationId FOO_APP_ID = new TestApplicationId("foo");
45 // Connect points
46 private static final String SDID1 = "of:00000000000001";
47 private static final DeviceId DID1 = DeviceId.deviceId(SDID1);
48 // OpenFlow scheme
49 private static final String OF_SCHEME = "of";
50 // Policers identifiers
51 private static final String SID1 = OF_SCHEME + ":" + Integer.toHexString(1);
52 private static final PolicerId ID1 = PolicerId.policerId(SID1);
53 private static final String SID2 = OF_SCHEME + ":" + Integer.toHexString(2);
54 private static final PolicerId ID2 = PolicerId.policerId(SID2);
55 private static final String SID3 = OF_SCHEME + ":" + Integer.toHexString(3);
56 private static final PolicerId ID3 = PolicerId.policerId(SID3);
57 private static final String SID4 = OF_SCHEME + ":" + Integer.toHexString(4);
58 private static final PolicerId ID4 = PolicerId.policerId(SID4);
59 private static final String SID5 = OF_SCHEME + ":" + Integer.toHexString(5);
60 private static final PolicerId ID5 = PolicerId.policerId(SID5);
61 private static final String SID6 = OF_SCHEME + ":" + Integer.toHexString(6);
62 private static final PolicerId ID6 = PolicerId.policerId(SID6);
63 private static final String SID7 = OF_SCHEME + ":" + Integer.toHexString(7);
64 private static final PolicerId ID7 = PolicerId.policerId(SID7);
65 private static final String SID8 = OF_SCHEME + ":" + Integer.toHexString(8);
66 private static final PolicerId ID8 = PolicerId.policerId(SID8);
67 private static final String SID9 = OF_SCHEME + ":" + Integer.toHexString(9);
68 private static final PolicerId ID9 = PolicerId.policerId(SID9);
69
70 /**
71 * Test block traffic policer.
72 */
73 @Test
74 public void testBlockCreation() {
75 // Create a block traffic token bucket
76 TokenBucket tokenBucket = DefaultTokenBucket.builder()
77 .withBurstSize(0)
78 .withAction(DROP)
Pier Luigi256d92b2017-11-15 13:22:15 +010079 .withType(COMMITTED)
Pier Luigi09220c22017-09-14 22:00:30 +020080 .build();
81 // Create a policer with above token bucket
82 Policer policer = DefaultPolicer.builder()
83 .forDeviceId(DID1)
84 .fromApp(FOO_APP_ID)
85 .withId(ID1)
86 .withTokenBuckets(ImmutableList.of(tokenBucket))
87 .build();
88 // Assert on device id
89 assertThat(policer.deviceId(), is(DID1));
90 // Assert on app id
91 assertThat(policer.applicationId(), is(FOO_APP_ID));
92 // Assert on policer id
93 assertThat(policer.policerId(), is(ID1));
94 // It is not color aware
95 assertFalse(policer.isColorAware());
96 // Unit is Mbps
97 assertThat(policer.unit(), is(MB_PER_SEC));
98 // One token bucket
99 assertThat(policer.tokenBuckets().size(), is(1));
100 // One token bucket equals to tokenBucket
101 assertTrue(policer.tokenBuckets().contains(tokenBucket));
102 }
103
104 /**
105 * Test simple drop policer.
106 */
107 @Test
108 public void testDropCreation() {
109 // Create a drop traffic token bucket at 1MB/s
110 TokenBucket tokenBucket = DefaultTokenBucket.builder()
111 .withRate(1)
112 .withAction(DROP)
Pier Luigi256d92b2017-11-15 13:22:15 +0100113 .withType(COMMITTED)
Pier Luigi09220c22017-09-14 22:00:30 +0200114 .build();
115 // Create a policer with above token bucket
116 Policer policer = DefaultPolicer.builder()
117 .forDeviceId(DID1)
118 .fromApp(FOO_APP_ID)
119 .withId(ID2)
120 .withTokenBuckets(ImmutableList.of(tokenBucket))
121 .build();
122 // Assert on device id
123 assertThat(policer.deviceId(), is(DID1));
124 // Assert on app id
125 assertThat(policer.applicationId(), is(FOO_APP_ID));
126 // Assert on policer id
127 assertThat(policer.policerId(), is(ID2));
128 // It is not color aware
129 assertFalse(policer.isColorAware());
130 // Unit is Mbps
131 assertThat(policer.unit(), is(MB_PER_SEC));
132 // One token bucket
133 assertThat(policer.tokenBuckets().size(), is(1));
134 // One token bucket equals to tokenBucket
135 assertTrue(policer.tokenBuckets().contains(tokenBucket));
136 }
137
138 /**
139 * Test simple mark policer.
140 */
141 @Test
142 public void testMarkCreation() {
143 // Create a drop traffic token bucket at 1MB/s
144 TokenBucket tokenBucket = DefaultTokenBucket.builder()
145 .withRate(1)
146 .withAction(DSCP_PRECEDENCE)
147 .withDscp((short) 2)
Pier Luigi256d92b2017-11-15 13:22:15 +0100148 .withType(COMMITTED)
Pier Luigi09220c22017-09-14 22:00:30 +0200149 .build();
150 // Create a policer with above token bucket
151 Policer policer = DefaultPolicer.builder()
152 .forDeviceId(DID1)
153 .fromApp(FOO_APP_ID)
154 .withId(ID3)
155 .withTokenBuckets(ImmutableList.of(tokenBucket))
156 .build();
157 // Assert on device id
158 assertThat(policer.deviceId(), is(DID1));
159 // Assert on app id
160 assertThat(policer.applicationId(), is(FOO_APP_ID));
161 // Assert on policer id
162 assertThat(policer.policerId(), is(ID3));
163 // It is not color aware
164 assertFalse(policer.isColorAware());
165 // Unit is Mbps
166 assertThat(policer.unit(), is(MB_PER_SEC));
167 // One token bucket
168 assertThat(policer.tokenBuckets().size(), is(1));
169 // One token bucket equals to tokenBucket
170 assertTrue(policer.tokenBuckets().contains(tokenBucket));
171 }
172
173 /**
174 * Test single rate three colors scenario (RFC 2697).
175 */
176 @Test
177 public void testSingleRateThreeColors() {
178 // Create token bucket for committed rate
179 TokenBucket crTokenBucket = DefaultTokenBucket.builder()
180 .withRate(1)
181 .withAction(DSCP_PRECEDENCE)
182 .withDscp((short) 2)
Pier Luigi256d92b2017-11-15 13:22:15 +0100183 .withType(COMMITTED)
Pier Luigi09220c22017-09-14 22:00:30 +0200184 .build();
185 // Create token bucket for excess rate
186 TokenBucket erTokenBucket = DefaultTokenBucket.builder()
187 .withRate(1)
188 .withBurstSize(4 * 1500)
189 .withAction(DROP)
Pier Luigi256d92b2017-11-15 13:22:15 +0100190 .withType(EXCESS)
Pier Luigi09220c22017-09-14 22:00:30 +0200191 .build();
192 // Create a policer with above token buckets
193 Policer policer = DefaultPolicer.builder()
194 .forDeviceId(DID1)
195 .fromApp(FOO_APP_ID)
196 .withId(ID4)
197 // The order is important
198 .withTokenBuckets(ImmutableList.of(crTokenBucket, erTokenBucket))
199 .build();
200 // Assert on device id
201 assertThat(policer.deviceId(), is(DID1));
202 // Assert on app id
203 assertThat(policer.applicationId(), is(FOO_APP_ID));
204 // Assert on policer id
205 assertThat(policer.policerId(), is(ID4));
206 // It is not color aware
207 assertFalse(policer.isColorAware());
208 // Unit is Mbps
209 assertThat(policer.unit(), is(MB_PER_SEC));
210 // Two token buckets
211 assertThat(policer.tokenBuckets().size(), is(2));
212 // One token bucket equals to crTokenBucket
213 assertTrue(policer.tokenBuckets().contains(crTokenBucket));
214 // One token bucket equals to erTokenBucket
215 assertTrue(policer.tokenBuckets().contains(erTokenBucket));
216 }
217
218 /**
219 * Test two rates three colors scenario (RFC 2698 and P4 meter).
220 */
221 @Test
222 public void testTwoRatesThreeColors() {
223 // Create token bucket for peak rate at 10Mb/s
224 TokenBucket prTokenBucket = DefaultTokenBucket.builder()
225 // (10 * 1000)/8 ---> 1250KB/s
226 .withRate(1250)
227 .withBurstSize(10 * 1500)
228 .withAction(DROP)
Pier Luigi256d92b2017-11-15 13:22:15 +0100229 .withType(PEAK)
Pier Luigi09220c22017-09-14 22:00:30 +0200230 .build();
231 // Create token bucket for committed rate at 1Mb/s
232 TokenBucket crTokenBucket = DefaultTokenBucket.builder()
233 // (1 * 1000)/8 ---> 125KB/s
234 .withRate(125)
235 .withAction(DSCP_CLASS)
236 .withDscp((short) 10)
Pier Luigi256d92b2017-11-15 13:22:15 +0100237 .withType(COMMITTED)
Pier Luigi09220c22017-09-14 22:00:30 +0200238 .build();
239 // Create a policer with above token buckets
240 Policer policer = DefaultPolicer.builder()
241 .forDeviceId(DID1)
242 .fromApp(FOO_APP_ID)
243 .withId(ID5)
244 .withUnit(KB_PER_SEC)
245 // The order is important
246 .withTokenBuckets(ImmutableList.of(prTokenBucket, crTokenBucket))
247 .build();
248 // Assert on device id
249 assertThat(policer.deviceId(), is(DID1));
250 // Assert on app id
251 assertThat(policer.applicationId(), is(FOO_APP_ID));
252 // Assert on policer id
253 assertThat(policer.policerId(), is(ID5));
254 // It is not color aware
255 assertFalse(policer.isColorAware());
256 // Unit is Mbps
257 assertThat(policer.unit(), is(KB_PER_SEC));
258 // Two token buckets
259 assertThat(policer.tokenBuckets().size(), is(2));
260 // One token bucket equals to prTokenBucket
261 assertTrue(policer.tokenBuckets().contains(prTokenBucket));
262 // One token bucket equals to crTokenBucket
263 assertTrue(policer.tokenBuckets().contains(crTokenBucket));
264 }
265
266 /**
267 * Exception expected to raise when creating a policer with null params.
268 */
269 @Rule
270 public ExpectedException exceptionNullParam = ExpectedException.none();
271
272 /**
273 * Test creation with null parameters.
274 */
275 @Test
276 public void testNullParam() {
277 // Define expected exception
278 exceptionNullParam.expect(NullPointerException.class);
279 // Invalid policer, device id is not defined
280 DefaultPolicer.builder()
281 .fromApp(FOO_APP_ID)
282 .withId(ID6)
283 .build();
284 }
285
286 /**
287 * Exception expected to raise when creating a policer without token buckets.
288 */
289 @Rule
290 public ExpectedException exceptionNoTokenBuckets = ExpectedException.none();
291
292 /**
293 * Test creation without token buckets.
294 */
295 @Test
296 public void testNoTokenBuckets() {
297 // Define expected exception
298 exceptionNoTokenBuckets.expect(IllegalArgumentException.class);
299 // Invalid policer, no token buckets
300 DefaultPolicer.builder()
301 .fromApp(FOO_APP_ID)
302 .withId(ID7)
303 .forDeviceId(DID1)
304 .withTokenBuckets(ImmutableList.of())
305 .build();
306 }
307
308 /**
309 * Test equality between policers.
310 */
311 @Test
312 public void testEqualilty() {
313 // Create a block traffic token bucket
314 TokenBucket blockTokenBucket = DefaultTokenBucket.builder()
315 .withBurstSize(0)
316 .withAction(DROP)
Pier Luigi256d92b2017-11-15 13:22:15 +0100317 .withType(COMMITTED)
Pier Luigi09220c22017-09-14 22:00:30 +0200318 .build();
319 // Create a mark traffic token bucket
320 TokenBucket markTokenBucket = DefaultTokenBucket.builder()
321 .withBurstSize(0)
322 .withAction(DSCP_CLASS)
323 .withDscp((short) 10)
Pier Luigi256d92b2017-11-15 13:22:15 +0100324 .withType(COMMITTED)
Pier Luigi09220c22017-09-14 22:00:30 +0200325 .build();
326 // Create first policer
327 Policer policerOne = DefaultPolicer.builder()
328 .forDeviceId(DID1)
329 .fromApp(FOO_APP_ID)
330 .withId(ID8)
331 .withTokenBuckets(ImmutableList.of(blockTokenBucket))
332 .build();
333 // Create second policer
334 Policer policerTwo = DefaultPolicer.builder()
335 .forDeviceId(DID1)
336 .fromApp(FOO_APP_ID)
337 .withId(ID9)
338 .withTokenBuckets(ImmutableList.of(markTokenBucket))
339 .build();
340 // Create third policer copy of one
341 // Create first policer
342 Policer policerThree = DefaultPolicer.builder()
343 .forDeviceId(DID1)
344 .fromApp(FOO_APP_ID)
345 .withId(ID8)
346 .withTokenBuckets(ImmutableList.of(blockTokenBucket))
347 .build();
348 // One and Three are equal
349 assertEquals(policerOne, policerThree);
350 // Two is different due to the different id
351 assertNotEquals(policerOne, policerTwo);
352 assertNotEquals(policerThree, policerTwo);
353 }
354
355}