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.PCEPObject;
12 import org.opendaylight.protocol.pcep.impl.PCEPObjectParser;
13 import org.opendaylight.protocol.pcep.impl.PCEPTlvParser;
14 import org.opendaylight.protocol.pcep.object.PCEPCloseObject;
15 import org.opendaylight.protocol.pcep.object.PCEPCloseObject.Reason;
16 import org.opendaylight.protocol.util.ByteArray;
19 * Parser for {@link org.opendaylight.protocol.pcep.object.PCEPCloseObject PCEPCloseObject}
21 public class PCEPCloseObjectParser implements PCEPObjectParser {
24 * lengths of fields in bytes
26 public static final int FLAGS_F_LENGTH = 1;
27 public static final int REASON_F_LENGTH = 1;
30 * offsets of fields in bytes
32 public static final int FLAGS_F_OFFSET = 2; // added reserved field of size 2 bytes
33 public static final int REASON_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
36 * total size of object in bytes
38 public static final int TLVS_F_OFFSET = REASON_F_OFFSET + REASON_F_LENGTH;
41 public PCEPObject parse(byte[] bytes, boolean processed, boolean ignored) throws PCEPDeserializerException {
43 throw new IllegalArgumentException("Byte array is mandatory.");
45 if (bytes.length != TLVS_F_OFFSET)
46 throw new PCEPDeserializerException("Size of byte array doesn't match defined size. Expected: " + TLVS_F_OFFSET + "; Passed: " + bytes.length);
49 switch ((short) (bytes[REASON_F_OFFSET] & 0xFF)) {
51 reason = Reason.UNKNOWN;
54 reason = Reason.EXP_DEADTIMER;
57 reason = Reason.MALFORMED_MSG;
60 reason = Reason.TOO_MANY_UNKNOWN_REQ_REP;
63 reason = Reason.TOO_MANY_UNKNOWN_MSG;
66 reason = Reason.UNKNOWN;
70 return new PCEPCloseObject(reason, PCEPTlvParser.parse(ByteArray.cutBytes(bytes, TLVS_F_OFFSET)));
74 public byte[] put(PCEPObject obj) {
75 if (!(obj instanceof PCEPCloseObject))
76 throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + obj.getClass() + ". Needed PCEPCloseObject.");
78 final byte[] tlvs = PCEPTlvParser.put(((PCEPCloseObject) obj).getTlvs());
79 final byte[] retBytes = new byte[TLVS_F_OFFSET + tlvs.length];
80 ByteArray.copyWhole(tlvs, retBytes, TLVS_F_OFFSET);
83 switch (((PCEPCloseObject) obj).getReason()) {
93 case TOO_MANY_UNKNOWN_REQ_REP:
96 case TOO_MANY_UNKNOWN_MSG:
104 retBytes[REASON_F_OFFSET] = (byte) reason;