Bump MDSAL to 4.0.0
[bgpcep.git] / bgp / extensions / mvpn / src / main / java / org / opendaylight / protocol / bgp / mvpn / spi / pojo / attributes / tunnel / identifier / SimpleTunnelIdentifierRegistry.java
1 /*
2  * Copyright (c) 2016 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.bgp.mvpn.spi.pojo.attributes.tunnel.identifier;
9
10 import io.netty.buffer.ByteBuf;
11 import org.opendaylight.protocol.bgp.mvpn.spi.attributes.tunnel.identifier.TunnelIdentifierParser;
12 import org.opendaylight.protocol.bgp.mvpn.spi.attributes.tunnel.identifier.TunnelIdentifierSerializer;
13 import org.opendaylight.protocol.concepts.HandlerRegistry;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev180329.pmsi.tunnel.pmsi.tunnel.TunnelIdentifier;
15 import org.opendaylight.yangtools.concepts.Registration;
16 import org.opendaylight.yangtools.yang.binding.DataContainer;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 public final class SimpleTunnelIdentifierRegistry {
21     public static final int NO_TUNNEL_INFORMATION_PRESENT = 0;
22     private static final SimpleTunnelIdentifierRegistry SINGLETON = new SimpleTunnelIdentifierRegistry();
23     private static final Logger LOG = LoggerFactory.getLogger(SimpleTunnelIdentifierRegistry.class);
24     private final HandlerRegistry<DataContainer, TunnelIdentifierParser<?>, TunnelIdentifierSerializer<?>> handlers =
25             new HandlerRegistry<>();
26
27     private SimpleTunnelIdentifierRegistry() {
28     }
29
30     public static SimpleTunnelIdentifierRegistry getInstance() {
31         return SINGLETON;
32     }
33
34     public TunnelIdentifier parse(final int tunnelType, final ByteBuf buffer) {
35         final TunnelIdentifierParser<?> parser = this.handlers.getParser(tunnelType);
36         if (!buffer.isReadable() || parser == null) {
37             LOG.debug("Skipping parsing of PMSI Tunnel Attribute type {}", tunnelType);
38             return null;
39         }
40         return parser.parse(buffer);
41     }
42
43     public int serialize(final TunnelIdentifier tunnel, final ByteBuf tunnelBuffer) {
44         if (tunnel == null) {
45             LOG.debug("Skipping serialization of null PMSI Tunnel Attribute");
46             return NO_TUNNEL_INFORMATION_PRESENT;
47         }
48         final TunnelIdentifierSerializer serializer = this.handlers.getSerializer(tunnel.implementedInterface());
49         if (serializer == null) {
50             LOG.debug("Skipping serialization of PMSI Tunnel Attribute {}", tunnel);
51             return NO_TUNNEL_INFORMATION_PRESENT;
52         }
53         return serializer.serialize(tunnel, tunnelBuffer);
54     }
55
56     public Registration registerParser(final TunnelIdentifierParser<?> parser) {
57         return this.handlers.registerParser(parser.getType(), parser);
58     }
59
60     public Registration registerSerializer(final TunnelIdentifierSerializer<?> serializer) {
61         return this.handlers.registerSerializer(serializer.getClazz(), serializer);
62     }
63 }