Moved stateful02 parsers to its own artefact.
[bgpcep.git] / pcep / ietf-stateful02 / src / main / java / org / opendaylight / protocol / pcep / ietf / stateful02 / LspDbVersionTlvParser.java
1 /*
2  * Copyright (c) 2014 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.stateful02;
9
10 import java.util.Arrays;
11
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.crabbe.stateful._02.rev140110.lsp.db.version.tlv.LspDbVersion;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.crabbe.stateful._02.rev140110.lsp.db.version.tlv.LspDbVersionBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
19
20 import com.google.common.primitives.UnsignedLong;
21
22 /**
23  * Parser for {@link LspDbVersion}
24  */
25 public class LspDbVersionTlvParser implements TlvParser, TlvSerializer {
26
27         public static final int TYPE = 23;
28
29         private static final int DBV_F_LENGTH = 8;
30
31         @Override
32         public LspDbVersion parseTlv(final byte[] buffer) throws PCEPDeserializerException {
33                 return new LspDbVersionBuilder().setVersion(
34                                 UnsignedLong.fromLongBits(ByteArray.bytesToLong(ByteArray.subByte(buffer, 0, DBV_F_LENGTH))).bigIntegerValue()).build();
35         }
36
37         @Override
38         public byte[] serializeTlv(final Tlv tlv) {
39                 if (tlv == null) {
40                         throw new IllegalArgumentException("LspDbVersionTlv is mandatory.");
41                 }
42                 final LspDbVersion lsp = (LspDbVersion) tlv;
43                 final byte[] array = ByteArray.longToBytes(UnsignedLong.valueOf(lsp.getVersion()).longValue(), DBV_F_LENGTH);
44                 if (array.length > DBV_F_LENGTH) {
45                         throw new IllegalArgumentException("LspDBVersion too big.");
46                 }
47                 final byte[] result = new byte[DBV_F_LENGTH];
48                 Arrays.fill(result, (byte) 0);
49                 int j = 7;
50                 for (int i = array.length - 1; i >= 0; i--) {
51                         result[j] |= array[i];
52                         j--;
53                 }
54                 return result;
55         }
56
57         @Override
58         public int getType() {
59                 return TYPE;
60         }
61 }