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.rev180329.PathId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
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.Attributes2;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2Builder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlri;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlriBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlri;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlriBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv4AddressFamily;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.UnicastSubsequentAddressFamily;
51 import org.opendaylight.yangtools.yang.common.Uint32;
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;
75 private PeerSpecificParserConstraint constraint;
78 private MultiPathSupport muliPathSupport;
80 private static Ipv4Prefixes createIpv4Prefix(final long pathId, final Ipv4Prefix prefix) {
81 return new Ipv4PrefixesBuilder().setPathId(new PathId(Uint32.valueOf(pathId))).setPrefix(prefix).build();
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());
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();
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();
107 final ByteBuf buffer1 = Unpooled.buffer(5);
108 Ipv4Util.writeMinimalPrefix(prefix1, buffer1);
109 this.inputBytes.writeBytes(buffer1.array());
111 final ByteBuf buffer2 = Unpooled.buffer(5);
112 Ipv4Util.writeMinimalPrefix(prefix2, buffer2);
113 this.inputBytes.writeBytes(buffer2.array());
115 Mockito.doReturn(Optional.of(this.muliPathSupport)).when(this.constraint).getPeerConstraint(Mockito.any());
116 Mockito.doReturn(true).when(this.muliPathSupport).isTableTypeSupported(Mockito.any());
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());
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());
134 assertEquals(this.ip4caseWD.hashCode(), b.getWithdrawnRoutes().getDestinationType().hashCode());
135 assertNotEquals(this.ip4caseWDWrong.hashCode(), b.getWithdrawnRoutes().getDestinationType().hashCode());
137 assertEquals(this.ip4caseWD.toString(), b.getWithdrawnRoutes().getDestinationType().toString());
138 assertNotEquals(this.ip4caseWDWrong.toString(), b.getWithdrawnRoutes().getDestinationType().toString());
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());
149 assertEquals(this.ip4caseAD.hashCode(), b.getAdvertizedRoutes().getDestinationType().hashCode());
150 assertNotEquals(this.ip4caseADWrong.hashCode(), b.getAdvertizedRoutes().getDestinationType().hashCode());
152 assertEquals(this.ip4caseAD.toString(), b.getAdvertizedRoutes().getDestinationType().toString());
153 assertNotEquals(this.ip4caseADWrong.toString(), b.getAdvertizedRoutes().getDestinationType().toString());
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());
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());
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());
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());