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