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