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