Clean pcep/base-parser code
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / object / PCEPErrorObjectParser.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.PCEPErrors;
20 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
21 import org.opendaylight.protocol.pcep.spi.VendorInformationTlvRegistry;
22 import org.opendaylight.protocol.util.ByteBufUtils;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Tlv;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcep.error.object.ErrorObject;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcep.error.object.ErrorObjectBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcep.error.object.error.object.Tlvs;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcep.error.object.error.object.TlvsBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.req.missing.tlv.ReqMissing;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.vendor.information.tlvs.VendorInformationTlv;
32
33 /**
34  * Parser for {@link ErrorObject}.
35  */
36 public final class PCEPErrorObjectParser extends AbstractObjectWithTlvsParser<ErrorObjectBuilder> {
37
38     private static final int CLASS = 13;
39     private static final int TYPE = 1;
40     private static final int FLAGS_F_LENGTH = 1;
41     private static final int RESERVED  = 1;
42
43     public PCEPErrorObjectParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg) {
44         super(tlvReg, viTlvReg, CLASS, TYPE);
45     }
46
47     @Override
48     public ErrorObject parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
49         Preconditions.checkArgument(bytes != null && bytes.isReadable(),
50             "Array of bytes is mandatory. Can't be null or empty.");
51         bytes.skipBytes(FLAGS_F_LENGTH + RESERVED);
52         final ErrorObjectBuilder builder = new ErrorObjectBuilder()
53                 .setIgnore(header.isIgnore())
54                 .setProcessingRule(header.isProcessingRule())
55                 .setType(ByteBufUtils.readUint8(bytes))
56                 .setValue(ByteBufUtils.readUint8(bytes));
57         parseTlvs(builder, bytes.slice());
58         return builder.build();
59     }
60
61     @Override
62     public void addTlv(final ErrorObjectBuilder builder, final Tlv tlv) {
63         if (tlv instanceof ReqMissing
64                 && PCEPErrors.SYNC_PATH_COMP_REQ_MISSING.getErrorType().equals(builder.getType())) {
65             builder.setTlvs(new TlvsBuilder().setReqMissing((ReqMissing) tlv).build());
66         }
67     }
68
69     @Override
70     public void serializeObject(final Object object, final ByteBuf buffer) {
71         Preconditions.checkArgument(object instanceof ErrorObject,
72             "Wrong instance of PCEPObject. Passed %s. Needed ErrorObject.", object.getClass());
73         final ErrorObject errObj = (ErrorObject) object;
74         final ByteBuf body = Unpooled.buffer();
75         body.writeZero(FLAGS_F_LENGTH + RESERVED);
76         Preconditions.checkArgument(errObj.getType() != null, "Type is mandatory.");
77         writeUnsignedByte(errObj.getType(), body);
78         Preconditions.checkArgument(errObj.getValue() != null, "Value is mandatory.");
79         writeUnsignedByte(errObj.getValue(), body);
80         serializeTlvs(errObj.getTlvs(), body);
81         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
82     }
83
84     public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
85         if (tlvs == null) {
86             return;
87         }
88         if (tlvs.getReqMissing() != null) {
89             serializeTlv(tlvs.getReqMissing(), body);
90         }
91         serializeVendorInformationTlvs(tlvs.getVendorInformationTlv(), body);
92     }
93
94     @Override
95     protected void addVendorInformationTlvs(
96         final ErrorObjectBuilder builder,
97         final List<VendorInformationTlv> tlvs) {
98         if (!tlvs.isEmpty()) {
99             builder.setTlvs(new TlvsBuilder(builder.getTlvs()).setVendorInformationTlv(tlvs).build());
100         }
101     }
102 }