Clean pcep/base-parser code
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / object / PCEPCloseObjectParser.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.object;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
12
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import java.util.List;
16 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
17 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
18 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
19 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
20 import org.opendaylight.protocol.pcep.spi.VendorInformationTlvRegistry;
21 import org.opendaylight.protocol.util.ByteBufUtils;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.close.object.CClose;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.close.object.CCloseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.close.object.c.close.Tlvs;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.close.object.c.close.TlvsBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.tlvs.VendorInformationTlv;
29 import org.opendaylight.yangtools.yang.common.Uint8;
30
31 /**
32  * Parser for {@link
33  *     org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.close.object.CClose
34  *     PCEPCloseObject
35  * }.
36  */
37 public final class PCEPCloseObjectParser extends AbstractObjectWithTlvsParser<TlvsBuilder> {
38
39     private static final int CLASS = 15;
40     private static final int TYPE = 1;
41
42     /*
43      * lengths of fields in bytes
44      */
45     private static final int RESERVED = 2;
46     private static final int FLAGS_F_LENGTH = 1;
47
48     public PCEPCloseObjectParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg) {
49         super(tlvReg, viTlvReg, CLASS, TYPE);
50     }
51
52
53     @Override
54     public CClose parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
55         checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Cannot be null or empty.");
56         bytes.skipBytes(FLAGS_F_LENGTH + RESERVED);
57         final Uint8 reason = ByteBufUtils.readUint8(bytes);
58         final TlvsBuilder tlvsBuilder = new TlvsBuilder();
59         parseTlvs(tlvsBuilder, bytes.slice());
60         final CCloseBuilder builder = new CCloseBuilder()
61                 .setIgnore(header.isIgnore())
62                 .setProcessingRule(header.isProcessingRule())
63                 .setReason(reason)
64                 .setTlvs(tlvsBuilder.build());
65         return builder.build();
66     }
67
68     @Override
69     public void serializeObject(final Object object, final ByteBuf buffer) {
70         checkArgument(object instanceof CClose, "Wrong instance of PCEPObject. Passed %s. Needed CCloseObject.",
71             object.getClass());
72         final CClose obj = (CClose) object;
73         final ByteBuf body = Unpooled.buffer();
74         body.writeZero(RESERVED + FLAGS_F_LENGTH);
75         checkArgument(obj.getReason() != null, "Reason is mandatory.");
76         writeUnsignedByte(obj.getReason(), body);
77         serializeTlvs(obj.getTlvs(), body);
78         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
79     }
80
81     public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
82         if (tlvs == null) {
83             return;
84         }
85         serializeVendorInformationTlvs(tlvs.getVendorInformationTlv(), body);
86     }
87
88
89     @Override
90     protected void addVendorInformationTlvs(final TlvsBuilder builder, final List<VendorInformationTlv> tlvs) {
91         if (!tlvs.isEmpty()) {
92             builder.setVendorInformationTlv(tlvs);
93         }
94     }
95 }