9642c7e2fd0524c83546d241d14d0e2c03c6eddf
[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.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.tspec.object.TspecObject;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.tspec.object.TspecObjectBuilder;
24
25 public class SenderTspecObjectParser extends AbstractRSVPObjectParser {
26     public static final short CLASS_NUM = 12;
27     public static final short CTYPE = 2;
28     private static final int BODY_SIZE = 32;
29     private static final int SERVICE_LENGHT = 6;
30
31     @Override
32     protected RsvpTeObject localParseObject(final ByteBuf byteBuf) throws RSVPParsingException {
33         //skip version number, reserved, Overall length
34         byteBuf.skipBytes(ByteBufWriteUtil.INT_BYTES_LENGTH);
35         //skip Service header, reserved, Length of service
36         byteBuf.skipBytes(ByteBufWriteUtil.INT_BYTES_LENGTH);
37         //skip Parameter ID, Parameter 127 flags, Parameter 127 length
38         byteBuf.skipBytes(ByteBufWriteUtil.INT_BYTES_LENGTH);
39
40         return new TspecObjectBuilder()
41                 .setTokenBucketRate(new Float32(ByteArray.readBytes(byteBuf, METRIC_VALUE_F_LENGTH)))
42                 .setTokenBucketSize(new Float32(ByteArray.readBytes(byteBuf, METRIC_VALUE_F_LENGTH)))
43                 .setPeakDataRate(new Float32(ByteArray.readBytes(byteBuf, METRIC_VALUE_F_LENGTH)))
44                 .setMinimumPolicedUnit(ByteBufUtils.readUint32(byteBuf))
45                 .setMaximumPacketSize(ByteBufUtils.readUint32(byteBuf))
46                 .build();
47     }
48
49     @Override
50     public void localSerializeObject(final RsvpTeObject teLspObject, final ByteBuf output) {
51         checkArgument(teLspObject instanceof TspecObject, "SenderTspecObject is mandatory.");
52         final TspecObject tspecObj = (TspecObject) teLspObject;
53         serializeAttributeHeader(BODY_SIZE, CLASS_NUM, CTYPE, output);
54         output.writeZero(ByteBufWriteUtil.SHORT_BYTES_LENGTH);
55         output.writeShort(OVERALL_LENGTH);
56         output.writeByte(ByteBufWriteUtil.ONE_BYTE_LENGTH);
57         output.writeZero(ByteBufWriteUtil.ONE_BYTE_LENGTH);
58         output.writeShort(SERVICE_LENGHT);
59         output.writeByte(TOKEN_BUCKET_TSPEC);
60         output.writeZero(ByteBufWriteUtil.ONE_BYTE_LENGTH);
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 }