Bump MRI upstreams
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / protocol / deserialization / multipart / MultipartReplyGroupFeaturesDeserializer.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 static org.opendaylight.yangtools.yang.common.netty.ByteBufUtils.readUint32;
11
12 import com.google.common.collect.ImmutableSet;
13 import io.netty.buffer.ByteBuf;
14 import java.util.Set;
15 import java.util.stream.Collectors;
16 import java.util.stream.IntStream;
17 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.multipart.reply.multipart.reply.body.MultipartReplyGroupFeaturesBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.Chaining;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.ChainingChecks;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupAll;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupCapability;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupFf;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupIndirect;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupSelect;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupType;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.SelectLiveness;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.SelectWeight;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.multipart.types.rev170112.multipart.reply.MultipartReplyBody;
30
31 public class MultipartReplyGroupFeaturesDeserializer implements OFDeserializer<MultipartReplyBody> {
32     private static final int GROUP_TYPES = 4;
33
34     @Override
35     public MultipartReplyBody deserialize(final ByteBuf message) {
36         return new MultipartReplyGroupFeaturesBuilder()
37             .setGroupTypesSupported(readGroupTypes(message))
38             .setGroupCapabilitiesSupported(readGroupCapabilities(message))
39             .setMaxGroups(IntStream.range(0, GROUP_TYPES)
40                 .mapToObj(i -> readUint32(message))
41                 .collect(Collectors.toUnmodifiableList()))
42             .setActions(IntStream.range(0, GROUP_TYPES)
43                 .mapToObj(i -> readUint32(message))
44                 .collect(Collectors.toUnmodifiableList()))
45             .build();
46     }
47
48     private static Set<Class<? extends GroupCapability>> readGroupCapabilities(final ByteBuf message) {
49         final long capabilitiesMask = message.readUnsignedInt();
50
51         final var builder = ImmutableSet.<Class<? extends GroupCapability>>builder();
52         if ((capabilitiesMask & 1 << 0) != 0) {
53             builder.add(SelectWeight.class);
54         }
55         if ((capabilitiesMask & 1 << 1) != 0) {
56             builder.add(SelectLiveness.class);
57         }
58         if ((capabilitiesMask & 1 << 2) != 0) {
59             builder.add(Chaining.class);
60         }
61         if ((capabilitiesMask & 1 << 3) != 0) {
62             builder.add(ChainingChecks.class);
63         }
64         return builder.build();
65     }
66
67     private static Set<Class<? extends GroupType>> readGroupTypes(final ByteBuf message) {
68         final long typesMask = message.readUnsignedInt();
69
70         final var builder = ImmutableSet.<Class<? extends GroupType>>builder();
71         if ((typesMask & 1 << 0) != 0) {
72             builder.add(GroupAll.class);
73         }
74         if ((typesMask & 1 << 1) != 0) {
75             builder.add(GroupSelect.class);
76         }
77         if ((typesMask & 1 << 2) != 0) {
78             builder.add(GroupIndirect.class);
79         }
80         if ((typesMask & 1 << 3) != 0) {
81             builder.add(GroupFf.class);
82         }
83         return builder.build();
84     }
85 }