BUG-2982 : moved path-attributes container to grouping
[bgpcep.git] / bgp / parser-spi / src / main / java / org / opendaylight / protocol / bgp / parser / spi / pojo / SimpleAttributeRegistry.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.bgp.parser.spi.pojo;
9
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;
15 import java.util.Map;
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;
39
40 final class SimpleAttributeRegistry implements AttributeRegistry {
41
42     private static final class RawAttribute {
43         private final AttributeParser parser;
44         private final ByteBuf buffer;
45
46         public RawAttribute(final AttributeParser parser, final ByteBuf buffer) {
47             this.parser = Preconditions.checkNotNull(parser);
48             this.buffer = Preconditions.checkNotNull(buffer);
49         }
50     }
51
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<>();
62
63
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);
67     }
68
69     synchronized AutoCloseable registerAttributeSerializer(final Class<? extends DataObject> paramClass, final AttributeSerializer serializer) {
70         final AbstractRegistration reg = this.handlers.registerSerializer(paramClass, serializer);
71
72         this.serializers.put(reg, serializer);
73         return new AbstractRegistration() {
74             @Override
75             protected void removeRegistration() {
76                 synchronized (SimpleAttributeRegistry.this) {
77                     SimpleAttributeRegistry.this.serializers.remove(reg);
78                     SimpleAttributeRegistry.this.roSerializers.set(SimpleAttributeRegistry.this.serializers.values());
79                 }
80                 reg.close();
81             }
82         };
83     }
84
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);
91             if (parser == null) {
92                 if (!flags.get(OPTIONAL_BIT)) {
93                     throw new BGPDocumentedException("Well known attribute not recognized.", BGPError.WELL_KNOWN_ATTR_NOT_RECOGNIZED);
94                 }
95                 final UnrecognizedAttributes unrecognizedAttribute = new UnrecognizedAttributesBuilder()
96                     .setKey(new UnrecognizedAttributesKey((short)type))
97                     .setPartial(flags.get(PARTIAL_BIT))
98                     .setTransitive(flags.get(TRANSITIVE_BIT))
99                     .setType((short)type)
100                     .setValue(ByteArray.readBytes(buffer, len)).build();
101                 this.unrecognizedAttributes.add(unrecognizedAttribute);
102                 LOG.debug("Unrecognized attribute were parsed: {}", unrecognizedAttribute);
103             } else {
104                 attributes.put(type, new RawAttribute(parser, buffer.readSlice(len)));
105             }
106         } else {
107             LOG.debug("Ignoring duplicate attribute type {}", type);
108         }
109     }
110
111     @Override
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);
116         }
117         /*
118          * TreeMap guarantees that we will be invoking the parser in the order
119          * of increasing attribute type.
120          */
121         final AttributesBuilder builder = new AttributesBuilder();
122         for (final Entry<Integer, RawAttribute> e : attributes.entrySet()) {
123             LOG.debug("Parsing attribute type {}", e.getKey());
124
125             final RawAttribute a = e.getValue();
126             a.parser.parseAttribute(a.buffer, builder);
127         }
128         builder.setUnrecognizedAttributes(this.unrecognizedAttributes);
129         return builder.build();
130     }
131
132     @Override
133     public void serializeAttribute(final DataObject attribute,final ByteBuf byteAggregator) {
134         for (final AttributeSerializer serializer : this.roSerializers.get()) {
135             serializer.serializeAttribute(attribute, byteAggregator);
136         }
137     }
138 }