BGP statistics CLI
[bgpcep.git] / bgp / parser-impl / src / test / java / org / opendaylight / protocol / bgp / parser / impl / Ipv4NlriParserTest.java
1 /*
2  * Copyright (c) 2015 Pantheon Technologies s.r.o. 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.impl;
10
11 import static org.junit.Assert.assertArrayEquals;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertFalse;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.assertTrue;
16
17 import com.google.common.collect.Lists;
18 import io.netty.buffer.ByteBuf;
19 import io.netty.buffer.Unpooled;
20 import java.io.UnsupportedEncodingException;
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.List;
24 import java.util.Optional;
25 import org.junit.Assert;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.mockito.Mock;
29 import org.mockito.Mockito;
30 import org.mockito.MockitoAnnotations;
31 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
32 import org.opendaylight.protocol.bgp.parser.impl.message.update.AdvertizedRoutesSerializer;
33 import org.opendaylight.protocol.bgp.parser.impl.message.update.Ipv4NlriParser;
34 import org.opendaylight.protocol.bgp.parser.impl.message.update.WithdrawnRoutesSerializer;
35 import org.opendaylight.protocol.bgp.parser.spi.MultiPathSupport;
36 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
37 import org.opendaylight.protocol.util.Ipv4Util;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.prefixes.DestinationIpv4Builder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.prefixes.destination.ipv4.Ipv4Prefixes;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.prefixes.destination.ipv4.Ipv4PrefixesBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationIpv4Case;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationIpv4CaseBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.PathId;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AttributesBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.message.Nlri;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.message.NlriBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1Builder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes2;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes2Builder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlri;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlriBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlri;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlriBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
61
62 public class Ipv4NlriParserTest {
63     private final Ipv4NlriParser parser = new Ipv4NlriParser();
64
65     private final String ipPrefix1 = "1.2.3.4/32";
66     private final String ipPrefix2 = "1.2.3.5/32";
67     private final String additionalIpWD = "1.2.3.6/32";
68
69     private final List<Ipv4Prefixes> prefixes = new ArrayList<>();
70     private final ByteBuf inputBytes = Unpooled.buffer();
71
72     private static final byte[] MP_NLRI_BYTES = new byte[] {
73         0x0, 0x0, 0x0, 0x1, 0x18, 0x1, 0x1, 0x1,
74         0x0, 0x0, 0x0, 0x2, 0x18, 0x1, 0x1, 0x1};
75
76     private static final Ipv4Prefix DESTINATION = new Ipv4Prefix("1.1.1.0/24");
77
78     private static final ArrayList<Ipv4Prefixes> PREFIXES = Lists.newArrayList(
79             createIpv4Prefix(1L, DESTINATION),
80             createIpv4Prefix(2L, DESTINATION));
81
82     private org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv4Case ip4caseWD;
83     private org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv4Case ip4caseWDWrong;
84     private DestinationIpv4Case ip4caseAD;
85     private DestinationIpv4Case ip4caseADWrong;
86
87     private Nlri nlri;
88     private Nlri nlriWrong;
89
90     @Mock
91     private PeerSpecificParserConstraint constraint;
92
93     @Mock
94     private MultiPathSupport muliPathSupport;
95
96     @Before
97     public void setUp() {
98         MockitoAnnotations.initMocks(this);
99         final Ipv4Prefix prefix1 = new Ipv4Prefix(this.ipPrefix1);
100         final Ipv4Prefix prefix2 = new Ipv4Prefix(this.ipPrefix2);
101         final Ipv4Prefix wrongPrefix = new Ipv4Prefix(this.additionalIpWD);
102         this.prefixes.add(new Ipv4PrefixesBuilder().setPrefix(prefix1).build());
103         this.prefixes.add(new Ipv4PrefixesBuilder().setPrefix(prefix2).build());
104
105         this.ip4caseWD = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv4CaseBuilder().setDestinationIpv4(
106             new DestinationIpv4Builder().setIpv4Prefixes(this.prefixes).build()).build();
107         this.ip4caseAD = new DestinationIpv4CaseBuilder().setDestinationIpv4(new DestinationIpv4Builder().setIpv4Prefixes(this.prefixes).build()).build();
108
109         final ArrayList<Ipv4Prefixes> fakePrefixes = new ArrayList<Ipv4Prefixes>(this.prefixes);
110         fakePrefixes.add(new Ipv4PrefixesBuilder().setPrefix(wrongPrefix).build());
111         this.ip4caseWDWrong = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv4CaseBuilder().setDestinationIpv4(
112             new DestinationIpv4Builder().setIpv4Prefixes(fakePrefixes).build()).build();
113         this.ip4caseADWrong = new DestinationIpv4CaseBuilder().setDestinationIpv4(new DestinationIpv4Builder().setIpv4Prefixes(fakePrefixes).build()).build();
114
115         this.inputBytes.writeBytes(Ipv4Util.bytesForPrefixBegin(prefix1));
116         this.inputBytes.writeBytes(Ipv4Util.bytesForPrefixBegin(prefix2));
117
118         final List<Ipv4Prefix> prefixList = new ArrayList<Ipv4Prefix>();
119         prefixList.add(prefix1);
120         prefixList.add(prefix2);
121         this.nlri = new NlriBuilder().setNlri(prefixList).build();
122         final List<Ipv4Prefix> prefixWrongList = Lists.newArrayList(prefixList.iterator());
123         prefixWrongList.add(wrongPrefix);
124         this.nlriWrong = new NlriBuilder().setNlri(prefixWrongList).build();
125         Mockito.doReturn(Optional.of(this.muliPathSupport)).when(constraint).getPeerConstraint(Mockito.any());
126         Mockito.doReturn(true).when(this.muliPathSupport).isTableTypeSupported(Mockito.any());
127     }
128
129     @Test
130     public void prefixesTest() {
131         assertEquals(this.ipPrefix1, this.prefixes.get(0).getPrefix().getValue());
132         assertEquals(this.ipPrefix2, this.prefixes.get(1).getPrefix().getValue());
133         assertEquals(2, this.prefixes.size());
134     }
135
136     @Test
137     public void serializeAttributeTest() throws UnsupportedEncodingException {
138         final ByteBuf outputBytes = Unpooled.buffer();
139
140         this.parser.serializeAttribute(this.nlri, outputBytes);
141         assertArrayEquals(this.inputBytes.array(), outputBytes.array());
142         this.parser.serializeAttribute(this.nlriWrong, outputBytes);
143         assertFalse(Arrays.equals(this.inputBytes.array(), outputBytes.array()));
144     }
145
146     @Test
147     public void parseUnreachedNlriTest() {
148         final MpUnreachNlriBuilder b = new MpUnreachNlriBuilder();
149         b.setAfi(Ipv4AddressFamily.class).setSafi(UnicastSubsequentAddressFamily.class);
150         this.parser.parseNlri(this.inputBytes, b);
151         assertNotNull("Withdrawn routes, destination type should not be null", b.getWithdrawnRoutes().getDestinationType());
152
153         assertEquals(this.ip4caseWD.hashCode(), b.getWithdrawnRoutes().getDestinationType().hashCode());
154         assertFalse(this.ip4caseWDWrong.hashCode() == b.getWithdrawnRoutes().getDestinationType().hashCode());
155
156         assertTrue(this.ip4caseWD.toString().equals(b.getWithdrawnRoutes().getDestinationType().toString()));
157         assertFalse(this.ip4caseWDWrong.toString().equals(b.getWithdrawnRoutes().getDestinationType().toString()));
158     }
159
160     @Test
161     public void parseReachedNlriTest() throws BGPParsingException {
162         final MpReachNlriBuilder b = new MpReachNlriBuilder();
163         b.setAfi(Ipv4AddressFamily.class).setSafi(UnicastSubsequentAddressFamily.class);
164         this.parser.parseNlri(this.inputBytes, b);
165         assertNotNull("Advertized routes, destination type should not be null", b.getAdvertizedRoutes().getDestinationType());
166
167         assertEquals(this.ip4caseAD.hashCode(), b.getAdvertizedRoutes().getDestinationType().hashCode());
168         assertFalse(this.ip4caseADWrong.hashCode() == b.getAdvertizedRoutes().getDestinationType().hashCode());
169
170         assertTrue(this.ip4caseAD.toString().equals(b.getAdvertizedRoutes().getDestinationType().toString()));
171         assertFalse(this.ip4caseADWrong.toString().equals(b.getAdvertizedRoutes().getDestinationType().toString()));
172     }
173
174     @Test
175     public void parseReachNlriMultiPathTest() {
176         final MpReachNlri mpReachNlri = new MpReachNlriBuilder().setAdvertizedRoutes(
177                 new AdvertizedRoutesBuilder().setDestinationType(
178                         new DestinationIpv4CaseBuilder().setDestinationIpv4(
179                                 new DestinationIpv4Builder().setIpv4Prefixes(
180                                         PREFIXES).build()).build()).build()).build();
181         final MpReachNlriBuilder mpReachNlriBuilder = new MpReachNlriBuilder();
182         mpReachNlriBuilder.setAfi(Ipv4AddressFamily.class).setSafi(UnicastSubsequentAddressFamily.class);
183         this.parser.parseNlri(Unpooled.wrappedBuffer(MP_NLRI_BYTES), mpReachNlriBuilder, this.constraint);
184         mpReachNlriBuilder.setAfi(null).setSafi(null);
185         Assert.assertEquals(mpReachNlri, mpReachNlriBuilder.build());
186
187         final AdvertizedRoutesSerializer serializer = new AdvertizedRoutesSerializer();
188         final ByteBuf output = Unpooled.buffer(MP_NLRI_BYTES.length);
189         final Attributes attributes = new AttributesBuilder().addAugmentation(Attributes1.class,
190                 new Attributes1Builder().setMpReachNlri(mpReachNlri).build()).build();
191         serializer.serializeAttribute(attributes, output);
192         Assert.assertArrayEquals(MP_NLRI_BYTES, output.array());
193     }
194
195     @Test
196     public void parseUnreachNlriMultiPathTest() {
197         final MpUnreachNlri mpUnreachNlri = new MpUnreachNlriBuilder().setWithdrawnRoutes(
198                 new WithdrawnRoutesBuilder().setDestinationType(
199                         new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv4CaseBuilder().setDestinationIpv4(
200                                 new DestinationIpv4Builder().setIpv4Prefixes(
201                                         PREFIXES).build()).build()).build()).build();
202         final MpUnreachNlriBuilder mpUnreachNlriBuilder = new MpUnreachNlriBuilder();
203         mpUnreachNlriBuilder.setAfi(Ipv4AddressFamily.class).setSafi(UnicastSubsequentAddressFamily.class);
204         this.parser.parseNlri(Unpooled.wrappedBuffer(MP_NLRI_BYTES), mpUnreachNlriBuilder, this.constraint);
205         mpUnreachNlriBuilder.setAfi(null).setSafi(null);
206         Assert.assertEquals(mpUnreachNlri, mpUnreachNlriBuilder.build());
207
208         final WithdrawnRoutesSerializer serializer = new WithdrawnRoutesSerializer();
209         final ByteBuf output = Unpooled.buffer(MP_NLRI_BYTES.length);
210         final Attributes attributes = new AttributesBuilder().addAugmentation(Attributes2.class,
211                 new Attributes2Builder().setMpUnreachNlri(mpUnreachNlri).build()).build();
212         serializer.serializeAttribute(attributes, output);
213         Assert.assertArrayEquals(MP_NLRI_BYTES, output.array());
214     }
215
216     private static Ipv4Prefixes createIpv4Prefix(final long pathId, final Ipv4Prefix prefix) {
217         return new Ipv4PrefixesBuilder().setPathId(new PathId(pathId)).setPrefix(prefix).build();
218     }
219 }