Migrate more List-based map setters
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / protocol / deserialization / multipart / MultipartReplyGroupDescDeserializer.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 package org.opendaylight.openflowplugin.impl.protocol.deserialization.multipart;
9
10 import io.netty.buffer.ByteBuf;
11 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;
12 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistryInjector;
13 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
14 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
15 import org.opendaylight.openflowplugin.extension.api.path.ActionPath;
16 import org.opendaylight.openflowplugin.impl.protocol.deserialization.util.ActionUtil;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.multipart.reply.multipart.reply.body.MultipartReplyGroupDescBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.BucketId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupTypes;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.BucketsBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.Bucket;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.BucketBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.BucketKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.desc.stats.reply.GroupDescStats;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.desc.stats.reply.GroupDescStatsBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.desc.stats.reply.GroupDescStatsKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.multipart.types.rev170112.multipart.reply.MultipartReplyBody;
32 import org.opendaylight.yangtools.yang.binding.util.BindingMap;
33
34 public class MultipartReplyGroupDescDeserializer implements OFDeserializer<MultipartReplyBody>,
35         DeserializerRegistryInjector {
36
37     private static final byte PADDING_IN_GROUP_DESC_HEADER = 1;
38     private static final byte PADDING_IN_BUCKETS_HEADER = 4;
39     private static final byte GROUP_DESC_HEADER_LENGTH = 8;
40     private static final byte BUCKETS_HEADER_LENGTH = 16;
41     private DeserializerRegistry registry;
42
43     @Override
44     public MultipartReplyBody deserialize(final ByteBuf message) {
45         final MultipartReplyGroupDescBuilder builder = new MultipartReplyGroupDescBuilder();
46         final var items =  BindingMap.<GroupDescStatsKey, GroupDescStats>orderedBuilder();
47
48         while (message.readableBytes() > 0) {
49             final int itemLength = message.readUnsignedShort();
50
51             final GroupDescStatsBuilder itemBuilder = new GroupDescStatsBuilder()
52                     .setGroupType(GroupTypes.forValue(message.readUnsignedByte()));
53
54             message.skipBytes(PADDING_IN_GROUP_DESC_HEADER);
55             itemBuilder.setGroupId(new GroupId(message.readUnsignedInt()));
56             itemBuilder.withKey(new GroupDescStatsKey(itemBuilder.getGroupId()));
57
58             final var subItems = BindingMap.<BucketKey, Bucket>orderedBuilder();
59             int actualLength = GROUP_DESC_HEADER_LENGTH;
60
61             long bucketKey = 0;
62             while (actualLength < itemLength) {
63                 final int bucketsLength = message.readUnsignedShort();
64
65                 final BucketBuilder bucketBuilder = new BucketBuilder()
66                         .setBucketId(new BucketId(bucketKey))
67                         .withKey(new BucketKey(new BucketId(bucketKey)))
68                         .setWeight(message.readUnsignedShort())
69                         .setWatchPort(message.readUnsignedInt())
70                         .setWatchGroup(message.readUnsignedInt());
71
72                 message.skipBytes(PADDING_IN_BUCKETS_HEADER);
73                 final var actions = BindingMap.<ActionKey, Action>orderedBuilder();
74                 final int startIndex = message.readerIndex();
75                 final int bucketLength = bucketsLength - BUCKETS_HEADER_LENGTH;
76                 int offset = 0;
77
78                 while (message.readerIndex() - startIndex < bucketLength) {
79                     actions.add(new ActionBuilder()
80                             .withKey(new ActionKey(offset))
81                             .setOrder(offset)
82                             .setAction(ActionUtil.readAction(EncodeConstants.OF13_VERSION_ID, message, registry,
83                                     ActionPath.GROUP_DESC_STATS_UPDATED_BUCKET_ACTION))
84                             .build());
85
86                     offset++;
87                 }
88
89                 subItems.add(bucketBuilder.setAction(actions.build()).build());
90                 bucketKey++;
91                 actualLength += bucketsLength;
92             }
93
94             items.add(itemBuilder.setBuckets(new BucketsBuilder().setBucket(subItems.build()).build()).build());
95         }
96
97         return builder.setGroupDescStats(items.build()).build();
98     }
99
100     @Override
101     public void injectDeserializerRegistry(final DeserializerRegistry deserializerRegistry) {
102         registry = deserializerRegistry;
103     }
104
105 }