Mass-convert all compontents to use -no-zone addresses
[bgpcep.git] / bgp / parser-mock / src / test / java / org / opendaylight / protocol / bgp / parser / mock / BGPMessageParserMockTest.java
index f1911ed2fb92f0dbf71436693e675d8736c622a1..6bdd9532a608b338481f1a3746c4dfc30d770a31 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 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.HashMap;
 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.BGPObject;
-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.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 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpAddressFamily;
-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.BgpSubsequentAddressFamily;
-
-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.rev130715.AsNumber;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6AddressNoZone;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.prefixes.DestinationIpv6Builder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.prefixes.destination.ipv6.Ipv6PrefixesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationIpv6CaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Open;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.OpenBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.ProtocolVersion;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Update;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.UpdateBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.BgpParameters;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.BgpParametersBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.bgp.parameters.OptionalCapabilities;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.bgp.parameters.OptionalCapabilitiesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.bgp.parameters.optional.capabilities.CParameters;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.open.message.bgp.parameters.optional.capabilities.CParametersBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.AsPathBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.OriginBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.as.path.Segments;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.as.path.SegmentsBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1Builder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.BgpTableType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.CParameters1;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.CParameters1Builder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.mp.capabilities.MultiprotocolCapabilityBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlriBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.BgpOrigin;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv4AddressFamily;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv6AddressFamily;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.MplsLabeledVpnSubsequentAddressFamily;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.UnicastSubsequentAddressFamily;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.CNextHop;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.Ipv6NextHopCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.ipv6.next.hop._case.Ipv6NextHopBuilder;
+import org.opendaylight.yangtools.yang.binding.Notification;
+import org.opendaylight.yangtools.yang.common.Uint16;
+import org.opendaylight.yangtools.yang.common.Uint32;
+import org.opendaylight.yangtools.yang.common.Uint8;
 
 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[], List<BGPMessage>> updateMap = Maps.newHashMap();
-               for (int i = 0; i < this.inputBytes.length; i++) {
-                       updateMap.put(this.inputBytes[i], Lists.newArrayList((BGPMessage) 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]).get(0));
-               }
-               assertThat(this.messages.get(3), not(mockParser.parse(this.inputBytes[8]).get(0)));
-       }
-
-       /**
-        * 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[], List<BGPMessage>> updateMap = Maps.newHashMap();
-               for (int i = 0; i < this.inputBytes.length; i++) {
-                       updateMap.put(this.inputBytes[i], Lists.newArrayList((BGPMessage) 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 long 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[], List<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, Lists.newArrayList((BGPMessage) new BGPOpenMessage(new AsNumber((long) 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).get(0)).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 = new ArrayList<>();
+
+    @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(fillMessages(i));
+        }
+    }
+
+    /**
+     * Test if mock implementation of parser returns correct message.
+     */
+    @Test
+    public void testGetUpdateMessage() throws BGPParsingException, BGPDocumentedException {
+        final Map<ByteBuf, Notification> updateMap = new HashMap<>();
+        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]), null));
+        }
+        assertNotSame(this.messages.get(3),
+                mockParser.parseMessage(Unpooled.copiedBuffer(this.inputBytes[8]), null));
+    }
+
+    /**
+     * Test if method throws IllegalArgumentException after finding no BGPUpdateMessage
+     * associated with given byte[] key.
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testGetUpdateMessageException() throws BGPParsingException, BGPDocumentedException {
+        final Map<ByteBuf, Notification> updateMap = new HashMap<>();
+        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}), null);
+    }
+
+    /**
+     * 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 read;
+        try (InputStream is = this.getClass().getResourceAsStream("/up" + fileNumber + ".bin")) {
+            while ((read = is.read(data, 0, data.length)) != -1) {
+                bis.write(data, 0, read);
+            }
+            bis.flush();
+            is.close();
+        }
+        return bis.toByteArray();
+    }
+
+    /**
+     * Helper method to fill messages variable.
+     *
+     * @param asn this parameter is passed to ASNumber constructor
+     */
+    private static Update fillMessages(final long asn) {
+
+        final UpdateBuilder builder = new UpdateBuilder();
+
+        final List<Segments> asPath = new ArrayList<>();
+        asPath.add(new SegmentsBuilder().setAsSequence(Lists.newArrayList(new AsNumber(Uint32.valueOf(asn)))).build());
+        final CNextHop nextHop = new Ipv6NextHopCaseBuilder().setIpv6NextHop(
+                new Ipv6NextHopBuilder().setGlobal(new Ipv6AddressNoZone("2001:db8::1"))
+                        .setLinkLocal(new Ipv6AddressNoZone("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 {
+        final Map<ByteBuf, Notification> openMap = new HashMap<>();
+
+        final Set<BgpTableType> type = new HashSet<>();
+        type.add(new BgpTableTypeImpl(Ipv4AddressFamily.class, MplsLabeledVpnSubsequentAddressFamily.class));
+
+        final List<BgpParameters> params = new ArrayList<>();
+
+        final CParameters par = new CParametersBuilder().addAugmentation(CParameters1.class, new CParameters1Builder()
+                .setMultiprotocolCapability(new MultiprotocolCapabilityBuilder().setAfi(Ipv4AddressFamily.class)
+                        .setSafi(MplsLabeledVpnSubsequentAddressFamily.class).build()).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(Uint16.valueOf(30))
+            .setHoldTimer(Uint16.valueOf(30))
+            .setBgpParameters(params)
+            .setVersion(new ProtocolVersion(Uint8.valueOf(4)))
+            .build());
+
+        final BGPMessageParserMock mockParser = new BGPMessageParserMock(openMap);
+
+        final Set<BgpTableType> result = new HashSet<>();
+        for (final BgpParameters p : ((Open) mockParser.parseMessage(Unpooled.copiedBuffer(input), null))
+                .getBgpParameters()) {
+            for (final OptionalCapabilities capa : p.getOptionalCapabilities()) {
+                final CParameters cp = capa.getCParameters();
+                if (cp.augmentation(CParameters1.class) != null && cp.augmentation(CParameters1.class)
+                        .getMultiprotocolCapability() != null) {
+                    final BgpTableType t = new BgpTableTypeImpl(cp.augmentation(CParameters1.class)
+                            .getMultiprotocolCapability().getAfi(),
+                            cp.augmentation(CParameters1.class).getMultiprotocolCapability().getSafi());
+                    result.add(t);
+                }
+            }
+        }
+        assertEquals(type, result);
+    }
 }