2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.pcep.parser.object;
10 import static com.google.common.base.Preconditions.checkArgument;
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import java.util.List;
15 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
16 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
17 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
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;
33 * Parser for {@link ErrorObject}.
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;
41 public PCEPErrorObjectParser(final TlvRegistry tlvReg, final VendorInformationTlvRegistry viTlvReg) {
42 super(tlvReg, viTlvReg, CLASS, TYPE);
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();
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());
67 public void serializeObject(final Object object, final ByteBuf buffer) {
68 checkArgument(object instanceof ErrorObject, "Wrong instance of PCEPObject. Passed %s. Needed ErrorObject.",
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);
79 public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
81 if (tlvs.getReqMissing() != null) {
82 serializeTlv(tlvs.getReqMissing(), body);
84 serializeVendorInformationTlvs(tlvs.getVendorInformationTlv(), body);
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());