BUG-113: switch BGP to proper activators
[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 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;
22
23 import com.google.common.base.Preconditions;
24 import com.google.common.primitives.UnsignedBytes;
25
26 final class SimpleAttributeRegistry implements AttributeRegistry {
27         private final HandlerRegistry<DataContainer, AttributeParser, AttributeSerializer> handlers = new HandlerRegistry<>();
28
29         AutoCloseable registerAttributeParser(final int attributeType, final AttributeParser parser) {
30                 Preconditions.checkArgument(attributeType >= 0 && attributeType <= 255);
31                 return handlers.registerParser(attributeType, parser);
32         }
33
34         AutoCloseable registerAttributeSerializer(final Class<? extends DataObject> paramClass, final AttributeSerializer serializer) {
35                 return handlers.registerSerializer(paramClass, serializer);
36         }
37
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]);
43                 final int hdrlen;
44                 final int len;
45                 if (flags[3]) {
46                         len = UnsignedBytes.toInt(bytes[offset + 2]) * 256 + UnsignedBytes.toInt(bytes[offset + 3]);
47                         hdrlen = 4;
48                 } else {
49                         len = UnsignedBytes.toInt(bytes[offset + 2]);
50                         hdrlen = 3;
51                 }
52
53                 final AttributeParser parser = handlers.getParser(type);
54                 if (parser == null) {
55                         if (!flags[0]) {
56                                 throw new BGPDocumentedException("Well known attribute not recognized.", BGPError.WELL_KNOWN_ATTR_NOT_RECOGNIZED);
57                         }
58                 } else {
59                         parser.parseAttribute(ByteArray.subByte(bytes, offset + hdrlen, len), builder);
60                 }
61
62                 return hdrlen + len;
63         }
64
65         @Override
66         public PathAttributes parseAttributes(final byte[] bytes) throws BGPDocumentedException, BGPParsingException {
67                 int byteOffset = 0;
68                 final PathAttributesBuilder builder = new PathAttributesBuilder();
69                 while (byteOffset < bytes.length) {
70                         byteOffset += parseAttribute(bytes, byteOffset, builder);
71                 }
72                 return builder.build();
73         }
74
75         @Override
76         public byte[] serializeAttribute(final DataObject attribute) {
77                 final AttributeSerializer serializer = handlers.getSerializer(attribute.getImplementedInterface());
78                 if (serializer == null) {
79                         return null;
80                 }
81
82                 return serializer.serializeAttribute(attribute);
83         }
84 }