Reduce ByteBuf.writeZero() usage
[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.protocol.util.ByteBufWriteUtil;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ieee754.rev130819.Float32;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.RsvpTeObject;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.metric.object.MetricObject;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.metric.object.MetricObjectBuilder;
24
25 public final class MetricObjectParser extends AbstractRSVPObjectParser {
26     public static final short CLASS_NUM = 6;
27     public static final short CTYPE = 1;
28     private static final Integer BODY_SIZE = 8;
29     private static final int BOUND = 7;
30     private static final int COMPUTED = 6;
31
32     @Override
33     protected RsvpTeObject localParseObject(final ByteBuf byteBuf) throws RSVPParsingException {
34         byteBuf.skipBytes(ByteBufWriteUtil.SHORT_BYTES_LENGTH);
35         final BitArray flags = BitArray.valueOf(byteBuf.readByte());
36
37         return new MetricObjectBuilder()
38                 .setBound(flags.get(BOUND))
39                 .setComputed(flags.get(COMPUTED))
40                 .setMetricType(ByteBufUtils.readUint8(byteBuf))
41                 .setValue(new Float32(ByteArray.readBytes(byteBuf, METRIC_VALUE_F_LENGTH)))
42                 .build();
43     }
44
45     @Override
46     public void localSerializeObject(final RsvpTeObject teLspObject, final ByteBuf output) {
47         checkArgument(teLspObject instanceof MetricObject, "BandwidthObject is mandatory.");
48         final MetricObject metric = (MetricObject) teLspObject;
49         serializeAttributeHeader(BODY_SIZE, CLASS_NUM, CTYPE, output);
50         output.writeShort(0);
51         final BitArray reflect = new BitArray(FLAGS_SIZE);
52         reflect.set(BOUND, metric.isBound());
53         reflect.set(COMPUTED, metric.isComputed());
54         reflect.toByteBuf(output);
55         output.writeByte(metric.getMetricType().toJava());
56         writeFloat32(metric.getValue(), output);
57     }
58 }