Merge "Fix junit dependencies in poms. Reuse existing from parent, add missing ones."
[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.PCEPDeserializerException;
13 import org.opendaylight.protocol.pcep.PCEPDocumentedException;
14 import org.opendaylight.protocol.pcep.spi.TlvHandlerRegistry;
15 import org.opendaylight.protocol.util.ByteArray;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ieee754.rev130819.Float32;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.MetricObject;
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.Tlv;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcreq.message.pcreq.message.svec.Metric;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcreq.message.pcreq.message.svec.MetricBuilder;
23
24 import com.google.common.primitives.UnsignedBytes;
25
26 /**
27  * Parser for {@link MetricObject}
28  */
29 public class PCEPMetricObjectParser extends AbstractObjectWithTlvsParser<MetricBuilder> {
30
31         public static final int CLASS = 6;
32
33         public static final int TYPE = 1;
34
35         /*
36          * lengths of fields in bytes
37          */
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;
41
42         /*
43          * offsets of fields in bytes
44          */
45         private static final int FLAGS_F_OFFSET = 2;
46         private static final int TYPE_F_OFFSET = FLAGS_F_OFFSET + FLAGS_F_LENGTH;
47         private static final int METRIC_VALUE_F_OFFSET = TYPE_F_OFFSET + TYPE_F_LENGTH;
48
49         /*
50          * flags offsets inside flags field in bits
51          */
52         private static final int C_FLAG_OFFSET = 6;
53         private static final int B_FLAG_OFFSET = 7;
54
55         private static final int SIZE = METRIC_VALUE_F_OFFSET + METRIC_VALUE_F_LENGTH;
56
57         public PCEPMetricObjectParser(final TlvHandlerRegistry tlvReg) {
58                 super(tlvReg);
59         }
60
61         @Override
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.");
66                 }
67                 if (bytes.length != SIZE) {
68                         throw new PCEPDeserializerException("Wrong length of array of bytes. Passed: " + bytes.length + "; Expected: " + SIZE + ".");
69                 }
70                 final byte[] flagBytes = { bytes[FLAGS_F_OFFSET] };
71                 final BitSet flags = ByteArray.bytesToBitSet(flagBytes);
72                 final MetricBuilder builder = new MetricBuilder();
73                 builder.setIgnore(header.isIgnore());
74                 builder.setProcessingRule(header.isProcessingRule());
75                 builder.setBound(flags.get(B_FLAG_OFFSET));
76                 builder.setComputed(flags.get(C_FLAG_OFFSET));
77                 builder.setMetricType((short) UnsignedBytes.toInt(bytes[TYPE_F_OFFSET]));
78                 builder.setValue(new Float32(ByteArray.subByte(bytes, METRIC_VALUE_F_OFFSET, METRIC_VALUE_F_LENGTH)));
79                 return builder.build();
80         }
81
82         @Override
83         public void addTlv(final MetricBuilder builder, final Tlv tlv) {
84                 // No tlvs defined
85         }
86
87         @Override
88         public byte[] serializeObject(final Object object) {
89                 if (!(object instanceof MetricObject)) {
90                         throw new IllegalArgumentException("Wrong instance of PCEPObject. Passed " + object.getClass() + ". Needed MetricObject.");
91                 }
92                 final MetricObject mObj = (MetricObject) object;
93                 final byte[] retBytes = new byte[SIZE];
94                 final BitSet flags = new BitSet(FLAGS_F_LENGTH * Byte.SIZE);
95                 flags.set(C_FLAG_OFFSET, ((Metric) mObj).isComputed());
96                 flags.set(B_FLAG_OFFSET, mObj.isBound());
97                 ByteArray.copyWhole(ByteArray.bitSetToBytes(flags, FLAGS_F_LENGTH), retBytes, FLAGS_F_OFFSET);
98                 retBytes[TYPE_F_OFFSET] = UnsignedBytes.checkedCast(mObj.getMetricType());
99                 System.arraycopy(mObj.getValue().getValue(), 0, retBytes, METRIC_VALUE_F_OFFSET, METRIC_VALUE_F_LENGTH);
100                 return retBytes;
101         }
102
103         @Override
104         public int getObjectType() {
105                 return TYPE;
106         }
107
108         @Override
109         public int getObjectClass() {
110                 return CLASS;
111         }
112 }