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