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.impl.message.update;
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;
23 import com.google.common.base.Preconditions;
24 import com.google.common.primitives.UnsignedBytes;
26 public final class SimpleAttributeRegistry implements AttributeRegistry {
27 public static final AttributeRegistry INSTANCE;
30 final AttributeRegistry reg = new SimpleAttributeRegistry();
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());
52 private final HandlerRegistry<DataContainer, AttributeParser, AttributeSerializer> handlers = new HandlerRegistry<>();
55 public AutoCloseable registerAttributeParser(final int attributeType, final AttributeParser parser) {
56 Preconditions.checkArgument(attributeType >= 0 && attributeType <= 255);
57 return handlers.registerParser(attributeType, parser);
61 public AutoCloseable registerAttributeSerializer(final Class<? extends DataObject> paramClass, final AttributeSerializer serializer) {
62 return handlers.registerSerializer(paramClass, serializer);
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]);
73 len = UnsignedBytes.toInt(bytes[offset + 2]) * 256 + UnsignedBytes.toInt(bytes[offset + 3]);
76 len = UnsignedBytes.toInt(bytes[offset + 2]);
80 final AttributeParser parser = handlers.getParser(type);
83 throw new BGPDocumentedException("Well known attribute not recognized.", BGPError.WELL_KNOWN_ATTR_NOT_RECOGNIZED);
86 parser.parseAttribute(ByteArray.subByte(bytes, offset + hdrlen, len), builder);
93 public PathAttributes parseAttributes(final byte[] bytes) throws BGPDocumentedException, BGPParsingException {
95 final PathAttributesBuilder builder = new PathAttributesBuilder();
96 while (byteOffset < bytes.length) {
97 byteOffset += parseAttribute(bytes, byteOffset, builder);
99 return builder.build();
103 public byte[] serializeAttribute(final DataObject attribute) {
104 final AttributeSerializer serializer = handlers.getSerializer(attribute.getImplementedInterface());
105 if (serializer == null) {
109 return serializer.serializeAttribute(attribute);