YANG revision dates mass-update
[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.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.Ipv4AddressNoZone;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.open.message.BgpParameters;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.Attributes;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.AttributesBuilder;
45 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;
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.rev200120.Ipv4AddressFamily;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv6AddressFamily;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.UnicastSubsequentAddressFamily;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.next.hop.c.next.hop.Ipv4NextHopCaseBuilder;
54 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;
55 import org.opendaylight.yangtools.yang.binding.Notification;
56
57 public class SimpleRegistryTest {
58
59     private static final MultiPathSupport ADD_PATH_SUPPORT = tableType -> true;
60
61     private static final PeerSpecificParserConstraint CONSTRAINT;
62
63     static {
64         PeerSpecificParserConstraintImpl peerConstraint = new PeerSpecificParserConstraintImpl();
65         peerConstraint.addPeerConstraint(MultiPathSupport.class, ADD_PATH_SUPPORT);
66         CONSTRAINT = peerConstraint;
67     }
68
69     protected BGPExtensionProviderContext ctx;
70     private BgpTestActivator activator;
71
72     @Before
73     public void setUp() {
74         this.ctx = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance();
75         this.activator = new BgpTestActivator();
76         this.activator.start(this.ctx);
77     }
78
79     @After
80     public void tearDown() {
81         this.activator.close();
82     }
83
84     @Test
85     public void testSimpleAttribute() throws BGPDocumentedException, BGPParsingException, BGPTreatAsWithdrawException {
86         final AttributeRegistry attrReg = this.ctx.getAttributeRegistry();
87         final byte[] attributeBytes = {
88             0x00, 0x00, 0x00
89         };
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));
96     }
97
98     @Test
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();
103
104         assertEquals(Optional.of(activator.paramParser), paramReg.findParser(0));
105         assertEquals(Optional.of(activator.paramSerializer), paramReg.findSerializer(param));
106     }
107
108     @Test
109     public void testSimpleCapability() throws Exception {
110         final CapabilityRegistry capaRegistry = this.ctx.getCapabilityRegistry();
111         final byte[] capabilityBytes = {
112             0x0, 0x00
113         };
114         capaRegistry.parseCapability(BgpTestActivator.TYPE, Unpooled.wrappedBuffer(capabilityBytes));
115         verify(this.activator.capaParser, times(1)).parseCapability(any(ByteBuf.class));
116     }
117
118     @Test
119     public void testSimpleBgpPrefixSidTlvRegistry() {
120         final BgpPrefixSidTlvRegistry sidTlvReg = this.ctx.getBgpPrefixSidTlvRegistry();
121         final byte[] tlvBytes = {
122             0x00, 0x03, 0x00, 0x00, 0x00
123         };
124
125         final BgpPrefixSidTlv tlv = mock(BgpPrefixSidTlv.class);
126         doReturn(BgpPrefixSidTlv.class).when(tlv).implementedInterface();
127
128         final ByteBuf buffer = Unpooled.buffer(tlvBytes.length);
129         sidTlvReg.serializeBgpPrefixSidTlv(tlv, buffer);
130         verify(this.activator.sidTlvSerializer, times(1)).serializeBgpPrefixSidTlv(any(BgpPrefixSidTlv.class),
131             any(ByteBuf.class));
132
133         sidTlvReg.parseBgpPrefixSidTlv(BgpTestActivator.TYPE, Unpooled.wrappedBuffer(tlvBytes));
134         verify(this.activator.sidTlvParser, times(1)).parseBgpPrefixSidTlv(any(ByteBuf.class));
135     }
136
137     @Test
138     public void testSimpleMessageRegistry() throws Exception {
139         final MessageRegistry msgRegistry = this.ctx.getMessageRegistry();
140
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
147         };
148         final Notification msg = mock(Notification.class);
149         doReturn(Notification.class).when(msg).implementedInterface();
150
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));
157     }
158
159     @Test
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());
164     }
165
166     @Test
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());
171     }
172
173     @Test
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
178         };
179         final MpReachNlri mpReach = new MpReachNlriBuilder()
180             .setAfi(Ipv4AddressFamily.class)
181             .setSafi(UnicastSubsequentAddressFamily.class)
182             .setCNextHop(new Ipv4NextHopCaseBuilder().setIpv4NextHop(new Ipv4NextHopBuilder().setGlobal(
183                 new Ipv4AddressNoZone("127.0.0.1")).build()).build())
184             .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());
190     }
191
192     @Test
193     public void testMpReachWithZeroNextHop() throws BGPParsingException {
194         final NlriRegistry nlriReg = this.ctx.getNlriRegistry();
195         final byte[] mpReachBytes = {
196             0x00, 0x01, 0x01, 0x00, 0x00
197         };
198         final MpReachNlri mpReach = new MpReachNlriBuilder()
199             .setAfi(Ipv4AddressFamily.class)
200             .setSafi(UnicastSubsequentAddressFamily.class)
201             .build();
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));
206     }
207
208     @Test
209     public void testMpReachIpv6() throws BGPParsingException {
210         final NlriRegistry nlriReg = this.ctx.getNlriRegistry();
211         final byte[] mpReachBytes = {
212             0x00, 0x02, 0x01, 0x00, 0x00
213         };
214         final MpReachNlri mpReach = new MpReachNlriBuilder()
215             .setAfi(Ipv6AddressFamily.class)
216             .setSafi(UnicastSubsequentAddressFamily.class)
217             .build();
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));
222     }
223
224     @Test
225     public void testEOTMpUnReachParser() throws BGPParsingException {
226         final NlriRegistry nlriReg = this.ctx.getNlriRegistry();
227         final byte[] mpUnreachBytes = {
228             0x00, 0x01, 0x01
229         };
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),
237             any());
238     }
239 }