Revert "BUG-47 : unfinished PCEP migration to generated DTOs."
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPClassTypeObjectParser.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.pcep.impl.object;
9
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;
17
18 /**
19  * Parser for {@link org.opendaylight.protocol.pcep.object.PCEPClassTypeObject PCEPClassTypeObject}
20  */
21 public class PCEPClassTypeObjectParser implements PCEPObjectParser {
22
23         /**
24          * Length of Class Type field in bits.
25          */
26         public static final int CT_F_LENGTH = 3;
27
28         /**
29          * Reserved field bit length.
30          */
31         public static final int RESERVED = 29;
32
33         /**
34          * Size of the object in bytes.
35          */
36         public static final int SIZE = (RESERVED + CT_F_LENGTH) / 8;
37
38         @Override
39         public PCEPObject parse(byte[] bytes, boolean processed, boolean ignored)
40                         throws PCEPDeserializerException, PCEPDocumentedException {
41                 if (bytes == null)
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);
45                 if (!processed)
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);
50                 }
51                 return new PCEPClassTypeObject(classType);
52         }
53
54         @Override
55         public byte[] put(PCEPObject obj) {
56                 if (!(obj instanceof PCEPClassTypeObject))
57                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + obj.getClass() + ". Needed PCEPClassTypeObject.");
58
59                 final byte[] retBytes = new byte[SIZE];
60                 retBytes[SIZE-1] = ByteArray.shortToBytes(((PCEPClassTypeObject) obj).getClassType())[1];
61                 return retBytes;
62         }
63 }