Bump MDSAL to 4.0.0
[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 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;
55
56 public class SimpleRegistryTest {
57
58     private static final MultiPathSupport ADD_PATH_SUPPORT = tableType -> true;
59
60     private static final PeerSpecificParserConstraint CONSTRAINT;
61
62     static {
63         PeerSpecificParserConstraintImpl peerConstraint = new PeerSpecificParserConstraintImpl();
64         peerConstraint.addPeerConstraint(MultiPathSupport.class, ADD_PATH_SUPPORT);
65         CONSTRAINT = peerConstraint;
66     }
67
68     protected BGPExtensionProviderContext ctx;
69     private BgpTestActivator activator;
70
71     @Before
72     public void setUp() {
73         this.ctx = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance();
74         this.activator = new BgpTestActivator();
75         this.activator.start(this.ctx);
76     }
77
78     @After
79     public void tearDown() {
80         this.activator.close();
81     }
82
83     @Test
84     public void testSimpleAttribute() throws BGPDocumentedException, BGPParsingException, BGPTreatAsWithdrawException {
85         final AttributeRegistry attrReg = this.ctx.getAttributeRegistry();
86         final byte[] attributeBytes = {
87             0x00, 0x00, 0x00
88         };
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));
95     }
96
97     @Test
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).implementedInterface();
102         final byte[] paramBytes = {
103             0x00, 0x00
104         };
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),
110             any(ByteBuf.class));
111     }
112
113     @Test
114     public void testSimpleCapability() throws Exception {
115         final CapabilityRegistry capaRegistry = this.ctx.getCapabilityRegistry();
116         final byte[] capabilityBytes = {
117             0x0, 0x00
118         };
119         capaRegistry.parseCapability(BgpTestActivator.TYPE, Unpooled.wrappedBuffer(capabilityBytes));
120         verify(this.activator.capaParser, times(1)).parseCapability(any(ByteBuf.class));
121     }
122
123     @Test
124     public void testSimpleBgpPrefixSidTlvRegistry() {
125         final BgpPrefixSidTlvRegistry sidTlvReg = this.ctx.getBgpPrefixSidTlvRegistry();
126         final byte[] tlvBytes = {
127             0x00, 0x03, 0x00, 0x00, 0x00
128         };
129
130         final BgpPrefixSidTlv tlv = mock(BgpPrefixSidTlv.class);
131         doReturn(BgpPrefixSidTlv.class).when(tlv).implementedInterface();
132
133         final ByteBuf buffer = Unpooled.buffer(tlvBytes.length);
134         sidTlvReg.serializeBgpPrefixSidTlv(tlv, buffer);
135         verify(this.activator.sidTlvSerializer, times(1)).serializeBgpPrefixSidTlv(any(BgpPrefixSidTlv.class),
136             any(ByteBuf.class));
137
138         sidTlvReg.parseBgpPrefixSidTlv(BgpTestActivator.TYPE, Unpooled.wrappedBuffer(tlvBytes));
139         verify(this.activator.sidTlvParser, times(1)).parseBgpPrefixSidTlv(any(ByteBuf.class));
140     }
141
142     @Test
143     public void testSimpleMessageRegistry() throws Exception {
144         final MessageRegistry msgRegistry = this.ctx.getMessageRegistry();
145
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
152         };
153         final Notification msg = mock(Notification.class);
154         doReturn(Notification.class).when(msg).implementedInterface();
155
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));
162     }
163
164     @Test
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());
169     }
170
171     @Test
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());
176     }
177
178     @Test
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
183         };
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())
189             .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());
195     }
196
197     @Test
198     public void testMpReachWithZeroNextHop() throws BGPParsingException {
199         final NlriRegistry nlriReg = this.ctx.getNlriRegistry();
200         final byte[] mpReachBytes = {
201             0x00, 0x01, 0x01, 0x00, 0x00
202         };
203         final MpReachNlri mpReach = new MpReachNlriBuilder()
204             .setAfi(Ipv4AddressFamily.class)
205             .setSafi(UnicastSubsequentAddressFamily.class)
206             .build();
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));
211     }
212
213     @Test
214     public void testMpReachIpv6() throws BGPParsingException {
215         final NlriRegistry nlriReg = this.ctx.getNlriRegistry();
216         final byte[] mpReachBytes = {
217             0x00, 0x02, 0x01, 0x00, 0x00
218         };
219         final MpReachNlri mpReach = new MpReachNlriBuilder()
220             .setAfi(Ipv6AddressFamily.class)
221             .setSafi(UnicastSubsequentAddressFamily.class)
222             .build();
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));
227     }
228
229     @Test
230     public void testEOTMpUnReachParser() throws BGPParsingException {
231         final NlriRegistry nlriReg = this.ctx.getNlriRegistry();
232         final byte[] mpUnreachBytes = {
233             0x00, 0x01, 0x01
234         };
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),
242             any());
243     }
244 }