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 com.google.common.base.Preconditions;
11 import io.netty.buffer.ByteBuf;
12 import java.util.LinkedHashMap;
14 import java.util.Map.Entry;
15 import java.util.TreeMap;
16 import java.util.concurrent.atomic.AtomicReference;
17 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
18 import org.opendaylight.protocol.bgp.parser.BGPError;
19 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
20 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
21 import org.opendaylight.protocol.bgp.parser.spi.AttributeRegistry;
22 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
23 import org.opendaylight.protocol.concepts.AbstractRegistration;
24 import org.opendaylight.protocol.concepts.HandlerRegistry;
25 import org.opendaylight.protocol.util.BitArray;
26 import org.opendaylight.protocol.util.Values;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributes;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
29 import org.opendaylight.yangtools.yang.binding.DataContainer;
30 import org.opendaylight.yangtools.yang.binding.DataObject;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 final class SimpleAttributeRegistry implements AttributeRegistry {
36 private static final class RawAttribute {
37 private final AttributeParser parser;
38 private final ByteBuf buffer;
40 public RawAttribute(final AttributeParser parser, final ByteBuf buffer) {
41 this.parser = Preconditions.checkNotNull(parser);
42 this.buffer = Preconditions.checkNotNull(buffer);
46 private static final Logger LOG = LoggerFactory.getLogger(SimpleAttributeRegistry.class);
47 private static final int OPTIONAL_BIT = 0;
48 private static final int TRANSITIVE_BIT = 1;
49 @SuppressWarnings("unused")
50 private static final int PARTIAL_BIT = 2;
51 private static final int EXTENDED_LENGTH_BIT = 3;
52 private final HandlerRegistry<DataContainer, AttributeParser, AttributeSerializer> handlers = new HandlerRegistry<>();
53 private final Map<AbstractRegistration, AttributeSerializer> serializers = new LinkedHashMap<>();
54 private final AtomicReference<Iterable<AttributeSerializer>> roSerializers =
55 new AtomicReference<Iterable<AttributeSerializer>>(this.serializers.values());
57 AutoCloseable registerAttributeParser(final int attributeType, final AttributeParser parser) {
58 Preconditions.checkArgument(attributeType >= 0 && attributeType <= Values.UNSIGNED_BYTE_MAX_VALUE);
59 return this.handlers.registerParser(attributeType, parser);
62 synchronized AutoCloseable registerAttributeSerializer(final Class<? extends DataObject> paramClass, final AttributeSerializer serializer) {
63 final AbstractRegistration reg = this.handlers.registerSerializer(paramClass, serializer);
65 this.serializers.put(reg, serializer);
66 return new AbstractRegistration() {
68 protected void removeRegistration() {
69 synchronized (SimpleAttributeRegistry.this) {
70 SimpleAttributeRegistry.this.serializers.remove(reg);
71 SimpleAttributeRegistry.this.roSerializers.set(SimpleAttributeRegistry.this.serializers.values());
78 private void addAttribute(final ByteBuf buffer, final Map<Integer, RawAttribute> attributes) throws BGPDocumentedException {
79 final BitArray flags = BitArray.valueOf(buffer.readByte());
80 final int type = buffer.readUnsignedByte();
81 final int len = (flags.get(EXTENDED_LENGTH_BIT)) ? buffer.readUnsignedShort() : buffer.readUnsignedByte();
82 if (!attributes.containsKey(type)) {
83 final AttributeParser parser = this.handlers.getParser(type);
85 if (!flags.get(OPTIONAL_BIT)) {
86 throw new BGPDocumentedException("Well known attribute not recognized.", BGPError.WELL_KNOWN_ATTR_NOT_RECOGNIZED);
88 if (flags.get(TRANSITIVE_BIT)) {
89 // FIXME: transitive attributes need to be preserved
90 LOG.warn("Losing unrecognized transitive attribute {}. Some data might be missing from the output.", type);
92 LOG.warn("Ignoring unrecognized attribute type {}. Some data might be missing from the output.", type);
95 attributes.put(type, new RawAttribute(parser, buffer.readSlice(len)));
98 LOG.debug("Ignoring duplicate attribute type {}", type);
103 public PathAttributes parseAttributes(final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
104 final Map<Integer, RawAttribute> attributes = new TreeMap<>();
105 while (buffer.isReadable()) {
106 addAttribute(buffer, attributes);
109 * TreeMap guarantees that we will be invoking the parser in the order
110 * of increasing attribute type.
112 final PathAttributesBuilder builder = new PathAttributesBuilder();
113 for (final Entry<Integer, RawAttribute> e : attributes.entrySet()) {
114 LOG.debug("Parsing attribute type {}", e.getKey());
116 final RawAttribute a = e.getValue();
117 a.parser.parseAttribute(a.buffer, builder);
119 return builder.build();
123 public void serializeAttribute(final DataObject attribute,final ByteBuf byteAggregator) {
124 for (final AttributeSerializer serializer : this.roSerializers.get()) {
125 serializer.serializeAttribute(attribute, byteAggregator);