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