Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [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 | |
| 17 | package org.onosproject.net.resource.impl; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import org.junit.After; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.packet.MplsLabel; |
| 24 | import org.onlab.packet.VlanId; |
| 25 | import org.onlab.util.Identifier; |
| 26 | import org.onosproject.core.IdGenerator; |
| 27 | import org.onosproject.net.ConnectPoint; |
| 28 | import org.onosproject.net.DefaultLink; |
| 29 | import org.onosproject.net.EncapsulationType; |
| 30 | import org.onosproject.net.Link; |
| 31 | import org.onosproject.net.LinkKey; |
| 32 | import org.onosproject.net.intent.IntentId; |
| 33 | import org.onosproject.net.intent.MockIdGenerator; |
| 34 | import org.onosproject.net.resource.MockResourceService; |
| 35 | import org.onosproject.net.resource.impl.LabelAllocator.FirstFitSelection; |
| 36 | import org.onosproject.net.resource.impl.LabelAllocator.LabelSelection; |
| 37 | import org.onosproject.net.resource.impl.LabelAllocator.RandomSelection; |
| 38 | |
| 39 | import java.util.Arrays; |
| 40 | import java.util.List; |
| 41 | import java.util.Map; |
| 42 | |
| 43 | import static org.hamcrest.CoreMatchers.instanceOf; |
| 44 | import static org.hamcrest.MatcherAssert.assertThat; |
| 45 | import static org.junit.Assert.assertEquals; |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 46 | import static org.junit.Assert.assertTrue; |
| 47 | import static org.onosproject.net.DefaultEdgeLink.createEdgeLink; |
| 48 | import static org.onosproject.net.Link.Type.DIRECT; |
| 49 | import static org.onosproject.net.NetTestTools.PID; |
| 50 | import static org.onosproject.net.NetTestTools.connectPoint; |
| 51 | |
| 52 | /** |
| 53 | * Unit tests for LabelAllocator. |
| 54 | */ |
| 55 | public class LabelAllocatorTest { |
| 56 | |
| 57 | private LabelAllocator allocator; |
| 58 | private MockResourceService resourceService; |
Thomas Vachuska | 2048c1f | 2017-05-10 19:32:22 -0700 | [diff] [blame] | 59 | private IdGenerator idGenerator = MockIdGenerator.INSTANCE; |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 60 | |
| 61 | private final ConnectPoint d1p0 = connectPoint("s1", 0); |
| 62 | private final ConnectPoint d1p1 = connectPoint("s1", 1); |
| 63 | private final ConnectPoint d2p0 = connectPoint("s2", 0); |
| 64 | private final ConnectPoint d2p1 = connectPoint("s2", 1); |
| 65 | |
| 66 | private final List<Link> links = Arrays.asList( |
| 67 | createEdgeLink(d1p0, true), |
| 68 | DefaultLink.builder().providerId(PID).src(d1p1).dst(d2p1).type(DIRECT).build(), |
| 69 | createEdgeLink(d2p0, false) |
| 70 | ); |
| 71 | |
| 72 | private final String firstFit = "FIRST_FIT"; |
| 73 | private final String random = "RANDOM"; |
| 74 | private final String wrong = "BLAHBLAHBLAH"; |
| 75 | |
| 76 | @Before |
| 77 | public void setUp() { |
| 78 | this.resourceService = new MockResourceService(); |
| 79 | this.allocator = new LabelAllocator(this.resourceService); |
| 80 | } |
| 81 | |
| 82 | @After |
| 83 | public void tearDown() { |
| 84 | |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * To test changes to the selection behavior. |
| 89 | */ |
| 90 | @Test |
| 91 | public void testChangeBehavior() { |
| 92 | // It has to be an instance of LabelSelection |
| 93 | assertThat(this.allocator.getLabelSelection(), instanceOf(LabelSelection.class)); |
| 94 | // By default we have Random Selection |
| 95 | assertThat(this.allocator.getLabelSelection(), instanceOf(RandomSelection.class)); |
| 96 | // We change to FirstFit and we test the change |
| 97 | this.allocator.setLabelSelection(firstFit); |
| 98 | assertThat(this.allocator.getLabelSelection(), instanceOf(FirstFitSelection.class)); |
| 99 | // We change to Random and we test the change |
| 100 | this.allocator.setLabelSelection(random); |
| 101 | assertThat(this.allocator.getLabelSelection(), instanceOf(RandomSelection.class)); |
| 102 | // We put a wrong type and we should have a Random selection |
| 103 | this.allocator.setLabelSelection(wrong); |
| 104 | assertThat(this.allocator.getLabelSelection(), instanceOf(RandomSelection.class)); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * To test the first fit behavior with VLAN Id. In the First step |
| 109 | * we use the default set, for the first selection the selected label |
| 110 | * has to be 1. In the Second step we change the default set and for |
| 111 | * the first fit selection the selected has to be 2. |
| 112 | */ |
| 113 | @Test |
| 114 | public void testFirstFitBehaviorVlan() { |
| 115 | // We change to FirstFit and we test the change |
| 116 | this.allocator.setLabelSelection(firstFit); |
| 117 | assertThat(this.allocator.getLabelSelection(), instanceOf(FirstFitSelection.class)); |
| 118 | // We test the behavior for VLAN |
| 119 | Map<LinkKey, Identifier<?>> allocation = this.allocator.assignLabelToLinks( |
| 120 | ImmutableSet.copyOf(links.subList(1, 2)), |
| 121 | IntentId.valueOf(idGenerator.getNewId()), |
| 122 | EncapsulationType.VLAN); |
| 123 | Identifier<?> id = allocation.get(LinkKey.linkKey(d1p1, d2p1)); |
| 124 | // value has to be a VlanId |
| 125 | assertThat(id, instanceOf(VlanId.class)); |
| 126 | // value should not be a forbidden value |
| 127 | VlanId vlanId = (VlanId) id; |
| 128 | assertTrue(VlanId.NO_VID < vlanId.toShort() && vlanId.toShort() < VlanId.MAX_VLAN); |
| 129 | // value will be always 1 |
| 130 | assertEquals(1, vlanId.toShort()); |
| 131 | |
| 132 | // We change the available Ids |
| 133 | this.resourceService.availableVlanLabels = ImmutableSet.of( |
| 134 | (short) 100, |
| 135 | (short) 11, |
| 136 | (short) 20, |
| 137 | (short) 2, |
| 138 | (short) 3 |
| 139 | ); |
| 140 | // We test again the behavior for VLAN |
| 141 | allocation = this.allocator.assignLabelToLinks( |
| 142 | ImmutableSet.copyOf(links.subList(1, 2)), |
| 143 | IntentId.valueOf(idGenerator.getNewId()), |
| 144 | EncapsulationType.VLAN); |
| 145 | id = allocation.get(LinkKey.linkKey(d1p1, d2p1)); |
| 146 | // value has to be a VlanId |
| 147 | assertThat(id, instanceOf(VlanId.class)); |
| 148 | // value should not be a forbidden value |
| 149 | vlanId = (VlanId) id; |
| 150 | assertTrue(VlanId.NO_VID < vlanId.toShort() && vlanId.toShort() < VlanId.MAX_VLAN); |
| 151 | // value will be always 2 |
| 152 | assertEquals(2, vlanId.toShort()); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * To test the first fit behavior with MPLS label. In the First step |
| 157 | * we use the default set, for the first selection the selected label |
| 158 | * has to be 1. In the Second step we change the default set and for |
| 159 | * the first fit selection the selected has to be 100. |
| 160 | */ |
| 161 | @Test |
| 162 | public void testFirstFitBehaviorMpls() { |
| 163 | // We change to FirstFit and we test the change |
| 164 | this.allocator.setLabelSelection(firstFit); |
| 165 | assertThat(this.allocator.getLabelSelection(), instanceOf(FirstFitSelection.class)); |
| 166 | // We test the behavior for MPLS |
| 167 | Map<LinkKey, Identifier<?>> allocation = this.allocator.assignLabelToLinks( |
| 168 | ImmutableSet.copyOf(links.subList(1, 2)), |
| 169 | IntentId.valueOf(idGenerator.getNewId()), |
| 170 | EncapsulationType.MPLS); |
| 171 | Identifier<?> id = allocation.get(LinkKey.linkKey(d1p1, d2p1)); |
| 172 | // value has to be a Mplslabel |
| 173 | assertThat(id, instanceOf(MplsLabel.class)); |
| 174 | // value should not be a forbidden value |
| 175 | MplsLabel mplsLabel = (MplsLabel) id; |
| 176 | assertTrue(0 < mplsLabel.toInt() && mplsLabel.toInt() < MplsLabel.MAX_MPLS); |
| 177 | // value will be always 1 |
| 178 | assertEquals(1, mplsLabel.toInt()); |
| 179 | |
| 180 | // We change the available Ids |
| 181 | this.resourceService.availableMplsLabels = ImmutableSet.of( |
| 182 | 100, |
| 183 | 200, |
| 184 | 1000 |
| 185 | ); |
| 186 | // We test again the behavior for MPLS |
| 187 | allocation = this.allocator.assignLabelToLinks( |
| 188 | ImmutableSet.copyOf(links.subList(1, 2)), |
| 189 | IntentId.valueOf(idGenerator.getNewId()), |
| 190 | EncapsulationType.MPLS); |
| 191 | id = allocation.get(LinkKey.linkKey(d1p1, d2p1)); |
| 192 | // value has to be a Mplslabel |
| 193 | assertThat(id, instanceOf(MplsLabel.class)); |
| 194 | // value should not be a forbidden value |
| 195 | mplsLabel = (MplsLabel) id; |
| 196 | assertTrue(0 < mplsLabel.toInt() && mplsLabel.toInt() < MplsLabel.MAX_MPLS); |
| 197 | // value will be always 100 |
| 198 | assertEquals(100, mplsLabel.toInt()); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * To test the random behavior with VLAN Id. We make two selection, |
| 203 | * we test that these two selection are different. |
| 204 | */ |
| 205 | @Test |
| 206 | public void testRandomBehaviorVlan() { |
| 207 | // Verify the random behavior |
| 208 | assertThat(this.allocator.getLabelSelection(), instanceOf(RandomSelection.class)); |
| 209 | // We test the behavior for VLAN |
| 210 | Map<LinkKey, Identifier<?>> allocation = this.allocator.assignLabelToLinks( |
| 211 | ImmutableSet.copyOf(links.subList(1, 2)), |
| 212 | IntentId.valueOf(idGenerator.getNewId()), |
| 213 | EncapsulationType.VLAN); |
| 214 | Identifier<?> id = allocation.get(LinkKey.linkKey(d1p1, d2p1)); |
| 215 | // value has to be a VlanId |
| 216 | assertThat(id, instanceOf(VlanId.class)); |
| 217 | // value should not be a forbidden value |
| 218 | Short value = Short.parseShort(id.toString()); |
| 219 | VlanId prevVlanId = VlanId.vlanId(value); |
Aaron Kruglikov | 1a15b06 | 2017-01-26 15:47:34 -0800 | [diff] [blame] | 220 | assertTrue(VlanId.NO_VID <= prevVlanId.toShort() && prevVlanId.toShort() <= VlanId.MAX_VLAN); |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 221 | |
| 222 | allocation = this.allocator.assignLabelToLinks( |
| 223 | ImmutableSet.copyOf(links.subList(1, 2)), |
| 224 | IntentId.valueOf(idGenerator.getNewId()), |
| 225 | EncapsulationType.VLAN); |
| 226 | id = allocation.get(LinkKey.linkKey(d1p1, d2p1)); |
| 227 | // value has to be a VlanId |
| 228 | assertThat(id, instanceOf(VlanId.class)); |
| 229 | // value should not be a forbidden value |
| 230 | VlanId vlanId = (VlanId) id; |
Aaron Kruglikov | 1a15b06 | 2017-01-26 15:47:34 -0800 | [diff] [blame] | 231 | assertTrue(VlanId.NO_VID <= vlanId.toShort() && vlanId.toShort() <= VlanId.MAX_VLAN); |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 232 | |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * To test random behavior with MPLS label. We make two selection, |
| 237 | * we test that these two selection are different. |
| 238 | */ |
| 239 | @Test |
| 240 | public void testRandomBehaviorMpls() { |
| 241 | // Verify the random behavior |
| 242 | assertThat(this.allocator.getLabelSelection(), instanceOf(RandomSelection.class)); |
| 243 | // We test the behavior for MPLS |
| 244 | Map<LinkKey, Identifier<?>> allocation = this.allocator.assignLabelToLinks( |
| 245 | ImmutableSet.copyOf(links.subList(1, 2)), |
| 246 | IntentId.valueOf(idGenerator.getNewId()), |
| 247 | EncapsulationType.MPLS); |
| 248 | Identifier<?> id = allocation.get(LinkKey.linkKey(d1p1, d2p1)); |
| 249 | // value has to be a Mplslabel |
| 250 | assertThat(id, instanceOf(MplsLabel.class)); |
| 251 | // value should not be a forbidden value |
| 252 | MplsLabel prevMplsId = (MplsLabel) id; |
Aaron Kruglikov | 1a15b06 | 2017-01-26 15:47:34 -0800 | [diff] [blame] | 253 | assertTrue(0 <= prevMplsId.toInt() && prevMplsId.toInt() <= MplsLabel.MAX_MPLS); |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 254 | |
| 255 | allocation = this.allocator.assignLabelToLinks( |
| 256 | ImmutableSet.copyOf(links.subList(1, 2)), |
| 257 | IntentId.valueOf(idGenerator.getNewId()), |
| 258 | EncapsulationType.MPLS); |
| 259 | id = allocation.get(LinkKey.linkKey(d1p1, d2p1)); |
| 260 | // value has to be a Mplslabel |
| 261 | assertThat(id, instanceOf(MplsLabel.class)); |
| 262 | // value should not be a forbidden value |
| 263 | MplsLabel mplsId = (MplsLabel) id; |
Aaron Kruglikov | 1a15b06 | 2017-01-26 15:47:34 -0800 | [diff] [blame] | 264 | assertTrue(0 <= mplsId.toInt() && mplsId.toInt() <= MplsLabel.MAX_MPLS); |
Pier Ventre | f8543d8 | 2016-09-28 19:49:33 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | /** |
| 268 | * To test the port key based API. |
| 269 | */ |
| 270 | @Test |
| 271 | public void testPortKey() { |
| 272 | // Verify the first behavior |
| 273 | this.allocator.setLabelSelection(firstFit); |
| 274 | assertThat(this.allocator.getLabelSelection(), instanceOf(FirstFitSelection.class)); |
| 275 | // We test the behavior for VLAN |
| 276 | Map<ConnectPoint, Identifier<?>> allocation = this.allocator.assignLabelToPorts( |
| 277 | ImmutableSet.copyOf(links.subList(1, 2)), |
| 278 | IntentId.valueOf(idGenerator.getNewId()), |
| 279 | EncapsulationType.VLAN); |
| 280 | Identifier<?> id = allocation.get(new ConnectPoint(d1p1.elementId(), d1p1.port())); |
| 281 | // value has to be a VlanId |
| 282 | assertThat(id, instanceOf(VlanId.class)); |
| 283 | // value should not be a forbidden value |
| 284 | VlanId prevVlanId = (VlanId) id; |
| 285 | assertTrue(VlanId.NO_VID < prevVlanId.toShort() && prevVlanId.toShort() < VlanId.MAX_VLAN); |
| 286 | // value has to be 1 |
| 287 | assertEquals(1, prevVlanId.toShort()); |
| 288 | // verify same applies for d2p1 |
| 289 | id = allocation.get(new ConnectPoint(d2p1.elementId(), d2p1.port())); |
| 290 | assertThat(id, instanceOf(VlanId.class)); |
| 291 | // value should not be a forbidden value |
| 292 | VlanId vlanId = (VlanId) id; |
| 293 | assertTrue(VlanId.NO_VID < vlanId.toShort() && vlanId.toShort() < VlanId.MAX_VLAN); |
| 294 | // value has to be 1 |
| 295 | assertEquals(prevVlanId, vlanId); |
| 296 | } |
| 297 | |
| 298 | |
| 299 | |
| 300 | } |