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.PCEPErrors;
13 import org.opendaylight.protocol.pcep.PCEPObject;
14 import org.opendaylight.protocol.pcep.impl.PCEPObjectParser;
15 import org.opendaylight.protocol.pcep.object.PCEPClassTypeObject;
16 import org.opendaylight.protocol.util.ByteArray;
19 * Parser for {@link org.opendaylight.protocol.pcep.object.PCEPClassTypeObject PCEPClassTypeObject}
21 public class PCEPClassTypeObjectParser implements PCEPObjectParser {
24 * Length of Class Type field in bits.
26 public static final int CT_F_LENGTH = 3;
29 * Reserved field bit length.
31 public static final int RESERVED = 29;
34 * Size of the object in bytes.
36 public static final int SIZE = (RESERVED + CT_F_LENGTH) / 8;
39 public PCEPObject parse(byte[] bytes, boolean processed, boolean ignored)
40 throws PCEPDeserializerException, PCEPDocumentedException {
42 throw new IllegalArgumentException("Byte array is mandatory.");
43 if (bytes.length != SIZE)
44 throw new PCEPDeserializerException("Size of byte array doesn't match defined size. Expected: " + SIZE + "; Passed: " + bytes.length);
46 throw new PCEPDocumentedException("Processed bit not set", PCEPErrors.P_FLAG_NOT_SET);
47 final short classType = (short) (bytes[SIZE-1] & 0xFF);
48 if (classType < 0 || classType > 8) {
49 throw new PCEPDocumentedException("Invalid class type " + classType, PCEPErrors.INVALID_CT);
51 return new PCEPClassTypeObject(classType);
55 public byte[] put(PCEPObject obj) {
56 if (!(obj instanceof PCEPClassTypeObject))
57 throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + obj.getClass() + ". Needed PCEPClassTypeObject.");
59 final byte[] retBytes = new byte[SIZE];
60 retBytes[SIZE-1] = ByteArray.shortToBytes(((PCEPClassTypeObject) obj).getClassType())[1];