16e7b67a86f8c2e2e028631cca95d341f90c1f95
[bgpcep.git] / bgp / evpn / src / main / java / org / opendaylight / protocol / bgp / evpn / impl / attributes / tunnel / identifier / RsvpTeP2MpLspParser.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.PAddressPMulticastGroupUtil.parseIpAddress;
12 import static org.opendaylight.protocol.bgp.evpn.impl.attributes.tunnel.identifier.PAddressPMulticastGroupUtil.serializeIpAddress;
13
14 import com.google.common.base.Preconditions;
15 import io.netty.buffer.ByteBuf;
16 import org.opendaylight.protocol.util.ByteBufWriteUtil;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev160812.pmsi.tunnel.pmsi.tunnel.TunnelIdentifier;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev160812.pmsi.tunnel.pmsi.tunnel.tunnel.identifier.RsvpTeP2mpLsp;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev160812.pmsi.tunnel.pmsi.tunnel.tunnel.identifier.RsvpTeP2mpLspBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev160812.pmsi.tunnel.pmsi.tunnel.tunnel.identifier.rsvp.te.p2mp.lsp.RsvpTeP2mpLps;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pmsi.tunnel.rev160812.pmsi.tunnel.pmsi.tunnel.tunnel.identifier.rsvp.te.p2mp.lsp.RsvpTeP2mpLpsBuilder;
22
23 final class RsvpTeP2MpLspParser implements TunnelIdentifierSerializer, TunnelIdentifierParser {
24
25     private static final int RESERVED = 2;
26
27     @Override
28     public int serialize(final TunnelIdentifier tunnelIdentifier, final ByteBuf buffer) {
29         Preconditions.checkArgument(tunnelIdentifier instanceof RsvpTeP2mpLsp,
30                 "The tunnelIdentifier %s is not RsvpTeP2mpLps type.", tunnelIdentifier);
31         final RsvpTeP2mpLps rsvpTeP2mpLsp = ((RsvpTeP2mpLsp) tunnelIdentifier).getRsvpTeP2mpLps();
32         ByteBufWriteUtil.writeUnsignedInt(rsvpTeP2mpLsp.getP2mpId(), buffer);
33         buffer.writeZero(RESERVED);
34         ByteBufWriteUtil.writeUnsignedShort(rsvpTeP2mpLsp.getTunnelId(), buffer);
35         serializeIpAddress(rsvpTeP2mpLsp.getExtendedTunnelId(), buffer);
36         return TunnelType.RSVP_TE_P2MP_LSP.getIntValue();
37     }
38
39     @Override
40     public TunnelIdentifier parse(final ByteBuf buffer) {
41         final RsvpTeP2mpLpsBuilder rsvpTeP2mpLps = new RsvpTeP2mpLpsBuilder();
42         rsvpTeP2mpLps.setP2mpId(buffer.readUnsignedInt());
43         buffer.skipBytes(2);
44         rsvpTeP2mpLps.setTunnelId(buffer.readUnsignedShort());
45         final int ipLength = buffer.readableBytes();
46         rsvpTeP2mpLps.setExtendedTunnelId(parseIpAddress(ipLength, buffer));
47         return new RsvpTeP2mpLspBuilder().setRsvpTeP2mpLps(rsvpTeP2mpLps.build()).build();
48     }
49 }