BUG-50 : added tests for simple PCEP objects.
[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.ClassType;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ClasstypeObject;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.lsp.attributes.ClassTypeBuilder;
20
21 /**
22  * Parser for {@link org.opendaylight.protocol.pcep.object.PCEPClassTypeObject PCEPClassTypeObject}
23  */
24 public class PCEPClassTypeObjectParser extends AbstractObjectWithTlvsParser<ClassTypeBuilder> {
25
26         public static final int CLASS = 22;
27
28         public static final int TYPE = 1;
29
30         /**
31          * Length of Class Type field in bits.
32          */
33         public static final int CT_F_LENGTH = 3;
34
35         /**
36          * Reserved field bit length.
37          */
38         public static final int RESERVED = 29;
39
40         /**
41          * Size of the object in bytes.
42          */
43         public static final int SIZE = (RESERVED + CT_F_LENGTH) / 8;
44
45         public PCEPClassTypeObjectParser(final TlvHandlerRegistry tlvReg) {
46                 super(tlvReg);
47         }
48
49         @Override
50         public ClasstypeObject parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException,
51         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
63                 final ClassTypeBuilder builder = new ClassTypeBuilder();
64
65                 builder.setIgnore(header.isIgnore());
66                 builder.setProcessingRule(header.isProcessingRule());
67
68                 final short ct = (short) (bytes[SIZE - 1] & 0xFF);
69                 builder.setClassType(new ClassType(ct));
70
71                 if (ct < 0 || ct > 8) {
72                         throw new PCEPDocumentedException("Invalid class type " + ct, PCEPErrors.INVALID_CT);
73                 }
74                 return builder.build();
75         }
76
77         @Override
78         public void addTlv(final ClassTypeBuilder builder, final Tlv tlv) {
79                 // No tlvs defined
80         }
81
82         @Override
83         public byte[] serializeObject(final Object object) {
84                 if (!(object instanceof ClasstypeObject)) {
85                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed ClasstypeObject.");
86                 }
87
88                 final byte[] retBytes = new byte[SIZE];
89                 retBytes[SIZE - 1] = ((ClasstypeObject) object).getClassType().getValue().byteValue();
90                 return retBytes;
91         }
92
93         @Override
94         public int getObjectType() {
95                 return TYPE;
96         }
97
98         @Override
99         public int getObjectClass() {
100                 return CLASS;
101         }
102 }