987ad0a3eb71fa6a8cba7c96847d8030cddc9185
[bgpcep.git] / bgp / evpn / src / main / java / org / opendaylight / protocol / bgp / evpn / impl / attributes / tunnel / identifier / TunnelIdentifierHandler.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.evpn.impl.attributes.tunnel.identifier;
10
11 import io.netty.buffer.ByteBuf;
12 import org.opendaylight.protocol.bgp.parser.spi.AddressFamilyRegistry;
13 import org.opendaylight.protocol.concepts.HandlerRegistry;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev160812.pmsi.tunnel.pmsi.tunnel.TunnelIdentifier;
15 import org.opendaylight.yangtools.yang.binding.DataContainer;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 public final class TunnelIdentifierHandler {
20     static final int NO_TUNNEL_INFORMATION_PRESENT = 0;
21     private static final Logger LOG = LoggerFactory.getLogger(TunnelIdentifierHandler.class);
22     private static final String SKIP_SERIALIZATION = "Skipping serialization of PMSI Tunnel Attribute {}";
23     private static final String SKIP_PARSE = "Skipping parsing of PMSI Tunnel Attribute type {}";
24     private final HandlerRegistry<DataContainer, TunnelIdentifierParser, TunnelIdentifierSerializer> handlers =
25             new HandlerRegistry<>();
26
27     public TunnelIdentifierHandler(final AddressFamilyRegistry addressFamilyRegistry) {
28         final RsvpTeP2MpLspParser rsvpTeP2MpLspParser = new RsvpTeP2MpLspParser();
29         this.handlers.registerParser(TunnelType.RSVP_TE_P2MP_LSP.getIntValue(), rsvpTeP2MpLspParser);
30         this.handlers.registerSerializer(TunnelType.RSVP_TE_P2MP_LSP.getClazz(), rsvpTeP2MpLspParser);
31
32         final MldpP2mpLspParser mldpP2mpLspParser = new MldpP2mpLspParser(addressFamilyRegistry);
33         this.handlers.registerParser(TunnelType.MLDP_P2MP_LSP.getIntValue(), mldpP2mpLspParser);
34         this.handlers.registerSerializer(TunnelType.MLDP_P2MP_LSP.getClazz(), mldpP2mpLspParser);
35
36         final PimSsmTreeParser pimSsmTreeParser = new PimSsmTreeParser();
37         this.handlers.registerParser(TunnelType.PIM_SSM_TREE.getIntValue(), pimSsmTreeParser);
38         this.handlers.registerSerializer(TunnelType.PIM_SSM_TREE.getClazz(), pimSsmTreeParser);
39
40         final PimSmTreeParser pimSmTreeParser = new PimSmTreeParser();
41         this.handlers.registerParser(TunnelType.PIM_SM_TREE.getIntValue(), pimSmTreeParser);
42         this.handlers.registerSerializer(TunnelType.PIM_SM_TREE.getClazz(), pimSmTreeParser);
43
44         final BidirPimTreeParser bidirPimTreeParser = new BidirPimTreeParser();
45         this.handlers.registerParser(TunnelType.BIDIR_PIM_TREE.getIntValue(), bidirPimTreeParser);
46         this.handlers.registerSerializer(TunnelType.BIDIR_PIM_TREE.getClazz(), bidirPimTreeParser);
47
48         final IngressReplicationParser ingressReplicationParser = new IngressReplicationParser();
49         this.handlers.registerParser(TunnelType.INGRESS_REPLICATION.getIntValue(), ingressReplicationParser);
50         this.handlers.registerSerializer(TunnelType.INGRESS_REPLICATION.getClazz(), ingressReplicationParser);
51
52         final MldpMp2mpLspParser mldpMp2mpLspParser = new MldpMp2mpLspParser();
53         this.handlers.registerParser(TunnelType.M_LDP_MP_2_MP_LSP.getIntValue(), mldpMp2mpLspParser);
54         this.handlers.registerSerializer(TunnelType.M_LDP_MP_2_MP_LSP.getClazz(), mldpMp2mpLspParser);
55     }
56
57     public TunnelIdentifier parse(final int tunnelType, final ByteBuf buffer) {
58         final TunnelIdentifierParser parser = this.handlers.getParser(tunnelType);
59         if (!buffer.isReadable() || parser == null) {
60             LOG.debug(SKIP_PARSE, tunnelType);
61             return null;
62         }
63         return parser.parse(buffer);
64     }
65
66     public int serialize(final TunnelIdentifier tunnel, final ByteBuf tunnelBuffer) {
67         if (tunnel == null) {
68             LOG.debug(SKIP_SERIALIZATION);
69             return NO_TUNNEL_INFORMATION_PRESENT;
70         }
71         final TunnelIdentifierSerializer serializer = this.handlers.getSerializer(tunnel.getImplementedInterface());
72         if (serializer == null) {
73             LOG.debug(SKIP_SERIALIZATION, tunnel);
74             return NO_TUNNEL_INFORMATION_PRESENT;
75         }
76         return serializer.serialize(tunnel, tunnelBuffer);
77     }
78 }