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 java.util.BitSet;
12 import org.opendaylight.protocol.pcep.PCEPDeserializerException;
13 import org.opendaylight.protocol.pcep.PCEPDocumentedException;
14 import org.opendaylight.protocol.pcep.spi.AbstractObjectParser;
15 import org.opendaylight.protocol.pcep.spi.SubobjectHandlerRegistry;
16 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
17 import org.opendaylight.protocol.util.ByteArray;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ieee754.rev130819.Float32;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.MetricObject;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcreq.message.pcreq.message.svec.Metric;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcreq.message.pcreq.message.svec.MetricBuilder;
27 * Parser for {@link org.opendaylight.protocol.pcep.object.PCEPMetricObject PCEPMetricObject}
29 public class PCEPMetricObjectParser extends AbstractObjectParser<MetricBuilder> {
31 public static final int CLASS = 6;
33 public static final int TYPE = 1;
36 * lengths of fields in bytes
38 private static final int FLAGS_F_LENGTH = 1;
39 private static final int TYPE_F_LENGTH = 1;
40 private static final int METRIC_VALUE_F_LENGTH = 4;
43 * offsets of fields in bytes
45 public static final int FLAGS_F_OFFSET = 2;
46 public static final int TYPE_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
47 public static final int METRIC_VALUE_F_OFFSET = TYPE_F_OFFSET + TYPE_F_LENGTH;
50 * flags offsets inside flags field in bits
52 private static final int C_FLAG_OFFSET = 6;
53 private static final int B_FLAG_OFFSET = 7;
55 public static final int SIZE = METRIC_VALUE_F_OFFSET + METRIC_VALUE_F_LENGTH;
57 public PCEPMetricObjectParser(final SubobjectHandlerRegistry subobjReg, final TlvHandlerRegistry tlvReg) {
58 super(subobjReg, tlvReg);
62 public MetricObject parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException,
63 PCEPDocumentedException {
64 if (bytes == null || bytes.length == 0) {
65 throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
67 if (bytes.length != SIZE) {
68 throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.length + "; Expected: " + SIZE + ".");
70 final byte[] flagBytes = { bytes[FLAGS_F_OFFSET] };
71 final BitSet flags = ByteArray.bytesToBitSet(flagBytes);
73 final MetricBuilder builder = new MetricBuilder();
75 builder.setIgnore(header.isIgnore());
76 builder.setProcessingRule(header.isProcessingRule());
78 builder.setBound(flags.get(B_FLAG_OFFSET));
79 builder.setComputed(flags.get(C_FLAG_OFFSET));
80 builder.setMetricType((short) (bytes[TYPE_F_OFFSET] & 0xFF));
81 builder.setValue(new Float32(ByteArray.subByte(bytes, METRIC_VALUE_F_OFFSET, METRIC_VALUE_F_LENGTH)));
83 return builder.build();
87 public void addTlv(final MetricBuilder builder, final Tlv tlv) {
92 public byte[] serializeObject(final Object object) {
93 if (!(object instanceof MetricObject)) {
94 throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed MetricObject.");
97 final MetricObject mObj = (MetricObject) object;
99 final byte[] retBytes = new byte[SIZE];
100 final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
101 flags.set(C_FLAG_OFFSET, ((Metric) mObj).isComputed());
102 flags.set(B_FLAG_OFFSET, mObj.isBound());
104 ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH), retBytes, FLAGS_F_OFFSET);
106 System.arraycopy(mObj.getValue().getValue(), 0, retBytes, METRIC_VALUE_F_OFFSET, METRIC_VALUE_F_LENGTH);
112 public int getObjectType() {
117 public int getObjectClass() {