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.impl.object;
10 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeBitSet;
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeFloat32;
12 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedByte;
14 import com.google.common.base.Preconditions;
15 import com.google.common.primitives.UnsignedBytes;
16 import io.netty.buffer.ByteBuf;
17 import io.netty.buffer.Unpooled;
18 import java.util.BitSet;
19 import org.opendaylight.protocol.pcep.spi.ObjectParser;
20 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
21 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
22 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
23 import org.opendaylight.protocol.util.ByteArray;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ieee754.rev130819.Float32;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.metric.object.Metric;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.metric.object.MetricBuilder;
31 * Parser for {@link Metric}
33 public class PCEPMetricObjectParser implements ObjectParser, ObjectSerializer {
35 public static final int CLASS = 6;
37 public static final int TYPE = 1;
40 * lengths of fields in bytes
42 private static final int FLAGS_F_LENGTH = 1;
43 private static final int METRIC_VALUE_F_LENGTH = 4;
46 * offsets of fields in bytes
48 private static final int RESERVED = 2;
51 * flags offsets inside flags field in bits
53 private static final int C_FLAG_OFFSET = 6;
54 private static final int B_FLAG_OFFSET = 7;
56 private static final int SIZE = 4 + METRIC_VALUE_F_LENGTH;
59 public Metric parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
60 Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
61 if (bytes.readableBytes() != SIZE) {
62 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.readableBytes() + "; Expected: " + SIZE
65 bytes.readerIndex(bytes.readerIndex() + RESERVED);
66 final byte[] flagBytes = { bytes.readByte() };
67 final BitSet flags = ByteArray.bytesToBitSet(flagBytes);
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((short) UnsignedBytes.toInt(bytes.readByte()));
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 BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
85 if (mObj.isComputed() != null) {
86 flags.set(C_FLAG_OFFSET, mObj.isComputed());
88 if (mObj.isBound() != null) {
89 flags.set(B_FLAG_OFFSET, mObj.isBound());
91 writeBitSet(flags, FLAGS_F_LENGTH, body);
92 Preconditions.checkArgument(mObj.getMetricType() != null, "MetricType is mandatory.");
93 writeUnsignedByte(mObj.getMetricType(), body);
94 writeFloat32(mObj.getValue(), body);
95 ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);