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