Merge "Initial support for RFC2385"
[bgpcep.git] / pcep / ietf-stateful07 / src / main / java / org / opendaylight / protocol / pcep / ietf / stateful07 / Stateful07LSPIdentifierIpv6TlvParser.java
1 /*
2  * Copyright (c) 2013 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.pcep.ietf.stateful07;
9
10 import org.opendaylight.protocol.concepts.Ipv6Util;
11 import org.opendaylight.protocol.pcep.impl.tlv.TlvUtil;
12 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
13 import org.opendaylight.protocol.pcep.spi.TlvParser;
14 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
15 import org.opendaylight.protocol.util.ByteArray;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.identifiers.tlv.LspIdentifiers;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.identifiers.tlv.LspIdentifiersBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.identifiers.tlv.lsp.identifiers.AddressFamily;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.identifiers.tlv.lsp.identifiers.address.family.Ipv6Case;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.identifiers.tlv.lsp.identifiers.address.family.Ipv6CaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.identifiers.tlv.lsp.identifiers.address.family.ipv6._case.Ipv6;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.ietf.stateful.rev131222.lsp.identifiers.tlv.lsp.identifiers.address.family.ipv6._case.Ipv6Builder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.Ipv6ExtendedTunnelId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.LspId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev130820.TunnelId;
27
28 /**
29  * Parser for {@link LspIdentifiers}
30  */
31 public final class Stateful07LSPIdentifierIpv6TlvParser implements TlvParser, TlvSerializer {
32
33         public static final int TYPE = 19;
34
35         private static final int IP6_F_LENGTH = 16;
36         private static final int EX_TUNNEL_ID6_F_LENGTH = 16;
37
38         private static final int LSP_ID_F_LENGTH = 2;
39         private static final int TUNNEL_ID_F_LENGTH = 2;
40
41         private static final int V6_LENGTH = 52;
42
43         @Override
44         public LspIdentifiers parseTlv(final byte[] valueBytes) throws PCEPDeserializerException {
45                 if (valueBytes == null || valueBytes.length == 0) {
46                         throw new IllegalArgumentException("Value bytes array is mandatory. Can't be null or empty.");
47                 }
48                 if (valueBytes.length != V6_LENGTH) {
49                         throw new IllegalArgumentException("Length " + valueBytes.length + " does not match LSP Identifiers Ipv6 tlv length.");
50                 }
51                 int position = 0;
52                 final Ipv6Builder builder = new Ipv6Builder();
53                 builder.setIpv6TunnelSenderAddress(Ipv6Util.addressForBytes(ByteArray.subByte(valueBytes, position, IP6_F_LENGTH)));
54                 position += IP6_F_LENGTH;
55                 final LspId lspId = new LspId(ByteArray.bytesToLong(ByteArray.subByte(valueBytes, position, LSP_ID_F_LENGTH)));
56                 position += LSP_ID_F_LENGTH;
57                 final TunnelId tunnelId = new TunnelId(ByteArray.bytesToInt(ByteArray.subByte(valueBytes, position, TUNNEL_ID_F_LENGTH)));
58                 position += TUNNEL_ID_F_LENGTH;
59                 builder.setIpv6ExtendedTunnelId(new Ipv6ExtendedTunnelId(Ipv6Util.addressForBytes(ByteArray.subByte(valueBytes, position,
60                                 EX_TUNNEL_ID6_F_LENGTH))));
61                 position += EX_TUNNEL_ID6_F_LENGTH;
62                 builder.setIpv6TunnelEndpointAddress(Ipv6Util.addressForBytes(ByteArray.subByte(valueBytes, position, IP6_F_LENGTH)));
63                 final AddressFamily afi = new Ipv6CaseBuilder().setIpv6(builder.build()).build();
64                 return new LspIdentifiersBuilder().setAddressFamily(afi).setLspId(lspId).setTunnelId(tunnelId).build();
65         }
66
67         @Override
68         public byte[] serializeTlv(final Tlv tlv) {
69                 if (tlv == null) {
70                         throw new IllegalArgumentException("LspIdentifiersTlv is mandatory.");
71                 }
72                 final byte[] bytes = new byte[V6_LENGTH];
73                 int offset = 0;
74                 final LspIdentifiers lsp = (LspIdentifiers) tlv;
75                 final Ipv6 ipv6 = ((Ipv6Case) lsp.getAddressFamily()).getIpv6();
76                 ByteArray.copyWhole(Ipv6Util.bytesForAddress(ipv6.getIpv6TunnelSenderAddress()), bytes, offset);
77                 offset += IP6_F_LENGTH;
78                 ByteArray.copyWhole(ByteArray.longToBytes(lsp.getLspId().getValue(), LSP_ID_F_LENGTH), bytes, offset);
79                 offset += LSP_ID_F_LENGTH;
80                 ByteArray.copyWhole(ByteArray.intToBytes(lsp.getTunnelId().getValue(), TUNNEL_ID_F_LENGTH), bytes, offset);
81                 offset += TUNNEL_ID_F_LENGTH;
82                 ByteArray.copyWhole(Ipv6Util.bytesForAddress(ipv6.getIpv6ExtendedTunnelId()), bytes, offset);
83                 offset += EX_TUNNEL_ID6_F_LENGTH;
84                 ByteArray.copyWhole(Ipv6Util.bytesForAddress(ipv6.getIpv6TunnelEndpointAddress()), bytes, offset);
85                 return TlvUtil.formatTlv(TYPE, bytes);
86         }
87
88         @Override
89         public int getType() {
90                 return TYPE;
91         }
92 }