2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.pcep.parser.object;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeFloat32;
12 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
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 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
30 * Parser for {@link Metric}.
32 public final class PCEPMetricObjectParser extends CommonObjectParser implements ObjectSerializer {
33 private static final int CLASS = 6;
34 private static final int TYPE = 1;
37 * lengths of fields in bytes
39 private static final int FLAGS_SIZE = 8;
40 private static final int METRIC_VALUE_F_LENGTH = 4;
43 * offsets of fields in bytes
45 private static final int RESERVED = 2;
48 * flags offsets inside flags field in bits
50 private static final int C_FLAG_OFFSET = 6;
51 private static final int B_FLAG_OFFSET = 7;
53 private static final int SIZE = METRIC_VALUE_F_LENGTH + METRIC_VALUE_F_LENGTH;
55 public PCEPMetricObjectParser() {
60 public Metric parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
61 checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Cannot be null or empty.");
62 if (bytes.readableBytes() != SIZE) {
63 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.readableBytes()
64 + "; Expected: " + SIZE + ".");
66 bytes.skipBytes(RESERVED);
67 final BitArray flags = BitArray.valueOf(bytes.readByte());
68 final MetricBuilder builder = new MetricBuilder()
69 .setIgnore(header.isIgnore())
70 .setProcessingRule(header.isProcessingRule())
71 .setBound(flags.get(B_FLAG_OFFSET))
72 .setComputed(flags.get(C_FLAG_OFFSET))
73 .setMetricType(ByteBufUtils.readUint8(bytes))
74 .setValue(new Float32(ByteArray.readBytes(bytes, METRIC_VALUE_F_LENGTH)));
75 return builder.build();
79 public void serializeObject(final Object object, final ByteBuf buffer) {
80 checkArgument(object instanceof Metric, "Wrong instance of PCEPObject. Passed %s. Needed MetricObject.",
82 final Metric mObj = (Metric) object;
83 final ByteBuf body = Unpooled.buffer(SIZE);
84 body.writeZero(RESERVED);
85 final BitArray flags = new BitArray(FLAGS_SIZE);
86 flags.set(C_FLAG_OFFSET, mObj.isComputed());
87 flags.set(B_FLAG_OFFSET, mObj.isBound());
88 flags.toByteBuf(body);
89 checkArgument(mObj.getMetricType() != null, "MetricType is mandatory.");
90 writeUnsignedByte(mObj.getMetricType(), body);
91 writeFloat32(mObj.getValue(), body);
92 ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);