4a1b63b03776ec92ea2544147d38266be1596139
[bgpcep.git] / bgp / parser-impl / src / main / java / org / opendaylight / protocol / bgp / parser / impl / message / update / 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.message.update;
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         public static final AttributeRegistry INSTANCE;
28
29         static {
30                 final AttributeRegistry reg = new SimpleAttributeRegistry();
31
32                 reg.registerAttributeParser(OriginAttributeParser.TYPE, new OriginAttributeParser());
33                 reg.registerAttributeParser(AsPathAttributeParser.TYPE, new AsPathAttributeParser());
34                 reg.registerAttributeParser(NextHopAttributeParser.TYPE, new NextHopAttributeParser());
35                 reg.registerAttributeParser(MultiExitDiscriminatorAttributeParser.TYPE, new MultiExitDiscriminatorAttributeParser());
36                 reg.registerAttributeParser(LocalPreferenceAttributeParser.TYPE, new LocalPreferenceAttributeParser());
37                 reg.registerAttributeParser(AtomicAggregateAttributeParser.TYPE, new AtomicAggregateAttributeParser());
38                 reg.registerAttributeParser(AggregatorAttributeParser.TYPE, new AggregatorAttributeParser());
39                 reg.registerAttributeParser(CommunitiesAttributeParser.TYPE, new CommunitiesAttributeParser());
40                 reg.registerAttributeParser(OriginatorIdAttributeParser.TYPE, new OriginatorIdAttributeParser());
41                 reg.registerAttributeParser(ClusterIdAttributeParser.TYPE, new ClusterIdAttributeParser());
42                 reg.registerAttributeParser(MPReachAttributeParser.TYPE, new MPReachAttributeParser(SimpleNlriRegistry.INSTANCE));
43                 reg.registerAttributeParser(MPUnreachAttributeParser.TYPE, new MPUnreachAttributeParser(SimpleNlriRegistry.INSTANCE));
44                 reg.registerAttributeParser(ExtendedCommunitiesAttributeParser.TYPE, new ExtendedCommunitiesAttributeParser());
45                 reg.registerAttributeParser(AS4AggregatorAttributeParser.TYPE, new AS4AggregatorAttributeParser());
46                 reg.registerAttributeParser(AS4PathAttributeParser.TYPE, new AS4PathAttributeParser());
47                 reg.registerAttributeParser(LinkstateAttributeParser.TYPE, new LinkstateAttributeParser());
48
49                 INSTANCE = reg;
50         }
51
52         private final HandlerRegistry<DataContainer, AttributeParser, AttributeSerializer> handlers = new HandlerRegistry<>();
53
54         @Override
55         public AutoCloseable registerAttributeParser(final int attributeType, final AttributeParser parser) {
56                 Preconditions.checkArgument(attributeType >= 0 && attributeType <= 255);
57                 return handlers.registerParser(attributeType, parser);
58         }
59
60         @Override
61         public AutoCloseable registerAttributeSerializer(final Class<? extends DataObject> paramClass, final AttributeSerializer serializer) {
62                 return handlers.registerSerializer(paramClass, serializer);
63         }
64
65         private int parseAttribute( final byte[] bytes, final int offset, final PathAttributesBuilder builder)
66                         throws BGPDocumentedException, BGPParsingException {
67                 // FIXME: validate minimum length
68                 final boolean[] flags = ByteArray.parseBits(bytes[offset]);
69                 final int type = UnsignedBytes.toInt(bytes[offset + 1]);
70                 final int hdrlen;
71                 final int len;
72                 if (flags[3]) {
73                         len = UnsignedBytes.toInt(bytes[offset + 2]) * 256 + UnsignedBytes.toInt(bytes[offset + 3]);
74                         hdrlen = 4;
75                 } else {
76                         len = UnsignedBytes.toInt(bytes[offset + 2]);
77                         hdrlen = 3;
78                 }
79
80                 final AttributeParser parser = handlers.getParser(type);
81                 if (parser == null) {
82                         if (!flags[0]) {
83                                 throw new BGPDocumentedException("Well known attribute not recognized.", BGPError.WELL_KNOWN_ATTR_NOT_RECOGNIZED);
84                         }
85                 } else {
86                         parser.parseAttribute(ByteArray.subByte(bytes, offset + hdrlen, len), builder);
87                 }
88
89                 return hdrlen + len;
90         }
91
92         @Override
93         public PathAttributes parseAttributes(final byte[] bytes) throws BGPDocumentedException, BGPParsingException {
94                 int byteOffset = 0;
95                 final PathAttributesBuilder builder = new PathAttributesBuilder();
96                 while (byteOffset < bytes.length) {
97                         byteOffset += parseAttribute(bytes, byteOffset, builder);
98                 }
99                 return builder.build();
100         }
101
102         @Override
103         public byte[] serializeAttribute(final DataObject attribute) {
104                 final AttributeSerializer serializer = handlers.getSerializer(attribute.getImplementedInterface());
105                 if (serializer == null) {
106                         return null;
107                 }
108
109                 return serializer.serializeAttribute(attribute);
110         }
111 }