d6c34c87b698caf874dd0ca79f66e74b4e6f69a2
[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 = new HandlerRegistry<>();
25
26     public TunnelIdentifierHandler(final AddressFamilyRegistry addressFamilyRegistry) {
27         final RsvpTeP2MpLspParser rsvpTeP2MpLspParser = new RsvpTeP2MpLspParser();
28         this.handlers.registerParser(TunnelType.RSVP_TE_P2MP_LSP.getIntValue(), rsvpTeP2MpLspParser);
29         this.handlers.registerSerializer(TunnelType.RSVP_TE_P2MP_LSP.getClazz(), rsvpTeP2MpLspParser);
30
31         final MldpP2mpLspParser mldpP2mpLspParser = new MldpP2mpLspParser(addressFamilyRegistry);
32         this.handlers.registerParser(TunnelType.MLDP_P2MP_LSP.getIntValue(), mldpP2mpLspParser);
33         this.handlers.registerSerializer(TunnelType.MLDP_P2MP_LSP.getClazz(), mldpP2mpLspParser);
34
35         final PimSsmTreeParser pimSsmTreeParser = new PimSsmTreeParser();
36         this.handlers.registerParser(TunnelType.PIM_SSM_TREE.getIntValue(), pimSsmTreeParser);
37         this.handlers.registerSerializer(TunnelType.PIM_SSM_TREE.getClazz(), pimSsmTreeParser);
38
39         final PimSmTreeParser pimSmTreeParser = new PimSmTreeParser();
40         this.handlers.registerParser(TunnelType.PIM_SM_TREE.getIntValue(), pimSmTreeParser);
41         this.handlers.registerSerializer(TunnelType.PIM_SM_TREE.getClazz(), pimSmTreeParser);
42
43         final BidirPimTreeParser bidirPimTreeParser = new BidirPimTreeParser();
44         this.handlers.registerParser(TunnelType.BIDIR_PIM_TREE.getIntValue(), bidirPimTreeParser);
45         this.handlers.registerSerializer(TunnelType.BIDIR_PIM_TREE.getClazz(), bidirPimTreeParser);
46
47         final IngressReplicationParser ingressReplicationParser = new IngressReplicationParser();
48         this.handlers.registerParser(TunnelType.INGRESS_REPLICATION.getIntValue(), ingressReplicationParser);
49         this.handlers.registerSerializer(TunnelType.INGRESS_REPLICATION.getClazz(), ingressReplicationParser);
50
51         final MldpMp2mpLspParser mldpMp2mpLspParser = new MldpMp2mpLspParser();
52         this.handlers.registerParser(TunnelType.M_LDP_MP_2_MP_LSP.getIntValue(), mldpMp2mpLspParser);
53         this.handlers.registerSerializer(TunnelType.M_LDP_MP_2_MP_LSP.getClazz(), mldpMp2mpLspParser);
54     }
55
56     public TunnelIdentifier parse(final int tunnelType, final ByteBuf buffer) {
57         final TunnelIdentifierParser parser = this.handlers.getParser(tunnelType);
58         if (!buffer.isReadable() || parser == null) {
59             LOG.debug(SKIP_PARSE, tunnelType);
60             return null;
61         }
62         return parser.parse(buffer);
63     }
64
65     public int serialize(final TunnelIdentifier tunnel, final ByteBuf tunnelBuffer) {
66         if (tunnel == null) {
67             LOG.debug(SKIP_SERIALIZATION);
68             return NO_TUNNEL_INFORMATION_PRESENT;
69         }
70         final TunnelIdentifierSerializer serializer = this.handlers.getSerializer(tunnel.getImplementedInterface());
71         if (serializer == null) {
72             LOG.debug(SKIP_SERIALIZATION, tunnel);
73             return NO_TUNNEL_INFORMATION_PRESENT;
74         }
75         return serializer.serialize(tunnel, tunnelBuffer);
76     }
77 }