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