f2db123c72e76a8217dec46ebd87e16a73d4a4c0
[bgpcep.git] / bmp / bmp-spi / src / main / java / org / opendaylight / protocol / bmp / spi / registry / SimpleBmpTlvRegistry.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.bmp.spi.registry;
10
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;
23
24 public class SimpleBmpTlvRegistry implements BmpTlvRegistry {
25
26     private static final Logger LOG = LoggerFactory.getLogger(SimpleBmpTlvRegistry.class);
27
28     private final HandlerRegistry<DataContainer, BmpTlvParser, BmpTlvSerializer> handlers = new HandlerRegistry<>();
29
30     @Override
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());
35             return;
36         }
37         serializer.serializeTlv(tlv, output);
38     }
39
40     @Override
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);
44         if (parser == null) {
45             LOG.warn("BMP parser for TLV type {} is not registered.", tlvType);
46             return null;
47         }
48         return parser.parseTlv(buffer);
49     }
50
51     @Override
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);
55     }
56
57     @Override
58     public AutoCloseable registerBmpTlvSerializer(final Class<? extends Tlv> tlvClass,
59             final BmpTlvSerializer serializer) {
60         return this.handlers.registerSerializer(tlvClass, serializer);
61     }
62
63 }