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 java.util.Optional;
22 import org.junit.After;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.Mockito;
26 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
27 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
28 import org.opendaylight.protocol.bgp.parser.BGPTreatAsWithdrawException;
29 import org.opendaylight.protocol.bgp.parser.spi.AddressFamilyRegistry;
30 import org.opendaylight.protocol.bgp.parser.spi.AttributeRegistry;
31 import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
32 import org.opendaylight.protocol.bgp.parser.spi.BgpPrefixSidTlvRegistry;
33 import org.opendaylight.protocol.bgp.parser.spi.CapabilityRegistry;
34 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
35 import org.opendaylight.protocol.bgp.parser.spi.MultiPathSupport;
36 import org.opendaylight.protocol.bgp.parser.spi.NlriRegistry;
37 import org.opendaylight.protocol.bgp.parser.spi.ParameterRegistry;
38 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
39 import org.opendaylight.protocol.bgp.parser.spi.RevisedErrorHandling;
40 import org.opendaylight.protocol.bgp.parser.spi.SubsequentAddressFamilyRegistry;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.BgpParameters;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
45 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;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlri;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlriBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlri;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlriBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv4AddressFamily;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv6AddressFamily;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.UnicastSubsequentAddressFamily;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.Ipv4NextHopCaseBuilder;
54 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;
55 import org.opendaylight.yangtools.yang.binding.Notification;
57 public class SimpleRegistryTest {
59 private static final MultiPathSupport ADD_PATH_SUPPORT = tableType -> true;
61 private static final PeerSpecificParserConstraint CONSTRAINT;
64 PeerSpecificParserConstraintImpl peerConstraint = new PeerSpecificParserConstraintImpl();
65 peerConstraint.addPeerConstraint(MultiPathSupport.class, ADD_PATH_SUPPORT);
66 CONSTRAINT = peerConstraint;
69 protected BGPExtensionProviderContext ctx;
70 private BgpTestActivator activator;
74 this.ctx = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance();
75 this.activator = new BgpTestActivator();
76 this.activator.start(this.ctx);
80 public void tearDown() {
81 this.activator.close();
85 public void testSimpleAttribute() throws BGPDocumentedException, BGPParsingException, BGPTreatAsWithdrawException {
86 final AttributeRegistry attrReg = this.ctx.getAttributeRegistry();
87 final byte[] attributeBytes = {
90 final ByteBuf byteAggregator = Unpooled.buffer(attributeBytes.length);
91 attrReg.serializeAttribute(mock(Attributes.class), byteAggregator);
92 attrReg.parseAttributes(Unpooled.wrappedBuffer(attributeBytes), CONSTRAINT);
93 verify(this.activator.attrParser, times(1)).parseAttribute(any(ByteBuf.class), any(AttributesBuilder.class),
94 any(RevisedErrorHandling.class), any(PeerSpecificParserConstraint.class));
95 verify(this.activator.attrSerializer, times(1)).serializeAttribute(any(Attributes.class), any(ByteBuf.class));
99 public void testSimpleParameter() throws Exception {
100 final ParameterRegistry paramReg = this.ctx.getParameterRegistry();
101 final BgpParameters param = mock(BgpParameters.class);
102 Mockito.doReturn(BgpParameters.class).when(param).implementedInterface();
104 assertEquals(Optional.of(activator.paramParser), paramReg.findParser(0));
105 assertEquals(Optional.of(activator.paramSerializer), paramReg.findSerializer(param));
109 public void testSimpleCapability() throws Exception {
110 final CapabilityRegistry capaRegistry = this.ctx.getCapabilityRegistry();
111 final byte[] capabilityBytes = {
114 capaRegistry.parseCapability(BgpTestActivator.TYPE, Unpooled.wrappedBuffer(capabilityBytes));
115 verify(this.activator.capaParser, times(1)).parseCapability(any(ByteBuf.class));
119 public void testSimpleBgpPrefixSidTlvRegistry() {
120 final BgpPrefixSidTlvRegistry sidTlvReg = this.ctx.getBgpPrefixSidTlvRegistry();
121 final byte[] tlvBytes = {
122 0x00, 0x03, 0x00, 0x00, 0x00
125 final BgpPrefixSidTlv tlv = mock(BgpPrefixSidTlv.class);
126 doReturn(BgpPrefixSidTlv.class).when(tlv).implementedInterface();
128 final ByteBuf buffer = Unpooled.buffer(tlvBytes.length);
129 sidTlvReg.serializeBgpPrefixSidTlv(tlv, buffer);
130 verify(this.activator.sidTlvSerializer, times(1)).serializeBgpPrefixSidTlv(any(BgpPrefixSidTlv.class),
133 sidTlvReg.parseBgpPrefixSidTlv(BgpTestActivator.TYPE, Unpooled.wrappedBuffer(tlvBytes));
134 verify(this.activator.sidTlvParser, times(1)).parseBgpPrefixSidTlv(any(ByteBuf.class));
138 public void testSimpleMessageRegistry() throws Exception {
139 final MessageRegistry msgRegistry = this.ctx.getMessageRegistry();
141 final byte[] msgBytes = {
142 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
143 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
144 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
145 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
146 (byte) 0x00, (byte) 0x13, (byte) 0x00
148 final Notification msg = mock(Notification.class);
149 doReturn(Notification.class).when(msg).implementedInterface();
151 final ByteBuf buffer = Unpooled.buffer(msgBytes.length);
152 msgRegistry.serializeMessage(msg, buffer);
153 msgRegistry.parseMessage(Unpooled.wrappedBuffer(msgBytes), CONSTRAINT);
154 verify(this.activator.msgParser, times(1)).parseMessageBody(any(ByteBuf.class), Mockito.anyInt(),
155 any(PeerSpecificParserConstraint.class));
156 verify(this.activator.msgSerializer, times(1)).serializeMessage(any(Notification.class), any(ByteBuf.class));
160 public void testAfiRegistry() throws Exception {
161 final AddressFamilyRegistry afiRegistry = this.ctx.getAddressFamilyRegistry();
162 assertEquals(Ipv4AddressFamily.class, afiRegistry.classForFamily(1));
163 assertEquals(1, afiRegistry.numberForClass(Ipv4AddressFamily.class).intValue());
167 public void testSafiRegistry() throws Exception {
168 final SubsequentAddressFamilyRegistry safiRegistry = this.ctx.getSubsequentAddressFamilyRegistry();
169 assertEquals(UnicastSubsequentAddressFamily.class, safiRegistry.classForFamily(1));
170 assertEquals(1, safiRegistry.numberForClass(UnicastSubsequentAddressFamily.class).intValue());
174 public void testMpReachParser() throws BGPParsingException {
175 final NlriRegistry nlriReg = this.ctx.getNlriRegistry();
176 final byte[] mpReachBytes = {
177 0x00, 0x01, 0x01, 0x04, 0x7f, 0x00, 0x00, 0x01, 0x00
179 final MpReachNlri mpReach = new MpReachNlriBuilder()
180 .setAfi(Ipv4AddressFamily.class)
181 .setSafi(UnicastSubsequentAddressFamily.class)
182 .setCNextHop(new Ipv4NextHopCaseBuilder().setIpv4NextHop(new Ipv4NextHopBuilder().setGlobal(
183 new Ipv4Address("127.0.0.1")).build()).build())
185 final ByteBuf buffer = Unpooled.buffer(mpReachBytes.length);
186 nlriReg.serializeMpReach(mpReach, buffer);
187 assertArrayEquals(mpReachBytes, buffer.array());
188 assertEquals(mpReach, nlriReg.parseMpReach(Unpooled.wrappedBuffer(mpReachBytes), CONSTRAINT));
189 verify(this.activator.nlriParser, times(1)).parseNlri(any(ByteBuf.class), any(MpReachNlriBuilder.class), any());
193 public void testMpReachWithZeroNextHop() throws BGPParsingException {
194 final NlriRegistry nlriReg = this.ctx.getNlriRegistry();
195 final byte[] mpReachBytes = {
196 0x00, 0x01, 0x01, 0x00, 0x00
198 final MpReachNlri mpReach = new MpReachNlriBuilder()
199 .setAfi(Ipv4AddressFamily.class)
200 .setSafi(UnicastSubsequentAddressFamily.class)
202 final ByteBuf buffer = Unpooled.buffer(mpReachBytes.length);
203 nlriReg.serializeMpReach(mpReach, buffer);
204 assertArrayEquals(mpReachBytes, buffer.array());
205 assertEquals(mpReach, nlriReg.parseMpReach(Unpooled.wrappedBuffer(mpReachBytes), CONSTRAINT));
209 public void testMpReachIpv6() throws BGPParsingException {
210 final NlriRegistry nlriReg = this.ctx.getNlriRegistry();
211 final byte[] mpReachBytes = {
212 0x00, 0x02, 0x01, 0x00, 0x00
214 final MpReachNlri mpReach = new MpReachNlriBuilder()
215 .setAfi(Ipv6AddressFamily.class)
216 .setSafi(UnicastSubsequentAddressFamily.class)
218 final ByteBuf buffer = Unpooled.buffer(mpReachBytes.length);
219 nlriReg.serializeMpReach(mpReach, buffer);
220 assertArrayEquals(mpReachBytes, buffer.array());
221 assertEquals(mpReach, nlriReg.parseMpReach(Unpooled.wrappedBuffer(mpReachBytes), CONSTRAINT));
225 public void testEOTMpUnReachParser() throws BGPParsingException {
226 final NlriRegistry nlriReg = this.ctx.getNlriRegistry();
227 final byte[] mpUnreachBytes = {
230 final MpUnreachNlri mpUnreach = new MpUnreachNlriBuilder().setAfi(Ipv4AddressFamily.class)
231 .setSafi(UnicastSubsequentAddressFamily.class).build();
232 final ByteBuf buffer = Unpooled.buffer(mpUnreachBytes.length);
233 nlriReg.serializeMpUnReach(mpUnreach, buffer);
234 assertArrayEquals(mpUnreachBytes, buffer.array());
235 assertEquals(mpUnreach, nlriReg.parseMpUnreach(Unpooled.wrappedBuffer(mpUnreachBytes), CONSTRAINT));
236 verify(this.activator.nlriParser, never()).parseNlri(any(ByteBuf.class), any(MpUnreachNlriBuilder.class),