2 * Copyright (c) 2016 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
8 package org.opendaylight.protocol.bgp.mvpn.spi.pojo.attributes.tunnel.identifier;
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;
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<>();
27 private SimpleTunnelIdentifierRegistry() {
30 public static SimpleTunnelIdentifierRegistry getInstance() {
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);
40 return parser.parse(buffer);
43 public int serialize(final TunnelIdentifier tunnel, final ByteBuf tunnelBuffer) {
45 LOG.debug("Skipping serialization of null PMSI Tunnel Attribute");
46 return NO_TUNNEL_INFORMATION_PRESENT;
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;
53 return serializer.serialize(tunnel, tunnelBuffer);
56 public Registration registerParser(final TunnelIdentifierParser<?> parser) {
57 return this.handlers.registerParser(parser.getType(), parser);
60 public Registration registerSerializer(final TunnelIdentifierSerializer<?> serializer) {
61 return this.handlers.registerSerializer(serializer.getClazz(), serializer);