Fixup Augmentable and Identifiable methods changing
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / protocol / deserialization / multipart / MultipartReplyGroupStatsDeserializer.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.group.statistics.rev131111.multipart.reply.multipart.reply.body.MultipartReplyGroupStatsBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.BucketId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.statistics.BucketsBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.statistics.DurationBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.statistics.buckets.BucketCounter;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.statistics.buckets.BucketCounterBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.statistics.buckets.BucketCounterKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.statistics.reply.GroupStats;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.statistics.reply.GroupStatsBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.statistics.reply.GroupStatsKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.multipart.types.rev170112.multipart.reply.MultipartReplyBody;
31
32 public class MultipartReplyGroupStatsDeserializer implements OFDeserializer<MultipartReplyBody> {
33
34     private static final byte PADDING_IN_GROUP_HEADER_01 = 2;
35     private static final byte PADDING_IN_GROUP_HEADER_02 = 4;
36     private static final byte GROUP_BODY_LENGTH = 40;
37     private static final byte BUCKET_COUNTER_LENGTH = 16;
38
39     @Override
40     public MultipartReplyBody deserialize(ByteBuf message) {
41         final MultipartReplyGroupStatsBuilder builder = new MultipartReplyGroupStatsBuilder();
42         final List<GroupStats> items = new ArrayList<>();
43
44         while (message.readableBytes() > 0) {
45             final int itemLength = message.readUnsignedShort();
46             message.skipBytes(PADDING_IN_GROUP_HEADER_01);
47
48             final GroupStatsBuilder itemBuilder = new GroupStatsBuilder()
49                 .setGroupId(new GroupId(message.readUnsignedInt()))
50                 .setRefCount(new Counter32(message.readUnsignedInt()));
51
52             message.skipBytes(PADDING_IN_GROUP_HEADER_02);
53
54             final byte[] packetCountg = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
55             message.readBytes(packetCountg);
56             final byte[] byteCountg = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
57             message.readBytes(byteCountg);
58
59             itemBuilder
60                 .withKey(new GroupStatsKey(itemBuilder.getGroupId()))
61                 .setPacketCount(new Counter64(new BigInteger(1, packetCountg)))
62                 .setByteCount(new Counter64(new BigInteger(1, byteCountg)))
63                 .setDuration(new DurationBuilder()
64                         .setSecond(new Counter32(message.readUnsignedInt()))
65                         .setNanosecond(new Counter32(message.readUnsignedInt()))
66                         .build());
67
68             final List<BucketCounter> subItems = new ArrayList<>();
69             int actualLength = GROUP_BODY_LENGTH;
70             long bucketKey = 0;
71
72             while (actualLength < itemLength) {
73                 final byte[] packetCount = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
74                 message.readBytes(packetCount);
75                 final byte[] byteCount = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
76                 message.readBytes(byteCount);
77
78                 subItems.add(new BucketCounterBuilder()
79                     .setBucketId(new BucketId(bucketKey))
80                     .withKey(new BucketCounterKey(new BucketId(bucketKey)))
81                     .setPacketCount(new Counter64(new BigInteger(1, packetCount)))
82                     .setByteCount(new Counter64(new BigInteger(1, byteCount)))
83                     .build());
84
85                 bucketKey++;
86                 actualLength += BUCKET_COUNTER_LENGTH;
87             }
88
89             items.add(itemBuilder
90                     .setBuckets(new BucketsBuilder()
91                         .setBucketCounter(subItems)
92                         .build())
93                     .build());
94         }
95
96         return builder
97             .setGroupStats(items)
98             .build();
99     }
100
101 }