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.ArrayList;
13 import java.util.LinkedHashMap;
14 import java.util.List;
16 import java.util.Map.Entry;
17 import java.util.TreeMap;
18 import java.util.concurrent.atomic.AtomicReference;
19 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
20 import org.opendaylight.protocol.bgp.parser.BGPError;
21 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
22 import org.opendaylight.protocol.bgp.parser.spi.AttributeParser;
23 import org.opendaylight.protocol.bgp.parser.spi.AttributeRegistry;
24 import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer;
25 import org.opendaylight.protocol.concepts.AbstractRegistration;
26 import org.opendaylight.protocol.concepts.HandlerRegistry;
27 import org.opendaylight.protocol.util.BitArray;
28 import org.opendaylight.protocol.util.ByteArray;
29 import org.opendaylight.protocol.util.Values;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AttributesBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.UnrecognizedAttributes;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.UnrecognizedAttributesBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.UnrecognizedAttributesKey;
35 import org.opendaylight.yangtools.yang.binding.DataContainer;
36 import org.opendaylight.yangtools.yang.binding.DataObject;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 final class SimpleAttributeRegistry implements AttributeRegistry {
42 private static final class RawAttribute {
43 private final AttributeParser parser;
44 private final ByteBuf buffer;
46 public RawAttribute(final AttributeParser parser, final ByteBuf buffer) {
47 this.parser = Preconditions.checkNotNull(parser);
48 this.buffer = Preconditions.checkNotNull(buffer);
52 private static final Logger LOG = LoggerFactory.getLogger(SimpleAttributeRegistry.class);
53 private static final int OPTIONAL_BIT = 0;
54 private static final int TRANSITIVE_BIT = 1;
55 private static final int PARTIAL_BIT = 2;
56 private static final int EXTENDED_LENGTH_BIT = 3;
57 private final HandlerRegistry<DataContainer, AttributeParser, AttributeSerializer> handlers = new HandlerRegistry<>();
58 private final Map<AbstractRegistration, AttributeSerializer> serializers = new LinkedHashMap<>();
59 private final AtomicReference<Iterable<AttributeSerializer>> roSerializers =
60 new AtomicReference<Iterable<AttributeSerializer>>(this.serializers.values());
61 private final List<UnrecognizedAttributes> unrecognizedAttributes = new ArrayList<>();
64 AutoCloseable registerAttributeParser(final int attributeType, final AttributeParser parser) {
65 Preconditions.checkArgument(attributeType >= 0 && attributeType <= Values.UNSIGNED_BYTE_MAX_VALUE);
66 return this.handlers.registerParser(attributeType, parser);
69 synchronized AutoCloseable registerAttributeSerializer(final Class<? extends DataObject> paramClass, final AttributeSerializer serializer) {
70 final AbstractRegistration reg = this.handlers.registerSerializer(paramClass, serializer);
72 this.serializers.put(reg, serializer);
73 return new AbstractRegistration() {
75 protected void removeRegistration() {
76 synchronized (SimpleAttributeRegistry.this) {
77 SimpleAttributeRegistry.this.serializers.remove(reg);
78 SimpleAttributeRegistry.this.roSerializers.set(SimpleAttributeRegistry.this.serializers.values());
85 private void addAttribute(final ByteBuf buffer, final Map<Integer, RawAttribute> attributes) throws BGPDocumentedException {
86 final BitArray flags = BitArray.valueOf(buffer.readByte());
87 final int type = buffer.readUnsignedByte();
88 final int len = (flags.get(EXTENDED_LENGTH_BIT)) ? buffer.readUnsignedShort() : buffer.readUnsignedByte();
89 if (!attributes.containsKey(type)) {
90 final AttributeParser parser = this.handlers.getParser(type);
92 if (!flags.get(OPTIONAL_BIT)) {
93 throw new BGPDocumentedException("Well known attribute not recognized.", BGPError.WELL_KNOWN_ATTR_NOT_RECOGNIZED);
95 final UnrecognizedAttributes unrecognizedAttribute = new UnrecognizedAttributesBuilder()
96 .setKey(new UnrecognizedAttributesKey((short)type))
97 .setPartial(flags.get(PARTIAL_BIT))
98 .setTransitive(flags.get(TRANSITIVE_BIT))
100 .setValue(ByteArray.readBytes(buffer, len)).build();
101 this.unrecognizedAttributes.add(unrecognizedAttribute);
102 LOG.debug("Unrecognized attribute were parsed: {}", unrecognizedAttribute);
104 attributes.put(type, new RawAttribute(parser, buffer.readSlice(len)));
107 LOG.debug("Ignoring duplicate attribute type {}", type);
112 public Attributes parseAttributes(final ByteBuf buffer) throws BGPDocumentedException, BGPParsingException {
113 final Map<Integer, RawAttribute> attributes = new TreeMap<>();
114 while (buffer.isReadable()) {
115 addAttribute(buffer, attributes);
118 * TreeMap guarantees that we will be invoking the parser in the order
119 * of increasing attribute type.
121 final AttributesBuilder builder = new AttributesBuilder();
122 for (final Entry<Integer, RawAttribute> e : attributes.entrySet()) {
123 LOG.debug("Parsing attribute type {}", e.getKey());
125 final RawAttribute a = e.getValue();
126 a.parser.parseAttribute(a.buffer, builder);
128 builder.setUnrecognizedAttributes(this.unrecognizedAttributes);
129 return builder.build();
133 public void serializeAttribute(final DataObject attribute,final ByteBuf byteAggregator) {
134 for (final AttributeSerializer serializer : this.roSerializers.get()) {
135 serializer.serializeAttribute(attribute, byteAggregator);