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 org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
11 import org.opendaylight.protocol.bgp.parser.BGPError;
12 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
13 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
14 import org.opendaylight.protocol.bgp.parser.spi.AttributeRegistry;
15 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
16 import org.opendaylight.protocol.concepts.HandlerRegistry;
17 import org.opendaylight.protocol.util.ByteArray;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.update.PathAttributes;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.update.PathAttributesBuilder;
20 import org.opendaylight.yangtools.yang.binding.DataContainer;
21 import org.opendaylight.yangtools.yang.binding.DataObject;
23 import com.google.common.base.Preconditions;
24 import com.google.common.primitives.UnsignedBytes;
26 public final class SimpleAttributeRegistry implements AttributeRegistry {
27 private final HandlerRegistry<DataContainer, AttributeParser, AttributeSerializer> handlers = new HandlerRegistry<>();
30 public AutoCloseable registerAttributeParser(final int attributeType, final AttributeParser parser) {
31 Preconditions.checkArgument(attributeType >= 0 && attributeType <= 255);
32 return handlers.registerParser(attributeType, parser);
36 public AutoCloseable registerAttributeSerializer(final Class<? extends DataObject> paramClass, final AttributeSerializer serializer) {
37 return handlers.registerSerializer(paramClass, serializer);
40 private int parseAttribute( final byte[] bytes, final int offset, final PathAttributesBuilder builder)
41 throws BGPDocumentedException, BGPParsingException {
42 // FIXME: validate minimum length
43 final boolean[] flags = ByteArray.parseBits(bytes[offset]);
44 final int type = UnsignedBytes.toInt(bytes[offset + 1]);
48 len = UnsignedBytes.toInt(bytes[offset + 2]) * 256 + UnsignedBytes.toInt(bytes[offset + 3]);
51 len = UnsignedBytes.toInt(bytes[offset + 2]);
55 final AttributeParser parser = handlers.getParser(type);
58 throw new BGPDocumentedException("Well known attribute not recognized.", BGPError.WELL_KNOWN_ATTR_NOT_RECOGNIZED);
61 parser.parseAttribute(ByteArray.subByte(bytes, offset + hdrlen, len), builder);
68 public PathAttributes parseAttributes(final byte[] bytes) throws BGPDocumentedException, BGPParsingException {
70 final PathAttributesBuilder builder = new PathAttributesBuilder();
71 while (byteOffset < bytes.length) {
72 byteOffset += parseAttribute(bytes, byteOffset, builder);
74 return builder.build();
78 public byte[] serializeAttribute(final DataObject attribute) {
79 final AttributeSerializer serializer = handlers.getSerializer(attribute.getImplementedInterface());
80 if (serializer == null) {
84 return serializer.serializeAttribute(attribute);