Removed checkstyle warnings.
[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 io.netty.buffer.ByteBuf;
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.types.rev131005.Tlv;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.order.tlv.Order;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.order.tlv.OrderBuilder;
19
20 /**
21  * Parser for {@link Order}
22  */
23 public class OrderTlvParser implements TlvParser, TlvSerializer {
24
25     public static final int TYPE = 5;
26
27     private static final int ORDR_DEL_LENGTH = 4;
28
29     private static final int ORDR_SETUP_LENGTH = 4;
30
31     @Override
32     public Order parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
33         if (buffer == null) {
34             return null;
35         }
36         return new OrderBuilder().setDelete(buffer.readUnsignedInt()).setSetup(buffer.readUnsignedInt()).build();
37     }
38
39     @Override
40     public byte[] serializeTlv(final Tlv tlv) {
41         if (tlv == null) {
42             throw new IllegalArgumentException("OrderTlv is mandatory.");
43         }
44         final Order otlv = (Order) tlv;
45         final byte[] bytes = new byte[ORDR_DEL_LENGTH + ORDR_SETUP_LENGTH];
46         int offset = 0;
47         ByteArray.copyWhole(ByteArray.longToBytes(otlv.getDelete(), ORDR_DEL_LENGTH), bytes, offset);
48         offset += ORDR_DEL_LENGTH;
49         ByteArray.copyWhole(ByteArray.longToBytes(otlv.getSetup(), ORDR_SETUP_LENGTH), bytes, offset);
50         return TlvUtil.formatTlv(TYPE, bytes);
51     }
52 }