Fixup Augmentable and Identifiable methods changing
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / protocol / deserialization / multipart / MultipartReplyMeterStatsDeserializer.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.openflowplugin.impl.protocol.deserialization.multipart;
10
11 import io.netty.buffer.ByteBuf;
12 import java.math.BigInteger;
13 import java.util.ArrayList;
14 import java.util.List;
15 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
16 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Counter32;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Counter64;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.multipart.reply.multipart.reply.body.MultipartReplyMeterStatsBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.BandId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.statistics.DurationBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.statistics.MeterBandStatsBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.statistics.meter.band.stats.BandStat;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.statistics.meter.band.stats.BandStatBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.statistics.meter.band.stats.BandStatKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.statistics.reply.MeterStats;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.statistics.reply.MeterStatsBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.meter.statistics.reply.MeterStatsKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.multipart.types.rev170112.multipart.reply.MultipartReplyBody;
31
32 public class MultipartReplyMeterStatsDeserializer implements OFDeserializer<MultipartReplyBody> {
33
34     private static final byte PADDING_IN_METER_STATS_HEADER = 6;
35     private static final byte METER_BODY_LENGTH = 40;
36     private static final byte METER_BAND_STATS_LENGTH = 16;
37
38     @Override
39     public MultipartReplyBody deserialize(ByteBuf message) {
40         final MultipartReplyMeterStatsBuilder builder = new MultipartReplyMeterStatsBuilder();
41         final List<MeterStats> items = new ArrayList<>();
42
43         while (message.readableBytes() > 0) {
44             final MeterStatsBuilder itemBuilder = new MeterStatsBuilder()
45                 .setMeterId(new MeterId(message.readUnsignedInt()));
46
47             final int itemLength = message.readUnsignedShort();
48             message.skipBytes(PADDING_IN_METER_STATS_HEADER);
49
50             itemBuilder
51                 .withKey(new MeterStatsKey(itemBuilder.getMeterId()))
52                 .setFlowCount(new Counter32(message.readUnsignedInt()));
53
54             final byte[] packetCount = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
55             message.readBytes(packetCount);
56             final byte[] byteCount = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
57             message.readBytes(byteCount);
58
59             itemBuilder
60                 .setPacketInCount(new Counter64(new BigInteger(1, packetCount)))
61                 .setByteInCount(new Counter64(new BigInteger(1, byteCount)))
62                 .setDuration(new DurationBuilder()
63                         .setSecond(new Counter32(message.readUnsignedInt()))
64                         .setNanosecond(new Counter32(message.readUnsignedInt()))
65                         .build());
66
67             final List<BandStat> subItems = new ArrayList<>();
68             int actualLength = METER_BODY_LENGTH;
69             long bandKey = 0;
70
71             while (actualLength < itemLength) {
72                 final byte[] packetCountB = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
73                 message.readBytes(packetCountB);
74                 final byte[] byteCountB = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
75                 message.readBytes(byteCountB);
76
77                 subItems.add(new BandStatBuilder()
78                     .setBandId(new BandId(bandKey))
79                     .withKey(new BandStatKey(new BandId(bandKey)))
80                     .setPacketBandCount(new Counter64(new BigInteger(1, packetCountB)))
81                     .setByteBandCount(new Counter64(new BigInteger(1, byteCountB)))
82                     .build());
83
84                 bandKey++;
85                 actualLength += METER_BAND_STATS_LENGTH;
86             }
87
88             items.add(itemBuilder
89                     .setMeterBandStats(new MeterBandStatsBuilder()
90                         .setBandStat(subItems)
91                         .build())
92                     .build());
93         }
94
95         return builder
96             .setMeterStats(items)
97             .build();
98     }
99
100 }