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