2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.protocol.bgp.parser.spi.pojo;
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.never;
16 import static org.mockito.Mockito.times;
17 import static org.mockito.Mockito.verify;
19 import io.netty.buffer.ByteBuf;
20 import io.netty.buffer.Unpooled;
21 import org.junit.After;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.mockito.Mockito;
25 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
26 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
27 import org.opendaylight.protocol.bgp.parser.BGPTreatAsWithdrawException;
28 import org.opendaylight.protocol.bgp.parser.spi.AddressFamilyRegistry;
29 import org.opendaylight.protocol.bgp.parser.spi.AttributeRegistry;
30 import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
31 import org.opendaylight.protocol.bgp.parser.spi.BgpPrefixSidTlvRegistry;
32 import org.opendaylight.protocol.bgp.parser.spi.CapabilityRegistry;
33 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
34 import org.opendaylight.protocol.bgp.parser.spi.MultiPathSupport;
35 import org.opendaylight.protocol.bgp.parser.spi.NlriRegistry;
36 import org.opendaylight.protocol.bgp.parser.spi.ParameterRegistry;
37 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
38 import org.opendaylight.protocol.bgp.parser.spi.RevisedErrorHandling;
39 import org.opendaylight.protocol.bgp.parser.spi.SubsequentAddressFamilyRegistry;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.BgpParameters;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.bgp.prefix.sid.bgp.prefix.sid.tlvs.BgpPrefixSidTlv;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlri;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlriBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlri;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlriBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv4AddressFamily;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv6AddressFamily;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.UnicastSubsequentAddressFamily;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.Ipv4NextHopCaseBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.ipv4.next.hop._case.Ipv4NextHopBuilder;
54 import org.opendaylight.yangtools.yang.binding.Notification;
56 public class SimpleRegistryTest {
58 private static final MultiPathSupport ADD_PATH_SUPPORT = tableType -> true;
60 private static final PeerSpecificParserConstraint CONSTRAINT;
63 PeerSpecificParserConstraintImpl peerConstraint = new PeerSpecificParserConstraintImpl();
64 peerConstraint.addPeerConstraint(MultiPathSupport.class, ADD_PATH_SUPPORT);
65 CONSTRAINT = peerConstraint;
68 protected BGPExtensionProviderContext ctx;
69 private BgpTestActivator activator;
73 this.ctx = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance();
74 this.activator = new BgpTestActivator();
75 this.activator.start(this.ctx);
79 public void tearDown() {
80 this.activator.close();
84 public void testSimpleAttribute() throws BGPDocumentedException, BGPParsingException, BGPTreatAsWithdrawException {
85 final AttributeRegistry attrReg = this.ctx.getAttributeRegistry();
86 final byte[] attributeBytes = {
89 final ByteBuf byteAggregator = Unpooled.buffer(attributeBytes.length);
90 attrReg.serializeAttribute(mock(Attributes.class), byteAggregator);
91 attrReg.parseAttributes(Unpooled.wrappedBuffer(attributeBytes), CONSTRAINT);
92 verify(this.activator.attrParser, times(1)).parseAttribute(any(ByteBuf.class), any(AttributesBuilder.class),
93 any(RevisedErrorHandling.class), any(PeerSpecificParserConstraint.class));
94 verify(this.activator.attrSerializer, times(1)).serializeAttribute(any(Attributes.class), any(ByteBuf.class));
98 public void testSimpleParameter() throws Exception {
99 final ParameterRegistry paramReg = this.ctx.getParameterRegistry();
100 final BgpParameters param = mock(BgpParameters.class);
101 Mockito.doReturn(BgpParameters.class).when(param).getImplementedInterface();
102 final byte[] paramBytes = {
105 final ByteBuf buffer = Unpooled.buffer(paramBytes.length);
106 paramReg.serializeParameter(param, buffer);
107 paramReg.parseParameter(0, Unpooled.wrappedBuffer(paramBytes));
108 verify(this.activator.paramParser, times(1)).parseParameter(any(ByteBuf.class));
109 verify(this.activator.paramSerializer, times(1)).serializeParameter(any(BgpParameters.class),
114 public void testSimpleCapability() throws Exception {
115 final CapabilityRegistry capaRegistry = this.ctx.getCapabilityRegistry();
116 final byte[] capabilityBytes = {
119 capaRegistry.parseCapability(BgpTestActivator.TYPE, Unpooled.wrappedBuffer(capabilityBytes));
120 verify(this.activator.capaParser, times(1)).parseCapability(any(ByteBuf.class));
124 public void testSimpleBgpPrefixSidTlvRegistry() {
125 final BgpPrefixSidTlvRegistry sidTlvReg = this.ctx.getBgpPrefixSidTlvRegistry();
126 final byte[] tlvBytes = {
127 0x00, 0x03, 0x00, 0x00, 0x00
130 final BgpPrefixSidTlv tlv = mock(BgpPrefixSidTlv.class);
131 doReturn(BgpPrefixSidTlv.class).when(tlv).getImplementedInterface();
133 final ByteBuf buffer = Unpooled.buffer(tlvBytes.length);
134 sidTlvReg.serializeBgpPrefixSidTlv(tlv, buffer);
135 verify(this.activator.sidTlvSerializer, times(1)).serializeBgpPrefixSidTlv(any(BgpPrefixSidTlv.class),
138 sidTlvReg.parseBgpPrefixSidTlv(BgpTestActivator.TYPE, Unpooled.wrappedBuffer(tlvBytes));
139 verify(this.activator.sidTlvParser, times(1)).parseBgpPrefixSidTlv(any(ByteBuf.class));
143 public void testSimpleMessageRegistry() throws Exception {
144 final MessageRegistry msgRegistry = this.ctx.getMessageRegistry();
146 final byte[] msgBytes = {
147 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
148 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
149 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
150 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
151 (byte) 0x00, (byte) 0x13, (byte) 0x00
153 final Notification msg = mock(Notification.class);
154 doReturn(Notification.class).when(msg).getImplementedInterface();
156 final ByteBuf buffer = Unpooled.buffer(msgBytes.length);
157 msgRegistry.serializeMessage(msg, buffer);
158 msgRegistry.parseMessage(Unpooled.wrappedBuffer(msgBytes), CONSTRAINT);
159 verify(this.activator.msgParser, times(1)).parseMessageBody(any(ByteBuf.class), Mockito.anyInt(),
160 any(PeerSpecificParserConstraint.class));
161 verify(this.activator.msgSerializer, times(1)).serializeMessage(any(Notification.class), any(ByteBuf.class));
165 public void testAfiRegistry() throws Exception {
166 final AddressFamilyRegistry afiRegistry = this.ctx.getAddressFamilyRegistry();
167 assertEquals(Ipv4AddressFamily.class, afiRegistry.classForFamily(1));
168 assertEquals(1, afiRegistry.numberForClass(Ipv4AddressFamily.class).intValue());
172 public void testSafiRegistry() throws Exception {
173 final SubsequentAddressFamilyRegistry safiRegistry = this.ctx.getSubsequentAddressFamilyRegistry();
174 assertEquals(UnicastSubsequentAddressFamily.class, safiRegistry.classForFamily(1));
175 assertEquals(1, safiRegistry.numberForClass(UnicastSubsequentAddressFamily.class).intValue());
179 public void testMpReachParser() throws BGPParsingException {
180 final NlriRegistry nlriReg = this.ctx.getNlriRegistry();
181 final byte[] mpReachBytes = {
182 0x00, 0x01, 0x01, 0x04, 0x7f, 0x00, 0x00, 0x01, 0x00
184 final MpReachNlri mpReach = new MpReachNlriBuilder()
185 .setAfi(Ipv4AddressFamily.class)
186 .setSafi(UnicastSubsequentAddressFamily.class)
187 .setCNextHop(new Ipv4NextHopCaseBuilder().setIpv4NextHop(new Ipv4NextHopBuilder().setGlobal(
188 new Ipv4Address("127.0.0.1")).build()).build())
190 final ByteBuf buffer = Unpooled.buffer(mpReachBytes.length);
191 nlriReg.serializeMpReach(mpReach, buffer);
192 assertArrayEquals(mpReachBytes, buffer.array());
193 assertEquals(mpReach, nlriReg.parseMpReach(Unpooled.wrappedBuffer(mpReachBytes), CONSTRAINT));
194 verify(this.activator.nlriParser, times(1)).parseNlri(any(ByteBuf.class), any(MpReachNlriBuilder.class), any());
198 public void testMpReachWithZeroNextHop() throws BGPParsingException {
199 final NlriRegistry nlriReg = this.ctx.getNlriRegistry();
200 final byte[] mpReachBytes = {
201 0x00, 0x01, 0x01, 0x00, 0x00
203 final MpReachNlri mpReach = new MpReachNlriBuilder()
204 .setAfi(Ipv4AddressFamily.class)
205 .setSafi(UnicastSubsequentAddressFamily.class)
207 final ByteBuf buffer = Unpooled.buffer(mpReachBytes.length);
208 nlriReg.serializeMpReach(mpReach, buffer);
209 assertArrayEquals(mpReachBytes, buffer.array());
210 assertEquals(mpReach, nlriReg.parseMpReach(Unpooled.wrappedBuffer(mpReachBytes), CONSTRAINT));
214 public void testMpReachIpv6() throws BGPParsingException {
215 final NlriRegistry nlriReg = this.ctx.getNlriRegistry();
216 final byte[] mpReachBytes = {
217 0x00, 0x02, 0x01, 0x00, 0x00
219 final MpReachNlri mpReach = new MpReachNlriBuilder()
220 .setAfi(Ipv6AddressFamily.class)
221 .setSafi(UnicastSubsequentAddressFamily.class)
223 final ByteBuf buffer = Unpooled.buffer(mpReachBytes.length);
224 nlriReg.serializeMpReach(mpReach, buffer);
225 assertArrayEquals(mpReachBytes, buffer.array());
226 assertEquals(mpReach, nlriReg.parseMpReach(Unpooled.wrappedBuffer(mpReachBytes), CONSTRAINT));
230 public void testEOTMpUnReachParser() throws BGPParsingException {
231 final NlriRegistry nlriReg = this.ctx.getNlriRegistry();
232 final byte[] mpUnreachBytes = {
235 final MpUnreachNlri mpUnreach = new MpUnreachNlriBuilder().setAfi(Ipv4AddressFamily.class)
236 .setSafi(UnicastSubsequentAddressFamily.class).build();
237 final ByteBuf buffer = Unpooled.buffer(mpUnreachBytes.length);
238 nlriReg.serializeMpUnReach(mpUnreach, buffer);
239 assertArrayEquals(mpUnreachBytes, buffer.array());
240 assertEquals(mpUnreach, nlriReg.parseMpUnreach(Unpooled.wrappedBuffer(mpUnreachBytes), CONSTRAINT));
241 verify(this.activator.nlriParser, never()).parseNlri(any(ByteBuf.class), any(MpUnreachNlriBuilder.class),