Clean pcep/base-parser code
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / 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.parser.tlv;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedInt;
12
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
16 import org.opendaylight.protocol.pcep.spi.TlvParser;
17 import org.opendaylight.protocol.pcep.spi.TlvSerializer;
18 import org.opendaylight.protocol.pcep.spi.TlvUtil;
19 import org.opendaylight.protocol.util.ByteBufUtils;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Tlv;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.order.tlv.Order;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.order.tlv.OrderBuilder;
23
24 /**
25  * Parser for {@link Order}.
26  */
27 public class OrderTlvParser implements TlvParser, TlvSerializer {
28
29     public static final int TYPE = 5;
30
31     @Override
32     public Order parseTlv(final ByteBuf buffer) throws PCEPDeserializerException {
33         if (buffer == null) {
34             return null;
35         }
36         return new OrderBuilder()
37                 .setDelete(ByteBufUtils.readUint32(buffer))
38                 .setSetup(ByteBufUtils.readUint32(buffer))
39                 .build();
40     }
41
42     @Override
43     public void serializeTlv(final Tlv tlv, final ByteBuf buffer) {
44         checkArgument(tlv instanceof Order, "OrderTlv is mandatory.");
45         final Order otlv = (Order) tlv;
46         final ByteBuf body = Unpooled.buffer();
47         writeUnsignedInt(otlv.getDelete(), body);
48         writeUnsignedInt(otlv.getSetup(), body);
49         TlvUtil.formatTlv(TYPE, body, buffer);
50     }
51 }