Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | ce2a03d | 2017-08-03 19:21:03 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 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.pcelabelstore; |
| 17 | |
| 18 | import static org.hamcrest.MatcherAssert.assertThat; |
| 19 | import static org.hamcrest.Matchers.is; |
| 20 | import static org.hamcrest.Matchers.notNullValue; |
| 21 | |
| 22 | import java.util.LinkedList; |
| 23 | import java.util.List; |
| 24 | import java.util.Map; |
| 25 | |
| 26 | import org.junit.After; |
| 27 | import org.junit.AfterClass; |
| 28 | import org.junit.Before; |
| 29 | import org.junit.BeforeClass; |
| 30 | import org.junit.Test; |
| 31 | |
| 32 | import org.onosproject.incubator.net.resource.label.DefaultLabelResource; |
| 33 | import org.onosproject.incubator.net.resource.label.LabelResource; |
| 34 | import org.onosproject.incubator.net.resource.label.LabelResourceId; |
| 35 | import org.onosproject.incubator.net.tunnel.TunnelId; |
| 36 | import org.onosproject.net.ConnectPoint; |
| 37 | import org.onosproject.net.DefaultAnnotations; |
| 38 | import org.onosproject.net.DefaultLink; |
| 39 | import org.onosproject.net.DeviceId; |
| 40 | import org.onosproject.net.Link; |
| 41 | import org.onosproject.net.PortNumber; |
| 42 | import org.onosproject.net.provider.ProviderId; |
| 43 | import org.onosproject.pcelabelstore.api.LspLocalLabelInfo; |
| 44 | import org.onosproject.pcelabelstore.util.TestStorageService; |
| 45 | |
| 46 | /** |
| 47 | * Unit tests for DistributedPceStore class. |
| 48 | */ |
| 49 | public class DistributedPceLabelStoreTest { |
| 50 | |
| 51 | private DistributedPceLabelStore distrPceStore; |
| 52 | private DeviceId deviceId1 = DeviceId.deviceId("foo"); |
| 53 | private DeviceId deviceId2 = DeviceId.deviceId("goo"); |
| 54 | private DeviceId deviceId3 = DeviceId.deviceId("yaa"); |
| 55 | private DeviceId deviceId4 = DeviceId.deviceId("zoo"); |
| 56 | private LabelResourceId labelId1 = LabelResourceId.labelResourceId(1); |
| 57 | private LabelResourceId labelId2 = LabelResourceId.labelResourceId(2); |
| 58 | private LabelResourceId labelId3 = LabelResourceId.labelResourceId(3); |
| 59 | private LabelResourceId labelId4 = LabelResourceId.labelResourceId(4); |
| 60 | private PortNumber portNumber1 = PortNumber.portNumber(1); |
| 61 | private PortNumber portNumber2 = PortNumber.portNumber(2); |
| 62 | private PortNumber portNumber3 = PortNumber.portNumber(3); |
| 63 | private PortNumber portNumber4 = PortNumber.portNumber(4); |
| 64 | private ConnectPoint srcConnectionPoint1 = new ConnectPoint(deviceId1, portNumber1); |
| 65 | private ConnectPoint dstConnectionPoint2 = new ConnectPoint(deviceId2, portNumber2); |
| 66 | private ConnectPoint srcConnectionPoint3 = new ConnectPoint(deviceId3, portNumber3); |
| 67 | private ConnectPoint dstConnectionPoint4 = new ConnectPoint(deviceId4, portNumber4); |
| 68 | private LabelResource labelResource1 = new DefaultLabelResource(deviceId1, labelId1); |
| 69 | private LabelResource labelResource2 = new DefaultLabelResource(deviceId2, labelId2); |
| 70 | private LabelResource labelResource3 = new DefaultLabelResource(deviceId3, labelId3); |
| 71 | private LabelResource labelResource4 = new DefaultLabelResource(deviceId4, labelId4); |
| 72 | private Link link1; |
| 73 | private Link link2; |
| 74 | private List<LabelResource> labelList1 = new LinkedList<>(); |
| 75 | private List<LabelResource> labelList2 = new LinkedList<>(); |
| 76 | private TunnelId tunnelId1 = TunnelId.valueOf("1"); |
| 77 | private TunnelId tunnelId2 = TunnelId.valueOf("2"); |
| 78 | private TunnelId tunnelId3 = TunnelId.valueOf("3"); |
| 79 | private TunnelId tunnelId4 = TunnelId.valueOf("4"); |
| 80 | |
| 81 | List<LspLocalLabelInfo> lspLocalLabelInfoList1 = new LinkedList<>(); |
| 82 | List<LspLocalLabelInfo> lspLocalLabelInfoList2 = new LinkedList<>(); |
| 83 | |
| 84 | @BeforeClass |
| 85 | public static void setUpBeforeClass() throws Exception { |
| 86 | } |
| 87 | |
| 88 | @AfterClass |
| 89 | public static void tearDownAfterClass() throws Exception { |
| 90 | } |
| 91 | |
| 92 | @Before |
| 93 | public void setUp() throws Exception { |
| 94 | distrPceStore = new DistributedPceLabelStore(); |
| 95 | // initialization |
| 96 | distrPceStore.storageService = new TestStorageService(); |
| 97 | distrPceStore.activate(); |
| 98 | |
| 99 | // Initialization of member variables |
| 100 | link1 = DefaultLink.builder() |
| 101 | .providerId(new ProviderId("eth", "1")) |
| 102 | .annotations(DefaultAnnotations.builder().set("key1", "yahoo").build()) |
| 103 | .src(srcConnectionPoint1) |
| 104 | .dst(dstConnectionPoint2) |
| 105 | .type(Link.Type.DIRECT) |
| 106 | .state(Link.State.ACTIVE) |
| 107 | .build(); |
| 108 | link2 = DefaultLink.builder() |
| 109 | .providerId(new ProviderId("mac", "2")) |
| 110 | .annotations(DefaultAnnotations.builder().set("key2", "google").build()) |
| 111 | .src(srcConnectionPoint3) |
| 112 | .dst(dstConnectionPoint4) |
| 113 | .type(Link.Type.DIRECT) |
| 114 | .state(Link.State.ACTIVE) |
| 115 | .build(); |
| 116 | labelList1.add(labelResource1); |
| 117 | labelList1.add(labelResource2); |
| 118 | labelList2.add(labelResource3); |
| 119 | labelList2.add(labelResource4); |
| 120 | |
| 121 | // Create pceccTunnelInfo1 |
| 122 | DeviceId deviceId1 = DeviceId.deviceId("foo"); |
| 123 | LabelResourceId inLabelId1 = LabelResourceId.labelResourceId(1); |
| 124 | LabelResourceId outLabelId1 = LabelResourceId.labelResourceId(2); |
| 125 | |
| 126 | LspLocalLabelInfo lspLocalLabel1 = DefaultLspLocalLabelInfo.builder() |
| 127 | .deviceId(deviceId1) |
| 128 | .inLabelId(inLabelId1) |
| 129 | .outLabelId(outLabelId1) |
| 130 | .build(); |
| 131 | lspLocalLabelInfoList1.add(lspLocalLabel1); |
| 132 | distrPceStore.addTunnelInfo(tunnelId1, lspLocalLabelInfoList1); |
| 133 | |
| 134 | // Create pceccTunnelInfo2 |
| 135 | DeviceId deviceId2 = DeviceId.deviceId("foo"); |
| 136 | LabelResourceId inLabelId2 = LabelResourceId.labelResourceId(3); |
| 137 | LabelResourceId outLabelId2 = LabelResourceId.labelResourceId(4); |
| 138 | |
| 139 | LspLocalLabelInfo lspLocalLabel2 = DefaultLspLocalLabelInfo.builder() |
| 140 | .deviceId(deviceId2) |
| 141 | .inLabelId(inLabelId2) |
| 142 | .outLabelId(outLabelId2) |
| 143 | .build(); |
| 144 | lspLocalLabelInfoList2.add(lspLocalLabel2); |
| 145 | distrPceStore.addTunnelInfo(tunnelId2, lspLocalLabelInfoList2); |
| 146 | } |
| 147 | |
| 148 | @After |
| 149 | public void tearDown() throws Exception { |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Checks the operation of addGlobalNodeLabel() method. |
| 154 | */ |
| 155 | @Test |
| 156 | public void testAddGlobalNodeLabel() { |
| 157 | // add device with label |
| 158 | distrPceStore.addGlobalNodeLabel(deviceId1, labelId1); |
| 159 | assertThat(distrPceStore.existsGlobalNodeLabel(deviceId1), is(true)); |
| 160 | assertThat(distrPceStore.getGlobalNodeLabel(deviceId1), is(labelId1)); |
| 161 | distrPceStore.addGlobalNodeLabel(deviceId2, labelId2); |
| 162 | assertThat(distrPceStore.existsGlobalNodeLabel(deviceId2), is(true)); |
| 163 | assertThat(distrPceStore.getGlobalNodeLabel(deviceId2), is(labelId2)); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Checks the operation of addAdjLabel() method. |
| 168 | */ |
| 169 | @Test |
| 170 | public void testAddAdjLabel() { |
| 171 | // link with list of labels |
| 172 | distrPceStore.addAdjLabel(link1, labelId1); |
| 173 | assertThat(distrPceStore.existsAdjLabel(link1), is(true)); |
| 174 | assertThat(distrPceStore.getAdjLabel(link1), is(labelId1)); |
| 175 | distrPceStore.addAdjLabel(link2, labelId2); |
| 176 | assertThat(distrPceStore.existsAdjLabel(link2), is(true)); |
| 177 | assertThat(distrPceStore.getAdjLabel(link2), is(labelId2)); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Checks the operation of addTunnelInfo() method. |
| 182 | */ |
| 183 | @Test |
| 184 | public void testAddTunnelInfo() { |
| 185 | // TunnelId with device label store information |
| 186 | distrPceStore.addTunnelInfo(tunnelId1, lspLocalLabelInfoList1); |
| 187 | assertThat(distrPceStore.existsTunnelInfo(tunnelId1), is(true)); |
| 188 | assertThat(distrPceStore.getTunnelInfo(tunnelId1), is(lspLocalLabelInfoList1)); |
| 189 | distrPceStore.addTunnelInfo(tunnelId2, lspLocalLabelInfoList2); |
| 190 | assertThat(distrPceStore.existsTunnelInfo(tunnelId2), is(true)); |
| 191 | assertThat(distrPceStore.getTunnelInfo(tunnelId2), is(lspLocalLabelInfoList2)); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Checks the operation of existsGlobalNodeLabel() method. |
| 196 | */ |
| 197 | @Test |
| 198 | public void testExistsGlobalNodeLabel() { |
| 199 | testAddGlobalNodeLabel(); |
| 200 | |
| 201 | assertThat(distrPceStore.existsGlobalNodeLabel(deviceId1), is(true)); |
| 202 | assertThat(distrPceStore.existsGlobalNodeLabel(deviceId2), is(true)); |
| 203 | assertThat(distrPceStore.existsGlobalNodeLabel(deviceId3), is(false)); |
| 204 | assertThat(distrPceStore.existsGlobalNodeLabel(deviceId4), is(false)); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Checks the operation of existsAdjLabel() method. |
| 209 | */ |
| 210 | @Test |
| 211 | public void testExistsAdjLabel() { |
| 212 | testAddAdjLabel(); |
| 213 | |
| 214 | assertThat(distrPceStore.existsAdjLabel(link1), is(true)); |
| 215 | assertThat(distrPceStore.existsAdjLabel(link2), is(true)); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Checks the operation of existsTunnelInfo() method. |
| 220 | */ |
| 221 | @Test |
| 222 | public void testExistsTunnelInfo() { |
| 223 | testAddTunnelInfo(); |
| 224 | |
| 225 | assertThat(distrPceStore.existsTunnelInfo(tunnelId1), is(true)); |
| 226 | assertThat(distrPceStore.existsTunnelInfo(tunnelId2), is(true)); |
| 227 | assertThat(distrPceStore.existsTunnelInfo(tunnelId3), is(false)); |
| 228 | assertThat(distrPceStore.existsTunnelInfo(tunnelId4), is(false)); |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Checks the operation of getGlobalNodeLabelCount() method. |
| 233 | */ |
| 234 | @Test |
| 235 | public void testGetGlobalNodeLabelCount() { |
| 236 | testAddGlobalNodeLabel(); |
| 237 | |
| 238 | assertThat(distrPceStore.getGlobalNodeLabelCount(), is(2)); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Checks the operation of getAdjLabelCount() method. |
| 243 | */ |
| 244 | @Test |
| 245 | public void testGetAdjLabelCount() { |
| 246 | testAddAdjLabel(); |
| 247 | |
| 248 | assertThat(distrPceStore.getAdjLabelCount(), is(2)); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Checks the operation of getTunnelInfoCount() method. |
| 253 | */ |
| 254 | @Test |
| 255 | public void testGetTunnelInfoCount() { |
| 256 | testAddTunnelInfo(); |
| 257 | |
| 258 | assertThat(distrPceStore.getTunnelInfoCount(), is(2)); |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Checks the operation of getGlobalNodeLabels() method. |
| 263 | */ |
| 264 | @Test |
| 265 | public void testGetGlobalNodeLabels() { |
| 266 | testAddGlobalNodeLabel(); |
| 267 | |
| 268 | Map<DeviceId, LabelResourceId> nodeLabelMap = distrPceStore.getGlobalNodeLabels(); |
| 269 | assertThat(nodeLabelMap, is(notNullValue())); |
| 270 | assertThat(nodeLabelMap.isEmpty(), is(false)); |
| 271 | assertThat(nodeLabelMap.size(), is(2)); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Checks the operation of getAdjLabels() method. |
| 276 | */ |
| 277 | @Test |
| 278 | public void testGetAdjLabels() { |
| 279 | testAddAdjLabel(); |
| 280 | |
| 281 | Map<Link, LabelResourceId> adjLabelMap = distrPceStore.getAdjLabels(); |
| 282 | assertThat(adjLabelMap, is(notNullValue())); |
| 283 | assertThat(adjLabelMap.isEmpty(), is(false)); |
| 284 | assertThat(adjLabelMap.size(), is(2)); |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Checks the operation of getTunnelInfos() method. |
| 289 | */ |
| 290 | @Test |
| 291 | public void testGetTunnelInfos() { |
| 292 | testAddTunnelInfo(); |
| 293 | |
| 294 | Map<TunnelId, List<LspLocalLabelInfo>> tunnelInfoMap = distrPceStore.getTunnelInfos(); |
| 295 | assertThat(tunnelInfoMap, is(notNullValue())); |
| 296 | assertThat(tunnelInfoMap.isEmpty(), is(false)); |
| 297 | assertThat(tunnelInfoMap.size(), is(2)); |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Checks the operation of getGlobalNodeLabel() method. |
| 302 | */ |
| 303 | @Test |
| 304 | public void testGetGlobalNodeLabel() { |
| 305 | testAddGlobalNodeLabel(); |
| 306 | |
| 307 | // deviceId1 with labelId1 |
| 308 | assertThat(deviceId1, is(notNullValue())); |
| 309 | assertThat(distrPceStore.getGlobalNodeLabel(deviceId1), is(labelId1)); |
| 310 | |
| 311 | // deviceId2 with labelId2 |
| 312 | assertThat(deviceId2, is(notNullValue())); |
| 313 | assertThat(distrPceStore.getGlobalNodeLabel(deviceId2), is(labelId2)); |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Checks the operation of getAdjLabel() method. |
| 318 | */ |
| 319 | @Test |
| 320 | public void testGetAdjLabel() { |
| 321 | testAddAdjLabel(); |
| 322 | |
| 323 | // link1 with labels |
| 324 | assertThat(link1, is(notNullValue())); |
| 325 | assertThat(distrPceStore.getAdjLabel(link1), is(labelId1)); |
| 326 | |
| 327 | // link2 with labels |
| 328 | assertThat(link2, is(notNullValue())); |
| 329 | assertThat(distrPceStore.getAdjLabel(link2), is(labelId2)); |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Checks the operation of getTunnelInfo() method. |
| 334 | */ |
| 335 | @Test |
| 336 | public void testGetTunnelInfo() { |
| 337 | testAddTunnelInfo(); |
| 338 | |
| 339 | // tunnelId1 with device label store info |
| 340 | assertThat(tunnelId1, is(notNullValue())); |
| 341 | assertThat(distrPceStore.getTunnelInfo(tunnelId1), is(lspLocalLabelInfoList1)); |
| 342 | |
| 343 | // tunnelId2 with device label store info |
| 344 | assertThat(tunnelId2, is(notNullValue())); |
| 345 | assertThat(distrPceStore.getTunnelInfo(tunnelId2), is(lspLocalLabelInfoList2)); |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Checks the operation of removeGlobalNodeLabel() method. |
| 350 | */ |
| 351 | @Test |
| 352 | public void testRemoveGlobalNodeLabel() { |
| 353 | testAddGlobalNodeLabel(); |
| 354 | |
| 355 | assertThat(distrPceStore.removeGlobalNodeLabel(deviceId1), is(true)); |
| 356 | assertThat(distrPceStore.removeGlobalNodeLabel(deviceId2), is(true)); |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Checks the operation of removeAdjLabel() method. |
| 361 | */ |
| 362 | @Test |
| 363 | public void testRemoveAdjLabel() { |
| 364 | testAddAdjLabel(); |
| 365 | |
| 366 | assertThat(distrPceStore.removeAdjLabel(link1), is(true)); |
| 367 | assertThat(distrPceStore.removeAdjLabel(link2), is(true)); |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * Checks the operation of removeTunnelInfo() method. |
| 372 | */ |
| 373 | @Test |
| 374 | public void testRemoveTunnelInfo() { |
| 375 | testAddTunnelInfo(); |
| 376 | |
| 377 | assertThat(distrPceStore.removeTunnelInfo(tunnelId1), is(true)); |
| 378 | assertThat(distrPceStore.removeTunnelInfo(tunnelId2), is(true)); |
| 379 | } |
| 380 | } |