Remove superfluous constants
[bgpcep.git] / rsvp / impl / src / main / java / org / opendaylight / protocol / rsvp / parser / impl / te / FlowSpecObjectParser.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 org.opendaylight.protocol.util.ByteBufWriteUtil.writeFloat32;
11 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedInt;
12
13 import com.google.common.base.Preconditions;
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.ServiceNumber;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.flow.spec.object.FlowSpecObject;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.flow.spec.object.FlowSpecObjectBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.tspec.object.TspecObject;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.rsvp.rev150820.tspec.object.TspecObjectBuilder;
26
27 public final class FlowSpecObjectParser extends AbstractRSVPObjectParser {
28     public static final short CLASS_NUM = 9;
29     public static final short CTYPE = 2;
30     private static final Integer BODY_SIZE_CONTROLLED = 32;
31     private static final Integer BODY_SIZE_REQUESTING = 44;
32     private static final Integer CONTROLLER_OVERALL_LENGHT = 7;
33     private static final Integer REQUESTING_OVERALL_LENGTH = 10;
34     private static final int SERVICE_LENGHT = 9;
35     private static final int CONTROLLED_LENGHT = 6;
36
37     @Override
38     protected RsvpTeObject localParseObject(final ByteBuf byteBuf) throws RSVPParsingException {
39         final FlowSpecObjectBuilder builder = new FlowSpecObjectBuilder();
40         //skip version number, reserved, overall length
41         byteBuf.skipBytes(Integer.BYTES);
42         builder.setServiceHeader(ServiceNumber.forValue(byteBuf.readUnsignedByte()));
43         //skip reserved
44         byteBuf.skipBytes(Byte.BYTES);
45         //skip Length of controlled-load data
46         byteBuf.skipBytes(Short.BYTES);
47         //skip parameter ID 127 and 127 flags
48         byteBuf.skipBytes(Integer.BYTES);
49         final TspecObjectBuilder tBuilder = new TspecObjectBuilder()
50                 .setTokenBucketRate(new Float32(ByteArray.readBytes(byteBuf, METRIC_VALUE_F_LENGTH)))
51                 .setTokenBucketSize(new Float32(ByteArray.readBytes(byteBuf, METRIC_VALUE_F_LENGTH)))
52                 .setPeakDataRate(new Float32(ByteArray.readBytes(byteBuf, METRIC_VALUE_F_LENGTH)))
53                 .setMinimumPolicedUnit(ByteBufUtils.readUint32(byteBuf))
54                 .setMaximumPacketSize(ByteBufUtils.readUint32(byteBuf));
55         builder.setTspecObject(tBuilder.build());
56         if (builder.getServiceHeader().getIntValue() == 2) {
57             //skip parameter ID 130, flags, lenght
58             byteBuf.skipBytes(Integer.BYTES);
59             builder.setRate(new Float32(ByteArray.readBytes(byteBuf, METRIC_VALUE_F_LENGTH)));
60             builder.setSlackTerm(ByteBufUtils.readUint32(byteBuf));
61         }
62         return builder.build();
63     }
64
65     @Override
66     public void localSerializeObject(final RsvpTeObject teLspObject, final ByteBuf output) {
67         Preconditions.checkArgument(teLspObject instanceof FlowSpecObject, "SenderTspecObject is mandatory.");
68         final FlowSpecObject flowObj = (FlowSpecObject) teLspObject;
69         final int sHeader = flowObj.getServiceHeader().getIntValue();
70         if (sHeader == 2) {
71             serializeAttributeHeader(BODY_SIZE_REQUESTING, CLASS_NUM, CTYPE, output);
72             output.writeShort(0);
73             output.writeShort(REQUESTING_OVERALL_LENGTH);
74         } else {
75             serializeAttributeHeader(BODY_SIZE_CONTROLLED, CLASS_NUM, CTYPE, output);
76             output.writeShort(0);
77             output.writeShort(CONTROLLER_OVERALL_LENGHT);
78         }
79         output.writeByte(sHeader);
80         output.writeByte(0);
81         if (sHeader == 2) {
82             output.writeShort(SERVICE_LENGHT);
83         } else {
84             output.writeShort(CONTROLLED_LENGHT);
85         }
86
87         output.writeByte(TOKEN_BUCKET_TSPEC);
88         output.writeByte(0);
89         output.writeShort(PARAMETER_127_LENGTH);
90         final TspecObject tSpec = flowObj.getTspecObject();
91         writeFloat32(tSpec.getTokenBucketRate(), output);
92         writeFloat32(tSpec.getTokenBucketSize(), output);
93         writeFloat32(tSpec.getPeakDataRate(), output);
94         writeUnsignedInt(tSpec.getMinimumPolicedUnit(), output);
95         writeUnsignedInt(tSpec.getMaximumPacketSize(), output);
96
97         if (sHeader != 2) {
98             return;
99         }
100         output.writeByte(GUARANTEED_SERVICE_RSPEC);
101         output.writeByte(0);
102         output.writeShort(PARAMETER_130_LENGTH);
103         writeFloat32(flowObj.getRate(), output);
104         writeUnsignedInt(flowObj.getSlackTerm(), output);
105     }
106 }