Merge "Initial support for RFC2385"
[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 java.util.BitSet;
11
12 import org.opendaylight.protocol.pcep.spi.AbstractObjectWithTlvsParser;
13 import org.opendaylight.protocol.pcep.spi.ObjectUtil;
14 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
15 import org.opendaylight.protocol.pcep.spi.TlvRegistry;
16 import org.opendaylight.protocol.util.ByteArray;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ieee754.rev130819.Float32;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Object;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.ObjectHeader;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.metric.object.Metric;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.metric.object.MetricBuilder;
22
23 import com.google.common.primitives.UnsignedBytes;
24
25 /**
26  * Parser for {@link Metric}
27  */
28 public class PCEPMetricObjectParser extends AbstractObjectWithTlvsParser<MetricBuilder> {
29
30         public static final int CLASS = 6;
31
32         public static final int TYPE = 1;
33
34         /*
35          * lengths of fields in bytes
36          */
37         private static final int FLAGS_F_LENGTH = 1;
38         private static final int TYPE_F_LENGTH = 1;
39         private static final int METRIC_VALUE_F_LENGTH = 4;
40
41         /*
42          * offsets of fields in bytes
43          */
44         private static final int FLAGS_F_OFFSET = 2;
45         private static final int TYPE_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
46         private static final int METRIC_VALUE_F_OFFSET = TYPE_F_OFFSET + TYPE_F_LENGTH;
47
48         /*
49          * flags offsets inside flags field in bits
50          */
51         private static final int C_FLAG_OFFSET = 6;
52         private static final int B_FLAG_OFFSET = 7;
53
54         private static final int SIZE = METRIC_VALUE_F_OFFSET + METRIC_VALUE_F_LENGTH;
55
56         public PCEPMetricObjectParser(final TlvRegistry tlvReg) {
57                 super(tlvReg);
58         }
59
60         @Override
61         public Metric parseObject(final ObjectHeader header, final byte[] bytes) throws PCEPDeserializerException {
62                 if (bytes == null || bytes.length == 0) {
63                         throw new IllegalArgumentException("Array of bytes is mandatory. Can't be null or empty.");
64                 }
65                 if (bytes.length != SIZE) {
66                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.length + "; Expected: " + SIZE + ".");
67                 }
68                 final byte[] flagBytes = { bytes[FLAGS_F_OFFSET] };
69                 final BitSet flags = ByteArray.bytesToBitSet(flagBytes);
70                 final MetricBuilder builder = new MetricBuilder();
71                 builder.setIgnore(header.isIgnore());
72                 builder.setProcessingRule(header.isProcessingRule());
73                 builder.setBound(flags.get(B_FLAG_OFFSET));
74                 builder.setComputed(flags.get(C_FLAG_OFFSET));
75                 builder.setMetricType((short) UnsignedBytes.toInt(bytes[TYPE_F_OFFSET]));
76                 builder.setValue(new Float32(ByteArray.subByte(bytes, METRIC_VALUE_F_OFFSET, METRIC_VALUE_F_LENGTH)));
77                 return builder.build();
78         }
79
80         @Override
81         public byte[] serializeObject(final Object object) {
82                 if (!(object instanceof Metric)) {
83                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed MetricObject.");
84                 }
85                 final Metric mObj = (Metric) object;
86                 final byte[] retBytes = new byte[SIZE];
87                 final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
88                 flags.set(C_FLAG_OFFSET, mObj.isComputed());
89                 flags.set(B_FLAG_OFFSET, mObj.isBound());
90                 ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH), retBytes, FLAGS_F_OFFSET);
91                 retBytes[TYPE_F_OFFSET] = UnsignedBytes.checkedCast(mObj.getMetricType());
92                 System.arraycopy(mObj.getValue().getValue(), 0, retBytes, METRIC_VALUE_F_OFFSET, METRIC_VALUE_F_LENGTH);
93                 return ObjectUtil.formatSubobject(TYPE, CLASS, object.isProcessingRule(), object.isIgnore(), retBytes);
94         }
95
96         @Override
97         public int getObjectType() {
98                 return TYPE;
99         }
100
101         @Override
102         public int getObjectClass() {
103                 return CLASS;
104         }
105 }