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
9 package org.opendaylight.protocol.bgp.mvpn.spi.pojo.attributes.tunnel.identifier;
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;
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<>();
28 private SimpleTunnelIdentifierRegistry() {
31 public static SimpleTunnelIdentifierRegistry getInstance() {
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);
41 return parser.parse(buffer);
44 public int serialize(final TunnelIdentifier tunnel, final ByteBuf tunnelBuffer) {
46 LOG.debug("Skipping serialization of PMSI Tunnel Attribute {}");
47 return NO_TUNNEL_INFORMATION_PRESENT;
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;
54 return serializer.serialize(tunnel, tunnelBuffer);
57 public AbstractRegistration registerParser(final TunnelIdentifierParser<?> parser) {
58 return this.handlers.registerParser(parser.getType(), parser);
61 public AbstractRegistration registerSerializer(final TunnelIdentifierSerializer<?> serializer) {
62 return this.handlers.registerSerializer(serializer.getClazz(), serializer);