Fix checkstyle
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / deserialization / factories / SetAsyncInputMessageFactory.java
1 /*
2  * Copyright (c) 2015 NetIDE Consortium 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.openflowjava.protocol.impl.deserialization.factories;
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.ArrayList;
15 import java.util.List;
16 import java.util.Set;
17 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
18 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowRemovedReason;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PacketInReason;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortReason;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetAsyncInput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetAsyncInputBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.FlowRemovedMask;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.FlowRemovedMaskBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.PacketInMask;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.PacketInMaskBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.PortStatusMask;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.PortStatusMaskBuilder;
30
31 /**
32  * Translates SetAsyncInput messages.
33  *
34  * @author giuseppex.petralia@intel.com
35  */
36 public class SetAsyncInputMessageFactory implements OFDeserializer<SetAsyncInput> {
37     private static final byte SEPARATE_ROLES = 2;
38
39     @Override
40     public SetAsyncInput deserialize(ByteBuf rawMessage) {
41         return new SetAsyncInputBuilder()
42                 .setVersion(EncodeConstants.OF_VERSION_1_3)
43                 .setXid(readUint32(rawMessage))
44                 .setPacketInMask(decodePacketInMask(rawMessage))
45                 .setPortStatusMask(decodePortStatusMask(rawMessage))
46                 .setFlowRemovedMask(decodeFlowRemovedMask(rawMessage))
47                 .build();
48     }
49
50     private static List<PacketInMask> decodePacketInMask(ByteBuf input) {
51         List<PacketInMask> inMasks = new ArrayList<>(SEPARATE_ROLES);
52         PacketInMaskBuilder maskBuilder;
53         for (int i = 0; i < SEPARATE_ROLES; i++) {
54             maskBuilder = new PacketInMaskBuilder();
55             maskBuilder.setMask(decodePacketInReasons(input.readUnsignedInt()));
56             inMasks.add(maskBuilder.build());
57         }
58         return inMasks;
59     }
60
61     private static Set<PacketInReason> decodePacketInReasons(long input) {
62         final var builder = ImmutableSet.<PacketInReason>builder();
63         if ((input & 1 << 0) != 0) {
64             builder.add(PacketInReason.OFPRNOMATCH);
65         }
66         if ((input & 1 << 1) != 0) {
67             builder.add(PacketInReason.OFPRACTION);
68         }
69         if ((input & 1 << 2) != 0) {
70             builder.add(PacketInReason.OFPRINVALIDTTL);
71         }
72         return builder.build();
73     }
74
75     private static List<PortStatusMask> decodePortStatusMask(ByteBuf input) {
76         List<PortStatusMask> inMasks = new ArrayList<>(SEPARATE_ROLES);
77         PortStatusMaskBuilder maskBuilder;
78         for (int i = 0; i < SEPARATE_ROLES; i++) {
79             maskBuilder = new PortStatusMaskBuilder();
80             maskBuilder.setMask(decodePortReasons(input.readUnsignedInt()));
81             inMasks.add(maskBuilder.build());
82         }
83         return inMasks;
84     }
85
86     private static Set<PortReason> decodePortReasons(long input) {
87         final var builder = ImmutableSet.<PortReason>builder();
88         if ((input & 1 << 0) != 0) {
89             builder.add(PortReason.OFPPRADD);
90         }
91         if ((input & 1 << 1) != 0) {
92             builder.add(PortReason.OFPPRDELETE);
93         }
94         if ((input & 1 << 2) != 0) {
95             builder.add(PortReason.OFPPRMODIFY);
96         }
97         return builder.build();
98     }
99
100     private static List<FlowRemovedMask> decodeFlowRemovedMask(ByteBuf input) {
101         List<FlowRemovedMask> inMasks = new ArrayList<>(SEPARATE_ROLES);
102         FlowRemovedMaskBuilder maskBuilder;
103         for (int i = 0; i < SEPARATE_ROLES; i++) {
104             maskBuilder = new FlowRemovedMaskBuilder();
105             maskBuilder.setMask(decodeFlowRemovedReasons(input.readUnsignedInt()));
106             inMasks.add(maskBuilder.build());
107         }
108         return inMasks;
109     }
110
111     private static Set<FlowRemovedReason> decodeFlowRemovedReasons(long input) {
112         final var builder = ImmutableSet.<FlowRemovedReason>builder();
113         if ((input & 1 << 0) != 0) {
114             builder.add(FlowRemovedReason.OFPRRIDLETIMEOUT);
115         }
116         if ((input & 1 << 1) != 0) {
117             builder.add(FlowRemovedReason.OFPRRHARDTIMEOUT);
118         }
119         if ((input & 1 << 2) != 0) {
120             builder.add(FlowRemovedReason.OFPRRDELETE);
121         }
122         if ((input & 1 << 3) != 0) {
123             builder.add(FlowRemovedReason.OFPRRGROUPDELETE);
124         }
125         return builder.build();
126     }
127 }