47a8863c738faec50bec64fee3d7e53bd2cc29ab
[bgpcep.git] / bgp / parser-impl / src / test / java / org / opendaylight / protocol / bgp / parser / impl / PathAttributeParserTest.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.impl;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15
16 import io.netty.buffer.ByteBuf;
17 import io.netty.buffer.Unpooled;
18 import java.util.Arrays;
19 import org.junit.Test;
20 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
21 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
22 import org.opendaylight.protocol.bgp.parser.impl.message.update.AigpAttributeParser;
23 import org.opendaylight.protocol.bgp.parser.spi.AttributeUtil;
24 import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderContext;
25 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.Attributes;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.AttributesBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.Aigp;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.attributes.aigp.AigpTlv;
30
31 /*
32  * This class is aimed to test parsing and serializing path attributes.
33  */
34 public class PathAttributeParserTest {
35
36     @Test
37     public void testOriginParser() {
38         try {
39             ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getAttributeRegistry().parseAttributes(
40                     Unpooled.copiedBuffer(new byte[] { 0x40, 0x01, 0x01, 0x04 }), null);
41             fail("This needs to fail.");
42         } catch (final BGPDocumentedException e) {
43             assertEquals("Unknown Origin type.", e.getMessage());
44             assertArrayEquals(new byte[] { 0x01, 0x01, 0x04 }, e.getData());
45         } catch (final BGPParsingException e) {
46             fail("This exception should not occur.");
47         }
48     }
49
50     @Test
51     public void testParsingAigpAttributeWithCorrectTLV() throws BGPDocumentedException, BGPParsingException {
52         final byte[] value = new byte[] { 1, 0, 11, 0, 0, 0, 0, 0, 0, 0, 8 };
53         final ByteBuf buffer = Unpooled.buffer();
54
55         AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL, AigpAttributeParser.TYPE,
56             Unpooled.copiedBuffer(value), buffer);
57
58         final BGPExtensionProviderContext providerContext = ServiceLoaderBGPExtensionProviderContext
59             .getSingletonInstance();
60         final Attributes pathAttributes = providerContext.getAttributeRegistry()
61             .parseAttributes(buffer, null);
62         final Aigp aigp = pathAttributes.getAigp();
63         final AigpTlv tlv = aigp.getAigpTlv();
64
65         assertNotNull("Tlv should not be null.", tlv);
66         assertEquals("Aigp tlv should have metric with value 8.", 8,
67             tlv.getMetric().getValue().intValue());
68     }
69
70     @Test
71     public void testSerializingAigpAttribute() throws BGPDocumentedException, BGPParsingException {
72         final byte[] value = new byte[] { 1, 0, 11, 0, 0, 0, 0, 0, 0, 0, 8 };
73         final ByteBuf inputData = Unpooled.buffer();
74         final ByteBuf testBuffer = Unpooled.buffer();
75
76         AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL, AigpAttributeParser.TYPE,
77             Unpooled.copiedBuffer(value), inputData);
78
79         final BGPExtensionProviderContext providerContext = ServiceLoaderBGPExtensionProviderContext
80             .getSingletonInstance();
81         final Attributes pathAttributes = providerContext.getAttributeRegistry()
82             .parseAttributes(inputData, null);
83         final Aigp aigp = pathAttributes.getAigp();
84
85         final AttributesBuilder pathAttributesBuilder = new AttributesBuilder();
86         pathAttributesBuilder.setAigp(aigp);
87
88         final AigpAttributeParser parser = new AigpAttributeParser();
89         parser.serializeAttribute(pathAttributesBuilder.build(), testBuffer);
90
91         final byte[] unparserData = inputData.copy(0, inputData.writerIndex()).array();
92         final byte[] serializedData = testBuffer.copy(0, inputData.writerIndex()).array();
93
94         assertTrue("Buffers should be the same.", Arrays.equals(unparserData, serializedData));
95     }
96 }