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