Create common parent for extensions families
[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
9 package org.opendaylight.protocol.bgp.inet.codec;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotEquals;
13 import static org.junit.Assert.assertNotNull;
14
15 import com.google.common.collect.Lists;
16 import io.netty.buffer.ByteBuf;
17 import io.netty.buffer.Unpooled;
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.Optional;
21 import org.junit.Assert;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.mockito.Mock;
25 import org.mockito.Mockito;
26 import org.mockito.MockitoAnnotations;
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.ByteBufWriteUtil;
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.rev180329.PathId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1Builder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2Builder;
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.multiprotocol.rev180329.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv4AddressFamily;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.UnicastSubsequentAddressFamily;
52
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(pathId)).setPrefix(prefix).build();
82     }
83
84     @Before
85     public void setUp() {
86         MockitoAnnotations.initMocks(this);
87         final Ipv4Prefix prefix1 = new Ipv4Prefix(this.ipPrefix1);
88         final Ipv4Prefix prefix2 = new Ipv4Prefix(this.ipPrefix2);
89         final Ipv4Prefix wrongPrefix = new Ipv4Prefix(this.additionalIpWD);
90         this.prefixes.add(new Ipv4PrefixesBuilder().setPrefix(prefix1).build());
91         this.prefixes.add(new Ipv4PrefixesBuilder().setPrefix(prefix2).build());
92
93         this.ip4caseWD = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329
94                 .update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv4CaseBuilder()
95                 .setDestinationIpv4(new DestinationIpv4Builder().setIpv4Prefixes(this.prefixes).build()).build();
96         this.ip4caseAD = new DestinationIpv4CaseBuilder().setDestinationIpv4(new DestinationIpv4Builder()
97                 .setIpv4Prefixes(this.prefixes).build()).build();
98
99         final ArrayList<Ipv4Prefixes> fakePrefixes = new ArrayList<>(this.prefixes);
100         fakePrefixes.add(new Ipv4PrefixesBuilder().setPrefix(wrongPrefix).build());
101         this.ip4caseWDWrong = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329
102                 .update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv4CaseBuilder()
103                 .setDestinationIpv4(new DestinationIpv4Builder().setIpv4Prefixes(fakePrefixes).build()).build();
104         this.ip4caseADWrong = new DestinationIpv4CaseBuilder().setDestinationIpv4(new DestinationIpv4Builder()
105                 .setIpv4Prefixes(fakePrefixes).build()).build();
106
107         final ByteBuf buffer1 = Unpooled.buffer(5);
108         ByteBufWriteUtil.writeMinimalPrefix(prefix1, buffer1);
109         this.inputBytes.writeBytes(buffer1.array());
110
111         final ByteBuf buffer2 = Unpooled.buffer(5);
112         ByteBufWriteUtil.writeMinimalPrefix(prefix2, buffer2);
113         this.inputBytes.writeBytes(buffer2.array());
114
115         Mockito.doReturn(Optional.of(this.muliPathSupport)).when(this.constraint).getPeerConstraint(Mockito.any());
116         Mockito.doReturn(true).when(this.muliPathSupport).isTableTypeSupported(Mockito.any());
117     }
118
119     @Test
120     public void prefixesTest() {
121         assertEquals(this.ipPrefix1, this.prefixes.get(0).getPrefix().getValue());
122         assertEquals(this.ipPrefix2, this.prefixes.get(1).getPrefix().getValue());
123         assertEquals(2, this.prefixes.size());
124     }
125
126     @Test
127     public void parseUnreachedNlriTest() {
128         final MpUnreachNlriBuilder b = new MpUnreachNlriBuilder();
129         b.setAfi(Ipv4AddressFamily.class).setSafi(UnicastSubsequentAddressFamily.class);
130         this.parser.parseNlri(this.inputBytes, b, null);
131         assertNotNull("Withdrawn routes, destination type should not be null", b.getWithdrawnRoutes()
132                 .getDestinationType());
133
134         assertEquals(this.ip4caseWD.hashCode(), b.getWithdrawnRoutes().getDestinationType().hashCode());
135         assertNotEquals(this.ip4caseWDWrong.hashCode(), b.getWithdrawnRoutes().getDestinationType().hashCode());
136
137         assertEquals(this.ip4caseWD.toString(), b.getWithdrawnRoutes().getDestinationType().toString());
138         assertNotEquals(this.ip4caseWDWrong.toString(), b.getWithdrawnRoutes().getDestinationType().toString());
139     }
140
141     @Test
142     public void parseReachedNlriTest() throws BGPParsingException {
143         final MpReachNlriBuilder b = new MpReachNlriBuilder();
144         b.setAfi(Ipv4AddressFamily.class).setSafi(UnicastSubsequentAddressFamily.class);
145         this.parser.parseNlri(this.inputBytes, b, null);
146         assertNotNull("Advertized routes, destination type should not be null", b.getAdvertizedRoutes()
147                 .getDestinationType());
148
149         assertEquals(this.ip4caseAD.hashCode(), b.getAdvertizedRoutes().getDestinationType().hashCode());
150         assertNotEquals(this.ip4caseADWrong.hashCode(), b.getAdvertizedRoutes().getDestinationType().hashCode());
151
152         assertEquals(this.ip4caseAD.toString(), b.getAdvertizedRoutes().getDestinationType().toString());
153         assertNotEquals(this.ip4caseADWrong.toString(), b.getAdvertizedRoutes().getDestinationType().toString());
154     }
155
156     @Test
157     public void parseReachNlriMultiPathTest() {
158         final MpReachNlri mpReachNlri = new MpReachNlriBuilder().setAdvertizedRoutes(
159                 new AdvertizedRoutesBuilder().setDestinationType(
160                         new DestinationIpv4CaseBuilder().setDestinationIpv4(
161                                 new DestinationIpv4Builder().setIpv4Prefixes(
162                                         PREFIXES).build()).build()).build()).build();
163         final MpReachNlriBuilder mpReachNlriBuilder = new MpReachNlriBuilder();
164         mpReachNlriBuilder.setAfi(Ipv4AddressFamily.class).setSafi(UnicastSubsequentAddressFamily.class);
165         this.parser.parseNlri(Unpooled.wrappedBuffer(MP_NLRI_BYTES), mpReachNlriBuilder, this.constraint);
166         mpReachNlriBuilder.setAfi(null).setSafi(null);
167         Assert.assertEquals(mpReachNlri, mpReachNlriBuilder.build());
168
169         final Ipv4NlriParser serializer = new Ipv4NlriParser();
170         final ByteBuf output = Unpooled.buffer(MP_NLRI_BYTES.length);
171         final Attributes attributes = new AttributesBuilder().addAugmentation(Attributes1.class,
172                 new Attributes1Builder().setMpReachNlri(mpReachNlri).build()).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().addAugmentation(Attributes2.class,
194                 new Attributes2Builder().setMpUnreachNlri(mpUnreachNlri).build()).build();
195         serializer.serializeAttribute(attributes, output);
196         Assert.assertArrayEquals(MP_NLRI_BYTES, output.array());
197     }
198 }