Avantika-Huawei | 56c1184 | 2016-04-28 00:56:56 +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.provider.pcep.tunnel.impl; |
| 17 | |
| 18 | import static org.hamcrest.MatcherAssert.assertThat; |
| 19 | import static org.hamcrest.core.Is.is; |
| 20 | import static org.onosproject.incubator.net.tunnel.Tunnel.Type.MPLS; |
| 21 | import static org.onosproject.incubator.net.tunnel.Tunnel.State.INIT; |
| 22 | import static org.onosproject.provider.pcep.tunnel.impl.PcepAnnotationKeys.BANDWIDTH; |
| 23 | import static org.onosproject.provider.pcep.tunnel.impl.PcepAnnotationKeys.LOCAL_LSP_ID; |
| 24 | import static org.onosproject.provider.pcep.tunnel.impl.PcepAnnotationKeys.LSP_SIG_TYPE; |
| 25 | import static org.onosproject.provider.pcep.tunnel.impl.PcepAnnotationKeys.PCC_TUNNEL_ID; |
| 26 | import static org.onosproject.provider.pcep.tunnel.impl.PcepAnnotationKeys.PLSP_ID; |
| 27 | import static org.onosproject.provider.pcep.tunnel.impl.LspType.WITHOUT_SIGNALLING_AND_WITHOUT_SR; |
| 28 | |
| 29 | import java.io.IOException; |
| 30 | import java.util.Collection; |
| 31 | import java.util.Collections; |
| 32 | import java.util.HashMap; |
| 33 | import java.util.HashSet; |
| 34 | |
| 35 | import org.jboss.netty.buffer.ChannelBuffer; |
| 36 | import org.jboss.netty.buffer.ChannelBuffers; |
| 37 | import org.junit.After; |
| 38 | import org.junit.Before; |
| 39 | import org.junit.Test; |
| 40 | import org.onlab.packet.IpAddress; |
| 41 | import org.onosproject.cfg.ComponentConfigAdapter; |
| 42 | import org.onosproject.core.ApplicationId; |
| 43 | import org.onosproject.incubator.net.tunnel.DefaultTunnel; |
| 44 | import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint; |
| 45 | import org.onosproject.incubator.net.tunnel.Tunnel; |
| 46 | import org.onosproject.incubator.net.tunnel.TunnelDescription; |
| 47 | import org.onosproject.incubator.net.tunnel.TunnelEndPoint; |
| 48 | import org.onosproject.incubator.net.tunnel.TunnelId; |
| 49 | import org.onosproject.incubator.net.tunnel.TunnelName; |
| 50 | import org.onosproject.incubator.net.tunnel.TunnelProvider; |
| 51 | import org.onosproject.incubator.net.tunnel.TunnelProviderService; |
| 52 | import org.onosproject.incubator.net.tunnel.Tunnel.State; |
| 53 | import org.onosproject.net.DefaultAnnotations; |
| 54 | import org.onosproject.net.ElementId; |
| 55 | import org.onosproject.net.Path; |
| 56 | import org.onosproject.net.SparseAnnotations; |
| 57 | import org.onosproject.pcepio.exceptions.PcepOutOfBoundMessageException; |
| 58 | import org.onosproject.pcepio.exceptions.PcepParseException; |
| 59 | import org.onosproject.pcepio.protocol.PcepFactories; |
| 60 | import org.onosproject.pcepio.protocol.PcepMessage; |
| 61 | import org.onosproject.pcepio.protocol.PcepMessageReader; |
| 62 | import org.onosproject.pcep.controller.ClientCapability; |
| 63 | import org.onosproject.pcep.controller.PccId; |
| 64 | |
| 65 | import com.google.common.collect.ImmutableSet; |
| 66 | |
| 67 | /** |
| 68 | * Tests handling of PCEP report message. |
| 69 | */ |
| 70 | public class PcepTunnelAddedTest { |
| 71 | |
| 72 | static final String PROVIDER_ID = "org.onosproject.provider.tunnel.pcep"; |
| 73 | PcepTunnelProvider tunnelProvider = new PcepTunnelProvider(); |
| 74 | private final MockTunnelProviderRegistryAdapter registry = new MockTunnelProviderRegistryAdapter(); |
| 75 | private final PcepClientControllerAdapter controller = new PcepClientControllerAdapter(); |
| 76 | private final PcepControllerAdapter ctl = new PcepControllerAdapter(); |
| 77 | private final PcepTunnelApiMapper pcepTunnelAPIMapper = new PcepTunnelApiMapper(); |
| 78 | private final MockTunnelServiceAdapter tunnelService = new MockTunnelServiceAdapter(); |
| 79 | |
| 80 | private class MockTunnelProviderRegistryAdapter extends TunnelProviderRegistryAdapter { |
| 81 | public long tunnelIdCounter; |
| 82 | |
| 83 | @Override |
| 84 | public TunnelProviderService register(TunnelProvider provider) { |
| 85 | this.provider = provider; |
| 86 | return new TestProviderService(); |
| 87 | } |
| 88 | |
| 89 | private class TestProviderService implements TunnelProviderService { |
| 90 | |
| 91 | @Override |
| 92 | public TunnelProvider provider() { |
| 93 | return null; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public TunnelId tunnelAdded(TunnelDescription tunnel) { |
| 98 | return TunnelId.valueOf(String.valueOf(++tunnelIdCounter)); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public TunnelId tunnelAdded(TunnelDescription tunnel, State state) { |
| 103 | return TunnelId.valueOf(String.valueOf(++tunnelIdCounter)); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public void tunnelRemoved(TunnelDescription tunnel) { |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public void tunnelUpdated(TunnelDescription tunnel) { |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public void tunnelUpdated(TunnelDescription tunnel, State state) { |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public Tunnel tunnelQueryById(TunnelId tunnelId) { |
| 120 | return null; |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | private class MockTunnelServiceAdapter extends TunnelServiceAdapter { |
| 126 | private HashMap<TunnelId, Tunnel> tunnelIdAsKeyStore = new HashMap<>(); |
| 127 | private int tunnelIdCounter = 0; |
| 128 | |
| 129 | @Override |
| 130 | public TunnelId setupTunnel(ApplicationId producerId, ElementId srcElementId, Tunnel tunnel, Path path) { |
| 131 | TunnelId tunnelId = TunnelId.valueOf(String.valueOf(++tunnelIdCounter)); |
| 132 | tunnelIdAsKeyStore.put(tunnelId, tunnel); |
| 133 | return tunnelId; |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public Collection<Tunnel> queryTunnel(TunnelEndPoint src, TunnelEndPoint dst) { |
| 138 | Collection<Tunnel> result = new HashSet<>(); |
| 139 | Tunnel tunnel = null; |
| 140 | for (TunnelId tunnelId : tunnelIdAsKeyStore.keySet()) { |
| 141 | tunnel = tunnelIdAsKeyStore.get(tunnelId); |
| 142 | |
| 143 | if ((null != tunnel) && (src.equals(tunnel.src())) && (dst.equals(tunnel.dst()))) { |
| 144 | result.add(tunnel); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | return result.isEmpty() ? Collections.emptySet() : ImmutableSet.copyOf(result); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public Collection<Tunnel> queryAllTunnels() { |
| 153 | Collection<Tunnel> result = new HashSet<>(); |
| 154 | |
| 155 | for (TunnelId tunnelId : tunnelIdAsKeyStore.keySet()) { |
| 156 | result.add(tunnelIdAsKeyStore.get(tunnelId)); |
| 157 | } |
| 158 | |
| 159 | return result.isEmpty() ? Collections.emptySet() : ImmutableSet.copyOf(result); |
| 160 | |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | @Before |
| 165 | public void preSetup() { |
| 166 | tunnelProvider.tunnelProviderRegistry = registry; |
| 167 | tunnelProvider.pcepClientController = controller; |
| 168 | tunnelProvider.controller = ctl; |
| 169 | tunnelProvider.pcepTunnelApiMapper = pcepTunnelAPIMapper; |
| 170 | tunnelProvider.cfgService = new ComponentConfigAdapter(); |
| 171 | tunnelProvider.tunnelService = tunnelService; |
| 172 | tunnelProvider.service = registry.register(tunnelProvider); |
| 173 | tunnelProvider.activate(); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Tests PCRpt msg with sync flag set. |
| 178 | */ |
| 179 | @Test |
| 180 | public void tunnelProviderAddedTest1() throws PcepParseException, PcepOutOfBoundMessageException { |
| 181 | byte[] reportMsg = new byte[] {0x20, 0x0a, 0x00, (byte) 0x84, |
| 182 | 0x21, 0x10, 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, //SRP object |
| 183 | 0x00, 0x1c, 0x00, 0x04, // PATH-SETUP-TYPE TLV |
| 184 | 0x00, 0x00, 0x00, 0x02, |
| 185 | 0x20, 0x10, 0x00, 0x24, 0x00, 0x00, 0x10, 0x03, //LSP object |
| 186 | 0x00, 0x11, 0x00, 0x02, 0x54, 0x31, 0x00, 0x00, //symbolic path tlv |
| 187 | 0x00, 0x12, 0x00, 0x10, // IPv4-LSP-IDENTIFIER-TLV |
| 188 | 0x01, 0x01, 0x01, 0x01, |
| 189 | 0x00, 0x01, 0x00, 0x01, |
| 190 | 0x01, 0x01, 0x01, 0x01, |
| 191 | 0x05, 0x05, 0x05, 0x05, |
| 192 | |
| 193 | 0x07, 0x10, 0x00, 0x14, //ERO object |
| 194 | 0x01, 0x08, (byte) 0x01, 0x01, 0x01, 0x01, 0x04, 0x00, // ERO IPv4 sub objects |
| 195 | 0x01, 0x08, (byte) 0x05, 0x05, 0x05, 0x05, 0x04, 0x00, |
| 196 | |
| 197 | 0x08, 0x10, 0x00, 0x34, //RRO object |
| 198 | 0x01, 0x08, 0x11, 0x01, 0x01, 0x01, 0x04, 0x00, // RRO IPv4 sub objects |
| 199 | 0x01, 0x08, 0x11, 0x01, 0x01, 0x02, 0x04, 0x00, |
| 200 | 0x01, 0x08, 0x06, 0x06, 0x06, 0x06, 0x04, 0x00, |
| 201 | 0x01, 0x08, 0x12, 0x01, 0x01, 0x02, 0x04, 0x00, |
| 202 | 0x01, 0x08, 0x12, 0x01, 0x01, 0x01, 0x04, 0x00, |
| 203 | 0x01, 0x08, 0x05, 0x05, 0x05, 0x05, 0x04, 0x00 |
| 204 | }; |
| 205 | |
| 206 | ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(); |
| 207 | buffer.writeBytes(reportMsg); |
| 208 | |
| 209 | PcepMessageReader<PcepMessage> reader = PcepFactories.getGenericReader(); |
| 210 | PcepMessage message = reader.readFrom(buffer); |
| 211 | controller.processClientMessage(PccId.pccId(IpAddress.valueOf("1.1.1.1")), message); |
| 212 | |
| 213 | assertThat(registry.tunnelIdCounter, is((long) 1)); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Tests updating an existing tunnel on receiving asynchronous PCRpt msg, |
| 218 | * i.e. without any SRP id. |
| 219 | */ |
| 220 | @Test |
| 221 | public void tunnelProviderAddedTest2() throws PcepParseException, PcepOutOfBoundMessageException { |
| 222 | byte[] reportMsg = new byte[] {0x20, 0x0a, 0x00, (byte) 0x50, |
| 223 | 0x21, 0x10, 0x00, 0x14, //SRP object |
| 224 | 0x00, 0x00, 0x00, 0x00, |
| 225 | 0x00, 0x00, 0x00, 0x00, |
| 226 | 0x00, 0x1c, 0x00, 0x04, // PATH-SETUP-TYPE TLV |
| 227 | 0x00, 0x00, 0x00, 0x02, |
| 228 | 0x20, 0x10, 0x00, 0x24, 0x00, 0x00, 0x10, 0x19, //LSP object |
| 229 | 0x00, 0x11, 0x00, 0x02, 0x54, 0x31, 0x00, 0x00, //symbolic path TLV |
| 230 | 0x00, 0x12, 0x00, 0x10, // IPv4-LSP-IDENTIFIER-TLV |
| 231 | 0x4e, 0x1f, 0x04, 0x00, |
| 232 | 0x00, 0x01, 0x00, 0x01, |
| 233 | 0x4e, 0x1f, 0x04, 0x00, |
| 234 | 0x4e, 0x20, 0x04, 0x00, |
| 235 | 0x07, 0x10, 0x00, 0x14, //ERO object |
| 236 | 0x01, 0x08, (byte) 0xb6, 0x02, 0x4e, 0x1f, 0x04, 0x00, // ERO IPv4 sub objects |
| 237 | 0x01, 0x08, (byte) 0xb6, 0x02, 0x4e, 0x20, 0x04, 0x00, |
| 238 | }; |
| 239 | |
| 240 | ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(); |
| 241 | buffer.writeBytes(reportMsg); |
| 242 | |
| 243 | PcepMessageReader<PcepMessage> reader = PcepFactories.getGenericReader(); |
| 244 | PcepMessage message = reader.readFrom(buffer); |
| 245 | |
| 246 | // create an existing tunnel. |
| 247 | IpTunnelEndPoint tunnelEndPointSrc = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(0x4e1f0400)); |
| 248 | IpTunnelEndPoint tunnelEndPointDst = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(0x4e200400)); |
| 249 | |
| 250 | SparseAnnotations annotations = DefaultAnnotations.builder() |
| 251 | .set(BANDWIDTH, (new Integer(1)).toString()) |
| 252 | .set(LSP_SIG_TYPE, WITHOUT_SIGNALLING_AND_WITHOUT_SR.name()) |
| 253 | .set(PCC_TUNNEL_ID, String.valueOf(1)) |
| 254 | .set(PLSP_ID, String.valueOf(1)) |
| 255 | .set(LOCAL_LSP_ID, String.valueOf(1)).build(); |
| 256 | |
| 257 | Tunnel tunnel = new DefaultTunnel(null, tunnelEndPointSrc, tunnelEndPointDst, MPLS, INIT, null, null, |
| 258 | TunnelName.tunnelName("T123"), null, annotations); |
| 259 | tunnelService.setupTunnel(null, null, tunnel, null); |
| 260 | |
| 261 | PccId pccId = PccId.pccId(IpAddress.valueOf(0x4e1f0400)); |
| 262 | controller.getClient(pccId).setCapability(new ClientCapability(true, true, true)); |
| 263 | controller.getClient(pccId).setIsSyncComplete(true); |
| 264 | |
| 265 | // Process update message. |
| 266 | controller.processClientMessage(pccId, message); |
| 267 | assertThat(tunnelService.queryAllTunnels().size(), is(1)); |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Tests adding a new tunnel on receiving asynchronous PCRpt msg, |
| 272 | * i.e. without any SRP id. |
| 273 | */ |
| 274 | @Test |
| 275 | public void tunnelProviderAddedTest3() throws PcepParseException, PcepOutOfBoundMessageException { |
| 276 | byte[] reportMsg = new byte[] {0x20, 0x0a, 0x00, (byte) 0x84, |
| 277 | 0x21, 0x10, 0x00, 0x14, //SRP object |
| 278 | 0x00, 0x00, 0x00, 0x00, |
| 279 | 0x00, 0x00, 0x00, 0x00, |
| 280 | 0x00, 0x1c, 0x00, 0x04, // PATH-SETUP-TYPE TLV |
| 281 | 0x00, 0x00, 0x00, 0x02, |
| 282 | 0x20, 0x10, 0x00, 0x24, 0x00, 0x00, 0x10, 0x19, // LSP object |
| 283 | 0x00, 0x11, 0x00, 0x02, 0x54, 0x31, 0x00, 0x00, // symbolic path TLV |
| 284 | 0x00, 0x12, 0x00, 0x10, // IPv4-LSP-IDENTIFIER-TLV |
| 285 | 0x01, 0x01, 0x01, 0x01, |
| 286 | 0x00, 0x01, 0x00, 0x01, |
| 287 | 0x01, 0x01, 0x01, 0x01, |
| 288 | 0x05, 0x05, 0x05, 0x05, |
| 289 | |
| 290 | 0x07, 0x10, 0x00, 0x14, //ERO object |
| 291 | 0x01, 0x08, (byte) 0x01, 0x01, 0x01, 0x01, 0x04, 0x00, // ERO IPv4 sub objects |
| 292 | 0x01, 0x08, (byte) 0x05, 0x05, 0x05, 0x05, 0x04, 0x00, |
| 293 | |
| 294 | 0x08, 0x10, 0x00, 0x34, //RRO object |
| 295 | 0x01, 0x08, 0x11, 0x01, 0x01, 0x01, 0x04, 0x00, // RRO IPv4 sub objects |
| 296 | 0x01, 0x08, 0x11, 0x01, 0x01, 0x02, 0x04, 0x00, |
| 297 | 0x01, 0x08, 0x06, 0x06, 0x06, 0x06, 0x04, 0x00, |
| 298 | 0x01, 0x08, 0x12, 0x01, 0x01, 0x02, 0x04, 0x00, |
| 299 | 0x01, 0x08, 0x12, 0x01, 0x01, 0x01, 0x04, 0x00, |
| 300 | 0x01, 0x08, 0x05, 0x05, 0x05, 0x05, 0x04, 0x00 |
| 301 | }; |
| 302 | |
| 303 | ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(); |
| 304 | buffer.writeBytes(reportMsg); |
| 305 | |
| 306 | PcepMessageReader<PcepMessage> reader = PcepFactories.getGenericReader(); |
| 307 | PcepMessage message = reader.readFrom(buffer); |
| 308 | |
| 309 | PccId pccId = PccId.pccId(IpAddress.valueOf("1.1.1.1")); |
| 310 | controller.getClient(pccId).setIsSyncComplete(true); |
| 311 | controller.getClient(pccId).setCapability(new ClientCapability(true, true, true)); |
| 312 | controller.processClientMessage(pccId, message); |
| 313 | |
| 314 | assertThat(registry.tunnelIdCounter, is((long) 1)); |
| 315 | } |
| 316 | |
| 317 | @After |
| 318 | public void tearDown() throws IOException { |
| 319 | tunnelProvider.deactivate(); |
| 320 | tunnelProvider.controller = null; |
| 321 | tunnelProvider.pcepClientController = null; |
| 322 | tunnelProvider.tunnelProviderRegistry = null; |
| 323 | |
| 324 | tunnelProvider.pcepTunnelApiMapper = null; |
| 325 | tunnelProvider.cfgService = null; |
| 326 | tunnelProvider.tunnelService = null; |
| 327 | tunnelProvider.service = null; |
| 328 | } |
| 329 | } |