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