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.rev180329.path.attributes.Attributes;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.Aigp;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.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 }), 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.");
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,
56 Unpooled.copiedBuffer(value), buffer);
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();
65 assertNotNull("Tlv should not be null.", tlv);
66 assertEquals("Aigp tlv should have metric with value 8.", 8,
67 tlv.getMetric().getValue().intValue());
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();
76 AttributeUtil.formatAttribute(AttributeUtil.OPTIONAL, AigpAttributeParser.TYPE,
77 Unpooled.copiedBuffer(value), inputData);
79 final BGPExtensionProviderContext providerContext = ServiceLoaderBGPExtensionProviderContext
80 .getSingletonInstance();
81 final Attributes pathAttributes = providerContext.getAttributeRegistry()
82 .parseAttributes(inputData, null);
83 final Aigp aigp = pathAttributes.getAigp();
85 final AttributesBuilder pathAttributesBuilder = new AttributesBuilder();
86 pathAttributesBuilder.setAigp(aigp);
88 final AigpAttributeParser parser = new AigpAttributeParser();
89 parser.serializeAttribute(pathAttributesBuilder.build(), testBuffer);
91 final byte[] unparserData = inputData.copy(0, inputData.writerIndex()).array();
92 final byte[] serializedData = testBuffer.copy(0, inputData.writerIndex()).array();
94 assertTrue("Buffers should be the same.", Arrays.equals(unparserData, serializedData));