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