MVPN RFC6514 Extendend communities
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / protocol / bgp / rib / impl / BGPParserTest.java
1 /*
2  * Copyright (c) 2014 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.rib.impl;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import java.util.ArrayList;
17 import java.util.List;
18 import org.junit.Test;
19 import org.opendaylight.protocol.bgp.parser.spi.MessageRegistry;
20 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
21 import org.opendaylight.protocol.util.ByteArray;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Keepalive;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.KeepaliveBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Notify;
25
26 public class BGPParserTest {
27
28     private final MessageRegistry registry = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getMessageRegistry();
29
30     @Test
31     public void testMessageToByteEncoding() {
32         final BGPMessageToByteEncoder encoder = new BGPMessageToByteEncoder(this.registry);
33         final ByteBuf out = Unpooled.buffer();
34         encoder.encode(null, new KeepaliveBuilder().build(), out);
35         assertArrayEquals(new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
36             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
37             (byte) 0xff, 0, 0x13, 4}, ByteArray.readAllBytes(out));
38     }
39
40     @Test
41     public void testByteToMessageEncoding() throws Exception {
42         final BGPByteToMessageDecoder decoder = new BGPByteToMessageDecoder(this.registry);
43         final List<Object> out = new ArrayList<>();
44         decoder.decode(null, Unpooled.wrappedBuffer(new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
45             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
46             (byte) 0xff, 0, 0x13, 4 }), out);
47         assertTrue(out.get(0) instanceof Keepalive);
48         decoder.decode(null, Unpooled.wrappedBuffer(new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
49             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
50             (byte) 0xff, (byte) 0xff, 0, 0x17, 3, 2, 4, 4, 9 }), out);
51         assertTrue(out.get(1) instanceof Notify);
52     }
53
54     @Test
55     public void testHandlerFactory() {
56         final BGPHandlerFactory handlers = new BGPHandlerFactory(this.registry);
57         assertEquals(1, handlers.getEncoders().length);
58         assertTrue(handlers.getEncoders()[0] instanceof BGPMessageToByteEncoder);
59         assertEquals(2, handlers.getDecoders().length);
60         assertTrue(handlers.getDecoders()[0] instanceof BGPMessageHeaderDecoder);
61         assertTrue(handlers.getDecoders()[1] instanceof BGPByteToMessageDecoder);
62     }
63 }