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.impl.object;
10 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
12 import com.google.common.base.Preconditions;
13 import com.google.common.primitives.UnsignedBytes;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
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.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.ErrorObject;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.ErrorObjectBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.error.object.Tlvs;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.error.object.TlvsBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.req.missing.tlv.ReqMissing;
30 * Parser for {@link ErrorObject}
32 public class PCEPErrorObjectParser extends AbstractObjectWithTlvsParser<ErrorObjectBuilder> {
34 public static final int CLASS = 13;
36 public static final int TYPE = 1;
38 private static final int FLAGS_F_LENGTH = 1;
40 private static final int RESERVED = 1;
42 public PCEPErrorObjectParser(final TlvRegistry tlvReg) {
47 public ErrorObject parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
48 Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
49 final ErrorObjectBuilder builder = new ErrorObjectBuilder();
50 builder.setIgnore(header.isIgnore());
51 builder.setProcessingRule(header.isProcessingRule());
52 bytes.readerIndex(bytes.readerIndex() + FLAGS_F_LENGTH + RESERVED);
53 builder.setType((short) UnsignedBytes.toInt(bytes.readByte()));
54 builder.setValue((short) UnsignedBytes.toInt(bytes.readByte()));
55 parseTlvs(builder, bytes.slice());
56 return builder.build();
60 public void addTlv(final ErrorObjectBuilder builder, final Tlv tlv) {
61 if (tlv instanceof ReqMissing && builder.getType() == 7) {
62 builder.setTlvs(new TlvsBuilder().setReqMissing((ReqMissing) tlv).build());
67 public void serializeObject(final Object object, final ByteBuf buffer) {
68 Preconditions.checkArgument(object instanceof ErrorObject, "Wrong instance of PCEPObject. Passed %s. Needed ErrorObject.", object.getClass());
69 final ErrorObject errObj = (ErrorObject) object;
70 final ByteBuf body = Unpooled.buffer();
71 body.writeZero(FLAGS_F_LENGTH + RESERVED);
72 Preconditions.checkArgument(errObj.getType() != null, "Type is mandatory.");
73 writeUnsignedByte(errObj.getType(), body);
74 Preconditions.checkArgument(errObj.getValue() != null, "Value is mandatory.");
75 writeUnsignedByte(errObj.getValue(), body);
76 serializeTlvs(errObj.getTlvs(), body);
77 ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
80 public void serializeTlvs(final Tlvs tlvs, final ByteBuf body) {
83 } else if (tlvs.getReqMissing() != null) {
84 serializeTlv(tlvs.getReqMissing(), body);