BUG-612 : switched PCEP message serializers to ByteBuf
[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 com.google.common.base.Preconditions;
11
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14
15 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
16 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
17 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
18 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
19 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
20 import org.opendaylight.protocol.pcep.spi.UnknownObject;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.classtype.object.ClassType;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.classtype.object.ClassTypeBuilder;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * Parser for {@link ClassType}
30  */
31 public class PCEPClassTypeObjectParser extends AbstractObjectWithTlvsParser<ClassTypeBuilder> {
32     private static final Logger LOG = LoggerFactory.getLogger(PCEPClassTypeObjectParser.class);
33
34     public static final int CLASS = 22;
35
36     public static final int TYPE = 1;
37
38     /**
39      * Length of Class Type field in bits.
40      */
41     private static final int CT_F_LENGTH = 3;
42
43     /**
44      * Reserved field bit length.
45      */
46     private static final int RESERVED = 29;
47
48     /**
49      * Size of the object in bytes.
50      */
51     private static final int SIZE = (RESERVED + CT_F_LENGTH) / Byte.SIZE;
52
53     public PCEPClassTypeObjectParser(final TlvRegistry tlvReg) {
54         super(tlvReg);
55     }
56
57     @Override
58     public Object parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
59         Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
60         if (!header.isProcessingRule()) {
61             LOG.debug("Processed bit not set on CLASS TYPE OBJECT, ignoring it");
62             return null;
63         }
64         if (bytes.readableBytes() != SIZE) {
65             throw new PCEPDeserializerException("Size of byte array doesn't match defined size. Expected: " + SIZE + "; Passed: "
66                     + bytes.readableBytes());
67         }
68         final ClassTypeBuilder builder = new ClassTypeBuilder();
69         builder.setIgnore(header.isIgnore());
70         builder.setProcessingRule(header.isProcessingRule());
71
72         final short ct = (short) bytes.readUnsignedInt();
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 void serializeObject(final Object object, final ByteBuf buffer) {
85         Preconditions.checkArgument(object instanceof ClassType, "Wrong instance of PCEPObject. Passed %s. Needed ClassTypeObject.", object.getClass());
86         final ByteBuf body = Unpooled.buffer(SIZE);
87         body.writeZero(SIZE -1);
88         body.writeByte(((ClassType) object).getClassType().getValue());
89         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
90     }
91 }