Bug-2226: RFC5886 - New objects parsers/seriazers
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPProcTimeObjectParser.java
1 /*
2  * Copyright (c) 2014 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
9 package org.opendaylight.protocol.pcep.impl.object;
10
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.INT_BYTES_LENGTH;
12
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;
27
28 /**
29  * Parser for {@link ProcTime}
30  * @see https://tools.ietf.org/html/rfc5886#section-4.4
31  */
32 public class PCEPProcTimeObjectParser implements ObjectParser, ObjectSerializer {
33
34     public static final int CLASS = 26;
35
36     public static final int TYPE = 1;
37
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;
42
43     @Override
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);
58     }
59
60     @Override
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();
73     }
74
75 }