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
9 package org.opendaylight.protocol.bmp.spi.registry;
11 import com.google.common.base.Preconditions;
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.rev171207.Tlv;
20 import org.opendaylight.yangtools.yang.binding.DataContainer;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 public class SimpleBmpTlvRegistry implements BmpTlvRegistry {
26 private static final Logger LOG = LoggerFactory.getLogger(SimpleBmpTlvRegistry.class);
28 private final HandlerRegistry<DataContainer, BmpTlvParser, BmpTlvSerializer> handlers = new HandlerRegistry<>();
31 public void serializeTlv(final Tlv tlv, final ByteBuf output) {
32 final BmpTlvSerializer serializer = this.handlers.getSerializer(tlv.getImplementedInterface());
33 if (serializer == null) {
34 LOG.warn("BMP serializer for TLV type {} is not registered.", tlv.getClass());
37 serializer.serializeTlv(tlv, output);
41 public Tlv parseTlv(final int tlvType, final ByteBuf buffer) throws BmpDeserializationException {
42 Preconditions.checkArgument(tlvType >= 0 && tlvType <= Values.UNSIGNED_SHORT_MAX_VALUE);
43 final BmpTlvParser parser = this.handlers.getParser(tlvType);
45 LOG.warn("BMP parser for TLV type {} is not registered.", tlvType);
48 return parser.parseTlv(buffer);
52 public AutoCloseable registerBmpTlvParser(final int tlvType, final BmpTlvParser parser) {
53 Preconditions.checkArgument(tlvType >= 0 && tlvType < Values.UNSIGNED_SHORT_MAX_VALUE);
54 return this.handlers.registerParser(tlvType, parser);
58 public AutoCloseable registerBmpTlvSerializer(final Class<? extends Tlv> tlvClass,
59 final BmpTlvSerializer serializer) {
60 return this.handlers.registerSerializer(tlvClass, serializer);