Remove superfluous constants
[bgpcep.git] / rsvp / impl / src / main / java / org / opendaylight / protocol / rsvp / parser / impl / te / SenderTspecObjectParser.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 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedInt;
13
14 import io.netty.buffer.ByteBuf;
15 import org.opendaylight.protocol.rsvp.parser.spi.RSVPParsingException;
16 import org.opendaylight.protocol.rsvp.parser.spi.subobjects.AbstractRSVPObjectParser;
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.tspec.object.TspecObject;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.tspec.object.TspecObjectBuilder;
23
24 public class SenderTspecObjectParser extends AbstractRSVPObjectParser {
25     public static final short CLASS_NUM = 12;
26     public static final short CTYPE = 2;
27     private static final int BODY_SIZE = 32;
28     private static final int SERVICE_LENGTH = 6;
29
30     @Override
31     protected RsvpTeObject localParseObject(final ByteBuf byteBuf) throws RSVPParsingException {
32         //skip version number, reserved, Overall length
33         byteBuf.skipBytes(Integer.BYTES);
34         //skip Service header, reserved, Length of service
35         byteBuf.skipBytes(Integer.BYTES);
36         //skip Parameter ID, Parameter 127 flags, Parameter 127 length
37         byteBuf.skipBytes(Integer.BYTES);
38
39         return new TspecObjectBuilder()
40                 .setTokenBucketRate(new Float32(ByteArray.readBytes(byteBuf, METRIC_VALUE_F_LENGTH)))
41                 .setTokenBucketSize(new Float32(ByteArray.readBytes(byteBuf, METRIC_VALUE_F_LENGTH)))
42                 .setPeakDataRate(new Float32(ByteArray.readBytes(byteBuf, METRIC_VALUE_F_LENGTH)))
43                 .setMinimumPolicedUnit(ByteBufUtils.readUint32(byteBuf))
44                 .setMaximumPacketSize(ByteBufUtils.readUint32(byteBuf))
45                 .build();
46     }
47
48     @Override
49     public void localSerializeObject(final RsvpTeObject teLspObject, final ByteBuf output) {
50         checkArgument(teLspObject instanceof TspecObject, "SenderTspecObject is mandatory.");
51         final TspecObject tspecObj = (TspecObject) teLspObject;
52         serializeAttributeHeader(BODY_SIZE, CLASS_NUM, CTYPE, output);
53         output.writeShort(0);
54         output.writeShort(OVERALL_LENGTH);
55         // FIXME: this is weird. Use explicit 1?
56         output.writeByte(Byte.BYTES);
57         output.writeByte(0);
58         output.writeShort(SERVICE_LENGTH);
59         output.writeByte(TOKEN_BUCKET_TSPEC);
60         output.writeByte(0);
61         output.writeShort(PARAMETER_127_LENGTH);
62         writeFloat32(tspecObj.getTokenBucketRate(), output);
63         writeFloat32(tspecObj.getTokenBucketSize(), output);
64         writeFloat32(tspecObj.getPeakDataRate(), output);
65         writeUnsignedInt(tspecObj.getMinimumPolicedUnit(), output);
66         writeUnsignedInt(tspecObj.getMaximumPacketSize(), output);
67     }
68 }