Optimize SetAsyncInputMessageFactory 48/94348/2
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 3 Jan 2021 00:11:40 +0000 (01:11 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 3 Jan 2021 00:14:01 +0000 (01:14 +0100)
This class is very similar to GetAsyncReplyMessageFactory, apply
same optimizations.

Change-Id: Iaeacd426fb99aaf1ce527d53bcb935aaf0551fcd
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
openflowjava/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/SetAsyncInputMessageFactory.java

index 2c471b04bcc3b95355a8a637a7845587c17b3371..ffee4e9ff35389d1d455382e06d32ed2618c0192 100644 (file)
@@ -9,19 +9,15 @@
 package org.opendaylight.openflowjava.protocol.impl.serialization.factories;
 
 import io.netty.buffer.ByteBuf;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
 import org.opendaylight.openflowjava.util.ByteBufUtils;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowRemovedReason;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PacketInReason;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortReason;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetAsyncInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.FlowRemovedMask;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.PacketInMask;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.PortStatusMask;
+import org.opendaylight.yangtools.yang.binding.Enumeration;
 
 /**
  * Translates SetAsync messages.
@@ -33,7 +29,7 @@ public class SetAsyncInputMessageFactory implements OFSerializer<SetAsyncInput>
     private static final byte MESSAGE_TYPE = 28;
 
     @Override
-    public void serialize(SetAsyncInput message, ByteBuf outBuffer) {
+    public void serialize(final SetAsyncInput message, final ByteBuf outBuffer) {
         ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
         serializePacketInMask(message.getPacketInMask(), outBuffer);
         serializePortStatusMask(message.getPortStatusMask(), outBuffer);
@@ -41,68 +37,38 @@ public class SetAsyncInputMessageFactory implements OFSerializer<SetAsyncInput>
         ByteBufUtils.updateOFHeaderLength(outBuffer);
     }
 
-    private static void serializePacketInMask(List<PacketInMask> packetInMask, ByteBuf outBuffer) {
+    private static void serializePacketInMask(final List<PacketInMask> packetInMask, final ByteBuf outBuffer) {
         if (packetInMask != null) {
             for (PacketInMask currentPacketMask : packetInMask) {
-                List<PacketInReason> mask = currentPacketMask.getMask();
-                if (mask != null)  {
-                    Map<Integer, Boolean> packetInReasonMap = new HashMap<>();
-                    for (PacketInReason packetInReason : mask) {
-                        if (PacketInReason.OFPRNOMATCH.equals(packetInReason)) {
-                            packetInReasonMap.put(PacketInReason.OFPRNOMATCH.getIntValue(), true);
-                        } else if (PacketInReason.OFPRACTION.equals(packetInReason)) {
-                            packetInReasonMap.put(PacketInReason.OFPRACTION.getIntValue(), true);
-                        } else if (PacketInReason.OFPRINVALIDTTL.equals(packetInReason)) {
-                            packetInReasonMap.put(PacketInReason.OFPRINVALIDTTL.getIntValue(), true);
-                        }
-                    }
-                    outBuffer.writeInt(ByteBufUtils.fillBitMaskFromMap(packetInReasonMap));
-                }
+                serializeReasons(currentPacketMask.getMask(), outBuffer);
             }
         }
     }
 
-    private static void serializePortStatusMask(List<PortStatusMask> portStatusMask, ByteBuf outBuffer) {
+    private static void serializePortStatusMask(final List<PortStatusMask> portStatusMask, final ByteBuf outBuffer) {
         if (portStatusMask != null) {
             for (PortStatusMask currentPortStatusMask : portStatusMask) {
-                List<PortReason> mask = currentPortStatusMask.getMask();
-                if (mask != null)  {
-                    Map<Integer, Boolean> portStatusReasonMap = new HashMap<>();
-                    for (PortReason packetInReason : mask) {
-                        if (PortReason.OFPPRADD.equals(packetInReason)) {
-                            portStatusReasonMap.put(PortReason.OFPPRADD.getIntValue(), true);
-                        } else if (PortReason.OFPPRDELETE.equals(packetInReason)) {
-                            portStatusReasonMap.put(PortReason.OFPPRDELETE.getIntValue(), true);
-                        } else if (PortReason.OFPPRMODIFY.equals(packetInReason)) {
-                            portStatusReasonMap.put(PortReason.OFPPRMODIFY.getIntValue(), true);
-                        }
-                    }
-                    outBuffer.writeInt(ByteBufUtils.fillBitMaskFromMap(portStatusReasonMap));
-                }
+                serializeReasons(currentPortStatusMask.getMask(), outBuffer);
             }
         }
     }
 
-    private static void serializerFlowRemovedMask(List<FlowRemovedMask> flowRemovedMask, ByteBuf outBuffer) {
+    private static void serializerFlowRemovedMask(final List<FlowRemovedMask> flowRemovedMask,
+            final ByteBuf outBuffer) {
         if (flowRemovedMask != null) {
             for (FlowRemovedMask currentFlowRemovedMask : flowRemovedMask) {
-                List<FlowRemovedReason> mask = currentFlowRemovedMask.getMask();
-                if (mask != null)  {
-                    Map<Integer, Boolean> flowRemovedReasonMap = new HashMap<>();
-                    for (FlowRemovedReason packetInReason : mask) {
-                        if (FlowRemovedReason.OFPRRIDLETIMEOUT.equals(packetInReason)) {
-                            flowRemovedReasonMap.put(FlowRemovedReason.OFPRRIDLETIMEOUT.getIntValue(), true);
-                        } else if (FlowRemovedReason.OFPRRHARDTIMEOUT.equals(packetInReason)) {
-                            flowRemovedReasonMap.put(FlowRemovedReason.OFPRRHARDTIMEOUT.getIntValue(), true);
-                        } else if (FlowRemovedReason.OFPRRDELETE.equals(packetInReason)) {
-                            flowRemovedReasonMap.put(FlowRemovedReason.OFPRRDELETE.getIntValue(), true);
-                        } else if (FlowRemovedReason.OFPRRGROUPDELETE.equals(packetInReason)) {
-                            flowRemovedReasonMap.put(FlowRemovedReason.OFPRRGROUPDELETE.getIntValue(), true);
-                        }
-                    }
-                    outBuffer.writeInt(ByteBufUtils.fillBitMaskFromMap(flowRemovedReasonMap));
-                }
+                serializeReasons(currentFlowRemovedMask.getMask(), outBuffer);
             }
         }
     }
+
+    private static void serializeReasons(final List<? extends Enumeration> reasons, final ByteBuf outBuffer) {
+        if (reasons != null) {
+            int bitmap = 0;
+            for (Enumeration reason : reasons) {
+                bitmap |= 1 << reason.getIntValue();
+            }
+            outBuffer.writeInt(bitmap);
+        }
+    }
 }