Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [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 | package org.onosproject.pce.pceservice; |
| 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; |
| 22 | |
| 23 | import static org.onosproject.net.Link.Type.DIRECT; |
| 24 | |
| 25 | import java.util.Iterator; |
| 26 | import java.util.List; |
| 27 | import java.util.LinkedList; |
| 28 | |
| 29 | import org.junit.After; |
| 30 | import org.junit.Before; |
| 31 | import org.junit.Test; |
| 32 | |
| 33 | import org.onlab.packet.IpAddress; |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 34 | import org.onosproject.core.ApplicationId; |
| 35 | import org.onosproject.core.CoreService; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 36 | import org.onosproject.core.DefaultGroupId; |
| 37 | import org.onosproject.incubator.net.tunnel.Tunnel; |
| 38 | import org.onosproject.incubator.net.tunnel.TunnelEndPoint; |
| 39 | import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint; |
| 40 | import org.onosproject.incubator.net.tunnel.TunnelName; |
| 41 | import org.onosproject.incubator.net.tunnel.TunnelId; |
| 42 | import org.onosproject.incubator.net.tunnel.DefaultTunnel; |
| 43 | import org.onosproject.incubator.net.resource.label.LabelResourceId; |
| 44 | import org.onosproject.incubator.net.resource.label.LabelResourceService; |
| 45 | import org.onosproject.net.ConnectPoint; |
| 46 | import org.onosproject.net.DefaultAnnotations; |
| 47 | import org.onosproject.net.DefaultPath; |
| 48 | import org.onosproject.net.DeviceId; |
| 49 | import org.onosproject.net.PortNumber; |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 50 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 51 | import org.onosproject.net.Path; |
| 52 | import org.onosproject.pce.pcestore.api.LspLocalLabelInfo; |
| 53 | import org.onosproject.pce.pcestore.api.PceStore; |
| 54 | import org.onosproject.pce.pcestore.PceccTunnelInfo; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 55 | import org.onosproject.net.provider.ProviderId; |
| 56 | import org.onosproject.pce.util.LabelResourceAdapter; |
| 57 | import org.onosproject.pce.util.PceStoreAdapter; |
| 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; |
| 68 | |
| 69 | private BasicPceccHandler pceccHandler; |
| 70 | protected LabelResourceService labelRsrcService; |
| 71 | protected PceStore pceStore; |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 72 | private FlowObjectiveService flowObjectiveService; |
| 73 | private CoreService coreService; |
| 74 | private ApplicationId appId; |
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)); |
| 77 | private DefaultGroupId groupId = new DefaultGroupId(92034); |
| 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; |
| 83 | private PceccTunnelInfo pceccTunnelInfo; |
| 84 | private DeviceId deviceId1; |
| 85 | private DeviceId deviceId2; |
| 86 | private DeviceId deviceId3; |
| 87 | private DeviceId deviceId4; |
| 88 | private DeviceId deviceId5; |
| 89 | private PortNumber port1; |
| 90 | private PortNumber port2; |
| 91 | private PortNumber port3; |
| 92 | private PortNumber port4; |
| 93 | private PortNumber port5; |
| 94 | |
| 95 | @Before |
| 96 | public void setUp() throws Exception { |
| 97 | pceccHandler = BasicPceccHandler.getInstance(); |
| 98 | labelRsrcService = new LabelResourceAdapter(); |
| 99 | pceStore = new PceStoreAdapter(); |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 100 | flowObjectiveService = new PceManagerTest.MockFlowObjService(); |
| 101 | coreService = new PceManagerTest.MockCoreService(); |
| 102 | appId = coreService.registerApplication("org.onosproject.pce"); |
| 103 | pceccHandler.initialize(labelRsrcService, flowObjectiveService, appId, pceStore); |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 104 | |
| 105 | // Cretae tunnel test |
| 106 | // Link |
| 107 | ProviderId providerId = new ProviderId("of", "foo"); |
| 108 | deviceId1 = DeviceId.deviceId("of:A"); |
| 109 | deviceId2 = DeviceId.deviceId("of:B"); |
| 110 | deviceId3 = DeviceId.deviceId("of:C"); |
| 111 | deviceId4 = DeviceId.deviceId("of:D"); |
| 112 | deviceId5 = DeviceId.deviceId("of:E"); |
| 113 | port1 = PortNumber.portNumber(1); |
| 114 | port2 = PortNumber.portNumber(2); |
| 115 | port3 = PortNumber.portNumber(3); |
| 116 | port4 = PortNumber.portNumber(4); |
| 117 | port5 = PortNumber.portNumber(5); |
| 118 | List<Link> linkList = new LinkedList<>(); |
| 119 | |
| 120 | Link l1 = DefaultLink.builder() |
| 121 | .providerId(providerId) |
| 122 | .annotations(DefaultAnnotations.builder().set("key1", "yahoo").build()) |
| 123 | .src(new ConnectPoint(deviceId1, port1)) |
| 124 | .dst(new ConnectPoint(deviceId2, port2)) |
| 125 | .type(DIRECT) |
| 126 | .state(Link.State.ACTIVE) |
| 127 | .build(); |
| 128 | linkList.add(l1); |
| 129 | Link l2 = DefaultLink.builder() |
| 130 | .providerId(providerId) |
| 131 | .annotations(DefaultAnnotations.builder().set("key2", "yahoo").build()) |
| 132 | .src(new ConnectPoint(deviceId2, port2)) |
| 133 | .dst(new ConnectPoint(deviceId3, port3)) |
| 134 | .type(DIRECT) |
| 135 | .state(Link.State.ACTIVE) |
| 136 | .build(); |
| 137 | linkList.add(l2); |
| 138 | Link l3 = DefaultLink.builder() |
| 139 | .providerId(providerId) |
| 140 | .annotations(DefaultAnnotations.builder().set("key3", "yahoo").build()) |
| 141 | .src(new ConnectPoint(deviceId3, port3)) |
| 142 | .dst(new ConnectPoint(deviceId4, port4)) |
| 143 | .type(DIRECT) |
| 144 | .state(Link.State.ACTIVE) |
| 145 | .build(); |
| 146 | linkList.add(l3); |
| 147 | Link l4 = DefaultLink.builder() |
| 148 | .providerId(providerId) |
| 149 | .annotations(DefaultAnnotations.builder().set("key4", "yahoo").build()) |
| 150 | .src(new ConnectPoint(deviceId4, port4)) |
| 151 | .dst(new ConnectPoint(deviceId5, port5)) |
| 152 | .type(DIRECT) |
| 153 | .state(Link.State.ACTIVE) |
| 154 | .build(); |
| 155 | linkList.add(l4); |
| 156 | |
| 157 | // Path |
| 158 | path = new DefaultPath(providerId, linkList, 10); |
| 159 | |
| 160 | // Tunnel |
| 161 | tunnel = new DefaultTunnel(producerName, src, dst, Tunnel.Type.VXLAN, |
| 162 | Tunnel.State.ACTIVE, groupId, tunnelId, |
| 163 | tunnelName, path); |
| 164 | } |
| 165 | |
| 166 | @After |
| 167 | public void tearDown() throws Exception { |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Checks the operation of getInstance() method. |
| 172 | */ |
| 173 | @Test |
| 174 | public void testGetInstance() { |
| 175 | assertThat(pceccHandler, is(notNullValue())); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Checks the operation of allocateLabel() method. |
| 180 | */ |
| 181 | @Test |
| 182 | public void testAllocateLabel() { |
| 183 | List<LspLocalLabelInfo> lspLocalLabelInfoList; |
| 184 | Iterator<LspLocalLabelInfo> iterator; |
| 185 | LspLocalLabelInfo lspLocalLabelInfo; |
| 186 | DeviceId deviceId; |
| 187 | LabelResourceId inLabelId; |
| 188 | LabelResourceId outLabelId; |
| 189 | PortNumber inPort; |
| 190 | PortNumber outPort; |
| 191 | |
| 192 | // check allocation result |
| 193 | assertThat(pceccHandler.allocateLabel(tunnel), is(true)); |
| 194 | |
| 195 | // Check list of devices with IN and OUT labels whether stored properly in store |
| 196 | pceccTunnelInfo = pceStore.getTunnelInfo(tunnel.tunnelId()); |
| 197 | lspLocalLabelInfoList = pceccTunnelInfo.lspLocalLabelInfoList(); |
| 198 | iterator = lspLocalLabelInfoList.iterator(); |
| 199 | |
| 200 | // Retrieve values and check device5 |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 201 | lspLocalLabelInfo = iterator.next(); |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 202 | deviceId = lspLocalLabelInfo.deviceId(); |
| 203 | inLabelId = lspLocalLabelInfo.inLabelId(); |
| 204 | outLabelId = lspLocalLabelInfo.outLabelId(); |
| 205 | inPort = lspLocalLabelInfo.inPort(); |
| 206 | outPort = lspLocalLabelInfo.outPort(); |
| 207 | |
| 208 | assertThat(deviceId, is(deviceId5)); |
| 209 | assertThat(inLabelId, is(notNullValue())); |
| 210 | assertThat(outLabelId, is(nullValue())); |
| 211 | assertThat(inPort, is(port5)); |
| 212 | assertThat(outPort, is(nullValue())); |
| 213 | |
| 214 | // Next element check |
| 215 | // Retrieve values and check device4 |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 216 | lspLocalLabelInfo = iterator.next(); |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 217 | deviceId = lspLocalLabelInfo.deviceId(); |
| 218 | inLabelId = lspLocalLabelInfo.inLabelId(); |
| 219 | outLabelId = lspLocalLabelInfo.outLabelId(); |
| 220 | inPort = lspLocalLabelInfo.inPort(); |
| 221 | outPort = lspLocalLabelInfo.outPort(); |
| 222 | |
| 223 | assertThat(deviceId, is(deviceId4)); |
| 224 | assertThat(inLabelId, is(notNullValue())); |
| 225 | assertThat(outLabelId, is(notNullValue())); |
| 226 | assertThat(inPort, is(port4)); |
| 227 | assertThat(outPort, is(port5)); |
| 228 | |
| 229 | // Next element check |
| 230 | // Retrieve values and check device3 |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 231 | lspLocalLabelInfo = iterator.next(); |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 232 | deviceId = lspLocalLabelInfo.deviceId(); |
| 233 | inLabelId = lspLocalLabelInfo.inLabelId(); |
| 234 | outLabelId = lspLocalLabelInfo.outLabelId(); |
| 235 | inPort = lspLocalLabelInfo.inPort(); |
| 236 | outPort = lspLocalLabelInfo.outPort(); |
| 237 | |
| 238 | assertThat(deviceId, is(deviceId3)); |
| 239 | assertThat(inLabelId, is(notNullValue())); |
| 240 | assertThat(outLabelId, is(notNullValue())); |
| 241 | assertThat(inPort, is(port3)); |
| 242 | assertThat(outPort, is(port4)); |
| 243 | |
| 244 | // Next element check |
| 245 | // Retrieve values and check device2 |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 246 | lspLocalLabelInfo = iterator.next(); |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 247 | deviceId = lspLocalLabelInfo.deviceId(); |
| 248 | inLabelId = lspLocalLabelInfo.inLabelId(); |
| 249 | outLabelId = lspLocalLabelInfo.outLabelId(); |
| 250 | inPort = lspLocalLabelInfo.inPort(); |
| 251 | outPort = lspLocalLabelInfo.outPort(); |
| 252 | |
| 253 | assertThat(deviceId, is(deviceId2)); |
| 254 | assertThat(inLabelId, is(notNullValue())); |
| 255 | assertThat(outLabelId, is(notNullValue())); |
| 256 | assertThat(inPort, is(port2)); |
| 257 | assertThat(outPort, is(port3)); |
| 258 | |
| 259 | // Next element check |
| 260 | // Retrieve values and check device1 |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 261 | lspLocalLabelInfo = iterator.next(); |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 262 | deviceId = lspLocalLabelInfo.deviceId(); |
| 263 | inLabelId = lspLocalLabelInfo.inLabelId(); |
| 264 | outLabelId = lspLocalLabelInfo.outLabelId(); |
| 265 | inPort = lspLocalLabelInfo.inPort(); |
| 266 | outPort = lspLocalLabelInfo.outPort(); |
| 267 | |
| 268 | assertThat(deviceId, is(deviceId1)); |
| 269 | assertThat(inLabelId, is(nullValue())); |
| 270 | assertThat(outLabelId, is(notNullValue())); |
| 271 | assertThat(inPort, is(nullValue())); |
| 272 | assertThat(outPort, is(port2)); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Checks the operation of releaseLabel() method. |
| 277 | */ |
| 278 | @Test |
| 279 | public void testReleaseLabel() { |
| 280 | // Release tunnels |
| 281 | assertThat(pceccHandler.allocateLabel(tunnel), is(true)); |
| 282 | pceccHandler.releaseLabel(tunnel); |
| 283 | |
| 284 | // Retrieve from store. Store should not contain this tunnel info. |
| 285 | pceccTunnelInfo = pceStore.getTunnelInfo(tunnel.tunnelId()); |
| 286 | assertThat(pceccTunnelInfo, is(nullValue())); |
| 287 | } |
| 288 | } |