Merge "OPNFLWPLUG-929 : Remove deprecated guava library"
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / SetAsyncInputMessageFactory.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
9 package org.opendaylight.openflowjava.protocol.impl.serialization.factories;
10
11 import io.netty.buffer.ByteBuf;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Map;
15 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
16 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
17 import org.opendaylight.openflowjava.util.ByteBufUtils;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowRemovedReason;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PacketInReason;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortReason;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetAsyncInput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.FlowRemovedMask;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.PacketInMask;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.PortStatusMask;
25
26 /**
27  * Translates SetAsync messages.
28  *
29  * @author timotej.kubas
30  * @author michal.polkorab
31  */
32 public class SetAsyncInputMessageFactory implements OFSerializer<SetAsyncInput> {
33     private static final byte MESSAGE_TYPE = 28;
34
35     @Override
36     public void serialize(SetAsyncInput message, ByteBuf outBuffer) {
37         ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
38         serializePacketInMask(message.getPacketInMask(), outBuffer);
39         serializePortStatusMask(message.getPortStatusMask(), outBuffer);
40         serializerFlowRemovedMask(message.getFlowRemovedMask(), outBuffer);
41         ByteBufUtils.updateOFHeaderLength(outBuffer);
42     }
43
44     private static void serializePacketInMask(List<PacketInMask> packetInMask, ByteBuf outBuffer) {
45         if (packetInMask != null) {
46             for (PacketInMask currentPacketMask : packetInMask) {
47                 List<PacketInReason> mask = currentPacketMask.getMask();
48                 if (mask != null)  {
49                     Map<Integer, Boolean> packetInReasonMap = new HashMap<>();
50                     for (PacketInReason packetInReason : mask) {
51                         if (PacketInReason.OFPRNOMATCH.equals(packetInReason)) {
52                             packetInReasonMap.put(PacketInReason.OFPRNOMATCH.getIntValue(), true);
53                         } else if (PacketInReason.OFPRACTION.equals(packetInReason)) {
54                             packetInReasonMap.put(PacketInReason.OFPRACTION.getIntValue(), true);
55                         } else if (PacketInReason.OFPRINVALIDTTL.equals(packetInReason)) {
56                             packetInReasonMap.put(PacketInReason.OFPRINVALIDTTL.getIntValue(), true);
57                         }
58                     }
59                     outBuffer.writeInt(ByteBufUtils.fillBitMaskFromMap(packetInReasonMap));
60                 }
61             }
62         }
63     }
64
65     private static void serializePortStatusMask(List<PortStatusMask> portStatusMask, ByteBuf outBuffer) {
66         if (portStatusMask != null) {
67             for (PortStatusMask currentPortStatusMask : portStatusMask) {
68                 List<PortReason> mask = currentPortStatusMask.getMask();
69                 if (mask != null)  {
70                     Map<Integer, Boolean> portStatusReasonMap = new HashMap<>();
71                     for (PortReason packetInReason : mask) {
72                         if (PortReason.OFPPRADD.equals(packetInReason)) {
73                             portStatusReasonMap.put(PortReason.OFPPRADD.getIntValue(), true);
74                         } else if (PortReason.OFPPRDELETE.equals(packetInReason)) {
75                             portStatusReasonMap.put(PortReason.OFPPRDELETE.getIntValue(), true);
76                         } else if (PortReason.OFPPRMODIFY.equals(packetInReason)) {
77                             portStatusReasonMap.put(PortReason.OFPPRMODIFY.getIntValue(), true);
78                         }
79                     }
80                     outBuffer.writeInt(ByteBufUtils.fillBitMaskFromMap(portStatusReasonMap));
81                 }
82             }
83         }
84     }
85
86     private static void serializerFlowRemovedMask(List<FlowRemovedMask> flowRemovedMask, ByteBuf outBuffer) {
87         if (flowRemovedMask != null) {
88             for (FlowRemovedMask currentFlowRemovedMask : flowRemovedMask) {
89                 List<FlowRemovedReason> mask = currentFlowRemovedMask.getMask();
90                 if (mask != null)  {
91                     Map<Integer, Boolean> flowRemovedReasonMap = new HashMap<>();
92                     for (FlowRemovedReason packetInReason : mask) {
93                         if (FlowRemovedReason.OFPRRIDLETIMEOUT.equals(packetInReason)) {
94                             flowRemovedReasonMap.put(FlowRemovedReason.OFPRRIDLETIMEOUT.getIntValue(), true);
95                         } else if (FlowRemovedReason.OFPRRHARDTIMEOUT.equals(packetInReason)) {
96                             flowRemovedReasonMap.put(FlowRemovedReason.OFPRRHARDTIMEOUT.getIntValue(), true);
97                         } else if (FlowRemovedReason.OFPRRDELETE.equals(packetInReason)) {
98                             flowRemovedReasonMap.put(FlowRemovedReason.OFPRRDELETE.getIntValue(), true);
99                         } else if (FlowRemovedReason.OFPRRGROUPDELETE.equals(packetInReason)) {
100                             flowRemovedReasonMap.put(FlowRemovedReason.OFPRRGROUPDELETE.getIntValue(), true);
101                         }
102                     }
103                     outBuffer.writeInt(ByteBufUtils.fillBitMaskFromMap(flowRemovedReasonMap));
104                 }
105             }
106         }
107     }
108 }