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.impl;
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;
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.rev130919.path.attributes.Attributes;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AttributesBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.Aigp;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.aigp.AigpTlv;
32 * This class is aimed to test parsing and serializing path attributes.
34 public class PathAttributeParserTest {
37 public void testOriginParser() {
39 ServiceLoaderBGPExtensionProviderContext.getSingletonInstance().getAttributeRegistry().parseAttributes(
40 Unpooled.copiedBuffer(new byte[] { 0x40, 0x01, 0x01, 0x04 }));
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.");
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();
55 AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL, AigpAttributeParser.TYPE, Unpooled.copiedBuffer(value), buffer);
57 final BGPExtensionProviderContext providerContext = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance();
58 final Attributes pathAttributes = providerContext.getAttributeRegistry().parseAttributes(buffer);
59 final Aigp aigp = pathAttributes.getAigp();
60 final AigpTlv tlv = aigp.getAigpTlv();
62 assertNotNull("Tlv should not be null.", tlv);
63 assertEquals("Aigp tlv should have metric with value 8.", 8, tlv.getMetric().getValue().intValue());
67 public void testSerializingAigpAttribute() throws BGPDocumentedException, BGPParsingException {
68 final byte[] value = new byte[] { 1, 0, 11, 0, 0, 0, 0, 0, 0, 0, 8 };
69 final ByteBuf inputData = Unpooled.buffer();
70 final ByteBuf testBuffer = Unpooled.buffer();
72 AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL, AigpAttributeParser.TYPE, Unpooled.copiedBuffer(value), inputData);
74 final BGPExtensionProviderContext providerContext = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance();
75 final Attributes pathAttributes = providerContext.getAttributeRegistry().parseAttributes(inputData);
76 final Aigp aigp = pathAttributes.getAigp();
78 final AttributesBuilder pathAttributesBuilder = new AttributesBuilder();
79 pathAttributesBuilder.setAigp(aigp);
81 final AigpAttributeParser parser = new AigpAttributeParser();
82 parser.serializeAttribute(pathAttributesBuilder.build(), testBuffer);
84 final byte[] unparserData = inputData.copy(0, inputData.writerIndex()).array();
85 final byte[] serializedData = testBuffer.copy(0, inputData.writerIndex()).array();
87 assertTrue("Buffers should be the same.", Arrays.equals(unparserData, serializedData));