BUG-50 : added tests for PCEP TLVs.
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / tlv / OrderTlvParser.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.impl.tlv;
9
10 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
11 import org.opendaylight.protocol.pcep.spi.TlvParser;
12 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
13 import org.opendaylight.protocol.util.ByteArray;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OrderTlv;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.rp.object.tlvs.OrderBuilder;
17
18 /**
19  * Parser for {@link OrderTlv}
20  */
21 public class OrderTlvParser implements TlvParser, TlvSerializer {
22
23         public static final int TYPE = 5;
24
25         private static final int ORDR_DEL_LENGTH = 4;
26
27         private static final int ORDR_SETUP_LENGTH = 4;
28
29         @Override
30         public OrderTlv parseTlv(final byte[] buffer) throws PCEPDeserializerException {
31                 return new OrderBuilder().setDelete(Long.valueOf(ByteArray.bytesToLong(ByteArray.subByte(buffer, 0, ORDR_DEL_LENGTH)))).setSetup(
32                                 ByteArray.bytesToLong(ByteArray.subByte(buffer, ORDR_DEL_LENGTH, ORDR_SETUP_LENGTH))).build();
33         }
34
35         @Override
36         public byte[] serializeTlv(final Tlv tlv) {
37                 if (tlv == null) {
38                         throw new IllegalArgumentException("OrderTlv is mandatory.");
39                 }
40                 final OrderTlv otlv = (OrderTlv) tlv;
41                 final byte[] bytes = new byte[ORDR_DEL_LENGTH + ORDR_SETUP_LENGTH];
42                 int offset = 0;
43                 ByteArray.copyWhole(ByteArray.subByte(ByteArray.longToBytes(otlv.getDelete()), 4, ORDR_DEL_LENGTH), bytes, offset);
44                 offset += ORDR_DEL_LENGTH;
45                 ByteArray.copyWhole(ByteArray.subByte(ByteArray.longToBytes(otlv.getSetup()), 4, ORDR_SETUP_LENGTH), bytes, offset);
46                 return bytes;
47         }
48
49         @Override
50         public int getType() {
51                 return TYPE;
52         }
53 }