2 * Copyright (c) 2015 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.spi.pojo;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunityUtil.isTransitive;
12 import static org.opendaylight.protocol.util.Values.UNSIGNED_BYTE_MAX_VALUE;
14 import com.google.common.primitives.Shorts;
15 import io.netty.buffer.ByteBuf;
16 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
17 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
18 import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunityParser;
19 import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunityRegistry;
20 import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunitySerializer;
21 import org.opendaylight.protocol.concepts.HandlerRegistry;
22 import org.opendaylight.protocol.util.ByteBufWriteUtil;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.ExtendedCommunities;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.ExtendedCommunitiesBuilder;
25 import org.opendaylight.yangtools.concepts.Registration;
26 import org.opendaylight.yangtools.yang.binding.DataContainer;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 final class SimpleExtendedCommunityRegistry implements ExtendedCommunityRegistry {
32 private static final Logger LOG = LoggerFactory.getLogger(SimpleExtendedCommunityRegistry.class);
34 private static final int EXTENDED_COMMUNITY_LENGTH = 6;
36 private final HandlerRegistry<DataContainer, ExtendedCommunityParser, ExtendedCommunitySerializer> handlers =
37 new HandlerRegistry<>();
39 private static int createKey(final int type, final int subtype) {
40 return type << Byte.SIZE | subtype;
44 public ExtendedCommunities parseExtendedCommunity(final ByteBuf buffer)
45 throws BGPDocumentedException, BGPParsingException {
46 final short type = buffer.readUnsignedByte();
47 final short subtype = buffer.readUnsignedByte();
48 final ExtendedCommunityParser parser = this.handlers.getParser(createKey(type, subtype));
50 buffer.skipBytes(EXTENDED_COMMUNITY_LENGTH);
51 LOG.info("Skipping unknown extended-community type/sub-type {}/{}.", type, subtype);
54 return new ExtendedCommunitiesBuilder()
55 .setTransitive(isTransitive(type))
56 .setExtendedCommunity(parser.parseExtendedCommunity(buffer))
61 public void serializeExtendedCommunity(final ExtendedCommunities extendedCommunity, final ByteBuf byteAggregator) {
62 final ExtendedCommunitySerializer serializer = this.handlers.getSerializer(extendedCommunity
63 .getExtendedCommunity().implementedInterface());
64 if (serializer == null) {
67 ByteBufWriteUtil.writeUnsignedByte(Shorts.checkedCast(serializer.getType(extendedCommunity.isTransitive())),
69 ByteBufWriteUtil.writeUnsignedByte(Shorts.checkedCast(serializer.getSubType()), byteAggregator);
70 serializer.serializeExtendedCommunity(extendedCommunity.getExtendedCommunity(), byteAggregator);
73 synchronized Registration registerExtendedCommunitySerializer(
74 final Class<? extends org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329
75 .extended.community.ExtendedCommunity> extendedCommunityClass,
76 final ExtendedCommunitySerializer serializer) {
77 return this.handlers.registerSerializer(extendedCommunityClass, serializer);
80 synchronized Registration registerExtendedCommunityParser(final int type, final int subtype,
81 final ExtendedCommunityParser parser) {
82 checkTypes(type, subtype);
83 return this.handlers.registerParser(createKey(type, subtype), parser);
86 private static void checkTypes(final int type, final int subtype) {
87 checkArgument(type >= 0 && type <= UNSIGNED_BYTE_MAX_VALUE, "Illegal extended-community type %s", type);
88 checkArgument(subtype >= 0 && subtype <= UNSIGNED_BYTE_MAX_VALUE, "Illegal extended-community sub-type %s",