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 org.opendaylight.protocol.util.ByteBufWriteUtil.writeFloat32;
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
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;
29 * Parser for {@link Metric}
31 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 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
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();
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);