Fix intermittent failures in LlGracefulCapabilityHandlerTest
[bgpcep.git] / bgp / parser-impl / src / test / java / org / opendaylight / protocol / bgp / parser / impl / LlGracefulCapabilityHandlerTest.java
1 /*
2  * Copyright (c) 2018 AT&T Intellectual Property. 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.impl;
9
10 import io.netty.buffer.ByteBuf;
11 import io.netty.buffer.Unpooled;
12 import org.junit.Assert;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.mockito.Mock;
16 import org.mockito.Mockito;
17 import org.mockito.MockitoAnnotations;
18 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
19 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
20 import org.opendaylight.protocol.bgp.parser.impl.message.open.LlGracefulCapabilityHandler;
21 import org.opendaylight.protocol.bgp.parser.spi.AddressFamilyRegistry;
22 import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
23 import org.opendaylight.protocol.bgp.parser.spi.SubsequentAddressFamilyRegistry;
24 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.routing.types.rev171204.Uint24;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.open.message.bgp.parameters.optional.capabilities.CParameters;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.open.message.bgp.parameters.optional.capabilities.CParametersBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.CParameters1Builder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.mp.capabilities.LlGracefulRestartCapability;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.mp.capabilities.LlGracefulRestartCapabilityBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.mp.capabilities.ll.graceful.restart.capability.Tables;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.mp.capabilities.ll.graceful.restart.capability.TablesBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.AddressFamily;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv4AddressFamily;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv6AddressFamily;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.SubsequentAddressFamily;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.UnicastSubsequentAddressFamily;
38 import org.opendaylight.yangtools.yang.binding.util.BindingMap;
39 import org.opendaylight.yangtools.yang.common.Uint32;
40
41 public class LlGracefulCapabilityHandlerTest {
42     private static final Uint24 TEN = new Uint24(Uint32.TEN);
43
44     private LlGracefulCapabilityHandler handler;
45
46     @Mock
47     private AddressFamilyRegistry afir;
48     @Mock
49     private SubsequentAddressFamilyRegistry safir;
50
51
52     @Before
53     public void setUp() {
54         MockitoAnnotations.initMocks(this);
55         Mockito.doReturn(Ipv4AddressFamily.class).when(this.afir).classForFamily(1);
56         Mockito.doReturn(Ipv6AddressFamily.class).when(this.afir).classForFamily(2);
57         Mockito.doReturn(null).when(this.afir).classForFamily(256);
58         Mockito.doReturn(UnicastSubsequentAddressFamily.class).when(this.safir).classForFamily(1);
59         Mockito.doReturn(null).when(this.safir).classForFamily(-123);
60
61         final BGPExtensionProviderContext ctx = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance();
62         this.handler = new LlGracefulCapabilityHandler(
63                 ctx.getAddressFamilyRegistry(), ctx.getSubsequentAddressFamilyRegistry());
64     }
65
66     @Test
67     public void testLongLivedGracefulCapabilityHandler() throws BGPParsingException, BGPDocumentedException {
68
69         final byte[] capaBytes = {
70             //header
71             (byte) 0x47, (byte) 0x0e,
72             // Ipv4 Unicast, afiFlags = false, timer = 10
73             (byte) 0x00, (byte) 0x01, (byte) 0x01,
74             (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0a,
75             // Ipv6 Unicast, afiFlags = true, timer = 160
76             (byte) 0x00, (byte) 0x02, (byte) 0x01,
77             (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0xa0
78         };
79
80         final LlGracefulRestartCapability capability = new LlGracefulRestartCapabilityBuilder()
81                 .setTables(BindingMap.ordered(new TablesBuilder()
82                                 .setAfi(Ipv4AddressFamily.class)
83                                 .setSafi(UnicastSubsequentAddressFamily.class)
84                                 .setAfiFlags(new Tables.AfiFlags(Boolean.FALSE))
85                                 .setLongLivedStaleTime(TEN)
86                                 .build(),
87                         new TablesBuilder()
88                                 .setAfi(Ipv6AddressFamily.class)
89                                 .setSafi(UnicastSubsequentAddressFamily.class)
90                                 .setAfiFlags(new Tables.AfiFlags(Boolean.TRUE))
91                                 .setLongLivedStaleTime(new Uint24(Uint32.valueOf(160)))
92                                 .build())
93                 ).build();
94
95         final CParameters cParameters = new CParametersBuilder()
96                 .addAugmentation(new CParameters1Builder().setLlGracefulRestartCapability(capability).build())
97                 .build();
98         final ByteBuf buffer = Unpooled.buffer(capaBytes.length);
99         this.handler.serializeCapability(cParameters, buffer);
100
101         Assert.assertArrayEquals(capaBytes, buffer.array());
102         Assert.assertEquals(cParameters, this.handler.parseCapability(Unpooled.wrappedBuffer(capaBytes)
103                 .slice(2, capaBytes.length - 2)));
104     }
105
106     @Test(expected = IllegalArgumentException.class)
107     public void testUnsupportedAfi() {
108         final LlGracefulRestartCapability capability = new LlGracefulRestartCapabilityBuilder()
109                 .setTables(BindingMap.of(new TablesBuilder()
110                                 .setAfi(AddressFamily.class)
111                                 .setSafi(UnicastSubsequentAddressFamily.class)
112                                 .setAfiFlags(new Tables.AfiFlags(Boolean.FALSE))
113                                 .setLongLivedStaleTime(TEN)
114                                 .build())).build();
115
116         final CParameters cParameters = new CParametersBuilder()
117                 .addAugmentation(new CParameters1Builder().setLlGracefulRestartCapability(capability).build())
118                 .build();
119         final ByteBuf buffer = Unpooled.buffer();
120         this.handler.serializeCapability(cParameters, buffer);
121     }
122
123     @Test
124     public void testRecvdUnsupportedAfi() {
125         final byte[] capaBytes = {
126             //header
127             (byte) 0x47, (byte) 0x15,
128             // Ipv4 Unicast, afiFlags = false, timer = 10
129             (byte) 0x00, (byte) 0x01, (byte) 0x01,
130             (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0a,
131             // Unsupported afi, afiFlags = true, timer = 160
132             (byte) 0x01, (byte) 0x00, (byte) 0x01,
133             (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0xa0,
134             //Ipv6  Unicast afiFlags = false, timer = 160
135             (byte) 0x00, (byte) 0x02, (byte) 0x01,
136             (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xa0
137         };
138         final LlGracefulRestartCapability capability = new LlGracefulRestartCapabilityBuilder()
139                 .setTables(BindingMap.ordered(new TablesBuilder()
140                                 .setAfi(Ipv4AddressFamily.class)
141                                 .setSafi(UnicastSubsequentAddressFamily.class)
142                                 .setAfiFlags(new Tables.AfiFlags(Boolean.FALSE))
143                                 .setLongLivedStaleTime(TEN)
144                                 .build(),
145                         new TablesBuilder()
146                                 .setAfi(Ipv6AddressFamily.class)
147                                 .setSafi(UnicastSubsequentAddressFamily.class)
148                                 .setAfiFlags(new Tables.AfiFlags(Boolean.FALSE))
149                                 .setLongLivedStaleTime(new Uint24(Uint32.valueOf(160)))
150                                 .build())).build();
151
152         final CParameters cParameters = new CParametersBuilder()
153                 .addAugmentation(new CParameters1Builder().setLlGracefulRestartCapability(capability).build())
154                 .build();
155         LlGracefulCapabilityHandler handler1 = new LlGracefulCapabilityHandler(
156                 afir, safir);
157         Assert.assertEquals(cParameters, handler1.parseCapability(Unpooled.wrappedBuffer(capaBytes)
158                 .slice(2, capaBytes.length - 2)));
159     }
160
161     @Test(expected = IllegalArgumentException.class)
162     public void testUnsupportedSafi() {
163         final LlGracefulRestartCapability capability = new LlGracefulRestartCapabilityBuilder()
164                 .setTables(BindingMap.of(new TablesBuilder()
165                         .setAfi(Ipv4AddressFamily.class)
166                         .setSafi(SubsequentAddressFamily.class)
167                         .setAfiFlags(new Tables.AfiFlags(Boolean.FALSE))
168                         .setLongLivedStaleTime(TEN)
169                         .build())).build();
170
171         final CParameters cParameters = new CParametersBuilder()
172                 .addAugmentation(new CParameters1Builder().setLlGracefulRestartCapability(capability).build())
173                 .build();
174         final ByteBuf buffer = Unpooled.buffer();
175         this.handler.serializeCapability(cParameters, buffer);
176     }
177
178     @Test
179     public void testRecvdUnsupportedSafi() {
180         final byte[] capaBytes = {
181             //header
182             (byte) 0x47, (byte) 0x15,
183             // Ipv4 Unicast, afiFlags = false, timer = 10
184             (byte) 0x00, (byte) 0x01, (byte) 0x01,
185             (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0a,
186             // Unsupported safi, afiFlags = true, timer = 160
187             (byte) 0x00, (byte) 0x01, (byte) 0x85,
188             (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0xa0,
189             //Ipv6  Unicast afiFlags = false, timer = 160
190             (byte) 0x00, (byte) 0x02, (byte) 0x01,
191             (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0xa0
192         };
193
194         final LlGracefulRestartCapability capability = new LlGracefulRestartCapabilityBuilder()
195                 .setTables(BindingMap.ordered(new TablesBuilder()
196                                 .setAfi(Ipv4AddressFamily.class)
197                                 .setSafi(UnicastSubsequentAddressFamily.class)
198                                 .setAfiFlags(new Tables.AfiFlags(Boolean.FALSE))
199                                 .setLongLivedStaleTime(TEN)
200                                 .build(),
201                         new TablesBuilder()
202                                 .setAfi(Ipv6AddressFamily.class)
203                                 .setSafi(UnicastSubsequentAddressFamily.class)
204                                 .setAfiFlags(new Tables.AfiFlags(Boolean.TRUE))
205                                 .setLongLivedStaleTime(new Uint24(Uint32.valueOf(160)))
206                                 .build())).build();
207
208         final CParameters cParameters = new CParametersBuilder()
209                 .addAugmentation(new CParameters1Builder().setLlGracefulRestartCapability(capability).build())
210                 .build();
211         LlGracefulCapabilityHandler handler1 = new LlGracefulCapabilityHandler(
212                 afir, safir);
213         Assert.assertEquals(cParameters, handler1.parseCapability(Unpooled.wrappedBuffer(capaBytes)
214                 .slice(2, capaBytes.length - 2)));
215     }
216 }