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.bmp.spi.registry;
10 import static com.google.common.base.Preconditions.checkArgument;
12 import io.netty.buffer.ByteBuf;
13 import org.opendaylight.protocol.bmp.spi.parser.BmpDeserializationException;
14 import org.opendaylight.protocol.bmp.spi.parser.BmpTlvParser;
15 import org.opendaylight.protocol.bmp.spi.parser.BmpTlvRegistry;
16 import org.opendaylight.protocol.bmp.spi.parser.BmpTlvSerializer;
17 import org.opendaylight.protocol.concepts.HandlerRegistry;
18 import org.opendaylight.protocol.util.Values;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.message.rev200120.Tlv;
20 import org.opendaylight.yangtools.concepts.Registration;
21 import org.opendaylight.yangtools.yang.binding.DataContainer;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 public class SimpleBmpTlvRegistry implements BmpTlvRegistry {
27 private static final Logger LOG = LoggerFactory.getLogger(SimpleBmpTlvRegistry.class);
29 private final HandlerRegistry<DataContainer, BmpTlvParser, BmpTlvSerializer> handlers = new HandlerRegistry<>();
32 public void serializeTlv(final Tlv tlv, final ByteBuf output) {
33 final BmpTlvSerializer serializer = this.handlers.getSerializer(tlv.implementedInterface());
34 if (serializer == null) {
35 LOG.warn("BMP serializer for TLV type {} is not registered.", tlv.getClass());
38 serializer.serializeTlv(tlv, output);
42 public Tlv parseTlv(final int tlvType, final ByteBuf buffer) throws BmpDeserializationException {
43 checkArgument(tlvType >= 0 && tlvType <= Values.UNSIGNED_SHORT_MAX_VALUE);
44 final BmpTlvParser parser = this.handlers.getParser(tlvType);
46 LOG.warn("BMP parser for TLV type {} is not registered.", tlvType);
49 return parser.parseTlv(buffer);
53 public Registration registerBmpTlvParser(final int tlvType, final BmpTlvParser parser) {
54 checkArgument(tlvType >= 0 && tlvType < Values.UNSIGNED_SHORT_MAX_VALUE);
55 return this.handlers.registerParser(tlvType, parser);
59 public Registration registerBmpTlvSerializer(final Class<? extends Tlv> tlvClass,
60 final BmpTlvSerializer serializer) {
61 return this.handlers.registerSerializer(tlvClass, serializer);