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 com.google.common.base.Preconditions;
11 import com.google.common.primitives.UnsignedBytes;
13 import io.netty.buffer.ByteBuf;
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.TlvRegistry;
19 import org.opendaylight.protocol.util.ByteArray;
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;
39 private static final int ET_F_LENGTH = 1;
40 private static final int EV_F_LENGTH = 1;
42 private static final int FLAGS_F_OFFSET = 1;
43 private static final int ET_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
44 private static final int EV_F_OFFSET = ET_F_OFFSET + ET_F_LENGTH;
45 private static final int TLVS_OFFSET = EV_F_OFFSET + EV_F_LENGTH;
47 public PCEPErrorObjectParser(final TlvRegistry tlvReg) {
52 public ErrorObject parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
53 Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
54 final ErrorObjectBuilder builder = new ErrorObjectBuilder();
55 builder.setIgnore(header.isIgnore());
56 builder.setProcessingRule(header.isProcessingRule());
57 bytes.readerIndex(bytes.readerIndex() + ET_F_OFFSET);
58 builder.setType((short) UnsignedBytes.toInt(bytes.readByte()));
59 builder.setValue((short) UnsignedBytes.toInt(bytes.readByte()));
60 parseTlvs(builder, bytes.slice());
61 return builder.build();
65 public void addTlv(final ErrorObjectBuilder builder, final Tlv tlv) {
66 if (tlv instanceof ReqMissing && builder.getType() == 7) {
67 builder.setTlvs(new TlvsBuilder().setReqMissing((ReqMissing) tlv).build());
72 public byte[] serializeObject(final Object object) {
73 if (!(object instanceof ErrorObject)) {
74 throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed PcepErrorObject.");
76 final ErrorObject errObj = (ErrorObject) object;
78 final byte[] tlvs = serializeTlvs(errObj.getTlvs());
80 final byte[] retBytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
81 if (tlvs.length != 0) {
82 ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
84 retBytes[ET_F_OFFSET] = UnsignedBytes.checkedCast(errObj.getType());
85 retBytes[EV_F_OFFSET] = UnsignedBytes.checkedCast(errObj.getValue());
86 return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
89 public byte[] serializeTlvs(final Tlvs tlvs) {
92 } else if (tlvs.getReqMissing() != null) {
93 return serializeTlv(tlvs.getReqMissing());