2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.pcep.parser.object;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
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.opendaylight.yangtools.yang.common.Uint8;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * Parser for {@link ClassType}.
32 public final class PCEPClassTypeObjectParser extends CommonObjectParser implements ObjectSerializer {
34 private static final Logger LOG = LoggerFactory.getLogger(PCEPClassTypeObjectParser.class);
35 private static final int CLASS = 22;
36 private static final int TYPE = 1;
39 * Length of Class Type field in bits.
41 private static final int CT_F_LENGTH = 3;
44 * Reserved field bit length.
46 private static final int RESERVED = 29;
49 * Size of the object in bytes.
51 private static final int SIZE = (RESERVED + CT_F_LENGTH) / Byte.SIZE;
53 public PCEPClassTypeObjectParser() {
58 public Object parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
59 checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Cannot be null or empty.");
60 if (!header.isProcessingRule()) {
61 LOG.debug("Processed bit not set on CLASS TYPE OBJECT, ignoring it");
64 if (bytes.readableBytes() != SIZE) {
65 throw new PCEPDeserializerException("Size of byte array doesn't match defined size. Expected: " + SIZE
66 + "; Passed: " + bytes.readableBytes());
68 final short ct = (short) bytes.readUnsignedInt();
69 final ClassTypeBuilder builder = new ClassTypeBuilder()
70 .setIgnore(header.isIgnore())
71 .setProcessingRule(header.isProcessingRule())
72 .setClassType(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109
73 .ClassType(Uint8.valueOf(ct)));
75 if (ct < 0 || ct > Byte.SIZE) {
76 LOG.debug("Invalid class type {}", ct);
77 return new UnknownObject(PCEPErrors.INVALID_CT, builder.build());
79 return builder.build();
83 public void serializeObject(final Object object, final ByteBuf buffer) {
84 checkArgument(object instanceof ClassType, "Wrong instance of PCEPObject. Passed %s. Needed ClassTypeObject.",
86 final ByteBuf body = Unpooled.buffer(SIZE);
87 body.writeZero(SIZE - 1);
88 final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109
89 .ClassType classType = ((ClassType) object).getClassType();
90 checkArgument(classType != null, "ClassType is mandatory.");
91 writeUnsignedByte(classType.getValue(), body);
92 ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);