e0d57a433acc0fcd0b7ae0f7c787197d2a5841c0
[bgpcep.git] / bgp / evpn / src / main / java / org / opendaylight / protocol / bgp / evpn / impl / attributes / tunnel / identifier / MldpP2mpLspParser.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 static org.opendaylight.protocol.bgp.evpn.impl.attributes.tunnel.identifier.OpaqueUtil.serializeOpaqueList;
12 import static org.opendaylight.protocol.bgp.evpn.impl.attributes.tunnel.identifier.PAddressPMulticastGroupUtil.parseIpAddress;
13 import static org.opendaylight.protocol.bgp.evpn.impl.attributes.tunnel.identifier.PAddressPMulticastGroupUtil.serializeIpAddress;
14 import static org.opendaylight.protocol.bgp.evpn.impl.attributes.tunnel.identifier.TunnelIdentifierHandler.NO_TUNNEL_INFORMATION_PRESENT;
15
16 import com.google.common.base.Preconditions;
17 import io.netty.buffer.ByteBuf;
18 import io.netty.buffer.ByteBufUtil;
19 import io.netty.buffer.Unpooled;
20 import org.opendaylight.protocol.bgp.parser.spi.AddressFamilyRegistry;
21 import org.opendaylight.protocol.util.ByteBufWriteUtil;
22 import org.opendaylight.protocol.util.Ipv4Util;
23 import org.opendaylight.protocol.util.Ipv6Util;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev160812.pmsi.tunnel.pmsi.tunnel.TunnelIdentifier;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev160812.pmsi.tunnel.pmsi.tunnel.tunnel.identifier.MldpP2mpLsp;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev160812.pmsi.tunnel.pmsi.tunnel.tunnel.identifier.MldpP2mpLspBuilder;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 final class MldpP2mpLspParser implements TunnelIdentifierSerializer, TunnelIdentifierParser {
33     private static final Logger LOG = LoggerFactory.getLogger(MldpP2mpLspParser.class);
34     private static final short P2MP_TYPE = 6;
35     private static final int RESERVED = 1;
36     private final AddressFamilyRegistry addressFamilyRegistry;
37
38     MldpP2mpLspParser(final AddressFamilyRegistry addressFamilyRegistry) {
39         this.addressFamilyRegistry = addressFamilyRegistry;
40     }
41
42     @Override
43     public int serialize(final TunnelIdentifier tunnelIdentifier, final ByteBuf buffer) {
44         Preconditions.checkArgument(tunnelIdentifier instanceof MldpP2mpLsp,
45                 "The tunnelIdentifier %s is not RsvpTeP2mpLps type.", tunnelIdentifier);
46         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev160812.pmsi.tunnel.pmsi
47                 .tunnel.tunnel.identifier.mldp.p2mp.lsp.MldpP2mpLsp mldpP2mpLsp =
48                 ((MldpP2mpLsp) tunnelIdentifier).getMldpP2mpLsp();
49
50         final ByteBuf opaqueValues = Unpooled.buffer();
51         final int addressFamily = getAddressFamilyValue(mldpP2mpLsp.getAddressFamily());
52
53         if (!serializeOpaqueList(mldpP2mpLsp.getOpaqueValue(), opaqueValues) || addressFamily == 0) {
54             return NO_TUNNEL_INFORMATION_PRESENT;
55         }
56         final IpAddress rootNode = mldpP2mpLsp.getRootNodeAddress();
57         ByteBufWriteUtil.writeUnsignedByte(P2MP_TYPE, buffer);
58         ByteBufWriteUtil.writeUnsignedShort(addressFamily, buffer);
59         ByteBufWriteUtil.writeUnsignedByte(getAdressFamilyLength(rootNode), buffer);
60         serializeIpAddress(rootNode, buffer);
61
62         ByteBufWriteUtil.writeUnsignedShort(opaqueValues.readableBytes(), buffer);
63         buffer.writeBytes(opaqueValues);
64         return TunnelType.MLDP_P2MP_LSP.getIntValue();
65     }
66
67     private static short getAdressFamilyLength(final IpAddress ipAddress) {
68         if (ipAddress.getIpv4Address() == null) {
69             return Ipv6Util.IPV6_LENGTH;
70         }
71         return Ipv4Util.IP4_LENGTH;
72     }
73
74     private int getAddressFamilyValue(final Class<? extends AddressFamily> addressFamily) {
75         final Integer type = this.addressFamilyRegistry.numberForClass(addressFamily);
76         if (type == null) {
77             return 0;
78         }
79         return type;
80     }
81
82     @Override
83     public TunnelIdentifier parse(final ByteBuf buffer) {
84         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev160812.pmsi.tunnel.pmsi
85                 .tunnel.tunnel.identifier.mldp.p2mp.lsp.MldpP2mpLspBuilder mldpP2mpLsp =
86                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev160812.pmsi.tunnel
87                         .pmsi.tunnel.tunnel.identifier.mldp.p2mp.lsp.MldpP2mpLspBuilder();
88         buffer.skipBytes(RESERVED);
89         final Class<? extends AddressFamily> addressFamily = this.addressFamilyRegistry
90                 .classForFamily(buffer.readUnsignedShort());
91         if (addressFamily == null) {
92             LOG.debug("Skipping serialization of TunnelIdentifier {}, address family type  supported",
93                     ByteBufUtil.hexDump(buffer));
94             return null;
95         }
96         mldpP2mpLsp.setAddressFamily(addressFamily);
97         final short rootNodeLength = buffer.readUnsignedByte();
98         mldpP2mpLsp.setRootNodeAddress(parseIpAddress(rootNodeLength, buffer.readBytes(rootNodeLength)));
99         final int opaqueValueLength = buffer.readUnsignedShort();
100         mldpP2mpLsp.setOpaqueValue(OpaqueUtil.parseOpaqueList(buffer.readBytes(opaqueValueLength)));
101         return new MldpP2mpLspBuilder().setMldpP2mpLsp(mldpP2mpLsp.build()).build();
102     }
103 }