Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +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 | */ |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 16 | package org.onosproject.pcelabelstore.label; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 17 | |
| 18 | import static org.hamcrest.MatcherAssert.assertThat; |
| 19 | import static org.hamcrest.Matchers.is; |
| 20 | import static org.hamcrest.Matchers.notNullValue; |
| 21 | import static org.hamcrest.Matchers.nullValue; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 22 | import static org.onosproject.net.Link.Type.DIRECT; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 23 | import java.util.Iterator; |
| 24 | import java.util.List; |
| 25 | import java.util.LinkedList; |
| 26 | |
| 27 | import org.junit.After; |
| 28 | import org.junit.Before; |
| 29 | import org.junit.Test; |
Ray Milkey | a7cf8c8 | 2018-02-08 15:07:06 -0800 | [diff] [blame] | 30 | import org.onlab.graph.ScalarWeight; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 31 | import org.onlab.packet.IpAddress; |
Yi Tseng | fa394de | 2017-02-01 11:26:40 -0800 | [diff] [blame] | 32 | import org.onosproject.core.GroupId; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 33 | import org.onosproject.incubator.net.tunnel.Tunnel; |
| 34 | import org.onosproject.incubator.net.tunnel.TunnelEndPoint; |
| 35 | import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint; |
| 36 | import org.onosproject.incubator.net.tunnel.TunnelName; |
| 37 | import org.onosproject.incubator.net.tunnel.TunnelId; |
| 38 | import org.onosproject.incubator.net.tunnel.DefaultTunnel; |
| 39 | import org.onosproject.incubator.net.resource.label.LabelResourceId; |
| 40 | import org.onosproject.incubator.net.resource.label.LabelResourceService; |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 41 | import org.onosproject.net.AnnotationKeys; |
| 42 | import org.onosproject.net.Annotations; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 43 | import org.onosproject.net.ConnectPoint; |
| 44 | import org.onosproject.net.DefaultAnnotations; |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 45 | import org.onosproject.net.DefaultDevice; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 46 | import org.onosproject.net.DefaultPath; |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 47 | import org.onosproject.net.Device; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 48 | import org.onosproject.net.DeviceId; |
| 49 | import org.onosproject.net.PortNumber; |
| 50 | import org.onosproject.net.Path; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 51 | import org.onosproject.net.provider.ProviderId; |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 52 | import org.onosproject.pcelabelstore.api.LspLocalLabelInfo; |
| 53 | import org.onosproject.pcelabelstore.api.PceLabelStore; |
| 54 | import org.onosproject.pcelabelstore.util.LabelResourceAdapter; |
| 55 | import org.onosproject.pcelabelstore.util.MockDeviceService; |
| 56 | import org.onosproject.pcelabelstore.util.PceLabelStoreAdapter; |
harikrushna-Huawei | a2c7c20 | 2017-04-10 18:22:00 +0530 | [diff] [blame] | 57 | import org.onosproject.pcep.server.impl.BasicPceccHandler; |
| 58 | import org.onosproject.pcep.server.impl.PcepClientControllerImpl; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 59 | import org.onosproject.net.DefaultLink; |
| 60 | import org.onosproject.net.Link; |
| 61 | |
| 62 | /** |
| 63 | * Unit tests for BasicPceccHandler class. |
| 64 | */ |
| 65 | public class BasicPceccHandlerTest { |
| 66 | |
| 67 | public static final long LOCAL_LABEL_SPACE_MIN = 5122; |
| 68 | public static final long LOCAL_LABEL_SPACE_MAX = 9217; |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 69 | private static final String L3 = "L3"; |
| 70 | private static final String LSRID = "lsrId"; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 71 | |
| 72 | private BasicPceccHandler pceccHandler; |
| 73 | protected LabelResourceService labelRsrcService; |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 74 | protected MockDeviceService deviceService; |
| 75 | protected PceLabelStore pceStore; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 76 | private TunnelEndPoint src = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(23423)); |
| 77 | private TunnelEndPoint dst = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(32421)); |
Yi Tseng | fa394de | 2017-02-01 11:26:40 -0800 | [diff] [blame] | 78 | private GroupId groupId = new GroupId(92034); |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 79 | private TunnelName tunnelName = TunnelName.tunnelName("TunnelName"); |
| 80 | private TunnelId tunnelId = TunnelId.valueOf("41654654"); |
| 81 | private ProviderId producerName = new ProviderId("producer1", "13"); |
| 82 | private Path path; |
| 83 | private Tunnel tunnel; |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 84 | List<LspLocalLabelInfo> lspLocalLabelInfoList; |
| 85 | private Device deviceD1, deviceD2, deviceD3, deviceD4, deviceD5; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 86 | private DeviceId deviceId1; |
| 87 | private DeviceId deviceId2; |
| 88 | private DeviceId deviceId3; |
| 89 | private DeviceId deviceId4; |
| 90 | private DeviceId deviceId5; |
| 91 | private PortNumber port1; |
| 92 | private PortNumber port2; |
| 93 | private PortNumber port3; |
| 94 | private PortNumber port4; |
| 95 | private PortNumber port5; |
| 96 | |
| 97 | @Before |
| 98 | public void setUp() throws Exception { |
| 99 | pceccHandler = BasicPceccHandler.getInstance(); |
| 100 | labelRsrcService = new LabelResourceAdapter(); |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 101 | pceStore = new PceLabelStoreAdapter(); |
| 102 | deviceService = new MockDeviceService(); |
| 103 | pceccHandler.initialize(labelRsrcService, |
| 104 | deviceService, |
| 105 | pceStore, |
| 106 | new PcepClientControllerImpl()); |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 107 | |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 108 | // Create tunnel test |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 109 | // Link |
| 110 | ProviderId providerId = new ProviderId("of", "foo"); |
| 111 | deviceId1 = DeviceId.deviceId("of:A"); |
| 112 | deviceId2 = DeviceId.deviceId("of:B"); |
| 113 | deviceId3 = DeviceId.deviceId("of:C"); |
| 114 | deviceId4 = DeviceId.deviceId("of:D"); |
| 115 | deviceId5 = DeviceId.deviceId("of:E"); |
| 116 | port1 = PortNumber.portNumber(1); |
| 117 | port2 = PortNumber.portNumber(2); |
| 118 | port3 = PortNumber.portNumber(3); |
| 119 | port4 = PortNumber.portNumber(4); |
| 120 | port5 = PortNumber.portNumber(5); |
| 121 | List<Link> linkList = new LinkedList<>(); |
| 122 | |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 123 | // Making L3 devices |
| 124 | DefaultAnnotations.Builder builderDev1 = DefaultAnnotations.builder(); |
| 125 | builderDev1.set(AnnotationKeys.TYPE, L3); |
| 126 | builderDev1.set(LSRID, "1.1.1.1"); |
| 127 | deviceD1 = new MockDevice(deviceId1, builderDev1.build()); |
| 128 | deviceService.addDevice(deviceD1); |
| 129 | |
| 130 | // Making L3 devices |
| 131 | DefaultAnnotations.Builder builderDev2 = DefaultAnnotations.builder(); |
| 132 | builderDev2.set(AnnotationKeys.TYPE, L3); |
| 133 | builderDev2.set(LSRID, "2.2.2.2"); |
| 134 | deviceD2 = new MockDevice(deviceId2, builderDev2.build()); |
| 135 | deviceService.addDevice(deviceD2); |
| 136 | |
| 137 | // Making L3 devices |
| 138 | DefaultAnnotations.Builder builderDev3 = DefaultAnnotations.builder(); |
| 139 | builderDev3.set(AnnotationKeys.TYPE, L3); |
| 140 | builderDev3.set(LSRID, "3.3.3.3"); |
| 141 | deviceD3 = new MockDevice(deviceId3, builderDev3.build()); |
| 142 | deviceService.addDevice(deviceD3); |
| 143 | |
| 144 | // Making L3 devices |
| 145 | DefaultAnnotations.Builder builderDev4 = DefaultAnnotations.builder(); |
| 146 | builderDev4.set(AnnotationKeys.TYPE, L3); |
| 147 | builderDev4.set(LSRID, "4.4.4.4"); |
| 148 | deviceD4 = new MockDevice(deviceId4, builderDev4.build()); |
| 149 | deviceService.addDevice(deviceD4); |
| 150 | |
| 151 | // Making L3 devices |
| 152 | DefaultAnnotations.Builder builderDev5 = DefaultAnnotations.builder(); |
| 153 | builderDev5.set(AnnotationKeys.TYPE, L3); |
| 154 | builderDev5.set(LSRID, "5.5.5.5"); |
| 155 | deviceD5 = new MockDevice(deviceId5, builderDev5.build()); |
| 156 | deviceService.addDevice(deviceD5); |
| 157 | |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 158 | Link l1 = DefaultLink.builder() |
| 159 | .providerId(providerId) |
| 160 | .annotations(DefaultAnnotations.builder().set("key1", "yahoo").build()) |
| 161 | .src(new ConnectPoint(deviceId1, port1)) |
| 162 | .dst(new ConnectPoint(deviceId2, port2)) |
| 163 | .type(DIRECT) |
| 164 | .state(Link.State.ACTIVE) |
| 165 | .build(); |
| 166 | linkList.add(l1); |
| 167 | Link l2 = DefaultLink.builder() |
| 168 | .providerId(providerId) |
| 169 | .annotations(DefaultAnnotations.builder().set("key2", "yahoo").build()) |
| 170 | .src(new ConnectPoint(deviceId2, port2)) |
| 171 | .dst(new ConnectPoint(deviceId3, port3)) |
| 172 | .type(DIRECT) |
| 173 | .state(Link.State.ACTIVE) |
| 174 | .build(); |
| 175 | linkList.add(l2); |
| 176 | Link l3 = DefaultLink.builder() |
| 177 | .providerId(providerId) |
| 178 | .annotations(DefaultAnnotations.builder().set("key3", "yahoo").build()) |
| 179 | .src(new ConnectPoint(deviceId3, port3)) |
| 180 | .dst(new ConnectPoint(deviceId4, port4)) |
| 181 | .type(DIRECT) |
| 182 | .state(Link.State.ACTIVE) |
| 183 | .build(); |
| 184 | linkList.add(l3); |
| 185 | Link l4 = DefaultLink.builder() |
| 186 | .providerId(providerId) |
| 187 | .annotations(DefaultAnnotations.builder().set("key4", "yahoo").build()) |
| 188 | .src(new ConnectPoint(deviceId4, port4)) |
| 189 | .dst(new ConnectPoint(deviceId5, port5)) |
| 190 | .type(DIRECT) |
| 191 | .state(Link.State.ACTIVE) |
| 192 | .build(); |
| 193 | linkList.add(l4); |
| 194 | |
| 195 | // Path |
Ray Milkey | a7cf8c8 | 2018-02-08 15:07:06 -0800 | [diff] [blame] | 196 | path = new DefaultPath(providerId, linkList, ScalarWeight.toWeight(10)); |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 197 | |
| 198 | // Tunnel |
| 199 | tunnel = new DefaultTunnel(producerName, src, dst, Tunnel.Type.VXLAN, |
| 200 | Tunnel.State.ACTIVE, groupId, tunnelId, |
| 201 | tunnelName, path); |
| 202 | } |
| 203 | |
| 204 | @After |
| 205 | public void tearDown() throws Exception { |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Checks the operation of getInstance() method. |
| 210 | */ |
| 211 | @Test |
| 212 | public void testGetInstance() { |
| 213 | assertThat(pceccHandler, is(notNullValue())); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Checks the operation of allocateLabel() method. |
| 218 | */ |
| 219 | @Test |
| 220 | public void testAllocateLabel() { |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 221 | Iterator<LspLocalLabelInfo> iterator; |
| 222 | LspLocalLabelInfo lspLocalLabelInfo; |
| 223 | DeviceId deviceId; |
| 224 | LabelResourceId inLabelId; |
| 225 | LabelResourceId outLabelId; |
| 226 | PortNumber inPort; |
| 227 | PortNumber outPort; |
| 228 | |
| 229 | // check allocation result |
| 230 | assertThat(pceccHandler.allocateLabel(tunnel), is(true)); |
| 231 | |
| 232 | // Check list of devices with IN and OUT labels whether stored properly in store |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 233 | lspLocalLabelInfoList = pceStore.getTunnelInfo(tunnel.tunnelId()); |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 234 | iterator = lspLocalLabelInfoList.iterator(); |
| 235 | |
| 236 | // Retrieve values and check device5 |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 237 | lspLocalLabelInfo = iterator.next(); |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 238 | deviceId = lspLocalLabelInfo.deviceId(); |
| 239 | inLabelId = lspLocalLabelInfo.inLabelId(); |
| 240 | outLabelId = lspLocalLabelInfo.outLabelId(); |
| 241 | inPort = lspLocalLabelInfo.inPort(); |
| 242 | outPort = lspLocalLabelInfo.outPort(); |
| 243 | |
| 244 | assertThat(deviceId, is(deviceId5)); |
| 245 | assertThat(inLabelId, is(notNullValue())); |
| 246 | assertThat(outLabelId, is(nullValue())); |
| 247 | assertThat(inPort, is(port5)); |
| 248 | assertThat(outPort, is(nullValue())); |
| 249 | |
| 250 | // Next element check |
| 251 | // Retrieve values and check device4 |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 252 | lspLocalLabelInfo = iterator.next(); |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 253 | deviceId = lspLocalLabelInfo.deviceId(); |
| 254 | inLabelId = lspLocalLabelInfo.inLabelId(); |
| 255 | outLabelId = lspLocalLabelInfo.outLabelId(); |
| 256 | inPort = lspLocalLabelInfo.inPort(); |
| 257 | outPort = lspLocalLabelInfo.outPort(); |
| 258 | |
| 259 | assertThat(deviceId, is(deviceId4)); |
| 260 | assertThat(inLabelId, is(notNullValue())); |
| 261 | assertThat(outLabelId, is(notNullValue())); |
| 262 | assertThat(inPort, is(port4)); |
| 263 | assertThat(outPort, is(port5)); |
| 264 | |
| 265 | // Next element check |
| 266 | // Retrieve values and check device3 |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 267 | lspLocalLabelInfo = iterator.next(); |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 268 | deviceId = lspLocalLabelInfo.deviceId(); |
| 269 | inLabelId = lspLocalLabelInfo.inLabelId(); |
| 270 | outLabelId = lspLocalLabelInfo.outLabelId(); |
| 271 | inPort = lspLocalLabelInfo.inPort(); |
| 272 | outPort = lspLocalLabelInfo.outPort(); |
| 273 | |
| 274 | assertThat(deviceId, is(deviceId3)); |
| 275 | assertThat(inLabelId, is(notNullValue())); |
| 276 | assertThat(outLabelId, is(notNullValue())); |
| 277 | assertThat(inPort, is(port3)); |
| 278 | assertThat(outPort, is(port4)); |
| 279 | |
| 280 | // Next element check |
| 281 | // Retrieve values and check device2 |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 282 | lspLocalLabelInfo = iterator.next(); |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 283 | deviceId = lspLocalLabelInfo.deviceId(); |
| 284 | inLabelId = lspLocalLabelInfo.inLabelId(); |
| 285 | outLabelId = lspLocalLabelInfo.outLabelId(); |
| 286 | inPort = lspLocalLabelInfo.inPort(); |
| 287 | outPort = lspLocalLabelInfo.outPort(); |
| 288 | |
| 289 | assertThat(deviceId, is(deviceId2)); |
| 290 | assertThat(inLabelId, is(notNullValue())); |
| 291 | assertThat(outLabelId, is(notNullValue())); |
| 292 | assertThat(inPort, is(port2)); |
| 293 | assertThat(outPort, is(port3)); |
| 294 | |
| 295 | // Next element check |
| 296 | // Retrieve values and check device1 |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 297 | lspLocalLabelInfo = iterator.next(); |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 298 | deviceId = lspLocalLabelInfo.deviceId(); |
| 299 | inLabelId = lspLocalLabelInfo.inLabelId(); |
| 300 | outLabelId = lspLocalLabelInfo.outLabelId(); |
| 301 | inPort = lspLocalLabelInfo.inPort(); |
| 302 | outPort = lspLocalLabelInfo.outPort(); |
| 303 | |
| 304 | assertThat(deviceId, is(deviceId1)); |
| 305 | assertThat(inLabelId, is(nullValue())); |
| 306 | assertThat(outLabelId, is(notNullValue())); |
| 307 | assertThat(inPort, is(nullValue())); |
| 308 | assertThat(outPort, is(port2)); |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Checks the operation of releaseLabel() method. |
| 313 | */ |
| 314 | @Test |
| 315 | public void testReleaseLabel() { |
| 316 | // Release tunnels |
| 317 | assertThat(pceccHandler.allocateLabel(tunnel), is(true)); |
| 318 | pceccHandler.releaseLabel(tunnel); |
| 319 | |
| 320 | // Retrieve from store. Store should not contain this tunnel info. |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 321 | lspLocalLabelInfoList = pceStore.getTunnelInfo(tunnel.tunnelId()); |
| 322 | assertThat(lspLocalLabelInfoList, is(nullValue())); |
| 323 | } |
| 324 | |
| 325 | private class MockDevice extends DefaultDevice { |
| 326 | MockDevice(DeviceId id, Annotations annotations) { |
| 327 | super(null, id, null, null, null, null, null, null, annotations); |
| 328 | } |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 329 | } |
| 330 | } |