Konstantinos Kanonakis | 0a9031d | 2016-09-22 11:35:11 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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 | package org.onosproject.net.behaviour; |
| 17 | |
| 18 | import org.junit.Test; |
| 19 | import org.onlab.packet.DscpClass; |
| 20 | import org.onlab.util.Bandwidth; |
| 21 | import org.onosproject.TestApplicationId; |
| 22 | import org.onosproject.core.ApplicationId; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.net.meter.Band; |
| 25 | import org.onosproject.net.meter.DefaultBand; |
| 26 | import org.onosproject.net.meter.DefaultMeter; |
| 27 | import org.onosproject.net.meter.Meter; |
| 28 | import org.onosproject.net.meter.MeterId; |
| 29 | import static org.onosproject.net.behaviour.BandwidthProfileAction.Action; |
| 30 | |
| 31 | import java.util.Arrays; |
| 32 | |
| 33 | import static org.junit.Assert.assertEquals; |
| 34 | import static org.junit.Assert.assertNull; |
| 35 | import static org.junit.Assert.assertTrue; |
| 36 | |
| 37 | /** |
| 38 | * Test for BandwidthProfile class. |
| 39 | */ |
| 40 | public class BandwidthProfileTest { |
| 41 | |
| 42 | private static final long ONE = 1L; |
| 43 | private static final long ONE_K = 1_000L; |
| 44 | private static final long TWO_K = 2_000L; |
| 45 | private static final long EIGHT_K = 8_000L; |
| 46 | private static final long ONE_M = 1_000_000L; |
| 47 | private static final long TEN_M = 10_000_000L; |
| 48 | |
| 49 | @Test |
| 50 | public void testMeterConversion() { |
| 51 | DeviceId deviceId = DeviceId.deviceId("netconf:10.0.0.1:22"); |
| 52 | ApplicationId appId = TestApplicationId.create("org.onosproject.foo.app"); |
| 53 | Meter.Builder meterBuilder = new DefaultMeter.Builder() |
| 54 | .withId(MeterId.meterId(ONE)) |
| 55 | .withUnit(Meter.Unit.KB_PER_SEC) |
| 56 | .forDevice(deviceId) |
| 57 | .burst(); |
| 58 | |
| 59 | // Create Meter with single band |
| 60 | Band band1 = DefaultBand.builder() |
| 61 | .ofType(Band.Type.DROP) |
| 62 | .withRate(TEN_M) |
| 63 | .burstSize(TWO_K) |
| 64 | .build(); |
| 65 | Meter meter = meterBuilder |
| 66 | .fromApp(appId) |
| 67 | .withBands(Arrays.asList(band1)) |
| 68 | .build(); |
| 69 | BandwidthProfile bandwidthProfile = BandwidthProfile.fromMeter(meter); |
| 70 | |
| 71 | assertEquals("wrong bw profile name", |
| 72 | bandwidthProfile.name(), meter.id().toString()); |
| 73 | assertEquals("wrong bw profile type", |
| 74 | bandwidthProfile.type(), BandwidthProfile.Type.sr2CM); |
| 75 | assertEquals("wrong bw profile CIR", |
| 76 | bandwidthProfile.cir().bps(), band1.rate() * EIGHT_K, 0); |
| 77 | assertEquals("wrong bw profile CBS", |
| 78 | (long) bandwidthProfile.cbs(), (long) band1.burst()); |
| 79 | assertNull(bandwidthProfile.pir()); |
| 80 | assertNull(bandwidthProfile.pbs()); |
| 81 | assertNull(bandwidthProfile.ebs()); |
| 82 | assertEquals("wrong green action", |
| 83 | bandwidthProfile.greenAction(), |
| 84 | getBuilder(Action.PASS).build()); |
| 85 | assertNull(bandwidthProfile.yellowAction()); |
| 86 | assertEquals("wrong red action", |
| 87 | bandwidthProfile.redAction(), |
| 88 | getBuilder(Action.DISCARD).build()); |
| 89 | assertEquals("wrong color-aware mode", |
| 90 | bandwidthProfile.colorAware(), false); |
| 91 | |
| 92 | // Create Meter with two bands |
| 93 | Band band2 = DefaultBand.builder().burstSize(ONE_K) |
| 94 | .ofType(Band.Type.REMARK) |
| 95 | .dropPrecedence((short) 0b001010) |
| 96 | .withRate(ONE_M) |
| 97 | .build(); |
| 98 | meter = meterBuilder |
| 99 | .fromApp(appId) |
| 100 | .withBands(Arrays.asList(band1, band2)) |
| 101 | .build(); |
| 102 | bandwidthProfile = BandwidthProfile.fromMeter(meter); |
| 103 | |
| 104 | assertEquals("wrong bw profile name", |
| 105 | bandwidthProfile.name(), meter.id().toString()); |
| 106 | assertEquals("wrong bw profile type", |
| 107 | bandwidthProfile.type(), BandwidthProfile.Type.trTCM); |
| 108 | assertEquals("wrong bw profile CIR", |
| 109 | bandwidthProfile.cir().bps(), band2.rate() * EIGHT_K, 0); |
| 110 | assertEquals("wrong bw profile CBS", |
| 111 | (long) bandwidthProfile.cbs(), (long) band2.burst()); |
| 112 | assertEquals("wrong bw profile PIR", |
| 113 | bandwidthProfile.pir().bps(), band1.rate() * EIGHT_K, 0); |
| 114 | assertEquals("wrong bw profile PBS", |
| 115 | (long) bandwidthProfile.pbs(), (long) band1.burst()); |
| 116 | assertNull(bandwidthProfile.ebs()); |
| 117 | assertEquals("wrong green action", |
| 118 | bandwidthProfile.greenAction(), |
| 119 | getBuilder(Action.PASS).build()); |
| 120 | assertEquals("wrong yellow action", |
| 121 | bandwidthProfile.yellowAction(), |
| 122 | getBuilder(Action.REMARK) |
| 123 | .dscpClass(DscpClass.AF11) |
| 124 | .build()); |
| 125 | assertEquals("wrong red action", |
| 126 | bandwidthProfile.redAction(), |
| 127 | getBuilder(Action.DISCARD).build()); |
| 128 | assertEquals("wrong color-aware mode", |
| 129 | bandwidthProfile.colorAware(), false); |
| 130 | } |
| 131 | |
| 132 | @Test |
| 133 | public void testType() { |
| 134 | BandwidthProfile.Builder bwProfileBuilder = BandwidthProfile.builder() |
| 135 | .name("profile") |
| 136 | .cir(Bandwidth.bps(ONE_M)) |
| 137 | .cbs((int) ONE_K) |
| 138 | .greenAction(getBuilder(Action.PASS).build()) |
| 139 | .redAction(getBuilder(Action.DISCARD).build()) |
| 140 | .colorAware(false); |
| 141 | assertEquals("wrong bw profile type", |
| 142 | bwProfileBuilder.build().type(), |
| 143 | BandwidthProfile.Type.sr2CM); |
| 144 | |
| 145 | bwProfileBuilder.ebs((int) TWO_K) |
| 146 | .yellowAction(getBuilder(Action.REMARK) |
| 147 | .dscpClass(DscpClass.AF11) |
| 148 | .build()); |
| 149 | assertEquals("wrong bw profile type", |
| 150 | bwProfileBuilder.build().type(), |
| 151 | BandwidthProfile.Type.srTCM); |
| 152 | bwProfileBuilder.ebs(null); |
| 153 | |
| 154 | bwProfileBuilder.pir(Bandwidth.bps(TEN_M)) |
| 155 | .pbs((int) TWO_K); |
| 156 | assertEquals("wrong bw profile type", |
| 157 | bwProfileBuilder.build().type(), |
| 158 | BandwidthProfile.Type.trTCM); |
| 159 | } |
| 160 | |
| 161 | @Test |
| 162 | public void testEquals() { |
| 163 | BandwidthProfile bwProfile1 = new BandwidthProfile.Builder() |
| 164 | .name("profile1") |
| 165 | .cir(Bandwidth.bps(ONE_M)) |
| 166 | .cbs((int) ONE_K) |
| 167 | .pir(Bandwidth.bps(TEN_M)) |
| 168 | .pbs((int) TWO_K) |
| 169 | .greenAction(getBuilder(Action.PASS).build()) |
| 170 | .yellowAction(getBuilder(Action.REMARK) |
| 171 | .dscpClass(DscpClass.AF11) |
| 172 | .build()) |
| 173 | .redAction(getBuilder(Action.DISCARD).build()) |
| 174 | .colorAware(false) |
| 175 | .build(); |
| 176 | BandwidthProfile bwProfile2 = new BandwidthProfile.Builder() |
| 177 | .name("profile2") |
| 178 | .cir(Bandwidth.bps(ONE_M)) |
| 179 | .cbs((int) ONE_K) |
| 180 | .pir(Bandwidth.bps(TEN_M)) |
| 181 | .pbs((int) TWO_K) |
| 182 | .greenAction(getBuilder(Action.PASS).build()) |
| 183 | .yellowAction(getBuilder(Action.REMARK) |
| 184 | .dscpClass(DscpClass.AF11) |
| 185 | .build()) |
| 186 | .redAction(getBuilder(Action.DISCARD).build()) |
| 187 | .colorAware(false) |
| 188 | .build(); |
| 189 | assertTrue("wrong equals method", bwProfile1.equals(bwProfile2)); |
| 190 | } |
| 191 | |
| 192 | private static BandwidthProfileAction.Builder getBuilder(Action action) { |
| 193 | return new BandwidthProfileAction.Builder().action(action); |
| 194 | } |
| 195 | } |