Remove Optional.ofNullable().map()
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / protocol / serialization / messages / FlowMessageSerializer.java
index fbe3f0c0eb955cae469a8b72e9c9311d03b23b78..3eb2ad90abdaaccfd30f2f101d4c6c4817f442b8 100644 (file)
@@ -12,10 +12,8 @@ import static java.util.Objects.requireNonNullElse;
 
 import io.netty.buffer.ByteBuf;
 import java.util.ArrayList;
-import java.util.List;
 import java.util.Map;
 import java.util.Objects;
-import java.util.Optional;
 import java.util.stream.Stream;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
@@ -284,52 +282,50 @@ public class FlowMessageSerializer extends AbstractMessageSerializer<FlowMessage
             updateSetVlanIdAction(final org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction
                 .list.Instruction insn) {
         if (insn.getInstruction() instanceof ApplyActionsCase applyActionsCase) {
-            final var actions = applyActionsCase.getApplyActions();
-            if (actions != null) {
-                final int[] offset = { 0 };
-
-                return Optional.of(actions)
-                    .flatMap(as -> Optional.ofNullable(as.nonnullAction()))
-                    .map(a -> a.values().stream()
-                        .sorted(OrderComparator.build())
-                        .flatMap(action -> {
-                            final List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112
-                                .action.list.Action> actionList = new ArrayList<>();
-
-                            // If current action is SetVlanId, insert PushVlan action before it and update order
-                            if (action.getAction() instanceof SetVlanIdActionCase setVlanIdActionCase) {
-                                actionList.add(new ActionBuilder()
-                                    .setAction(new PushVlanActionCaseBuilder()
-                                        .setPushVlanAction(new PushVlanActionBuilder()
-                                            .setCfi(PUSH_CFI)
-                                            .setVlanId(setVlanIdActionCase.getSetVlanIdAction().getVlanId())
-                                            .setEthernetType(PUSH_VLAN)
-                                            .setTag(PUSH_TAG)
-                                            .build())
-                                        .build())
-                                    .withKey(action.key())
-                                    .setOrder(action.getOrder() + offset[0])
-                                    .build());
-                                offset[0]++;
-                            }
+            final var applyActions = applyActionsCase.getApplyActions();
+            if (applyActions != null) {
+                final var actions = applyActions.getAction();
+                if (actions != null && !actions.isEmpty()) {
+                    final int[] offset = { 0 };
 
-                            // Update offset of action if there is any inserted PushVlan actions
-                            actionList.add(offset[0] > 0
-                                ? new ActionBuilder(action).setOrder(action.getOrder() + offset[0])
-                                    .withKey(new ActionKey(action.getOrder() + offset[0]))
-                                    .build()
-                                    : action);
-
-                            return actionList.stream();
-                        }))
-                    .map(as -> new InstructionBuilder(insn)
+                    return new InstructionBuilder(insn)
                         .setInstruction(new ApplyActionsCaseBuilder()
                             .setApplyActions(new ApplyActionsBuilder()
-                                .setAction(as.collect(BindingMap.toOrderedMap()))
+                                .setAction(actions.values().stream()
+                                    .sorted(OrderComparator.build())
+                                    .flatMap(action -> {
+                                        final var actionList = new ArrayList<org.opendaylight.yang.gen.v1.urn
+                                            .opendaylight.action.types.rev131112.action.list.Action>();
+
+                                        // If current action is SetVlanId, insert PushVlan action before it and update
+                                        // order
+                                        if (action.getAction() instanceof SetVlanIdActionCase setVlanIdActionCase) {
+                                            actionList.add(new ActionBuilder()
+                                                .setAction(new PushVlanActionCaseBuilder()
+                                                    .setPushVlanAction(new PushVlanActionBuilder()
+                                                        .setCfi(PUSH_CFI)
+                                                        .setVlanId(setVlanIdActionCase.getSetVlanIdAction().getVlanId())
+                                                        .setEthernetType(PUSH_VLAN)
+                                                        .setTag(PUSH_TAG)
+                                                        .build())
+                                                    .build())
+                                                .withKey(action.key())
+                                                .setOrder(action.getOrder() + offset[0])
+                                                .build());
+                                            offset[0]++;
+                                        }
+
+                                        // Update offset of action if there is any inserted PushVlan actions
+                                        actionList.add(offset[0] <= 0 ? action : new ActionBuilder(action)
+                                            .setOrder(action.getOrder() + offset[0])
+                                            .withKey(new ActionKey(action.getOrder() + offset[0]))
+                                            .build());
+                                        return actionList.stream();
+                                    }).collect(BindingMap.toOrderedMap()))
                                 .build())
                             .build())
-                        .build())
-                    .orElse(insn);
+                        .build();
+                }
             }
         }
         return insn;