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.spi.pojo;
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 final class SimpleAttributeRegistry implements AttributeRegistry {
27 private final HandlerRegistry<DataContainer, AttributeParser, AttributeSerializer> handlers = new HandlerRegistry<>();
29 AutoCloseable registerAttributeParser(final int attributeType, final AttributeParser parser) {
30 Preconditions.checkArgument(attributeType >= 0 && attributeType <= 255);
31 return handlers.registerParser(attributeType, parser);
34 AutoCloseable registerAttributeSerializer(final Class<? extends DataObject> paramClass, final AttributeSerializer serializer) {
35 return handlers.registerSerializer(paramClass, serializer);
38 private int parseAttribute( final byte[] bytes, final int offset, final PathAttributesBuilder builder)
39 throws BGPDocumentedException, BGPParsingException {
40 // FIXME: validate minimum length
41 final boolean[] flags = ByteArray.parseBits(bytes[offset]);
42 final int type = UnsignedBytes.toInt(bytes[offset + 1]);
46 len = UnsignedBytes.toInt(bytes[offset + 2]) * 256 + UnsignedBytes.toInt(bytes[offset + 3]);
49 len = UnsignedBytes.toInt(bytes[offset + 2]);
53 final AttributeParser parser = handlers.getParser(type);
56 throw new BGPDocumentedException("Well known attribute not recognized.", BGPError.WELL_KNOWN_ATTR_NOT_RECOGNIZED);
59 parser.parseAttribute(ByteArray.subByte(bytes, offset + hdrlen, len), builder);
66 public PathAttributes parseAttributes(final byte[] bytes) throws BGPDocumentedException, BGPParsingException {
68 final PathAttributesBuilder builder = new PathAttributesBuilder();
69 while (byteOffset < bytes.length) {
70 byteOffset += parseAttribute(bytes, byteOffset, builder);
72 return builder.build();
76 public byte[] serializeAttribute(final DataObject attribute) {
77 final AttributeSerializer serializer = handlers.getSerializer(attribute.getImplementedInterface());
78 if (serializer == null) {
82 return serializer.serializeAttribute(attribute);