Initial framework migration to netty.
[bgpcep.git] / bgp / parser-mock / src / test / java / org / opendaylight / protocol / bgp / parser / mock / BGPMessageParserMockTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.bgp.parser.mock;
9
10 import static org.hamcrest.core.IsNot.not;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertThat;
13
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;
23 import java.util.Map;
24 import java.util.Set;
25
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.BGPOrigin;
32 import org.opendaylight.protocol.bgp.concepts.BGPSubsequentAddressFamily;
33 import org.opendaylight.protocol.bgp.concepts.BGPTableType;
34 import org.opendaylight.protocol.bgp.concepts.BaseBGPObjectState;
35 import org.opendaylight.protocol.bgp.concepts.Community;
36 import org.opendaylight.protocol.bgp.concepts.ExtendedCommunity;
37 import org.opendaylight.protocol.bgp.concepts.IPv6NextHop;
38 import org.opendaylight.protocol.bgp.concepts.NextHop;
39 import org.opendaylight.protocol.bgp.linkstate.NetworkObjectState;
40 import org.opendaylight.protocol.bgp.linkstate.NetworkRouteState;
41 import org.opendaylight.protocol.bgp.parser.BGPMessage;
42 import org.opendaylight.protocol.bgp.parser.BGPParameter;
43 import org.opendaylight.protocol.bgp.parser.BGPRoute;
44 import org.opendaylight.protocol.bgp.parser.BGPUpdateMessage;
45 import org.opendaylight.protocol.bgp.parser.impl.BGPUpdateMessageImpl;
46 import org.opendaylight.protocol.bgp.parser.message.BGPOpenMessage;
47 import org.opendaylight.protocol.bgp.parser.parameter.MultiprotocolCapability;
48 import org.opendaylight.protocol.bgp.util.BGPIPv6RouteImpl;
49 import org.opendaylight.protocol.concepts.ASNumber;
50 import org.opendaylight.protocol.concepts.IPv6Address;
51 import org.opendaylight.protocol.concepts.IPv6Prefix;
52 import org.opendaylight.protocol.concepts.Identifier;
53 import org.opendaylight.protocol.concepts.Prefix;
54 import org.opendaylight.protocol.framework.DeserializerException;
55 import org.opendaylight.protocol.framework.DocumentedException;
56
57 import com.google.common.collect.Lists;
58 import com.google.common.collect.Maps;
59 import com.google.common.collect.Sets;
60
61 public class BGPMessageParserMockTest {
62
63         private final byte[][] inputBytes = new byte[11][];
64         private final List<BGPUpdateMessage> messages = new ArrayList<BGPUpdateMessage>();
65
66         @Before
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));
72                 }
73         }
74
75         /**
76          * Test if mock implementation of parser returns correct message
77          * 
78          * @throws DocumentedException
79          * @throws DeserializerException
80          * @throws IOException
81          */
82         @Test
83         public void testGetUpdateMessage() throws DeserializerException, DocumentedException, IOException {
84                 final Map<byte[], BGPMessage> updateMap = Maps.newHashMap();
85                 for (int i = 0; i < this.inputBytes.length; i++) {
86                         updateMap.put(this.inputBytes[i], this.messages.get(i));
87                 }
88
89                 final BGPMessageParserMock mockParser = new BGPMessageParserMock(updateMap);
90
91                 for (int i = 0; i < this.inputBytes.length; i++) {
92                         assertEquals(this.messages.get(i), mockParser.parse(this.inputBytes[i]));
93                 }
94                 assertThat(this.messages.get(3), not(mockParser.parse(this.inputBytes[8])));
95         }
96
97         /**
98          * Test if method throws IllegalArgumentException after finding no BGPUpdateMessage associated with given byte[] key
99          * 
100          * @throws DocumentedException
101          * @throws DeserializerException
102          * @throws IOException
103          */
104         @Test(expected = IllegalArgumentException.class)
105         public void testGetUpdateMessageException() throws DeserializerException, DocumentedException, IOException {
106                 final Map<byte[], BGPMessage> updateMap = Maps.newHashMap();
107                 for (int i = 0; i < this.inputBytes.length; i++) {
108                         updateMap.put(this.inputBytes[i], this.messages.get(i));
109                 }
110
111                 final BGPMessageParserMock mockParser = new BGPMessageParserMock(updateMap);
112                 mockParser.parse(new byte[] { 7, 4, 6 });
113         }
114
115         /**
116          * Helper method to fill inputBytes variable
117          * 
118          * @param fileNumber parameter to distinguish between files from which bytes are read
119          */
120         private byte[] fillInputBytes(final int fileNumber) throws Exception {
121
122                 final InputStream is = this.getClass().getResourceAsStream("/up" + fileNumber + ".bin");
123                 final ByteArrayOutputStream bis = new ByteArrayOutputStream();
124                 final byte[] data = new byte[60];
125                 int nRead = 0;
126
127                 while ((nRead = is.read(data, 0, data.length)) != -1) {
128                         bis.write(data, 0, nRead);
129                 }
130                 bis.flush();
131                 return bis.toByteArray();
132         }
133
134         /**
135          * Helper method to fill messages variable
136          * 
137          * @param asn this parameter is passed to ASNumber constructor
138          */
139         private BGPUpdateMessage fillMessages(final int asn) throws UnknownHostException {
140
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")));
145
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);
149
150                 final Set<BGPObject> addedObjects = new HashSet<BGPObject>();
151
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);
154
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);
161
162                 return new BGPUpdateMessageImpl(addedObjects, Collections.<Identifier> emptySet());
163         }
164
165         @Test
166         public void testGetOpenMessage() throws DeserializerException, DocumentedException, IOException {
167                 final Map<byte[], BGPMessage> openMap = Maps.newHashMap();
168
169                 final Set<BGPTableType> type = Sets.newHashSet();
170                 type.add(new BGPTableType(BGPAddressFamily.IPv4, BGPSubsequentAddressFamily.MPLSLabeledVPN));
171
172                 final List<BGPParameter> params = Lists.newArrayList();
173                 params.add(new MultiprotocolCapability(new BGPTableType(BGPAddressFamily.IPv4, BGPSubsequentAddressFamily.MPLSLabeledVPN)));
174
175                 final byte[] input = new byte[] { 5, 8, 13, 21 };
176
177                 openMap.put(input, new BGPOpenMessage(new ASNumber(30), (short) 30, null, params));
178
179                 final BGPMessageParserMock mockParser = new BGPMessageParserMock(openMap);
180
181                 final Set<BGPTableType> result = Sets.newHashSet();
182                 for (final BGPParameter p : ((BGPOpenMessage) mockParser.parse(input)).getOptParams()) {
183                         if (p instanceof MultiprotocolCapability) {
184                                 result.add(((MultiprotocolCapability) p).getTableType());
185                         }
186                 }
187
188                 assertEquals(type, result);
189         }
190 }