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;
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.Unpooled;
15 import org.opendaylight.protocol.pcep.spi.CommonObjectParser;
16 import org.opendaylight.protocol.pcep.spi.ObjectSerializer;
17 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
18 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
19 import org.opendaylight.protocol.util.BitArray;
20 import org.opendaylight.protocol.util.ByteArray;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ieee754.rev130819.Float32;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.Object;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.ObjectHeader;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.metric.object.Metric;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.metric.object.MetricBuilder;
26 import org.opendaylight.yangtools.yang.common.netty.ByteBufUtils;
29 * Parser for {@link Metric}.
31 public final class PCEPMetricObjectParser extends CommonObjectParser implements ObjectSerializer {
32 private static final int CLASS = 6;
33 private static final int TYPE = 1;
36 * lengths of fields in bytes
38 private static final int FLAGS_SIZE = 8;
39 private static final int METRIC_VALUE_F_LENGTH = 4;
41 * offsets of fields in bytes
43 private static final int RESERVED = 2;
45 * flags offsets inside flags field in bits
47 private static final int C_FLAG_OFFSET = 6;
48 private static final int B_FLAG_OFFSET = 7;
50 private static final int SIZE = METRIC_VALUE_F_LENGTH + METRIC_VALUE_F_LENGTH;
52 public PCEPMetricObjectParser() {
57 public Metric parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
58 checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Cannot be null or empty.");
59 if (bytes.readableBytes() != SIZE) {
60 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.readableBytes()
61 + "; Expected: " + SIZE + ".");
63 bytes.skipBytes(RESERVED);
64 final BitArray flags = BitArray.valueOf(bytes.readByte());
65 return new MetricBuilder()
66 .setIgnore(header.isIgnore())
67 .setProcessingRule(header.isProcessingRule())
68 .setBound(flags.get(B_FLAG_OFFSET))
69 .setComputed(flags.get(C_FLAG_OFFSET))
70 .setMetricType(ByteBufUtils.readUint8(bytes))
71 .setValue(new Float32(ByteArray.readBytes(bytes, METRIC_VALUE_F_LENGTH)))
76 public void serializeObject(final Object object, final ByteBuf buffer) {
77 checkArgument(object instanceof Metric, "Wrong instance of PCEPObject. Passed %s. Needed MetricObject.",
79 final Metric mObj = (Metric) object;
80 final ByteBuf body = Unpooled.buffer(SIZE);
81 body.writeZero(RESERVED);
82 final BitArray flags = new BitArray(FLAGS_SIZE);
83 flags.set(C_FLAG_OFFSET, mObj.isComputed());
84 flags.set(B_FLAG_OFFSET, mObj.isBound());
85 flags.toByteBuf(body);
86 ByteBufUtils.writeMandatory(body, mObj.getMetricType(), "MetricType");
87 writeFloat32(mObj.getValue(), body);
88 ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), body, buffer);