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
8 package org.opendaylight.protocol.pcep.parser.object;
10 import static org.opendaylight.protocol.util.ByteBufWriteUtil.INT_BYTES_LENGTH;
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.util.BitArray;
20 import org.opendaylight.protocol.util.ByteBufWriteUtil;
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.proc.time.object.ProcTime;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.proc.time.object.ProcTimeBuilder;
27 * Parser for {@link ProcTime}
28 * @see <a href="https://tools.ietf.org/html/rfc5886#section-4.4">PROC-TIME Object</a>
30 public class PCEPProcTimeObjectParser implements ObjectParser, ObjectSerializer {
32 public static final int CLASS = 26;
34 public static final int TYPE = 1;
36 private static final int RESERVED = 2;
37 private static final int FLAGS = 16;
38 private static final int COUNT_FIELDS = 5;
39 private static final int BODY_SIZE = RESERVED + FLAGS + COUNT_FIELDS * INT_BYTES_LENGTH;
40 private static final int E_FLAG_POSITION = 15;
43 public void serializeObject(final Object object, final ByteBuf buffer) {
44 Preconditions.checkArgument(object instanceof ProcTime, "Wrong instance of PCEPObject. Passed %s. Needed ProcTimeObject.", object.getClass());
45 final ProcTime procTime = (ProcTime) object;
46 final ByteBuf body = Unpooled.buffer(BODY_SIZE);
47 body.writeZero(RESERVED);
48 final BitArray flagBits = new BitArray(FLAGS);
49 flagBits.set(E_FLAG_POSITION, procTime.isEstimated());
50 flagBits.toByteBuf(body);
51 ByteBufWriteUtil.writeUnsignedInt(procTime.getCurrentProcTime(), body);
52 ByteBufWriteUtil.writeUnsignedInt(procTime.getMinProcTime(), body);
53 ByteBufWriteUtil.writeUnsignedInt(procTime.getMaxProcTime(), body);
54 ByteBufWriteUtil.writeUnsignedInt(procTime.getAverageProcTime(), body);
55 ByteBufWriteUtil.writeUnsignedInt(procTime.getVarianceProcTime(), body);
56 ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
60 public Object parseObject(final ObjectHeader header, final ByteBuf buffer) throws PCEPDeserializerException {
61 Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
62 final ProcTimeBuilder builder = new ProcTimeBuilder();
63 buffer.skipBytes(RESERVED);
64 final BitArray flagBits = BitArray.valueOf(buffer, FLAGS);
65 builder.setEstimated(flagBits.get(E_FLAG_POSITION));
66 builder.setCurrentProcTime(buffer.readUnsignedInt());
67 builder.setMinProcTime(buffer.readUnsignedInt());
68 builder.setMaxProcTime(buffer.readUnsignedInt());
69 builder.setAverageProcTime(buffer.readUnsignedInt());
70 builder.setVarianceProcTime(buffer.readUnsignedInt());
71 return builder.build();