2 * Copyright (c) 2013 Cisco Systems, Inc. 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.parser.mock;
10 import static org.hamcrest.core.IsNot.not;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertThat;
14 import java.io.ByteArrayOutputStream;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.net.InetAddress;
18 import java.net.UnknownHostException;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.HashSet;
22 import java.util.List;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.opendaylight.protocol.bgp.concepts.ASPath;
29 import org.opendaylight.protocol.bgp.concepts.BGPAddressFamily;
30 import org.opendaylight.protocol.bgp.concepts.BGPObject;
31 import org.opendaylight.protocol.bgp.concepts.BGPTableType;
32 import org.opendaylight.protocol.bgp.concepts.BaseBGPObjectState;
33 import org.opendaylight.protocol.bgp.concepts.Community;
34 import org.opendaylight.protocol.bgp.concepts.ExtendedCommunity;
35 import org.opendaylight.protocol.bgp.concepts.IPv6NextHop;
36 import org.opendaylight.protocol.bgp.concepts.NextHop;
37 import org.opendaylight.protocol.bgp.linkstate.NetworkObjectState;
38 import org.opendaylight.protocol.bgp.linkstate.NetworkRouteState;
39 import org.opendaylight.protocol.bgp.parser.BGPMessage;
40 import org.opendaylight.protocol.bgp.parser.BGPParameter;
41 import org.opendaylight.protocol.bgp.parser.BGPRoute;
42 import org.opendaylight.protocol.bgp.parser.BGPUpdateMessage;
43 import org.opendaylight.protocol.bgp.parser.impl.BGPUpdateMessageImpl;
44 import org.opendaylight.protocol.bgp.parser.message.BGPOpenMessage;
45 import org.opendaylight.protocol.bgp.parser.parameter.MultiprotocolCapability;
46 import org.opendaylight.protocol.bgp.util.BGPIPv6RouteImpl;
47 import org.opendaylight.protocol.concepts.ASNumber;
48 import org.opendaylight.protocol.concepts.IPv6Address;
49 import org.opendaylight.protocol.concepts.IPv6Prefix;
50 import org.opendaylight.protocol.concepts.Identifier;
51 import org.opendaylight.protocol.concepts.Prefix;
52 import org.opendaylight.protocol.framework.DeserializerException;
53 import org.opendaylight.protocol.framework.DocumentedException;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpOrigin;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpSubsequentAddressFamily;
57 import com.google.common.collect.Lists;
58 import com.google.common.collect.Maps;
59 import com.google.common.collect.Sets;
61 public class BGPMessageParserMockTest {
63 private final byte[][] inputBytes = new byte[11][];
64 private final List<BGPUpdateMessage> messages = new ArrayList<BGPUpdateMessage>();
67 public void init() throws Exception {
68 // Creating input bytes and update messages
69 for (int i = 0; i < this.inputBytes.length; i++) {
70 this.inputBytes[i] = this.fillInputBytes(i);
71 this.messages.add(this.fillMessages(i));
76 * Test if mock implementation of parser returns correct message
78 * @throws DocumentedException
79 * @throws DeserializerException
83 public void testGetUpdateMessage() throws DeserializerException, DocumentedException, IOException {
84 final Map<byte[], List<BGPMessage>> updateMap = Maps.newHashMap();
85 for (int i = 0; i < this.inputBytes.length; i++) {
86 updateMap.put(this.inputBytes[i], Lists.newArrayList((BGPMessage) this.messages.get(i)));
89 final BGPMessageParserMock mockParser = new BGPMessageParserMock(updateMap);
91 for (int i = 0; i < this.inputBytes.length; i++) {
92 assertEquals(this.messages.get(i), mockParser.parse(this.inputBytes[i]).get(0));
94 assertThat(this.messages.get(3), not(mockParser.parse(this.inputBytes[8]).get(0)));
98 * Test if method throws IllegalArgumentException after finding no BGPUpdateMessage associated with given byte[] key
100 * @throws DocumentedException
101 * @throws DeserializerException
102 * @throws IOException
104 @Test(expected = IllegalArgumentException.class)
105 public void testGetUpdateMessageException() throws DeserializerException, DocumentedException, IOException {
106 final Map<byte[], List<BGPMessage>> updateMap = Maps.newHashMap();
107 for (int i = 0; i < this.inputBytes.length; i++) {
108 updateMap.put(this.inputBytes[i], Lists.newArrayList((BGPMessage) this.messages.get(i)));
111 final BGPMessageParserMock mockParser = new BGPMessageParserMock(updateMap);
112 mockParser.parse(new byte[] { 7, 4, 6 });
116 * Helper method to fill inputBytes variable
118 * @param fileNumber parameter to distinguish between files from which bytes are read
120 private byte[] fillInputBytes(final int fileNumber) throws Exception {
122 final InputStream is = this.getClass().getResourceAsStream("/up" + fileNumber + ".bin");
123 final ByteArrayOutputStream bis = new ByteArrayOutputStream();
124 final byte[] data = new byte[60];
127 while ((nRead = is.read(data, 0, data.length)) != -1) {
128 bis.write(data, 0, nRead);
131 return bis.toByteArray();
135 * Helper method to fill messages variable
137 * @param asn this parameter is passed to ASNumber constructor
139 private BGPUpdateMessage fillMessages(final int asn) throws UnknownHostException {
141 final List<ASNumber> asnums = new ArrayList<ASNumber>();
142 asnums.add(new ASNumber(asn));
143 final ASPath asPath = new ASPath(asnums);
144 final NextHop<IPv6Address> nextHop = new IPv6NextHop(new IPv6Address(InetAddress.getByName("2001:db8::1")), new IPv6Address(InetAddress.getByName("fe80::c001:bff:fe7e:0")));
146 final Prefix<IPv6Address> pref1 = new IPv6Prefix(new IPv6Address(InetAddress.getByName("2001:db8:1:2::")), 64);
147 final Prefix<IPv6Address> pref2 = new IPv6Prefix(new IPv6Address(InetAddress.getByName("2001:db8:1:1::")), 64);
148 final Prefix<IPv6Address> pref3 = new IPv6Prefix(new IPv6Address(InetAddress.getByName("2001:db8:1::")), 64);
150 final Set<BGPObject> addedObjects = new HashSet<BGPObject>();
152 final NetworkRouteState<IPv6Address> nstate = new NetworkRouteState<>(new NetworkObjectState(asPath, Collections.<Community> emptySet(), Collections.<ExtendedCommunity> emptySet()), nextHop);
153 final BaseBGPObjectState state = new BaseBGPObjectState(BgpOrigin.Igp, null);
155 final BGPRoute<IPv6Address> route1 = new BGPIPv6RouteImpl(pref1, state, nstate);
156 final BGPRoute<IPv6Address> route2 = new BGPIPv6RouteImpl(pref2, state, nstate);
157 final BGPRoute<IPv6Address> route3 = new BGPIPv6RouteImpl(pref3, state, nstate);
158 addedObjects.add(route1);
159 addedObjects.add(route2);
160 addedObjects.add(route3);
162 return new BGPUpdateMessageImpl(addedObjects, Collections.<Identifier> emptySet());
166 public void testGetOpenMessage() throws DeserializerException, DocumentedException, IOException {
167 final Map<byte[], List<BGPMessage>> openMap = Maps.newHashMap();
169 final Set<BGPTableType> type = Sets.newHashSet();
170 type.add(new BGPTableType(BGPAddressFamily.IPv4, BgpSubsequentAddressFamily.MplsLabeledVpn));
172 final List<BGPParameter> params = Lists.newArrayList();
173 params.add(new MultiprotocolCapability(new BGPTableType(BGPAddressFamily.IPv4, BgpSubsequentAddressFamily.MplsLabeledVpn)));
175 final byte[] input = new byte[] { 5, 8, 13, 21 };
177 openMap.put(input, Lists.newArrayList((BGPMessage) new BGPOpenMessage(new ASNumber(30), (short) 30, null, params)));
179 final BGPMessageParserMock mockParser = new BGPMessageParserMock(openMap);
181 final Set<BGPTableType> result = Sets.newHashSet();
182 for (final BGPParameter p : ((BGPOpenMessage) mockParser.parse(input).get(0)).getOptParams()) {
183 if (p instanceof MultiprotocolCapability) {
184 result.add(((MultiprotocolCapability) p).getTableType());
188 assertEquals(type, result);