e1dd4ad8790c867a2fd287078f254935e4bc510a
[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.spi.TlvHandlerRegistry;
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.Tlv;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.classtype.object.ClassType;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.classtype.object.ClassTypeBuilder;
19
20 import com.google.common.primitives.UnsignedBytes;
21
22 /**
23  * Parser for {@link ClassType}
24  */
25 public class PCEPClassTypeObjectParser extends AbstractObjectWithTlvsParser<ClassTypeBuilder> {
26
27         public static final int CLASS = 22;
28
29         public static final int TYPE = 1;
30
31         /**
32          * Length of Class Type field in bits.
33          */
34         private static final int CT_F_LENGTH = 3;
35
36         /**
37          * Reserved field bit length.
38          */
39         private static final int RESERVED = 29;
40
41         /**
42          * Size of the object in bytes.
43          */
44         private static final int SIZE = (RESERVED + CT_F_LENGTH) / 8;
45
46         public PCEPClassTypeObjectParser(final TlvHandlerRegistry tlvReg) {
47                 super(tlvReg);
48         }
49
50         @Override
51         public ClassType parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException, PCEPDocumentedException {
52                 if (bytes == null) {
53                         throw new IllegalArgumentException("Byte array is mandatory.");
54                 }
55                 if (bytes.length != SIZE) {
56                         throw new PCEPDeserializerException("Size of byte array doesn't match defined size. Expected: " + SIZE + "; Passed: "
57                                         + bytes.length);
58                 }
59                 if (!header.isProcessingRule()) {
60                         throw new PCEPDocumentedException("Processed bit not set", PCEPErrors.P_FLAG_NOT_SET);
61                 }
62                 final ClassTypeBuilder builder = new ClassTypeBuilder();
63
64                 builder.setIgnore(header.isIgnore());
65                 builder.setProcessingRule(header.isProcessingRule());
66
67                 final short ct = (short) UnsignedBytes.toInt(bytes[SIZE - 1]);
68                 builder.setClassType(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ClassType(ct));
69
70                 if (ct < 0 || ct > 8) {
71                         throw new PCEPDocumentedException("Invalid class type " + ct, PCEPErrors.INVALID_CT);
72                 }
73                 return builder.build();
74         }
75
76         @Override
77         public void addTlv(final ClassTypeBuilder builder, final Tlv tlv) {
78                 // No tlvs defined
79         }
80
81         @Override
82         public byte[] serializeObject(final Object object) {
83                 if (!(object instanceof ClassType)) {
84                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed ClasstypeObject.");
85                 }
86                 final byte[] retBytes = new byte[SIZE];
87                 retBytes[SIZE - 1] = UnsignedBytes.checkedCast(((ClassType) object).getClassType().getValue());
88                 return retBytes;
89         }
90
91         @Override
92         public int getObjectType() {
93                 return TYPE;
94         }
95
96         @Override
97         public int getObjectClass() {
98                 return CLASS;
99         }
100 }