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 org.opendaylight.protocol.pcep.PCEPDeserializerException;
11 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
12 import org.opendaylight.protocol.util.ByteArray;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.ErrorObject;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.ErrorObjectBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.error.object.Tlvs;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.error.object.TlvsBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.req.missing.tlv.ReqMissing;
22 import com.google.common.primitives.UnsignedBytes;
25 * Parser for {@link ErrorObject}
27 public class PCEPErrorObjectParser extends AbstractObjectWithTlvsParser<ErrorObjectBuilder> {
29 public static final int CLASS = 13;
31 public static final int TYPE = 1;
33 private static final int FLAGS_F_LENGTH = 1;
34 private static final int ET_F_LENGTH = 1;
35 private static final int EV_F_LENGTH = 1;
37 private static final int FLAGS_F_OFFSET = 1;
38 private static final int ET_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
39 private static final int EV_F_OFFSET = ET_F_OFFSET + ET_F_LENGTH;
40 private static final int TLVS_OFFSET = EV_F_OFFSET + EV_F_LENGTH;
42 public PCEPErrorObjectParser(final TlvHandlerRegistry tlvReg) {
47 public ErrorObject parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException {
49 throw new IllegalArgumentException("Array of bytes is mandatory.");
52 final ErrorObjectBuilder builder = new ErrorObjectBuilder();
53 builder.setIgnore(header.isIgnore());
54 builder.setProcessingRule(header.isProcessingRule());
55 builder.setType((short) UnsignedBytes.toInt(bytes[ET_F_OFFSET]));
56 builder.setValue((short) UnsignedBytes.toInt(bytes[EV_F_OFFSET]));
57 parseTlvs(builder, ByteArray.cutBytes(bytes, TLVS_OFFSET));
58 return builder.build();
62 public void addTlv(final ErrorObjectBuilder builder, final Tlv tlv) {
63 if (tlv instanceof ReqMissing && builder.getType() == 7) {
64 builder.setTlvs(new TlvsBuilder().setReqMissing((ReqMissing) tlv).build());
69 public byte[] serializeObject(final Object object) {
70 if (!(object instanceof ErrorObject)) {
71 throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed PcepErrorObject.");
73 final ErrorObject errObj = (ErrorObject) object;
75 final byte[] tlvs = serializeTlvs(errObj.getTlvs());
77 final byte[] retBytes = new byte[TLVS_OFFSET + tlvs.length + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
78 if (tlvs.length != 0) {
79 ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
81 retBytes[ET_F_OFFSET] = UnsignedBytes.checkedCast(errObj.getType());
82 retBytes[EV_F_OFFSET] = UnsignedBytes.checkedCast(errObj.getValue());
86 public byte[] serializeTlvs(final Tlvs tlvs) {
89 } else if (tlvs.getReqMissing() != null) {
90 return serializeTlv(tlvs.getReqMissing());
96 public int getObjectType() {
101 public int getObjectClass() {