Code Clean Up
[bgpcep.git] / bgp / parser-spi / src / main / java / org / opendaylight / protocol / bgp / parser / spi / pojo / SimpleExtendedCommunityRegistry.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.protocol.bgp.parser.spi.pojo;
10
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;
13
14 import com.google.common.base.Preconditions;
15 import com.google.common.primitives.Shorts;
16 import io.netty.buffer.ByteBuf;
17 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
18 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
19 import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunityParser;
20 import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunityRegistry;
21 import org.opendaylight.protocol.bgp.parser.spi.extended.community.ExtendedCommunitySerializer;
22 import org.opendaylight.protocol.concepts.HandlerRegistry;
23 import org.opendaylight.protocol.util.ByteBufWriteUtil;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.ExtendedCommunities;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.attributes.ExtendedCommunitiesBuilder;
26 import org.opendaylight.yangtools.yang.binding.DataContainer;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 final class SimpleExtendedCommunityRegistry implements ExtendedCommunityRegistry {
31
32     private static final Logger LOG = LoggerFactory.getLogger(SimpleExtendedCommunityRegistry.class);
33
34     private static final int EXTENDED_COMMUNITY_LENGTH = 6;
35
36     private final HandlerRegistry<DataContainer, ExtendedCommunityParser, ExtendedCommunitySerializer> handlers = new HandlerRegistry<>();
37
38     private static int createKey(final int type, final int subtype) {
39         return (type << Byte.SIZE) | subtype;
40     }
41
42     @Override
43     public ExtendedCommunities parseExtendedCommunity(final ByteBuf buffer)
44             throws BGPDocumentedException, BGPParsingException {
45         final short type = buffer.readUnsignedByte();
46         final short subtype = buffer.readUnsignedByte();
47         final ExtendedCommunityParser parser = this.handlers.getParser(createKey(type, subtype));
48         if (parser == null) {
49             buffer.skipBytes(EXTENDED_COMMUNITY_LENGTH);
50             LOG.info("Skipping unknown extended-community type/sub-type {}/{}.", type, subtype);
51             return null;
52         }
53         return new ExtendedCommunitiesBuilder()
54             .setTransitive(isTransitive(type))
55             .setExtendedCommunity(parser.parseExtendedCommunity(buffer))
56             .build();
57     }
58
59     @Override
60     public void serializeExtendedCommunity(final ExtendedCommunities extendedCommunity, final ByteBuf byteAggregator) {
61         final ExtendedCommunitySerializer serializer = this.handlers.getSerializer(extendedCommunity.getExtendedCommunity().getImplementedInterface());
62         if (serializer == null) {
63             return;
64         }
65         ByteBufWriteUtil.writeUnsignedByte(Shorts.checkedCast(serializer.getType(extendedCommunity.isTransitive())), byteAggregator);
66         ByteBufWriteUtil.writeUnsignedByte(Shorts.checkedCast(serializer.getSubType()), byteAggregator);
67         serializer.serializeExtendedCommunity(extendedCommunity.getExtendedCommunity(), byteAggregator);
68     }
69
70     synchronized AutoCloseable registerExtendedCommunitySerializer(
71             final Class<? extends org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.extended.community.ExtendedCommunity> extendedCommunityClass,
72             final ExtendedCommunitySerializer serializer) {
73         return this.handlers.registerSerializer(extendedCommunityClass, serializer);
74     }
75
76     synchronized AutoCloseable registerExtendedCommunityParser(final int type, final int subtype, final ExtendedCommunityParser parser) {
77         checkTypes(type, subtype);
78         return this.handlers.registerParser(createKey(type, subtype), parser);
79     }
80
81     private static void checkTypes(final int type, final int subtype) {
82         Preconditions.checkArgument(type >= 0 && type <= UNSIGNED_BYTE_MAX_VALUE, "Illegal extended-community type %s",
83                 type);
84         Preconditions.checkArgument(subtype >= 0 && subtype <= UNSIGNED_BYTE_MAX_VALUE, "Illegal extended-community sub-type %s", subtype);
85     }
86
87 }