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 com.google.common.base.Preconditions;
11 import com.google.common.primitives.UnsignedBytes;
13 import io.netty.buffer.ByteBuf;
15 import java.util.BitSet;
17 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
18 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
19 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
20 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
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 extends AbstractObjectWithTlvsParser<MetricBuilder> {
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_F_LENGTH = 1;
41 private static final int TYPE_F_LENGTH = 1;
42 private static final int METRIC_VALUE_F_LENGTH = 4;
45 * offsets of fields in bytes
47 private static final int FLAGS_F_OFFSET = 2;
48 private static final int TYPE_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
49 private static final int METRIC_VALUE_F_OFFSET = TYPE_F_OFFSET + TYPE_F_LENGTH;
52 * flags offsets inside flags field in bits
54 private static final int C_FLAG_OFFSET = 6;
55 private static final int B_FLAG_OFFSET = 7;
57 private static final int SIZE = METRIC_VALUE_F_OFFSET + METRIC_VALUE_F_LENGTH;
59 public PCEPMetricObjectParser(final TlvRegistry tlvReg) {
64 public Metric parseObject(final ObjectHeader header, final ByteBuf bytes) throws PCEPDeserializerException {
65 Preconditions.checkArgument(bytes != null && bytes.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
66 if (bytes.readableBytes() != SIZE) {
67 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.readableBytes() + "; Expected: " + SIZE
70 bytes.readerIndex(bytes.readerIndex() + FLAGS_F_OFFSET);
71 final byte[] flagBytes = { bytes.readByte() };
72 final BitSet flags = ByteArray.bytesToBitSet(flagBytes);
73 final MetricBuilder builder = new MetricBuilder();
74 builder.setIgnore(header.isIgnore());
75 builder.setProcessingRule(header.isProcessingRule());
76 builder.setBound(flags.get(B_FLAG_OFFSET));
77 builder.setComputed(flags.get(C_FLAG_OFFSET));
78 builder.setMetricType((short) UnsignedBytes.toInt(bytes.readByte()));
79 builder.setValue(new Float32(ByteArray.readBytes(bytes, METRIC_VALUE_F_LENGTH)));
80 return builder.build();
84 public byte[] serializeObject(final Object object) {
85 if (!(object instanceof Metric)) {
86 throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed MetricObject.");
88 final Metric mObj = (Metric) object;
89 final byte[] retBytes = new byte[SIZE];
90 final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
91 flags.set(C_FLAG_OFFSET, mObj.isComputed());
92 flags.set(B_FLAG_OFFSET, mObj.isBound());
93 ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH), retBytes, FLAGS_F_OFFSET);
94 retBytes[TYPE_F_OFFSET] = UnsignedBytes.checkedCast(mObj.getMetricType());
95 System.arraycopy(mObj.getValue().getValue(), 0, retBytes, METRIC_VALUE_F_OFFSET, METRIC_VALUE_F_LENGTH);
96 return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);