YANG revision dates mass-update
[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 package org.opendaylight.protocol.bmp.spi.registry;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
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;
24
25 public class SimpleBmpTlvRegistry implements BmpTlvRegistry {
26
27     private static final Logger LOG = LoggerFactory.getLogger(SimpleBmpTlvRegistry.class);
28
29     private final HandlerRegistry<DataContainer, BmpTlvParser, BmpTlvSerializer> handlers = new HandlerRegistry<>();
30
31     @Override
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());
36             return;
37         }
38         serializer.serializeTlv(tlv, output);
39     }
40
41     @Override
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);
45         if (parser == null) {
46             LOG.warn("BMP parser for TLV type {} is not registered.", tlvType);
47             return null;
48         }
49         return parser.parseTlv(buffer);
50     }
51
52     @Override
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);
56     }
57
58     @Override
59     public Registration registerBmpTlvSerializer(final Class<? extends Tlv> tlvClass,
60             final BmpTlvSerializer serializer) {
61         return this.handlers.registerSerializer(tlvClass, serializer);
62     }
63
64 }