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