2 * Copyright (c) 2014 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
9 package org.opendaylight.protocol.pcep.impl.object;
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.INT_BYTES_LENGTH;
13 import com.google.common.base.Preconditions;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import java.util.BitSet;
17 import org.opendaylight.protocol.pcep.spi.ObjectParser;
18 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
19 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
20 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
21 import org.opendaylight.protocol.util.ByteArray;
22 import org.opendaylight.protocol.util.ByteBufWriteUtil;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.proc.time.object.ProcTime;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.proc.time.object.ProcTimeBuilder;
29 * Parser for {@link ProcTime}
30 * @see https://tools.ietf.org/html/rfc5886#section-4.4
32 public class PCEPProcTimeObjectParser implements ObjectParser, ObjectSerializer {
34 public static final int CLASS = 26;
36 public static final int TYPE = 1;
38 private static final int RESERVED = 2;
39 private static final int FLAGS = RESERVED;
40 private static final int BODY_SIZE = RESERVED + FLAGS + 5 * INT_BYTES_LENGTH;
41 private static final int E_FLAG_POSITION = 15;
44 public void serializeObject(final Object object, final ByteBuf buffer) {
45 Preconditions.checkArgument(object instanceof ProcTime, "Wrong instance of PCEPObject. Passed %s. Needed ProcTimeObject.", object.getClass());
46 final ProcTime procTime = (ProcTime) object;
47 final ByteBuf body = Unpooled.buffer(BODY_SIZE);
48 body.writeZero(RESERVED);
49 final BitSet flagBits = new BitSet(FLAGS * Byte.SIZE);
50 flagBits.set(E_FLAG_POSITION, procTime.isEstimated());
51 ByteBufWriteUtil.writeBitSet(flagBits, FLAGS, body);
52 ByteBufWriteUtil.writeUnsignedInt(procTime.getCurrentProcTime(), body);
53 ByteBufWriteUtil.writeUnsignedInt(procTime.getMinProcTime(), body);
54 ByteBufWriteUtil.writeUnsignedInt(procTime.getMaxProcTime(), body);
55 ByteBufWriteUtil.writeUnsignedInt(procTime.getAverageProcTime(), body);
56 ByteBufWriteUtil.writeUnsignedInt(procTime.getVarianceProcTime(), body);
57 ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
61 public Object parseObject(final ObjectHeader header, final ByteBuf buffer) throws PCEPDeserializerException {
62 Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
63 final ProcTimeBuilder builder = new ProcTimeBuilder();
64 buffer.readBytes(RESERVED);
65 final BitSet flagBits = ByteArray.bytesToBitSet(buffer.readBytes(FLAGS).array());
66 builder.setEstimated(flagBits.get(E_FLAG_POSITION));
67 builder.setCurrentProcTime(buffer.readUnsignedInt());
68 builder.setMinProcTime(buffer.readUnsignedInt());
69 builder.setMaxProcTime(buffer.readUnsignedInt());
70 builder.setAverageProcTime(buffer.readUnsignedInt());
71 builder.setVarianceProcTime(buffer.readUnsignedInt());
72 return builder.build();