/* * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.protocol.bgp.parser.impl.message.update; import org.opendaylight.protocol.bgp.parser.BGPDocumentedException; import org.opendaylight.protocol.bgp.parser.BGPError; import org.opendaylight.protocol.bgp.parser.BGPParsingException; import org.opendaylight.protocol.bgp.parser.spi.AttributeParser; import org.opendaylight.protocol.bgp.parser.spi.AttributeRegistry; import org.opendaylight.protocol.bgp.parser.spi.AttributeSerializer; import org.opendaylight.protocol.concepts.HandlerRegistry; import org.opendaylight.protocol.util.ByteArray; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.update.PathAttributes; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.update.PathAttributesBuilder; import org.opendaylight.yangtools.yang.binding.DataContainer; import org.opendaylight.yangtools.yang.binding.DataObject; import com.google.common.base.Preconditions; import com.google.common.primitives.UnsignedBytes; public final class SimpleAttributeRegistry implements AttributeRegistry { public static final AttributeRegistry INSTANCE; static { final AttributeRegistry reg = new SimpleAttributeRegistry(); reg.registerAttributeParser(OriginAttributeParser.TYPE, new OriginAttributeParser()); reg.registerAttributeParser(AsPathAttributeParser.TYPE, new AsPathAttributeParser()); reg.registerAttributeParser(NextHopAttributeParser.TYPE, new NextHopAttributeParser()); reg.registerAttributeParser(MultiExitDiscriminatorAttributeParser.TYPE, new MultiExitDiscriminatorAttributeParser()); reg.registerAttributeParser(LocalPreferenceAttributeParser.TYPE, new LocalPreferenceAttributeParser()); reg.registerAttributeParser(AtomicAggregateAttributeParser.TYPE, new AtomicAggregateAttributeParser()); reg.registerAttributeParser(AggregatorAttributeParser.TYPE, new AggregatorAttributeParser()); reg.registerAttributeParser(CommunitiesAttributeParser.TYPE, new CommunitiesAttributeParser()); reg.registerAttributeParser(OriginatorIdAttributeParser.TYPE, new OriginatorIdAttributeParser()); reg.registerAttributeParser(ClusterIdAttributeParser.TYPE, new ClusterIdAttributeParser()); reg.registerAttributeParser(MPReachAttributeParser.TYPE, new MPReachAttributeParser(SimpleNlriRegistry.INSTANCE)); reg.registerAttributeParser(MPUnreachAttributeParser.TYPE, new MPUnreachAttributeParser(SimpleNlriRegistry.INSTANCE)); reg.registerAttributeParser(ExtendedCommunitiesAttributeParser.TYPE, new ExtendedCommunitiesAttributeParser()); reg.registerAttributeParser(AS4AggregatorAttributeParser.TYPE, new AS4AggregatorAttributeParser()); reg.registerAttributeParser(AS4PathAttributeParser.TYPE, new AS4PathAttributeParser()); reg.registerAttributeParser(LinkstateAttributeParser.TYPE, new LinkstateAttributeParser()); INSTANCE = reg; } private final HandlerRegistry handlers = new HandlerRegistry<>(); @Override public AutoCloseable registerAttributeParser(final int attributeType, final AttributeParser parser) { Preconditions.checkArgument(attributeType >= 0 && attributeType <= 255); return handlers.registerParser(attributeType, parser); } @Override public AutoCloseable registerAttributeSerializer(final Class paramClass, final AttributeSerializer serializer) { return handlers.registerSerializer(paramClass, serializer); } private int parseAttribute( final byte[] bytes, final int offset, final PathAttributesBuilder builder) throws BGPDocumentedException, BGPParsingException { // FIXME: validate minimum length final boolean[] flags = ByteArray.parseBits(bytes[offset]); final int type = UnsignedBytes.toInt(bytes[offset + 1]); final int hdrlen; final int len; if (flags[3]) { len = UnsignedBytes.toInt(bytes[offset + 2]) * 256 + UnsignedBytes.toInt(bytes[offset + 3]); hdrlen = 4; } else { len = UnsignedBytes.toInt(bytes[offset + 2]); hdrlen = 3; } final AttributeParser parser = handlers.getParser(type); if (parser == null) { if (!flags[0]) { throw new BGPDocumentedException("Well known attribute not recognized.", BGPError.WELL_KNOWN_ATTR_NOT_RECOGNIZED); } } else { parser.parseAttribute(ByteArray.subByte(bytes, offset + hdrlen, len), builder); } return hdrlen + len; } @Override public PathAttributes parseAttributes(final byte[] bytes) throws BGPDocumentedException, BGPParsingException { int byteOffset = 0; final PathAttributesBuilder builder = new PathAttributesBuilder(); while (byteOffset < bytes.length) { byteOffset += parseAttribute(bytes, byteOffset, builder); } return builder.build(); } @Override public byte[] serializeAttribute(final DataObject attribute) { final AttributeSerializer serializer = handlers.getSerializer(attribute.getImplementedInterface()); if (serializer == null) { return null; } return serializer.serializeAttribute(attribute); } }