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