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