2 * Copyright (c) 2015 Pantheon Technologies s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.inet.codec;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotEquals;
12 import static org.junit.Assert.assertNotNull;
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.mockito.Mock;
24 import org.mockito.Mockito;
25 import org.mockito.MockitoAnnotations;
26 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
27 import org.opendaylight.protocol.bgp.parser.spi.MultiPathSupport;
28 import org.opendaylight.protocol.bgp.parser.spi.PeerSpecificParserConstraint;
29 import org.opendaylight.protocol.util.Ipv4Util;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.prefixes.DestinationIpv4Builder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.prefixes.destination.ipv4.Ipv4Prefixes;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.prefixes.destination.ipv4.Ipv4PrefixesBuilder;
34 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;
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.DestinationIpv4CaseBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.PathId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.Attributes;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.AttributesBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1Builder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2Builder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlri;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlriBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlri;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlriBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv4AddressFamily;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.UnicastSubsequentAddressFamily;
49 import org.opendaylight.yangtools.yang.common.Uint32;
51 public class Ipv4NlriParserTest {
52 private static final byte[] MP_NLRI_BYTES = new byte[]{
53 0x0, 0x0, 0x0, 0x1, 0x18, 0x1, 0x1, 0x1,
54 0x0, 0x0, 0x0, 0x2, 0x18, 0x1, 0x1, 0x1};
55 private static final Ipv4Prefix DESTINATION = new Ipv4Prefix("1.1.1.0/24");
56 private static final ArrayList<Ipv4Prefixes> PREFIXES = Lists.newArrayList(
57 createIpv4Prefix(1L, DESTINATION),
58 createIpv4Prefix(2L, DESTINATION));
59 private final Ipv4NlriParser parser = new Ipv4NlriParser();
60 private final String ipPrefix1 = "1.2.3.4/32";
61 private final String ipPrefix2 = "1.2.3.5/32";
62 private final String additionalIpWD = "1.2.3.6/32";
63 private final List<Ipv4Prefixes> prefixes = new ArrayList<>();
64 private final ByteBuf inputBytes = Unpooled.buffer();
65 private org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.update.attributes.mp
66 .unreach.nlri.withdrawn.routes.destination.type.DestinationIpv4Case ip4caseWD;
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 ip4caseWDWrong;
69 private DestinationIpv4Case ip4caseAD;
70 private DestinationIpv4Case ip4caseADWrong;
73 private PeerSpecificParserConstraint constraint;
76 private MultiPathSupport muliPathSupport;
78 private static Ipv4Prefixes createIpv4Prefix(final long pathId, final Ipv4Prefix prefix) {
79 return new Ipv4PrefixesBuilder().setPathId(new PathId(Uint32.valueOf(pathId))).setPrefix(prefix).build();
84 MockitoAnnotations.initMocks(this);
85 final Ipv4Prefix prefix1 = new Ipv4Prefix(this.ipPrefix1);
86 final Ipv4Prefix prefix2 = new Ipv4Prefix(this.ipPrefix2);
87 final Ipv4Prefix wrongPrefix = new Ipv4Prefix(this.additionalIpWD);
88 this.prefixes.add(new Ipv4PrefixesBuilder().setPrefix(prefix1).build());
89 this.prefixes.add(new Ipv4PrefixesBuilder().setPrefix(prefix2).build());
91 this.ip4caseWD = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329
92 .update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv4CaseBuilder()
93 .setDestinationIpv4(new DestinationIpv4Builder().setIpv4Prefixes(this.prefixes).build()).build();
94 this.ip4caseAD = new DestinationIpv4CaseBuilder().setDestinationIpv4(new DestinationIpv4Builder()
95 .setIpv4Prefixes(this.prefixes).build()).build();
97 final ArrayList<Ipv4Prefixes> fakePrefixes = new ArrayList<>(this.prefixes);
98 fakePrefixes.add(new Ipv4PrefixesBuilder().setPrefix(wrongPrefix).build());
99 this.ip4caseWDWrong = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329
100 .update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv4CaseBuilder()
101 .setDestinationIpv4(new DestinationIpv4Builder().setIpv4Prefixes(fakePrefixes).build()).build();
102 this.ip4caseADWrong = new DestinationIpv4CaseBuilder().setDestinationIpv4(new DestinationIpv4Builder()
103 .setIpv4Prefixes(fakePrefixes).build()).build();
105 final ByteBuf buffer1 = Unpooled.buffer(5);
106 Ipv4Util.writeMinimalPrefix(prefix1, buffer1);
107 this.inputBytes.writeBytes(buffer1.array());
109 final ByteBuf buffer2 = Unpooled.buffer(5);
110 Ipv4Util.writeMinimalPrefix(prefix2, buffer2);
111 this.inputBytes.writeBytes(buffer2.array());
113 Mockito.doReturn(Optional.of(this.muliPathSupport)).when(this.constraint).getPeerConstraint(Mockito.any());
114 Mockito.doReturn(true).when(this.muliPathSupport).isTableTypeSupported(Mockito.any());
118 public void prefixesTest() {
119 assertEquals(this.ipPrefix1, this.prefixes.get(0).getPrefix().getValue());
120 assertEquals(this.ipPrefix2, this.prefixes.get(1).getPrefix().getValue());
121 assertEquals(2, this.prefixes.size());
125 public void parseUnreachedNlriTest() {
126 final MpUnreachNlriBuilder b = new MpUnreachNlriBuilder();
127 b.setAfi(Ipv4AddressFamily.class).setSafi(UnicastSubsequentAddressFamily.class);
128 this.parser.parseNlri(this.inputBytes, b, null);
129 assertNotNull("Withdrawn routes, destination type should not be null", b.getWithdrawnRoutes()
130 .getDestinationType());
132 assertEquals(this.ip4caseWD.hashCode(), b.getWithdrawnRoutes().getDestinationType().hashCode());
133 assertNotEquals(this.ip4caseWDWrong.hashCode(), b.getWithdrawnRoutes().getDestinationType().hashCode());
135 assertEquals(this.ip4caseWD.toString(), b.getWithdrawnRoutes().getDestinationType().toString());
136 assertNotEquals(this.ip4caseWDWrong.toString(), b.getWithdrawnRoutes().getDestinationType().toString());
140 public void parseReachedNlriTest() throws BGPParsingException {
141 final MpReachNlriBuilder b = new MpReachNlriBuilder();
142 b.setAfi(Ipv4AddressFamily.class).setSafi(UnicastSubsequentAddressFamily.class);
143 this.parser.parseNlri(this.inputBytes, b, null);
144 assertNotNull("Advertized routes, destination type should not be null", b.getAdvertizedRoutes()
145 .getDestinationType());
147 assertEquals(this.ip4caseAD.hashCode(), b.getAdvertizedRoutes().getDestinationType().hashCode());
148 assertNotEquals(this.ip4caseADWrong.hashCode(), b.getAdvertizedRoutes().getDestinationType().hashCode());
150 assertEquals(this.ip4caseAD.toString(), b.getAdvertizedRoutes().getDestinationType().toString());
151 assertNotEquals(this.ip4caseADWrong.toString(), b.getAdvertizedRoutes().getDestinationType().toString());
155 public void parseReachNlriMultiPathTest() {
156 final MpReachNlri mpReachNlri = new MpReachNlriBuilder().setAdvertizedRoutes(
157 new AdvertizedRoutesBuilder().setDestinationType(
158 new DestinationIpv4CaseBuilder().setDestinationIpv4(
159 new DestinationIpv4Builder().setIpv4Prefixes(
160 PREFIXES).build()).build()).build()).build();
161 final MpReachNlriBuilder mpReachNlriBuilder = new MpReachNlriBuilder();
162 mpReachNlriBuilder.setAfi(Ipv4AddressFamily.class).setSafi(UnicastSubsequentAddressFamily.class);
163 this.parser.parseNlri(Unpooled.wrappedBuffer(MP_NLRI_BYTES), mpReachNlriBuilder, this.constraint);
164 mpReachNlriBuilder.setAfi(null).setSafi(null);
165 Assert.assertEquals(mpReachNlri, mpReachNlriBuilder.build());
167 final Ipv4NlriParser serializer = new Ipv4NlriParser();
168 final ByteBuf output = Unpooled.buffer(MP_NLRI_BYTES.length);
169 final Attributes attributes = new AttributesBuilder()
170 .addAugmentation(new Attributes1Builder().setMpReachNlri(mpReachNlri).build())
172 serializer.serializeAttribute(attributes, output);
173 Assert.assertArrayEquals(MP_NLRI_BYTES, output.array());
177 public void parseUnreachNlriMultiPathTest() {
178 final MpUnreachNlri mpUnreachNlri = new MpUnreachNlriBuilder().setWithdrawnRoutes(
179 new WithdrawnRoutesBuilder().setDestinationType(
180 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.update
181 .attributes.mp.unreach.nlri.withdrawn.routes.destination.type
182 .DestinationIpv4CaseBuilder().setDestinationIpv4(new DestinationIpv4Builder()
183 .setIpv4Prefixes(PREFIXES).build()).build()).build()).build();
184 final MpUnreachNlriBuilder mpUnreachNlriBuilder = new MpUnreachNlriBuilder();
185 mpUnreachNlriBuilder.setAfi(Ipv4AddressFamily.class).setSafi(UnicastSubsequentAddressFamily.class);
186 this.parser.parseNlri(Unpooled.wrappedBuffer(MP_NLRI_BYTES), mpUnreachNlriBuilder, this.constraint);
187 mpUnreachNlriBuilder.setAfi(null).setSafi(null);
188 Assert.assertEquals(mpUnreachNlri, mpUnreachNlriBuilder.build());
190 final Ipv4NlriParser serializer = new Ipv4NlriParser();
191 final ByteBuf output = Unpooled.buffer(MP_NLRI_BYTES.length);
192 final Attributes attributes = new AttributesBuilder()
193 .addAugmentation(new Attributes2Builder().setMpUnreachNlri(mpUnreachNlri).build())
195 serializer.serializeAttribute(attributes, output);
196 Assert.assertArrayEquals(MP_NLRI_BYTES, output.array());