2 * Copyright (c) 2015 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.spi.pojo;
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertThrows;
13 import static org.junit.Assert.assertTrue;
15 import io.netty.buffer.Unpooled;
17 import org.junit.Test;
18 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
19 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
20 import org.opendaylight.protocol.util.ByteArray;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.UnrecognizedAttributes;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.attributes.UnrecognizedAttributesKey;
24 public class UnrecognizedAttributesTest {
26 private static final int UNRECOGNIZED_ATTRIBUTE_COUNT = 1;
27 private static final int FIRST_ATTRIBUTE = 0;
28 private static final short NON_EXISTENT_TYPE = 0;
29 private static final int NON_VALUE_BYTES = 3;
31 private static final SimpleAttributeRegistry SIMPLE_ATTR_REG = new SimpleAttributeRegistry();
34 public void testUnrecognizedAttributesWithoutOptionalFlag() throws BGPDocumentedException, BGPParsingException {
35 final BGPDocumentedException ex = assertThrows(BGPDocumentedException.class, () -> {
36 SIMPLE_ATTR_REG.parseAttributes(
37 Unpooled.wrappedBuffer(new byte[] { 0x03, 0x00, 0x05, 0x01, 0x02, 0x03, 0x04, 0x05 }), null);
40 assertEquals("Well known attribute not recognized.", ex.getMessage());
44 public void testUnrecognizedAttributes() throws BGPDocumentedException, BGPParsingException {
45 final byte[] attributeBytes = { (byte)0xe0, 0x00, 0x05, 0x01, 0x02, 0x03, 0x04, 0x05 };
46 final Map<UnrecognizedAttributesKey, UnrecognizedAttributes> unrecogAttribs = SIMPLE_ATTR_REG.parseAttributes(
47 Unpooled.wrappedBuffer(attributeBytes), null).getAttributes().getUnrecognizedAttributes();
48 assertEquals(UNRECOGNIZED_ATTRIBUTE_COUNT, unrecogAttribs.size());
49 final UnrecognizedAttributes unrecogAttrib = unrecogAttribs.values().iterator().next();
50 final UnrecognizedAttributesKey expectedAttribKey =
51 new UnrecognizedAttributesKey(unrecogAttrib.getType());
53 assertTrue(unrecogAttrib.isPartial());
54 assertTrue(unrecogAttrib.isTransitive());
55 assertArrayEquals(ByteArray.cutBytes(attributeBytes, NON_VALUE_BYTES), unrecogAttrib.getValue());
56 assertEquals(NON_EXISTENT_TYPE, unrecogAttrib.getType().shortValue());
57 assertEquals(expectedAttribKey, unrecogAttrib.key());