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