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