03284cbd6b76c82d253a87ca9ea6f7566e1ffd79
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / object / PCEPMetricObjectParser.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.pcep.impl.object;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.primitives.UnsignedBytes;
12
13 import io.netty.buffer.ByteBuf;
14
15 import java.util.BitSet;
16
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;
27
28 /**
29  * Parser for {@link Metric}
30  */
31 public class PCEPMetricObjectParser extends AbstractObjectWithTlvsParser<MetricBuilder> {
32
33     public static final int CLASS = 6;
34
35     public static final int TYPE = 1;
36
37     /*
38      * lengths of fields in bytes
39      */
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;
43
44     /*
45      * offsets of fields in bytes
46      */
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;
50
51     /*
52      * flags offsets inside flags field in bits
53      */
54     private static final int C_FLAG_OFFSET = 6;
55     private static final int B_FLAG_OFFSET = 7;
56
57     private static final int SIZE = METRIC_VALUE_F_OFFSET + METRIC_VALUE_F_LENGTH;
58
59     public PCEPMetricObjectParser(final TlvRegistry tlvReg) {
60         super(tlvReg);
61     }
62
63     @Override
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
68                     + ".");
69         }
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();
81     }
82
83     @Override
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.");
87         }
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);
97     }
98 }