BUG-2982 : moved path-attributes container to grouping
[bgpcep.git] / bgp / parser-mock / src / test / java / org / opendaylight / protocol / bgp / parser / mock / BGPMessageParserMockTest.java
index 8ae4abe3734d1682cb19ff55141284ad35cd524c..8eb4c9f3e2e6aa58ee9727226fd8666bc3baebbf 100644 (file)
  */
 package org.opendaylight.protocol.bgp.parser.mock;
 
-import static org.hamcrest.core.IsNot.not;
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertNotSame;
 
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Sets;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.net.InetAddress;
 import java.net.UnknownHostException;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
-
 import org.junit.Before;
 import org.junit.Test;
-import org.opendaylight.protocol.bgp.concepts.ASPath;
-import org.opendaylight.protocol.bgp.concepts.BGPAddressFamily;
-import org.opendaylight.protocol.bgp.concepts.BGPObject;
-import org.opendaylight.protocol.bgp.concepts.BGPOrigin;
-import org.opendaylight.protocol.bgp.concepts.BGPSubsequentAddressFamily;
-import org.opendaylight.protocol.bgp.concepts.BGPTableType;
-import org.opendaylight.protocol.bgp.concepts.BaseBGPObjectState;
-import org.opendaylight.protocol.bgp.concepts.Community;
-import org.opendaylight.protocol.bgp.concepts.ExtendedCommunity;
-import org.opendaylight.protocol.bgp.concepts.IPv6NextHop;
-import org.opendaylight.protocol.bgp.concepts.NextHop;
-import org.opendaylight.protocol.bgp.linkstate.NetworkObjectState;
-import org.opendaylight.protocol.bgp.linkstate.NetworkRouteState;
-import org.opendaylight.protocol.bgp.parser.BGPMessage;
-import org.opendaylight.protocol.bgp.parser.BGPParameter;
-import org.opendaylight.protocol.bgp.parser.BGPRoute;
-import org.opendaylight.protocol.bgp.parser.BGPUpdateMessage;
-import org.opendaylight.protocol.bgp.parser.impl.BGPUpdateMessageImpl;
-import org.opendaylight.protocol.bgp.parser.message.BGPOpenMessage;
-import org.opendaylight.protocol.bgp.parser.parameter.MultiprotocolCapability;
-import org.opendaylight.protocol.bgp.util.BGPIPv6RouteImpl;
-import org.opendaylight.protocol.concepts.ASNumber;
-import org.opendaylight.protocol.concepts.IPv6Address;
-import org.opendaylight.protocol.concepts.IPv6Prefix;
-import org.opendaylight.protocol.concepts.Identifier;
-import org.opendaylight.protocol.concepts.Prefix;
-import org.opendaylight.protocol.framework.DeserializerException;
-import org.opendaylight.protocol.framework.DocumentedException;
-
-import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
-import com.google.common.collect.Sets;
+import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
+import org.opendaylight.protocol.bgp.parser.BGPParsingException;
+import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv6.prefixes.DestinationIpv6Builder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv6.prefixes.destination.ipv6.Ipv6PrefixesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationIpv6CaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Open;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.OpenBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.ProtocolVersion;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.UpdateBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.BgpParameters;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.BgpParametersBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.OptionalCapabilities;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.OptionalCapabilitiesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.bgp.parameters.optional.capabilities.CParameters;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AttributesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.AsPathBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.OriginBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.as.path.Segments;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.as.path.SegmentsBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1Builder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.MultiprotocolCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.MultiprotocolCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.open.bgp.parameters.optional.capabilities.c.parameters.multiprotocol._case.MultiprotocolCapabilityBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlriBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpOrigin;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv6AddressFamily;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.MplsLabeledVpnSubsequentAddressFamily;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.AListCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.a.list._case.AListBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.a.list._case.a.list.AsSequence;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.as.path.segment.c.segment.a.list._case.a.list.AsSequenceBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.CNextHop;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv6NextHopCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.ipv6.next.hop._case.Ipv6NextHopBuilder;
+import org.opendaylight.yangtools.yang.binding.Notification;
 
 public class BGPMessageParserMockTest {
 
-       private final byte[][] inputBytes = new byte[11][];
-       private final List<BGPUpdateMessage> messages = new ArrayList<BGPUpdateMessage>();
-
-       @Before
-       public void init() throws Exception {
-               // Creating input bytes and update messages
-               for (int i = 0; i < this.inputBytes.length; i++) {
-                       this.inputBytes[i] = this.fillInputBytes(i);
-                       this.messages.add(this.fillMessages(i));
-               }
-       }
-
-       /**
-        * Test if mock implementation of parser returns correct message
-        * 
-        * @throws DocumentedException
-        * @throws DeserializerException
-        * @throws IOException
-        */
-       @Test
-       public void testGetUpdateMessage() throws DeserializerException, DocumentedException, IOException {
-               final Map<byte[], BGPMessage> updateMap = Maps.newHashMap();
-               for (int i = 0; i < this.inputBytes.length; i++) {
-                       updateMap.put(this.inputBytes[i], this.messages.get(i));
-               }
-
-               final BGPMessageParserMock mockParser = new BGPMessageParserMock(updateMap);
-
-               for (int i = 0; i < this.inputBytes.length; i++) {
-                       assertEquals(this.messages.get(i), mockParser.parse(this.inputBytes[i]));
-               }
-               assertThat(this.messages.get(3), not(mockParser.parse(this.inputBytes[8])));
-       }
-
-       /**
-        * Test if method throws IllegalArgumentException after finding no BGPUpdateMessage associated with given byte[] key
-        * 
-        * @throws DocumentedException
-        * @throws DeserializerException
-        * @throws IOException
-        */
-       @Test(expected = IllegalArgumentException.class)
-       public void testGetUpdateMessageException() throws DeserializerException, DocumentedException, IOException {
-               final Map<byte[], BGPMessage> updateMap = Maps.newHashMap();
-               for (int i = 0; i < this.inputBytes.length; i++) {
-                       updateMap.put(this.inputBytes[i], this.messages.get(i));
-               }
-
-               final BGPMessageParserMock mockParser = new BGPMessageParserMock(updateMap);
-               mockParser.parse(new byte[] { 7, 4, 6 });
-       }
-
-       /**
-        * Helper method to fill inputBytes variable
-        * 
-        * @param fileNumber parameter to distinguish between files from which bytes are read
-        */
-       private byte[] fillInputBytes(final int fileNumber) throws Exception {
-
-               final InputStream is = this.getClass().getResourceAsStream("/up" + fileNumber + ".bin");
-               final ByteArrayOutputStream bis = new ByteArrayOutputStream();
-               final byte[] data = new byte[60];
-               int nRead = 0;
-
-               while ((nRead = is.read(data, 0, data.length)) != -1) {
-                       bis.write(data, 0, nRead);
-               }
-               bis.flush();
-               return bis.toByteArray();
-       }
-
-       /**
-        * Helper method to fill messages variable
-        * 
-        * @param asn this parameter is passed to ASNumber constructor
-        */
-       private BGPUpdateMessage fillMessages(final int asn) throws UnknownHostException {
-
-               final List<ASNumber> asnums = new ArrayList<ASNumber>();
-               asnums.add(new ASNumber(asn));
-               final ASPath asPath = new ASPath(asnums);
-               final NextHop<IPv6Address> nextHop = new IPv6NextHop(new IPv6Address(InetAddress.getByName("2001:db8::1")), new IPv6Address(InetAddress.getByName("fe80::c001:bff:fe7e:0")));
-
-               final Prefix<IPv6Address> pref1 = new IPv6Prefix(new IPv6Address(InetAddress.getByName("2001:db8:1:2::")), 64);
-               final Prefix<IPv6Address> pref2 = new IPv6Prefix(new IPv6Address(InetAddress.getByName("2001:db8:1:1::")), 64);
-               final Prefix<IPv6Address> pref3 = new IPv6Prefix(new IPv6Address(InetAddress.getByName("2001:db8:1::")), 64);
-
-               final Set<BGPObject> addedObjects = new HashSet<BGPObject>();
-
-               final NetworkRouteState<IPv6Address> nstate = new NetworkRouteState<>(new NetworkObjectState(asPath, Collections.<Community> emptySet(), Collections.<ExtendedCommunity> emptySet()), nextHop);
-               final BaseBGPObjectState state = new BaseBGPObjectState(BGPOrigin.IGP, null);
-
-               final BGPRoute<IPv6Address> route1 = new BGPIPv6RouteImpl(pref1, state, nstate);
-               final BGPRoute<IPv6Address> route2 = new BGPIPv6RouteImpl(pref2, state, nstate);
-               final BGPRoute<IPv6Address> route3 = new BGPIPv6RouteImpl(pref3, state, nstate);
-               addedObjects.add(route1);
-               addedObjects.add(route2);
-               addedObjects.add(route3);
-
-               return new BGPUpdateMessageImpl(addedObjects, Collections.<Identifier> emptySet());
-       }
-
-       @Test
-       public void testGetOpenMessage() throws DeserializerException, DocumentedException, IOException {
-               final Map<byte[], BGPMessage> openMap = Maps.newHashMap();
-
-               final Set<BGPTableType> type = Sets.newHashSet();
-               type.add(new BGPTableType(BGPAddressFamily.IPv4, BGPSubsequentAddressFamily.MPLSLabeledVPN));
-
-               final List<BGPParameter> params = Lists.newArrayList();
-               params.add(new MultiprotocolCapability(new BGPTableType(BGPAddressFamily.IPv4, BGPSubsequentAddressFamily.MPLSLabeledVPN)));
-
-               final byte[] input = new byte[] { 5, 8, 13, 21 };
-
-               openMap.put(input, new BGPOpenMessage(new ASNumber(30), (short) 30, null, params));
-
-               final BGPMessageParserMock mockParser = new BGPMessageParserMock(openMap);
-
-               final Set<BGPTableType> result = Sets.newHashSet();
-               for (final BGPParameter p : ((BGPOpenMessage) mockParser.parse(input)).getOptParams()) {
-                       if (p instanceof MultiprotocolCapability) {
-                               result.add(((MultiprotocolCapability) p).getTableType());
-                       }
-               }
-
-               assertEquals(type, result);
-       }
+    private final byte[][] inputBytes = new byte[11][];
+    private final List<Update> messages = Lists.newArrayList();
+
+    @Before
+    public void init() throws Exception {
+        // Creating input bytes and update messages
+        for (int i = 0; i < this.inputBytes.length; i++) {
+            this.inputBytes[i] = this.fillInputBytes(i);
+            this.messages.add(this.fillMessages(i));
+        }
+    }
+
+    /**
+     * Test if mock implementation of parser returns correct message
+     *
+     * @throws BGPParsingException
+     * @throws BGPDocumentedException
+     * @throws IOException
+     */
+    @Test
+    public void testGetUpdateMessage() throws BGPParsingException, BGPDocumentedException, IOException {
+        final Map<ByteBuf, Notification> updateMap = Maps.newHashMap();
+        for (int i = 0; i < this.inputBytes.length; i++) {
+            updateMap.put(Unpooled.copiedBuffer(this.inputBytes[i]), this.messages.get(i));
+        }
+        final BGPMessageParserMock mockParser = new BGPMessageParserMock(updateMap);
+
+        for (int i = 0; i < this.inputBytes.length; i++) {
+            assertEquals(this.messages.get(i), mockParser.parseMessage(Unpooled.copiedBuffer(this.inputBytes[i])));
+        }
+        assertNotSame(this.messages.get(3), mockParser.parseMessage(Unpooled.copiedBuffer(this.inputBytes[8])));
+    }
+
+    /**
+     * Test if method throws IllegalArgumentException after finding no BGPUpdateMessage associated with given byte[] key
+     *
+     * @throws BGPDocumentedException
+     * @throws BGPParsingException
+     * @throws IOException
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testGetUpdateMessageException() throws BGPParsingException, BGPDocumentedException, IOException {
+        final Map<ByteBuf, Notification> updateMap = Maps.newHashMap();
+        for (int i = 0; i < this.inputBytes.length; i++) {
+            updateMap.put(Unpooled.copiedBuffer(this.inputBytes[i]), this.messages.get(i));
+        }
+        final BGPMessageParserMock mockParser = new BGPMessageParserMock(updateMap);
+        mockParser.parseMessage(Unpooled.copiedBuffer(new byte[] { 7, 4, 6 }));
+    }
+
+    /**
+     * Helper method to fill inputBytes variable
+     *
+     * @param fileNumber parameter to distinguish between files from which bytes are read
+     */
+    private byte[] fillInputBytes(final int fileNumber) throws Exception {
+        final ByteArrayOutputStream bis = new ByteArrayOutputStream();
+        final byte[] data = new byte[60];
+        int nRead = 0;
+        try (final InputStream is = this.getClass().getResourceAsStream("/up" + fileNumber + ".bin")) {
+            while ((nRead = is.read(data, 0, data.length)) != -1) {
+                bis.write(data, 0, nRead);
+            }
+            bis.flush();
+            is.close();
+        }
+        return bis.toByteArray();
+    }
+
+    /**
+     * Helper method to fill messages variable
+     *
+     * @param asn this parameter is passed to ASNumber constructor
+     */
+    private Update fillMessages(final long asn) throws UnknownHostException {
+
+        final UpdateBuilder builder = new UpdateBuilder();
+
+        final List<AsSequence> asnums = Lists.newArrayList(new AsSequenceBuilder().setAs(new AsNumber(asn)).build());
+        final List<Segments> asPath = Lists.newArrayList();
+        asPath.add(new SegmentsBuilder().setCSegment(
+            new AListCaseBuilder().setAList(new AListBuilder().setAsSequence(asnums).build()).build()).build());
+        final CNextHop nextHop = new Ipv6NextHopCaseBuilder().setIpv6NextHop(
+            new Ipv6NextHopBuilder().setGlobal(new Ipv6Address("2001:db8::1")).setLinkLocal(new Ipv6Address("fe80::c001:bff:fe7e:0")).build()).build();
+
+        final Ipv6Prefix pref1 = new Ipv6Prefix("2001:db8:1:2::/64");
+        final Ipv6Prefix pref2 = new Ipv6Prefix("2001:db8:1:1::/64");
+        final Ipv6Prefix pref3 = new Ipv6Prefix("2001:db8:1::/64");
+
+        final AttributesBuilder paBuilder = new AttributesBuilder();
+        paBuilder.setOrigin(new OriginBuilder().setValue(BgpOrigin.Igp).build());
+        paBuilder.setAsPath(new AsPathBuilder().setSegments(asPath).build());
+
+        final MpReachNlriBuilder mpReachBuilder = new MpReachNlriBuilder();
+        mpReachBuilder.setAfi(Ipv6AddressFamily.class);
+        mpReachBuilder.setSafi(UnicastSubsequentAddressFamily.class);
+        mpReachBuilder.setCNextHop(nextHop);
+        mpReachBuilder.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
+            new DestinationIpv6CaseBuilder().setDestinationIpv6(
+                new DestinationIpv6Builder().setIpv6Prefixes(Lists.newArrayList(new Ipv6PrefixesBuilder().setPrefix(pref1).build(), new Ipv6PrefixesBuilder().setPrefix(pref2).build(), new Ipv6PrefixesBuilder().setPrefix(pref3).build())).build()).build()).build());
+
+        paBuilder.addAugmentation(Attributes1.class, new Attributes1Builder().setMpReachNlri(mpReachBuilder.build()).build());
+
+        builder.setAttributes(paBuilder.build());
+
+        return builder.build();
+    }
+
+    @Test
+    public void testGetOpenMessage() throws BGPParsingException, BGPDocumentedException, IOException {
+        final Map<ByteBuf, Notification> openMap = Maps.newHashMap();
+
+        final Set<BgpTableType> type = Sets.newHashSet();
+        type.add(new BgpTableTypeImpl(Ipv4AddressFamily.class, MplsLabeledVpnSubsequentAddressFamily.class));
+
+        final List<BgpParameters> params = Lists.newArrayList();
+
+        final CParameters par = new MultiprotocolCaseBuilder().setMultiprotocolCapability(
+            new MultiprotocolCapabilityBuilder().setAfi(Ipv4AddressFamily.class).setSafi(MplsLabeledVpnSubsequentAddressFamily.class).build()).build();
+        params.add(new BgpParametersBuilder().setOptionalCapabilities(Lists.newArrayList(new OptionalCapabilitiesBuilder().setCParameters(par).build())).build());
+
+        final byte[] input = new byte[] { 5, 8, 13, 21 };
+
+        openMap.put(Unpooled.copiedBuffer(input), new OpenBuilder().setMyAsNumber(30).setHoldTimer(30).setBgpParameters(params).setVersion(
+            new ProtocolVersion((short) 4)).build());
+
+        final BGPMessageParserMock mockParser = new BGPMessageParserMock(openMap);
+
+        final Set<BgpTableType> result = Sets.newHashSet();
+        for (final BgpParameters p : ((Open) mockParser.parseMessage(Unpooled.copiedBuffer(input))).getBgpParameters()) {
+            for (final OptionalCapabilities capa : p.getOptionalCapabilities()) {
+                final CParameters cp = capa.getCParameters();
+                final BgpTableType t = new BgpTableTypeImpl(((MultiprotocolCase) cp).getMultiprotocolCapability().getAfi(), ((MultiprotocolCase) cp).getMultiprotocolCapability().getSafi());
+                result.add(t);
+            }
+        }
+        assertEquals(type, result);
+    }
 }