Fixup Augmentable and Identifiable methods changing
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / protocol / deserialization / messages / GroupMessageDeserializer.java
1 /*
2  * Copyright (c) 2016 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.messages;
10
11 import io.netty.buffer.ByteBuf;
12 import java.util.ArrayList;
13 import java.util.Comparator;
14 import java.util.List;
15 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;
16 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistryInjector;
17 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
18 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
19 import org.opendaylight.openflowplugin.extension.api.path.ActionPath;
20 import org.opendaylight.openflowplugin.impl.protocol.deserialization.util.ActionUtil;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupMessage;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupMessageBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupTypes;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.BucketsBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.Bucket;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.BucketBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.GroupModCommand;
31
32 public class GroupMessageDeserializer implements OFDeserializer<GroupMessage>, DeserializerRegistryInjector {
33
34     private static final byte PADDING = 1;
35     private static final byte PADDING_IN_BUCKETS_HEADER = 4;
36     private static final byte BUCKETS_HEADER_LENGTH = 16;
37
38     private static final Comparator<Bucket> COMPARATOR = (bucket1, bucket2) -> {
39         if (bucket1.getBucketId() == null || bucket2.getBucketId() == null) {
40             return 0;
41         }
42         return bucket1.getBucketId().getValue().compareTo(bucket2.getBucketId().getValue());
43     };
44
45     private DeserializerRegistry registry;
46
47     @Override
48     public GroupMessage deserialize(ByteBuf message) {
49         final GroupMessageBuilder builder = new GroupMessageBuilder()
50             .setVersion((short) EncodeConstants.OF13_VERSION_ID)
51             .setXid(message.readUnsignedInt())
52             .setCommand(GroupModCommand.forValue(message.readUnsignedShort()));
53
54         builder.setGroupType(GroupTypes.forValue(message.readUnsignedByte()));
55         message.skipBytes(PADDING);
56         builder.setGroupId(new GroupId(message.readUnsignedInt()));
57
58         final List<Bucket> buckets = new ArrayList<>();
59
60         while (message.readableBytes() > 0) {
61             final int length = message.readUnsignedShort();
62
63             final BucketBuilder bucket = new BucketBuilder()
64                 .setWeight(message.readUnsignedShort())
65                 .setWatchPort(message.readUnsignedInt())
66                 .setWatchGroup(message.readUnsignedInt());
67
68             message.skipBytes(PADDING_IN_BUCKETS_HEADER);
69
70             if (message.readableBytes() > 0) {
71                 final List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list
72                     .Action> actions = new ArrayList<>();
73                 final int startIndex = message.readerIndex();
74                 final int bucketLength = length - BUCKETS_HEADER_LENGTH;
75                 int offset = 0;
76
77                 while (message.readerIndex() - startIndex < bucketLength) {
78                     actions.add(new ActionBuilder()
79                         .withKey(new ActionKey(offset))
80                         .setOrder(offset)
81                         .setAction(ActionUtil.readAction(EncodeConstants.OF13_VERSION_ID, message, registry,
82                                 ActionPath.GROUP_DESC_STATS_UPDATED_BUCKET_ACTION))
83                         .build());
84
85                     offset++;
86                 }
87
88                 bucket.setAction(actions);
89             }
90
91             buckets.add(bucket.build());
92         }
93
94         buckets.sort(COMPARATOR);
95         return builder
96             .setBuckets(new BucketsBuilder()
97                 .setBucket(buckets)
98                 .build())
99             .build();
100     }
101
102     @Override
103     public void injectDeserializerRegistry(DeserializerRegistry deserializerRegistry) {
104         registry = deserializerRegistry;
105     }
106
107 }