Removed some sonar warnings.
[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 COUNT_FIELDS = 5;
41     private static final int BODY_SIZE = RESERVED + FLAGS + COUNT_FIELDS * INT_BYTES_LENGTH;
42     private static final int E_FLAG_POSITION = 15;
43
44     @Override
45     public void serializeObject(final Object object, final ByteBuf buffer) {
46         Preconditions.checkArgument(object instanceof ProcTime, "Wrong instance of PCEPObject. Passed %s. Needed ProcTimeObject.", object.getClass());
47         final ProcTime procTime = (ProcTime) object;
48         final ByteBuf body = Unpooled.buffer(BODY_SIZE);
49         body.writeZero(RESERVED);
50         final BitSet flagBits = new BitSet(FLAGS * Byte.SIZE);
51         flagBits.set(E_FLAG_POSITION, procTime.isEstimated());
52         ByteBufWriteUtil.writeBitSet(flagBits, FLAGS, body);
53         ByteBufWriteUtil.writeUnsignedInt(procTime.getCurrentProcTime(), body);
54         ByteBufWriteUtil.writeUnsignedInt(procTime.getMinProcTime(), body);
55         ByteBufWriteUtil.writeUnsignedInt(procTime.getMaxProcTime(), body);
56         ByteBufWriteUtil.writeUnsignedInt(procTime.getAverageProcTime(), body);
57         ByteBufWriteUtil.writeUnsignedInt(procTime.getVarianceProcTime(), body);
58         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
59     }
60
61     @Override
62     public Object parseObject(final ObjectHeader header, final ByteBuf buffer) throws PCEPDeserializerException {
63         Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
64         final ProcTimeBuilder builder = new ProcTimeBuilder();
65         buffer.readBytes(RESERVED);
66         final BitSet flagBits = ByteArray.bytesToBitSet(buffer.readBytes(FLAGS).array());
67         builder.setEstimated(flagBits.get(E_FLAG_POSITION));
68         builder.setCurrentProcTime(buffer.readUnsignedInt());
69         builder.setMinProcTime(buffer.readUnsignedInt());
70         builder.setMaxProcTime(buffer.readUnsignedInt());
71         builder.setAverageProcTime(buffer.readUnsignedInt());
72         builder.setVarianceProcTime(buffer.readUnsignedInt());
73         return builder.build();
74     }
75
76 }