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