Use OSGi DS in bgp-parser-spi
[bgpcep.git] / bgp / parser-spi / src / test / java / org / opendaylight / protocol / bgp / parser / spi / pojo / SimpleRegistryTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.bgp.parser.spi.pojo;
9
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;
18
19 import io.netty.buffer.ByteBuf;
20 import io.netty.buffer.Unpooled;
21 import java.util.List;
22 import java.util.Optional;
23 import org.junit.After;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.Mockito;
27 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
28 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
29 import org.opendaylight.protocol.bgp.parser.BGPTreatAsWithdrawException;
30 import org.opendaylight.protocol.bgp.parser.spi.AddressFamilyRegistry;
31 import org.opendaylight.protocol.bgp.parser.spi.AttributeRegistry;
32 import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
33 import org.opendaylight.protocol.bgp.parser.spi.BgpPrefixSidTlvRegistry;
34 import org.opendaylight.protocol.bgp.parser.spi.CapabilityRegistry;
35 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
36 import org.opendaylight.protocol.bgp.parser.spi.MultiPathSupport;
37 import org.opendaylight.protocol.bgp.parser.spi.NlriRegistry;
38 import org.opendaylight.protocol.bgp.parser.spi.ParameterRegistry;
39 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
40 import org.opendaylight.protocol.bgp.parser.spi.RevisedErrorHandling;
41 import org.opendaylight.protocol.bgp.parser.spi.SubsequentAddressFamilyRegistry;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.open.message.BgpParameters;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.Attributes;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.AttributesBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.bgp.prefix.sid.bgp.prefix.sid.tlvs.BgpPrefixSidTlv;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlri;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlriBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlri;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlriBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv4AddressFamily;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv6AddressFamily;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.UnicastSubsequentAddressFamily;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.next.hop.c.next.hop.Ipv4NextHopCaseBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.next.hop.c.next.hop.ipv4.next.hop._case.Ipv4NextHopBuilder;
56 import org.opendaylight.yangtools.concepts.Registration;
57 import org.opendaylight.yangtools.yang.binding.Notification;
58
59 public class SimpleRegistryTest {
60
61     private static final MultiPathSupport ADD_PATH_SUPPORT = tableType -> true;
62
63     private static final PeerSpecificParserConstraint CONSTRAINT;
64
65     static {
66         PeerSpecificParserConstraintImpl peerConstraint = new PeerSpecificParserConstraintImpl();
67         peerConstraint.addPeerConstraint(MultiPathSupport.class, ADD_PATH_SUPPORT);
68         CONSTRAINT = peerConstraint;
69     }
70
71     protected BGPExtensionProviderContext ctx;
72
73     private final BgpTestActivator activator = new BgpTestActivator();
74     private List<? extends Registration> regs;
75
76     @Before
77     public void setUp() {
78         this.ctx = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance();
79         regs = this.activator.start(this.ctx);
80     }
81
82     @After
83     public void tearDown() {
84         regs.forEach(Registration::close);
85     }
86
87     @Test
88     public void testSimpleAttribute() throws BGPDocumentedException, BGPParsingException, BGPTreatAsWithdrawException {
89         final AttributeRegistry attrReg = this.ctx.getAttributeRegistry();
90         final byte[] attributeBytes = {
91             0x00, 0x00, 0x00
92         };
93         final ByteBuf byteAggregator = Unpooled.buffer(attributeBytes.length);
94         attrReg.serializeAttribute(mock(Attributes.class), byteAggregator);
95         attrReg.parseAttributes(Unpooled.wrappedBuffer(attributeBytes), CONSTRAINT);
96         verify(this.activator.attrParser, times(1)).parseAttribute(any(ByteBuf.class), any(AttributesBuilder.class),
97             any(RevisedErrorHandling.class), any(PeerSpecificParserConstraint.class));
98         verify(this.activator.attrSerializer, times(1)).serializeAttribute(any(Attributes.class), any(ByteBuf.class));
99     }
100
101     @Test
102     public void testSimpleParameter() throws Exception {
103         final ParameterRegistry paramReg = this.ctx.getParameterRegistry();
104         final BgpParameters param = mock(BgpParameters.class);
105         Mockito.doReturn(BgpParameters.class).when(param).implementedInterface();
106
107         assertEquals(Optional.of(activator.paramParser), paramReg.findParser(0));
108         assertEquals(Optional.of(activator.paramSerializer), paramReg.findSerializer(param));
109     }
110
111     @Test
112     public void testSimpleCapability() throws Exception {
113         final CapabilityRegistry capaRegistry = this.ctx.getCapabilityRegistry();
114         final byte[] capabilityBytes = {
115             0x0, 0x00
116         };
117         capaRegistry.parseCapability(BgpTestActivator.TYPE, Unpooled.wrappedBuffer(capabilityBytes));
118         verify(this.activator.capaParser, times(1)).parseCapability(any(ByteBuf.class));
119     }
120
121     @Test
122     public void testSimpleBgpPrefixSidTlvRegistry() {
123         final BgpPrefixSidTlvRegistry sidTlvReg = this.ctx.getBgpPrefixSidTlvRegistry();
124         final byte[] tlvBytes = {
125             0x00, 0x03, 0x00, 0x00, 0x00
126         };
127
128         final BgpPrefixSidTlv tlv = mock(BgpPrefixSidTlv.class);
129         doReturn(BgpPrefixSidTlv.class).when(tlv).implementedInterface();
130
131         final ByteBuf buffer = Unpooled.buffer(tlvBytes.length);
132         sidTlvReg.serializeBgpPrefixSidTlv(tlv, buffer);
133         verify(this.activator.sidTlvSerializer, times(1)).serializeBgpPrefixSidTlv(any(BgpPrefixSidTlv.class),
134             any(ByteBuf.class));
135
136         sidTlvReg.parseBgpPrefixSidTlv(BgpTestActivator.TYPE, Unpooled.wrappedBuffer(tlvBytes));
137         verify(this.activator.sidTlvParser, times(1)).parseBgpPrefixSidTlv(any(ByteBuf.class));
138     }
139
140     @Test
141     public void testSimpleMessageRegistry() throws Exception {
142         final MessageRegistry msgRegistry = this.ctx.getMessageRegistry();
143
144         final byte[] msgBytes = {
145             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
146             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
147             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
148             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
149             (byte) 0x00, (byte) 0x13, (byte) 0x00
150         };
151         final Notification msg = mock(Notification.class);
152         doReturn(Notification.class).when(msg).implementedInterface();
153
154         final ByteBuf buffer = Unpooled.buffer(msgBytes.length);
155         msgRegistry.serializeMessage(msg, buffer);
156         msgRegistry.parseMessage(Unpooled.wrappedBuffer(msgBytes), CONSTRAINT);
157         verify(this.activator.msgParser, times(1)).parseMessageBody(any(ByteBuf.class), Mockito.anyInt(),
158             any(PeerSpecificParserConstraint.class));
159         verify(this.activator.msgSerializer, times(1)).serializeMessage(any(Notification.class), any(ByteBuf.class));
160     }
161
162     @Test
163     public void testAfiRegistry() throws Exception {
164         final AddressFamilyRegistry afiRegistry = this.ctx.getAddressFamilyRegistry();
165         assertEquals(Ipv4AddressFamily.class, afiRegistry.classForFamily(1));
166         assertEquals(1, afiRegistry.numberForClass(Ipv4AddressFamily.class).intValue());
167     }
168
169     @Test
170     public void testSafiRegistry() throws Exception {
171         final SubsequentAddressFamilyRegistry safiRegistry = this.ctx.getSubsequentAddressFamilyRegistry();
172         assertEquals(UnicastSubsequentAddressFamily.class, safiRegistry.classForFamily(1));
173         assertEquals(1, safiRegistry.numberForClass(UnicastSubsequentAddressFamily.class).intValue());
174     }
175
176     @Test
177     public void testMpReachParser() throws BGPParsingException {
178         final NlriRegistry nlriReg = this.ctx.getNlriRegistry();
179         final byte[] mpReachBytes = {
180             0x00, 0x01, 0x01, 0x04, 0x7f, 0x00, 0x00, 0x01, 0x00
181         };
182         final MpReachNlri mpReach = new MpReachNlriBuilder()
183             .setAfi(Ipv4AddressFamily.class)
184             .setSafi(UnicastSubsequentAddressFamily.class)
185             .setCNextHop(new Ipv4NextHopCaseBuilder().setIpv4NextHop(new Ipv4NextHopBuilder().setGlobal(
186                 new Ipv4AddressNoZone("127.0.0.1")).build()).build())
187             .build();
188         final ByteBuf buffer = Unpooled.buffer(mpReachBytes.length);
189         nlriReg.serializeMpReach(mpReach, buffer);
190         assertArrayEquals(mpReachBytes, buffer.array());
191         assertEquals(mpReach, nlriReg.parseMpReach(Unpooled.wrappedBuffer(mpReachBytes), CONSTRAINT));
192         verify(this.activator.nlriParser, times(1)).parseNlri(any(ByteBuf.class), any(MpReachNlriBuilder.class), any());
193     }
194
195     @Test
196     public void testMpReachWithZeroNextHop() throws BGPParsingException {
197         final NlriRegistry nlriReg = this.ctx.getNlriRegistry();
198         final byte[] mpReachBytes = {
199             0x00, 0x01, 0x01, 0x00, 0x00
200         };
201         final MpReachNlri mpReach = new MpReachNlriBuilder()
202             .setAfi(Ipv4AddressFamily.class)
203             .setSafi(UnicastSubsequentAddressFamily.class)
204             .build();
205         final ByteBuf buffer = Unpooled.buffer(mpReachBytes.length);
206         nlriReg.serializeMpReach(mpReach, buffer);
207         assertArrayEquals(mpReachBytes, buffer.array());
208         assertEquals(mpReach, nlriReg.parseMpReach(Unpooled.wrappedBuffer(mpReachBytes), CONSTRAINT));
209     }
210
211     @Test
212     public void testMpReachIpv6() throws BGPParsingException {
213         final NlriRegistry nlriReg = this.ctx.getNlriRegistry();
214         final byte[] mpReachBytes = {
215             0x00, 0x02, 0x01, 0x00, 0x00
216         };
217         final MpReachNlri mpReach = new MpReachNlriBuilder()
218             .setAfi(Ipv6AddressFamily.class)
219             .setSafi(UnicastSubsequentAddressFamily.class)
220             .build();
221         final ByteBuf buffer = Unpooled.buffer(mpReachBytes.length);
222         nlriReg.serializeMpReach(mpReach, buffer);
223         assertArrayEquals(mpReachBytes, buffer.array());
224         assertEquals(mpReach, nlriReg.parseMpReach(Unpooled.wrappedBuffer(mpReachBytes), CONSTRAINT));
225     }
226
227     @Test
228     public void testEOTMpUnReachParser() throws BGPParsingException {
229         final NlriRegistry nlriReg = this.ctx.getNlriRegistry();
230         final byte[] mpUnreachBytes = {
231             0x00, 0x01, 0x01
232         };
233         final MpUnreachNlri mpUnreach = new MpUnreachNlriBuilder().setAfi(Ipv4AddressFamily.class)
234                 .setSafi(UnicastSubsequentAddressFamily.class).build();
235         final ByteBuf buffer = Unpooled.buffer(mpUnreachBytes.length);
236         nlriReg.serializeMpUnReach(mpUnreach, buffer);
237         assertArrayEquals(mpUnreachBytes, buffer.array());
238         assertEquals(mpUnreach, nlriReg.parseMpUnreach(Unpooled.wrappedBuffer(mpUnreachBytes), CONSTRAINT));
239         verify(this.activator.nlriParser, never()).parseNlri(any(ByteBuf.class), any(MpUnreachNlriBuilder.class),
240             any());
241     }
242 }