Testtools should not be bundles
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / 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.impl;
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 public final class SimpleAttributeRegistry implements AttributeRegistry {
27         private final HandlerRegistry<DataContainer, AttributeParser, AttributeSerializer> handlers = new HandlerRegistry<>();
28
29         @Override
30         public AutoCloseable registerAttributeParser(final int attributeType, final AttributeParser parser) {
31                 Preconditions.checkArgument(attributeType >= 0 && attributeType <= 255);
32                 return handlers.registerParser(attributeType, parser);
33         }
34
35         @Override
36         public AutoCloseable registerAttributeSerializer(final Class<? extends DataObject> paramClass, final AttributeSerializer serializer) {
37                 return handlers.registerSerializer(paramClass, serializer);
38         }
39
40         private int parseAttribute( final byte[] bytes, final int offset, final PathAttributesBuilder builder)
41                         throws BGPDocumentedException, BGPParsingException {
42                 // FIXME: validate minimum length
43                 final boolean[] flags = ByteArray.parseBits(bytes[offset]);
44                 final int type = UnsignedBytes.toInt(bytes[offset + 1]);
45                 final int hdrlen;
46                 final int len;
47                 if (flags[3]) {
48                         len = UnsignedBytes.toInt(bytes[offset + 2]) * 256 + UnsignedBytes.toInt(bytes[offset + 3]);
49                         hdrlen = 4;
50                 } else {
51                         len = UnsignedBytes.toInt(bytes[offset + 2]);
52                         hdrlen = 3;
53                 }
54
55                 final AttributeParser parser = handlers.getParser(type);
56                 if (parser == null) {
57                         if (!flags[0]) {
58                                 throw new BGPDocumentedException("Well known attribute not recognized.", BGPError.WELL_KNOWN_ATTR_NOT_RECOGNIZED);
59                         }
60                 } else {
61                         parser.parseAttribute(ByteArray.subByte(bytes, offset + hdrlen, len), builder);
62                 }
63
64                 return hdrlen + len;
65         }
66
67         @Override
68         public PathAttributes parseAttributes(final byte[] bytes) throws BGPDocumentedException, BGPParsingException {
69                 int byteOffset = 0;
70                 final PathAttributesBuilder builder = new PathAttributesBuilder();
71                 while (byteOffset < bytes.length) {
72                         byteOffset += parseAttribute(bytes, byteOffset, builder);
73                 }
74                 return builder.build();
75         }
76
77         @Override
78         public byte[] serializeAttribute(final DataObject attribute) {
79                 final AttributeSerializer serializer = handlers.getSerializer(attribute.getImplementedInterface());
80                 if (serializer == null) {
81                         return null;
82                 }
83
84                 return serializer.serializeAttribute(attribute);
85         }
86 }