Clean pcep/base-parser code
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / 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 package org.opendaylight.protocol.pcep.parser.object;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.INT_BYTES_LENGTH;
12
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import org.opendaylight.protocol.pcep.spi.CommonObjectParser;
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.ByteBufUtils;
21 import org.opendaylight.protocol.util.ByteBufWriteUtil;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.proc.time.object.ProcTime;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.proc.time.object.ProcTimeBuilder;
26
27 /**
28  * Parser for {@link ProcTime}.
29  * @see <a href="https://tools.ietf.org/html/rfc5886#section-4.4">PROC-TIME Object</a>
30  */
31 public class PCEPProcTimeObjectParser extends CommonObjectParser implements ObjectSerializer {
32
33     private static final int CLASS = 26;
34     private static final int TYPE = 1;
35     private static final int RESERVED = 2;
36     private static final int FLAGS = 16;
37     private static final int COUNT_FIELDS = 5;
38     private static final int BODY_SIZE = RESERVED + FLAGS + COUNT_FIELDS * INT_BYTES_LENGTH;
39     private static final int E_FLAG_POSITION = 15;
40
41     public PCEPProcTimeObjectParser() {
42         super(CLASS, TYPE);
43     }
44
45     @Override
46     public void serializeObject(final Object object, final ByteBuf buffer) {
47         checkArgument(object instanceof ProcTime,
48             "Wrong instance of PCEPObject. Passed %s. Needed ProcTimeObject.", object.getClass());
49         final ProcTime procTime = (ProcTime) object;
50         final ByteBuf body = Unpooled.buffer(BODY_SIZE);
51         body.writeZero(RESERVED);
52         final BitArray flagBits = new BitArray(FLAGS);
53         flagBits.set(E_FLAG_POSITION, procTime.isEstimated());
54         flagBits.toByteBuf(body);
55         ByteBufWriteUtil.writeUnsignedInt(procTime.getCurrentProcTime(), body);
56         ByteBufWriteUtil.writeUnsignedInt(procTime.getMinProcTime(), body);
57         ByteBufWriteUtil.writeUnsignedInt(procTime.getMaxProcTime(), body);
58         ByteBufWriteUtil.writeUnsignedInt(procTime.getAverageProcTime(), body);
59         ByteBufWriteUtil.writeUnsignedInt(procTime.getVarianceProcTime(), body);
60         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
61     }
62
63     @Override
64     public Object parseObject(final ObjectHeader header, final ByteBuf buffer) throws PCEPDeserializerException {
65         checkArgument(buffer != null && buffer.isReadable(),
66             "Array of bytes is mandatory. Can't be null or empty.");
67         buffer.skipBytes(RESERVED);
68         final BitArray flagBits = BitArray.valueOf(buffer, FLAGS);
69
70         return new ProcTimeBuilder()
71                 .setEstimated(flagBits.get(E_FLAG_POSITION))
72                 .setCurrentProcTime(ByteBufUtils.readUint32(buffer))
73                 .setMinProcTime(ByteBufUtils.readUint32(buffer))
74                 .setMaxProcTime(ByteBufUtils.readUint32(buffer))
75                 .setAverageProcTime(ByteBufUtils.readUint32(buffer))
76                 .setVarianceProcTime(ByteBufUtils.readUint32(buffer))
77                 .build();
78     }
79 }