Eliminate use of ByteBufWriteUtil from mvpn
[bgpcep.git] / bgp / extensions / mvpn / src / main / java / org / opendaylight / protocol / bgp / mvpn / 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 package org.opendaylight.protocol.bgp.mvpn.impl.attributes.tunnel.identifier;
9
10 import static org.opendaylight.protocol.bgp.mvpn.impl.attributes.OpaqueUtil.serializeOpaqueList;
11 import static org.opendaylight.protocol.bgp.mvpn.impl.attributes.tunnel.identifier.PAddressPMulticastGroupUtil.parseIpAddress;
12 import static org.opendaylight.protocol.bgp.mvpn.impl.attributes.tunnel.identifier.PAddressPMulticastGroupUtil.serializeIpAddress;
13 import static org.opendaylight.protocol.bgp.mvpn.spi.pojo.attributes.tunnel.identifier.SimpleTunnelIdentifierRegistry.NO_TUNNEL_INFORMATION_PRESENT;
14
15 import io.netty.buffer.ByteBuf;
16 import io.netty.buffer.ByteBufUtil;
17 import io.netty.buffer.Unpooled;
18 import org.opendaylight.protocol.bgp.mvpn.impl.attributes.OpaqueUtil;
19 import org.opendaylight.protocol.bgp.mvpn.spi.attributes.tunnel.identifier.AbstractTunnelIdentifier;
20 import org.opendaylight.protocol.bgp.parser.spi.AddressFamilyRegistry;
21 import org.opendaylight.protocol.util.Ipv4Util;
22 import org.opendaylight.protocol.util.Ipv6Util;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.AddressFamily;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev180329.PmsiTunnelType;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev180329.pmsi.tunnel.pmsi.tunnel.TunnelIdentifier;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev180329.pmsi.tunnel.pmsi.tunnel.tunnel.identifier.MldpP2mpLsp;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev180329.pmsi.tunnel.pmsi.tunnel.tunnel.identifier.MldpP2mpLspBuilder;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public final class MldpP2mpLspParser extends AbstractTunnelIdentifier<MldpP2mpLsp> {
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     public MldpP2mpLspParser(final AddressFamilyRegistry addressFamilyRegistry) {
39         this.addressFamilyRegistry = addressFamilyRegistry;
40     }
41
42     @Override
43     public int serialize(final MldpP2mpLsp tunnelIdentifier, final ByteBuf buffer) {
44         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev180329.pmsi.tunnel.pmsi
45                 .tunnel.tunnel.identifier.mldp.p2mp.lsp.MldpP2mpLsp mldpP2mpLsp = tunnelIdentifier.getMldpP2mpLsp();
46
47         final ByteBuf opaqueValues = Unpooled.buffer();
48         final int addressFamily = getAddressFamilyValue(mldpP2mpLsp.getAddressFamily());
49
50         if (!serializeOpaqueList(mldpP2mpLsp.getOpaqueValue(), opaqueValues) || addressFamily == 0) {
51             return NO_TUNNEL_INFORMATION_PRESENT;
52         }
53         final IpAddress rootNode = mldpP2mpLsp.getRootNodeAddress();
54         buffer.writeByte(P2MP_TYPE);
55         buffer.writeShort(addressFamily);
56         buffer.writeByte(rootNode.getIpv4Address() != null ? Ipv4Util.IP4_LENGTH : Ipv6Util.IPV6_LENGTH);
57         serializeIpAddress(rootNode, buffer);
58
59         buffer.writeShort(opaqueValues.readableBytes());
60         buffer.writeBytes(opaqueValues);
61         return getType();
62     }
63
64     @Override
65     public Class<? extends TunnelIdentifier> getClazz() {
66         return MldpP2mpLsp.class;
67     }
68
69     @Override
70     public int getType() {
71         return PmsiTunnelType.MldpP2mpLsp.getIntValue();
72     }
73
74     private int getAddressFamilyValue(final Class<? extends AddressFamily> addressFamily) {
75         final Integer type = this.addressFamilyRegistry.numberForClass(addressFamily);
76         return type == null ? 0 : type;
77     }
78
79     @Override
80     public MldpP2mpLsp parse(final ByteBuf buffer) {
81         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev180329.pmsi.tunnel.pmsi
82                 .tunnel.tunnel.identifier.mldp.p2mp.lsp.MldpP2mpLspBuilder mldpP2mpLsp =
83                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev180329.pmsi.tunnel
84                         .pmsi.tunnel.tunnel.identifier.mldp.p2mp.lsp.MldpP2mpLspBuilder();
85         buffer.skipBytes(RESERVED);
86         final Class<? extends AddressFamily> addressFamily = this.addressFamilyRegistry
87                 .classForFamily(buffer.readUnsignedShort());
88         if (addressFamily == null) {
89             LOG.debug("Skipping serialization of TunnelIdentifier {}, address family type  supported",
90                     ByteBufUtil.hexDump(buffer));
91             return null;
92         }
93         mldpP2mpLsp.setAddressFamily(addressFamily);
94         final short rootNodeLength = buffer.readUnsignedByte();
95         mldpP2mpLsp.setRootNodeAddress(parseIpAddress(rootNodeLength, buffer.readBytes(rootNodeLength)));
96         final int opaqueValueLength = buffer.readUnsignedShort();
97         mldpP2mpLsp.setOpaqueValue(OpaqueUtil.parseOpaqueList(buffer.readBytes(opaqueValueLength)));
98         return new MldpP2mpLspBuilder().setMldpP2mpLsp(mldpP2mpLsp.build()).build();
99     }
100 }