From 734f0886b8caf65dd749136ef8707b498d4e80fb Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Thu, 8 Feb 2024 21:31:15 +0100 Subject: [PATCH] Remove Optional.ofNullable().map() This is the final bit in conversion making it pretty clear what is going on. Change-Id: Ibf5dfdba47773890dc06da4f1a5662f32adfa236 Signed-off-by: Robert Varga --- .../messages/FlowMessageSerializer.java | 82 +++++++++---------- 1 file changed, 39 insertions(+), 43 deletions(-) diff --git a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/protocol/serialization/messages/FlowMessageSerializer.java b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/protocol/serialization/messages/FlowMessageSerializer.java index fbe3f0c0eb..3eb2ad90ab 100644 --- a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/protocol/serialization/messages/FlowMessageSerializer.java +++ b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/protocol/serialization/messages/FlowMessageSerializer.java @@ -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 Optional.ofNullable(as.nonnullAction())) - .map(a -> a.values().stream() - .sorted(OrderComparator.build()) - .flatMap(action -> { - final List 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(); + + // 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; -- 2.36.6