e8da9c76e0e12b70d00c2a4d561261dd5e814db4
[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
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import org.opendaylight.protocol.pcep.spi.CommonObjectParser;
15 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
16 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
17 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
18 import org.opendaylight.protocol.util.BitArray;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.proc.time.object.ProcTime;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.proc.time.object.ProcTimeBuilder;
23 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
24
25 /**
26  * Parser for {@link ProcTime}.
27  * @see <a href="https://tools.ietf.org/html/rfc5886#section-4.4">PROC-TIME Object</a>
28  */
29 public class PCEPProcTimeObjectParser extends CommonObjectParser implements ObjectSerializer {
30     private static final int CLASS = 26;
31     private static final int TYPE = 1;
32     private static final int RESERVED = 2;
33     private static final int FLAGS = 16;
34     private static final int COUNT_FIELDS = 5;
35     private static final int BODY_SIZE = RESERVED + FLAGS + COUNT_FIELDS * Integer.BYTES;
36     private static final int E_FLAG_POSITION = 15;
37
38     public PCEPProcTimeObjectParser() {
39         super(CLASS, TYPE);
40     }
41
42     @Override
43     public void serializeObject(final Object object, final ByteBuf buffer) {
44         checkArgument(object instanceof ProcTime,
45             "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 BitArray flagBits = new BitArray(FLAGS);
50         flagBits.set(E_FLAG_POSITION, procTime.getEstimated());
51         flagBits.toByteBuf(body);
52         ByteBufUtils.writeOrZero(body, procTime.getCurrentProcTime());
53         ByteBufUtils.writeOrZero(body, procTime.getMinProcTime());
54         ByteBufUtils.writeOrZero(body, procTime.getMaxProcTime());
55         ByteBufUtils.writeOrZero(body, procTime.getAverageProcTime());
56         ByteBufUtils.writeOrZero(body, procTime.getVarianceProcTime());
57         ObjectUtil.formatSubobject(TYPE, CLASS, object.getProcessingRule(), object.getIgnore(), body, buffer);
58     }
59
60     @Override
61     public Object parseObject(final ObjectHeader header, final ByteBuf buffer) throws PCEPDeserializerException {
62         checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
63         buffer.skipBytes(RESERVED);
64         final BitArray flagBits = BitArray.valueOf(buffer, FLAGS);
65
66         return new ProcTimeBuilder()
67                 .setEstimated(flagBits.get(E_FLAG_POSITION))
68                 .setCurrentProcTime(ByteBufUtils.readUint32(buffer))
69                 .setMinProcTime(ByteBufUtils.readUint32(buffer))
70                 .setMaxProcTime(ByteBufUtils.readUint32(buffer))
71                 .setAverageProcTime(ByteBufUtils.readUint32(buffer))
72                 .setVarianceProcTime(ByteBufUtils.readUint32(buffer))
73                 .build();
74     }
75 }