BUG-47 : switched subobjects to generated source code.
[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.impl.message.AbstractObjectWithTlvsParser;
14 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ClassType;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ClasstypeObject;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.attributes.ClassTypeBuilder;
21
22 /**
23  * Parser for {@link org.opendaylight.protocol.pcep.object.PCEPClassTypeObject PCEPClassTypeObject}
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         public static final int CT_F_LENGTH = 3;
35
36         /**
37          * Reserved field bit length.
38          */
39         public static final int RESERVED = 29;
40
41         /**
42          * Size of the object in bytes.
43          */
44         public 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 ClasstypeObject parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException,
52         PCEPDocumentedException {
53                 if (bytes == null) {
54                         throw new IllegalArgumentException("Byte array is mandatory.");
55                 }
56                 if (bytes.length != SIZE) {
57                         throw new PCEPDeserializerException("Size of byte array doesn't match defined size. Expected: " + SIZE + "; Passed: "
58                                         + bytes.length);
59                 }
60                 if (!header.isProcessingRule()) {
61                         throw new PCEPDocumentedException("Processed bit not set", PCEPErrors.P_FLAG_NOT_SET);
62                 }
63
64                 final ClassTypeBuilder builder = new ClassTypeBuilder();
65
66                 builder.setIgnore(header.isIgnore());
67                 builder.setProcessingRule(header.isProcessingRule());
68
69                 final short ct = (short) (bytes[SIZE - 1] & 0xFF);
70                 builder.setClassType(new ClassType(ct));
71
72                 if (ct < 0 || ct > 8) {
73                         throw new PCEPDocumentedException("Invalid class type " + ct, PCEPErrors.INVALID_CT);
74                 }
75                 return builder.build();
76         }
77
78         @Override
79         public void addTlv(final ClassTypeBuilder builder, final Tlv tlv) {
80                 // No tlvs defined
81         }
82
83         @Override
84         public byte[] serializeObject(final Object object) {
85                 if (!(object instanceof ClasstypeObject)) {
86                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed ClasstypeObject.");
87                 }
88
89                 final byte[] retBytes = new byte[SIZE];
90                 retBytes[SIZE - 1] = ((ClasstypeObject) object).getClassType().getValue().byteValue();
91                 return retBytes;
92         }
93
94         @Override
95         public int getObjectType() {
96                 return TYPE;
97         }
98
99         @Override
100         public int getObjectClass() {
101                 return CLASS;
102         }
103 }