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.impl.object;
10 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
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.ObjectParser;
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.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;
29 * Parser for {@link ClassType}
31 public class PCEPClassTypeObjectParser implements ObjectParser, ObjectSerializer {
33 private static final Logger LOG = LoggerFactory.getLogger(PCEPClassTypeObjectParser.class);
35 public static final int CLASS = 22;
37 public static final int TYPE = 1;
40 * Length of Class Type field in bits.
42 private static final int CT_F_LENGTH = 3;
45 * Reserved field bit length.
47 private static final int RESERVED = 29;
50 * Size of the object in bytes.
52 private static final int SIZE = (RESERVED + CT_F_LENGTH) / Byte.SIZE;
55 public Object parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
56 Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
57 if (!header.isProcessingRule()) {
58 LOG.debug("Processed bit not set on CLASS TYPE OBJECT, ignoring it");
61 if (bytes.readableBytes() != SIZE) {
62 throw new PCEPDeserializerException("Size of byte array doesn't match defined size. Expected: " + SIZE + "; Passed: "
63 + bytes.readableBytes());
65 final ClassTypeBuilder builder = new ClassTypeBuilder();
66 builder.setIgnore(header.isIgnore());
67 builder.setProcessingRule(header.isProcessingRule());
69 final short ct = (short) bytes.readUnsignedInt();
70 builder.setClassType(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ClassType(ct));
72 final Object obj = builder.build();
73 if (ct < 0 || ct > Byte.SIZE) {
74 LOG.debug("Invalid class type {}", ct);
75 return new UnknownObject(PCEPErrors.INVALID_CT, obj);
81 public void serializeObject(final Object object, final ByteBuf buffer) {
82 Preconditions.checkArgument(object instanceof ClassType, "Wrong instance of PCEPObject. Passed %s. Needed ClassTypeObject.", 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.rev131005.ClassType classType = ((ClassType) object).getClassType();
86 Preconditions.checkArgument(classType != null, "ClassType is mandatory.");
87 writeUnsignedByte(classType.getValue(), body);
88 ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);