d880153ae8347bdc237e64cf54c096a570c12032
[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 com.google.common.base.Preconditions.checkArgument;
11
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import org.opendaylight.protocol.pcep.spi.CommonObjectParser;
15 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
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.UnknownObject;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.classtype.object.ClassType;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.classtype.object.ClassTypeBuilder;
24 import org.opendaylight.yangtools.yang.common.Uint8;
25 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * Parser for {@link ClassType}.
31  */
32 public final class PCEPClassTypeObjectParser extends CommonObjectParser implements ObjectSerializer {
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      * Reserved field bit length.
43      */
44     private static final int RESERVED = 29;
45     /**
46      * Size of the object in bytes.
47      */
48     private static final int SIZE = (RESERVED + CT_F_LENGTH) / Byte.SIZE;
49
50     public PCEPClassTypeObjectParser() {
51         super(CLASS, TYPE);
52     }
53
54     @Override
55     public Object parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
56         checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Cannot be null or empty.");
57         if (!header.isProcessingRule()) {
58             LOG.debug("Processed bit not set on CLASS TYPE OBJECT, ignoring it");
59             return null;
60         }
61         if (bytes.readableBytes() != SIZE) {
62             throw new PCEPDeserializerException("Size of byte array doesn't match defined size. Expected: " + SIZE
63                 + "; Passed: " + bytes.readableBytes());
64         }
65         final short ct = (short) bytes.readUnsignedInt();
66         final ClassTypeBuilder builder = new ClassTypeBuilder()
67                 .setIgnore(header.isIgnore())
68                 .setProcessingRule(header.isProcessingRule())
69                 .setClassType(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109
70                         .ClassType(Uint8.valueOf(ct)));
71
72         if (ct < 0 || ct > Byte.SIZE) {
73             LOG.debug("Invalid class type {}", ct);
74             return new UnknownObject(PCEPErrors.INVALID_CT, builder.build());
75         }
76         return builder.build();
77     }
78
79     @Override
80     public void serializeObject(final Object object, final ByteBuf buffer) {
81         checkArgument(object instanceof ClassType, "Wrong instance of PCEPObject. Passed %s. Needed ClassTypeObject.",
82             object.getClass());
83         final ByteBuf body = Unpooled.buffer(SIZE);
84         body.writeZero(SIZE - 1);
85         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109
86             .ClassType classType = ((ClassType) object).getClassType();
87         checkArgument(classType != null, "ClassType is mandatory.");
88         ByteBufUtils.write(body, classType.getValue());
89         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
90     }
91 }