Remove superfluous constants
[bgpcep.git] / rsvp / impl / src / main / java / org / opendaylight / protocol / rsvp / parser / impl / te / MetricObjectParser.java
1 /*
2  * Copyright (c) 2015 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.rsvp.parser.impl.te;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeFloat32;
12
13 import io.netty.buffer.ByteBuf;
14 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
15 import org.opendaylight.protocol.rsvp.parser.spi.subobjects.AbstractRSVPObjectParser;
16 import org.opendaylight.protocol.util.BitArray;
17 import org.opendaylight.protocol.util.ByteArray;
18 import org.opendaylight.protocol.util.ByteBufUtils;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ieee754.rev130819.Float32;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.RsvpTeObject;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.metric.object.MetricObject;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.metric.object.MetricObjectBuilder;
23
24 public final class MetricObjectParser extends AbstractRSVPObjectParser {
25     public static final short CLASS_NUM = 6;
26     public static final short CTYPE = 1;
27     private static final Integer BODY_SIZE = 8;
28     private static final int BOUND = 7;
29     private static final int COMPUTED = 6;
30
31     @Override
32     protected RsvpTeObject localParseObject(final ByteBuf byteBuf) throws RSVPParsingException {
33         byteBuf.skipBytes(Short.BYTES);
34         final BitArray flags = BitArray.valueOf(byteBuf.readByte());
35
36         return new MetricObjectBuilder()
37                 .setBound(flags.get(BOUND))
38                 .setComputed(flags.get(COMPUTED))
39                 .setMetricType(ByteBufUtils.readUint8(byteBuf))
40                 .setValue(new Float32(ByteArray.readBytes(byteBuf, METRIC_VALUE_F_LENGTH)))
41                 .build();
42     }
43
44     @Override
45     public void localSerializeObject(final RsvpTeObject teLspObject, final ByteBuf output) {
46         checkArgument(teLspObject instanceof MetricObject, "BandwidthObject is mandatory.");
47         final MetricObject metric = (MetricObject) teLspObject;
48         serializeAttributeHeader(BODY_SIZE, CLASS_NUM, CTYPE, output);
49         output.writeShort(0);
50         final BitArray reflect = new BitArray(FLAGS_SIZE);
51         reflect.set(BOUND, metric.isBound());
52         reflect.set(COMPUTED, metric.isComputed());
53         reflect.toByteBuf(output);
54         output.writeByte(metric.getMetricType().toJava());
55         writeFloat32(metric.getValue(), output);
56     }
57 }