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