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.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.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;
29 * Parser for {@link Metric}
31 public class PCEPMetricObjectParser implements ObjectParser, ObjectSerializer {
33 public static final int CLASS = 6;
35 public static final int TYPE = 1;
38 * lengths of fields in bytes
40 private static final int FLAGS_SIZE = 8;
41 private static final int METRIC_VALUE_F_LENGTH = 4;
44 * offsets of fields in bytes
46 private static final int RESERVED = 2;
49 * flags offsets inside flags field in bits
51 private static final int C_FLAG_OFFSET = 6;
52 private static final int B_FLAG_OFFSET = 7;
54 private static final int SIZE = METRIC_VALUE_F_LENGTH + METRIC_VALUE_F_LENGTH;
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
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();
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);