438ad68d467f0c9d3de3398dc62af80194a0dbb1
[bgpcep.git] / pcep / base-parser / src / main / java / org / opendaylight / protocol / pcep / parser / object / PCEPMetricObjectParser.java
1 /*
2  * Copyright (c) 2013 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.writeFloat32;
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.ByteArray;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ieee754.rev130819.Float32;
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.metric.object.Metric;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.metric.object.MetricBuilder;
26 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
27
28 /**
29  * Parser for {@link Metric}.
30  */
31 public final class PCEPMetricObjectParser extends CommonObjectParser implements ObjectSerializer {
32     private static final int CLASS = 6;
33     private static final int TYPE = 1;
34
35     /*
36      * lengths of fields in bytes
37      */
38     private static final int FLAGS_SIZE = 8;
39     private static final int METRIC_VALUE_F_LENGTH = 4;
40     /*
41      * offsets of fields in bytes
42      */
43     private static final int RESERVED = 2;
44     /*
45      * flags offsets inside flags field in bits
46      */
47     private static final int C_FLAG_OFFSET = 6;
48     private static final int B_FLAG_OFFSET = 7;
49
50     private static final int SIZE = METRIC_VALUE_F_LENGTH + METRIC_VALUE_F_LENGTH;
51
52     public PCEPMetricObjectParser() {
53         super(CLASS, TYPE);
54     }
55
56     @Override
57     public Metric parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
58         checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Cannot be null or empty.");
59         if (bytes.readableBytes() != SIZE) {
60             throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.readableBytes()
61                 + "; Expected: " + SIZE + ".");
62         }
63         bytes.skipBytes(RESERVED);
64         final BitArray flags = BitArray.valueOf(bytes.readByte());
65         return new MetricBuilder()
66                 .setIgnore(header.isIgnore())
67                 .setProcessingRule(header.isProcessingRule())
68                 .setBound(flags.get(B_FLAG_OFFSET))
69                 .setComputed(flags.get(C_FLAG_OFFSET))
70                 .setMetricType(ByteBufUtils.readUint8(bytes))
71                 .setValue(new Float32(ByteArray.readBytes(bytes, METRIC_VALUE_F_LENGTH)))
72                 .build();
73     }
74
75     @Override
76     public void serializeObject(final Object object, final ByteBuf buffer) {
77         checkArgument(object instanceof Metric, "Wrong instance of PCEPObject. Passed %s. Needed MetricObject.",
78             object.getClass());
79         final Metric mObj = (Metric) object;
80         final ByteBuf body = Unpooled.buffer(SIZE);
81         body.writeZero(RESERVED);
82         final BitArray flags = new BitArray(FLAGS_SIZE);
83         flags.set(C_FLAG_OFFSET, mObj.isComputed());
84         flags.set(B_FLAG_OFFSET, mObj.isBound());
85         flags.toByteBuf(body);
86         ByteBufUtils.writeMandatory(body, mObj.getMetricType(), "MetricType");
87         writeFloat32(mObj.getValue(), body);
88         ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);
89     }
90 }