Improve Attribute serializer/parser Interfaces
[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
9 package org.opendaylight.protocol.bgp.parser.spi.pojo;
10
11 import static org.junit.Assert.assertArrayEquals;
12 import static org.junit.Assert.assertEquals;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.times;
17 import static org.mockito.Mockito.verify;
18 import io.netty.buffer.ByteBuf;
19 import io.netty.buffer.Unpooled;
20 import java.util.Optional;
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.spi.AddressFamilyRegistry;
28 import org.opendaylight.protocol.bgp.parser.spi.AttributeRegistry;
29 import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
30 import org.opendaylight.protocol.bgp.parser.spi.BgpPrefixSidTlvRegistry;
31 import org.opendaylight.protocol.bgp.parser.spi.CapabilityRegistry;
32 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
33 import org.opendaylight.protocol.bgp.parser.spi.MultiPathSupport;
34 import org.opendaylight.protocol.bgp.parser.spi.NlriRegistry;
35 import org.opendaylight.protocol.bgp.parser.spi.ParameterRegistry;
36 import org.opendaylight.protocol.bgp.parser.spi.PeerConstraint;
37 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
38 import org.opendaylight.protocol.bgp.parser.spi.SubsequentAddressFamilyRegistry;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.BgpParameters;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
43 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;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlri;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlriBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlri;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlriBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv4AddressFamily;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv6AddressFamily;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.UnicastSubsequentAddressFamily;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.Ipv4NextHopCaseBuilder;
52 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;
53 import org.opendaylight.yangtools.yang.binding.DataObject;
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 = new PeerSpecificParserConstraint() {
61         @Override
62         public <T extends PeerConstraint> Optional<T> getPeerConstraint(final Class<T> peerConstraintType) {
63             return (Optional<T>) Optional.of(ADD_PATH_SUPPORT);
64         }
65     };
66
67     protected BGPExtensionProviderContext ctx;
68     private BgpTestActivator activator;
69
70     @Before
71     public void setUp() {
72         this.ctx = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance();
73         this.activator = new BgpTestActivator();
74         this.activator.start(this.ctx);
75     }
76
77     @After
78     public void tearDown() {
79         this.activator.close();
80     }
81
82     @Test
83     public void testSimpleAttribute() throws BGPDocumentedException, BGPParsingException {
84         final AttributeRegistry attrReg = this.ctx.getAttributeRegistry();
85         final byte[] attributeBytes = {
86             0x00, 0x00, 0x00
87         };
88         final ByteBuf byteAggregator = Unpooled.buffer(attributeBytes.length);
89         attrReg.serializeAttribute(mock(Attributes.class), byteAggregator);
90         attrReg.parseAttributes(Unpooled.wrappedBuffer(attributeBytes), CONSTRAINT);
91         verify(this.activator.attrParser, times(1)).parseAttribute(any(ByteBuf.class), any(AttributesBuilder.class),
92             any(PeerSpecificParserConstraint.class));
93         verify(this.activator.attrSerializer, times(1)).serializeAttribute(any(Attributes.class), any(ByteBuf.class));
94     }
95
96     @Test
97     public void testSimpleParameter() throws Exception {
98         final ParameterRegistry paramReg = this.ctx.getParameterRegistry();
99         final BgpParameters param = mock(BgpParameters.class);
100         Mockito.doReturn(BgpParameters.class).when(param).getImplementedInterface();
101         final byte[] paramBytes = {
102             0x00, 0x00
103         };
104         final ByteBuf buffer = Unpooled.buffer(paramBytes.length);
105         paramReg.serializeParameter(param, buffer);
106         paramReg.parseParameter(0, Unpooled.wrappedBuffer(paramBytes));
107         verify(this.activator.paramParser, times(1)).parseParameter(any(ByteBuf.class));
108         verify(this.activator.paramSerializer, times(1)).serializeParameter(any(BgpParameters.class), any(ByteBuf.class));
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).getImplementedInterface();
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), any(ByteBuf.class));
134
135         sidTlvReg.parseBgpPrefixSidTlv(BgpTestActivator.TYPE, Unpooled.wrappedBuffer(tlvBytes));
136         verify(this.activator.sidTlvParser, times(1)).parseBgpPrefixSidTlv(any(ByteBuf.class));
137     }
138
139     @Test
140     public void testSimpleMessageRegistry() throws Exception {
141         final MessageRegistry msgRegistry = this.ctx.getMessageRegistry();
142
143         final byte[] msgBytes = {
144             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
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) 0x00, (byte) 0x13, (byte) 0x00
149         };
150         final Notification msg = mock(Notification.class);
151         doReturn(Notification.class).when(msg).getImplementedInterface();
152
153         final ByteBuf buffer = Unpooled.buffer(msgBytes.length);
154         msgRegistry.serializeMessage(msg, buffer);
155         msgRegistry.parseMessage(Unpooled.wrappedBuffer(msgBytes), CONSTRAINT);
156         verify(this.activator.msgParser, times(1)).parseMessageBody(any(ByteBuf.class), Mockito.anyInt(),
157             any(PeerSpecificParserConstraint.class));
158         verify(this.activator.msgSerializer, times(1)).serializeMessage(any(Notification.class), any(ByteBuf.class));
159     }
160
161     @Test
162     public void testAfiRegistry() throws Exception {
163         final AddressFamilyRegistry afiRegistry = this.ctx.getAddressFamilyRegistry();
164         assertEquals(Ipv4AddressFamily.class, afiRegistry.classForFamily(1));
165         assertEquals(1, afiRegistry.numberForClass(Ipv4AddressFamily.class).intValue());
166     }
167
168     @Test
169     public void testSafiRegistry() throws Exception {
170         final SubsequentAddressFamilyRegistry safiRegistry = this.ctx.getSubsequentAddressFamilyRegistry();
171         assertEquals(UnicastSubsequentAddressFamily.class, safiRegistry.classForFamily(1));
172         assertEquals(1, safiRegistry.numberForClass(UnicastSubsequentAddressFamily.class).intValue());
173     }
174
175     @Test
176     public void testMpReachParser() throws BGPParsingException {
177         final NlriRegistry nlriReg = this.ctx.getNlriRegistry();
178         final byte[] mpReachBytes = {
179             0x00, 0x01, 0x01, 0x04, 0x7f, 0x00, 0x00, 0x01, 0x00
180         };
181         final MpReachNlri mpReach = new MpReachNlriBuilder()
182             .setAfi(Ipv4AddressFamily.class)
183             .setSafi(UnicastSubsequentAddressFamily.class)
184             .setCNextHop(new Ipv4NextHopCaseBuilder().setIpv4NextHop(new Ipv4NextHopBuilder().setGlobal(new Ipv4Address("127.0.0.1")).build()).build())
185             .build();
186         final ByteBuf buffer = Unpooled.buffer(mpReachBytes.length);
187         nlriReg.serializeMpReach(mpReach, buffer);
188         assertArrayEquals(mpReachBytes, buffer.array());
189         assertEquals(mpReach, nlriReg.parseMpReach(Unpooled.wrappedBuffer(mpReachBytes), CONSTRAINT));
190         verify(this.activator.nlriParser, times(1)).parseNlri(any(ByteBuf.class), any(MpReachNlriBuilder.class), any());
191     }
192
193     @Test
194     public void testMpReachWithZeroNextHop() throws BGPParsingException {
195         final NlriRegistry nlriReg = this.ctx.getNlriRegistry();
196         final byte[] mpReachBytes = {
197             0x00, 0x01, 0x01, 0x00, 0x00
198         };
199         final MpReachNlri mpReach = new MpReachNlriBuilder()
200             .setAfi(Ipv4AddressFamily.class)
201             .setSafi(UnicastSubsequentAddressFamily.class)
202             .build();
203         final ByteBuf buffer = Unpooled.buffer(mpReachBytes.length);
204         nlriReg.serializeMpReach(mpReach, buffer);
205         assertArrayEquals(mpReachBytes, buffer.array());
206         assertEquals(mpReach, nlriReg.parseMpReach(Unpooled.wrappedBuffer(mpReachBytes), CONSTRAINT));
207     }
208
209     @Test
210     public void testMpReachIpv6() throws BGPParsingException {
211         final NlriRegistry nlriReg = this.ctx.getNlriRegistry();
212         final byte[] mpReachBytes = {
213             0x00, 0x02, 0x01, 0x00, 0x00
214         };
215         final MpReachNlri mpReach = new MpReachNlriBuilder()
216             .setAfi(Ipv6AddressFamily.class)
217             .setSafi(UnicastSubsequentAddressFamily.class)
218             .build();
219         final ByteBuf buffer = Unpooled.buffer(mpReachBytes.length);
220         nlriReg.serializeMpReach(mpReach, buffer);
221         assertArrayEquals(mpReachBytes, buffer.array());
222         assertEquals(mpReach, nlriReg.parseMpReach(Unpooled.wrappedBuffer(mpReachBytes), CONSTRAINT));
223     }
224
225     @Test
226     public void testMpUnReachParser() throws BGPParsingException {
227         final NlriRegistry nlriReg = this.ctx.getNlriRegistry();
228         final byte[] mpUnreachBytes = {
229             0x00, 0x01, 0x01
230         };
231         final MpUnreachNlri mpUnreach = new MpUnreachNlriBuilder().setAfi(Ipv4AddressFamily.class).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, times(1)).parseNlri(any(ByteBuf.class), any(MpUnreachNlriBuilder.class), any());
237     }
238 }