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.PCEPDocumentedException;
12 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
13 import org.opendaylight.protocol.util.ByteArray;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.PcepErrorObject;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ReqMissingTlv;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.Tlvs;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.TlvsBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcep.error.object.tlvs.ReqMissingBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.Errors;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.pcerr.message.ErrorsBuilder;
26 * Parser for {@link org.opendaylight.protocol.pcep.object.PCEPErrorObject PCEPErrorObject}
28 public class PCEPErrorObjectParser extends AbstractObjectWithTlvsParser<ErrorsBuilder> {
30 public static final int CLASS = 13;
32 public static final int TYPE = 1;
34 public static final int FLAGS_F_LENGTH = 1;
35 public static final int ET_F_LENGTH = 1;
36 public static final int EV_F_LENGTH = 1;
38 public static final int FLAGS_F_OFFSET = 1; // added reserved field of size 1 byte
39 public static final int ET_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
40 public static final int EV_F_OFFSET = ET_F_OFFSET + ET_F_LENGTH;
41 public static final int TLVS_OFFSET = EV_F_OFFSET + EV_F_LENGTH;
43 public PCEPErrorObjectParser(final TlvHandlerRegistry tlvReg) {
48 public PcepErrorObject parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException,
49 PCEPDocumentedException {
51 throw new IllegalArgumentException("Array of bytes is mandatory.");
54 final ErrorsBuilder builder = new ErrorsBuilder();
56 parseTlvs(builder, ByteArray.cutBytes(bytes, TLVS_OFFSET));
58 builder.setIgnore(header.isIgnore());
59 builder.setProcessingRule(header.isProcessingRule());
61 builder.setType((short) (bytes[ET_F_OFFSET] & 0xFF));
62 builder.setValue((short) (bytes[EV_F_OFFSET] & 0xFF));
64 return builder.build();
68 public void addTlv(final ErrorsBuilder builder, final Tlv tlv) {
69 if (tlv instanceof ReqMissingTlv && builder.getType() == 7) {
70 builder.setTlvs(new TlvsBuilder().setReqMissing(
71 new ReqMissingBuilder().setRequestId(((ReqMissingTlv) tlv).getRequestId()).build()).build());
76 public byte[] serializeObject(final Object object) {
77 if (!(object instanceof PcepErrorObject)) {
78 throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed PcepErrorObject.");
81 final PcepErrorObject errObj = (PcepErrorObject) object;
83 final byte[] tlvs = serializeTlvs(((Errors) errObj).getTlvs());
86 tlvsLength = tlvs.length;
88 final byte[] retBytes = new byte[TLVS_OFFSET + tlvsLength + getPadding(TLVS_OFFSET + tlvs.length, PADDED_TO)];
91 ByteArray.copyWhole(tlvs, retBytes, TLVS_OFFSET);
94 retBytes[ET_F_OFFSET] = ByteArray.shortToBytes(errObj.getType())[1];
95 retBytes[EV_F_OFFSET] = ByteArray.shortToBytes(errObj.getValue())[1];
100 public byte[] serializeTlvs(final Tlvs tlvs) {
101 if (tlvs.getReqMissing() != null) {
102 return serializeTlv(new ReqMissingBuilder().setRequestId(tlvs.getReqMissing().getRequestId()).build());
108 public int getObjectType() {
113 public int getObjectClass() {