2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.protocol.bgp.evpn.impl.attributes.tunnel.identifier;
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;
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;
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;
38 MldpP2mpLspParser(final AddressFamilyRegistry addressFamilyRegistry) {
39 this.addressFamilyRegistry = addressFamilyRegistry;
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();
50 final ByteBuf opaqueValues = Unpooled.buffer();
51 final int addressFamily = getAddressFamilyValue(mldpP2mpLsp.getAddressFamily());
53 if (!serializeOpaqueList(mldpP2mpLsp.getOpaqueValue(), opaqueValues) || addressFamily == 0) {
54 return NO_TUNNEL_INFORMATION_PRESENT;
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);
62 ByteBufWriteUtil.writeUnsignedShort(opaqueValues.readableBytes(), buffer);
63 buffer.writeBytes(opaqueValues);
64 return TunnelType.MLDP_P2MP_LSP.getIntValue();
67 private static short getAdressFamilyLength(final IpAddress ipAddress) {
68 if (ipAddress.getIpv4Address() == null) {
69 return Ipv6Util.IPV6_LENGTH;
71 return Ipv4Util.IP4_LENGTH;
74 private int getAddressFamilyValue(final Class<? extends AddressFamily> addressFamily) {
75 final Integer type = this.addressFamilyRegistry.numberForClass(addressFamily);
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));
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();