Adjust to odlparent 3 Checkstyle settings
[genius.git] / mdsalutil / mdsalutil-api / src / main / java / org / opendaylight / genius / mdsalutil / MDSALUtil.java
index 7cad7eec3e5435e248c31b9b9535fe35091f323a..e87d0f8a89ce819c268c45fc3c789252b060e350 100644 (file)
@@ -10,8 +10,10 @@ package org.opendaylight.genius.mdsalutil;
 
 import com.google.common.base.Optional;
 import com.google.common.net.InetAddresses;
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import java.math.BigInteger;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -93,7 +95,8 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdenti
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-@SuppressWarnings("checkstyle:AbbreviationAsWordInName")
+// This class needs to be mocked
+@SuppressWarnings({ "checkstyle:AbbreviationAsWordInName", "checkstyle:FinalClass" })
 public class MDSALUtil {
 
     private static final Logger LOG = LoggerFactory.getLogger(MDSALUtil.class);
@@ -110,23 +113,28 @@ public class MDSALUtil {
             new ArrayList<>()).build();
     private static final Match EMPTY_MATCHES = new MatchBuilder().build();
 
+    private MDSALUtil() { }
+
     public static FlowEntity buildFlowEntity(BigInteger dpnId, short tableId, String flowId, int priority,
             String flowName, int idleTimeOut, int hardTimeOut, BigInteger cookie,
             List<? extends MatchInfoBase> listMatchInfoBase, List<InstructionInfo> listInstructionInfo) {
 
-        FlowEntity flowEntity = new FlowEntity(dpnId);
-
-        flowEntity.setTableId(tableId);
-        flowEntity.setFlowId(flowId);
-        flowEntity.setPriority(priority);
-        flowEntity.setFlowName(flowName);
-        flowEntity.setIdleTimeOut(idleTimeOut);
-        flowEntity.setHardTimeOut(hardTimeOut);
-        flowEntity.setCookie(cookie);
-        flowEntity.setMatchInfoList(listMatchInfoBase);
-        flowEntity.setInstructionInfoList(listInstructionInfo);
-
-        return flowEntity;
+        FlowEntityBuilder builder = new FlowEntityBuilder()
+            .setDpnId(dpnId)
+            .setTableId(tableId)
+            .setFlowId(flowId)
+            .setPriority(priority)
+            .setFlowName(flowName)
+            .setIdleTimeOut(idleTimeOut)
+            .setHardTimeOut(hardTimeOut)
+            .setCookie(cookie);
+        if (listMatchInfoBase != null) {
+            builder.addAllMatchInfoList(listMatchInfoBase);
+        }
+        if (listInstructionInfo != null) {
+            builder.addAllInstructionInfoList(listInstructionInfo);
+        }
+        return builder.build();
     }
 
     // TODO: CHECK IF THIS IS USED
@@ -174,14 +182,13 @@ public class MDSALUtil {
     public static GroupEntity buildGroupEntity(BigInteger dpnId, long groupId, String groupName, GroupTypes groupType,
             List<BucketInfo> listBucketInfo) {
 
-        GroupEntity groupEntity = new GroupEntity(dpnId);
-
+        GroupEntityBuilder groupEntity = new GroupEntityBuilder();
+        groupEntity.setDpnId(dpnId);
         groupEntity.setGroupId(groupId);
         groupEntity.setGroupName(groupName);
         groupEntity.setGroupType(groupType);
         groupEntity.setBucketInfoList(listBucketInfo);
-
-        return groupEntity;
+        return groupEntity.build();
     }
 
     public static Group buildGroup(long groupId, String groupName, GroupTypes groupType, Buckets buckets) {
@@ -373,7 +380,7 @@ public class MDSALUtil {
     }
 
     public static BigInteger getDpnIdFromNodeName(String mdsalNodeName) {
-        String dpId = mdsalNodeName.substring(mdsalNodeName.lastIndexOf(":") + 1);
+        String dpId = mdsalNodeName.substring(mdsalNodeName.lastIndexOf(':') + 1);
         return new BigInteger(dpId);
     }
 
@@ -382,7 +389,7 @@ public class MDSALUtil {
     }
 
     public static long getOfPortNumberFromPortName(String mdsalPortName) {
-        String portNumber = mdsalPortName.substring(mdsalPortName.lastIndexOf(":") + 1);
+        String portNumber = mdsalPortName.substring(mdsalPortName.lastIndexOf(':') + 1);
         return Long.parseLong(portNumber);
     }
 
@@ -392,8 +399,8 @@ public class MDSALUtil {
         }
         try {
             String ofPortName = nodeConnectorId.getValue();
-            return Long.parseLong(ofPortName.substring(ofPortName.indexOf(":") + 1,
-                    ofPortName.lastIndexOf(":")));
+            return Long.parseLong(ofPortName.substring(ofPortName.indexOf(':') + 1,
+                    ofPortName.lastIndexOf(':')));
         } catch (NumberFormatException | IndexOutOfBoundsException e) {
             LOG.error("NodeConnectorId not of expected format openflow:dpnid:portnum");
             return -1;
@@ -418,8 +425,17 @@ public class MDSALUtil {
         return buildApplyActionsInstruction(listAction, instructionKey);
     }
 
-    public static Instruction buildAndGetSetReg6ActionInstruction(int actionKey, int instructionKey,
-                                                                  int startOffSet, int endOffSet, long value) {
+
+    /**
+     * Create action to set REG6 to the given value.
+     *
+     * @param actionKey the action key.
+     * @param startOffSet the start offset.
+     * @param endOffSet the end offset.
+     * @param value the value.
+     * @return the action.
+     */
+    public static Action createSetReg6Action(int actionKey, int startOffSet, int endOffSet, long value) {
         NxRegLoadBuilder nxRegLoadBuilder = new NxRegLoadBuilder();
         Dst dst =  new DstBuilder()
                 .setDstChoice(new DstNxRegCaseBuilder().setNxReg(NxmNxReg6.class).build())
@@ -432,9 +448,14 @@ public class MDSALUtil {
         ab.setAction(new NxActionRegLoadNodesNodeTableFlowApplyActionsCaseBuilder()
                 .setNxRegLoad(nxRegLoadBuilder.build()).build());
         ab.setKey(new ActionKey(actionKey));
-        List<Action> listAction = new ArrayList<>();
-        listAction.add(ab.build());
-        return buildApplyActionsInstruction(listAction, instructionKey);
+        return ab.build();
+    }
+
+    public static Instruction buildAndGetSetReg6ActionInstruction(int actionKey, int instructionKey,
+                                                                  int startOffSet, int endOffSet, long value) {
+        return buildApplyActionsInstruction(
+                Collections.singletonList(createSetReg6Action(actionKey, startOffSet, endOffSet, value)),
+                instructionKey);
     }
 
     public static Instruction buildApplyActionsInstruction(List<Action> actions) {
@@ -452,15 +473,30 @@ public class MDSALUtil {
     }
 
     public static Instruction buildWriteActionsInstruction(List<Action> actions) {
+        return buildWriteActionsInstruction(actions, 0);
+    }
+
+    /**
+     * Build write actions instruction with the given actions and key.
+     *
+     * @param actions the actions.
+     * @param instructionKey the instruction key.
+     * @return the instruction.
+     */
+    public static Instruction buildWriteActionsInstruction(List<Action> actions, int instructionKey) {
         WriteActions writeActions = new WriteActionsBuilder().setAction(actions).build();
         WriteActionsCase writeActionsCase = new WriteActionsCaseBuilder().setWriteActions(writeActions).build();
         InstructionBuilder instructionBuilder = new InstructionBuilder();
 
         instructionBuilder.setInstruction(writeActionsCase);
-        instructionBuilder.setKey(new InstructionKey(0));
+        instructionBuilder.setKey(new InstructionKey(instructionKey));
         return instructionBuilder.build();
     }
 
+    public static Instruction buildInstruction(Instruction instruction, int instructionKey) {
+        return new InstructionBuilder(instruction).setKey(new InstructionKey(instructionKey)).build();
+    }
+
     public static List<Instruction> buildInstructionsDrop() {
         return buildInstructionsDrop(0);
     }
@@ -473,21 +509,17 @@ public class MDSALUtil {
         return mkInstructions;
     }
 
-
+    /**
+     * Build write actions instruction with the given actions and key.
+     *
+     * @param listAction the actions.
+     * @param instructionKey the instruction key.
+     * @return the instruction.
+     * @deprecated Use buildWriteActionsInstruction
+     */
+    @Deprecated
     public static Instruction getWriteActionsInstruction(List<Action> listAction, int instructionKey) {
-        WriteActions writeActions = new WriteActionsBuilder().setAction(listAction).build();
-        WriteActionsCase writeActionsCase = new WriteActionsCaseBuilder().setWriteActions(writeActions).build();
-        InstructionBuilder instructionBuilder = new InstructionBuilder();
-
-        instructionBuilder.setInstruction(writeActionsCase);
-        instructionBuilder.setKey(new InstructionKey(instructionKey));
-        return instructionBuilder.build();
-    }
-
-    public static Action buildAction(int actionKey, int instruction) {
-        return new ActionBuilder().setAction(
-                new PopVlanActionCaseBuilder().setPopVlanAction(new PopVlanActionBuilder().build()).build())
-                .setKey(new ActionKey(actionKey)).build();
+        return buildWriteActionsInstruction(listAction, instructionKey);
     }
 
     public static Instruction buildAndGetWriteMetadaInstruction(BigInteger metadata,
@@ -589,6 +621,8 @@ public class MDSALUtil {
         }
     }
 
+    // "Consider returning a zero length array rather than null" - too late to change behavior plus it's deprecated.
+    @SuppressFBWarnings("PZLA_PREFER_ZERO_LENGTH_ARRAYS")
     public static byte[] getMacAddressForNodeConnector(DataBroker broker,
             InstanceIdentifier<NodeConnector> nodeConnectorId)  {
         Optional<NodeConnector> optNc = MDSALDataStoreUtils.read(broker,
@@ -604,7 +638,7 @@ public class MDSALUtil {
 
     public static NodeId getNodeIdFromNodeConnectorId(NodeConnectorId ncId) {
         return new NodeId(ncId.getValue().substring(0,
-                ncId.getValue().lastIndexOf(":")));
+                ncId.getValue().lastIndexOf(':')));
     }
 
     public static String getInterfaceName(NodeConnectorRef ref, DataBroker dataBroker) {
@@ -615,14 +649,15 @@ public class MDSALUtil {
                 .child(Node.class, new NodeKey(nodeId))
                 .child(NodeConnector.class,
                         new NodeConnectorKey(nodeConnectorId)).build();
-        return read(dataBroker, LogicalDatastoreType.OPERATIONAL, ncIdentifier).transform(
-            nc -> nc.getAugmentation(FlowCapableNodeConnector.class).getName()).orNull();
+        return read(dataBroker, LogicalDatastoreType.OPERATIONAL, ncIdentifier).toJavaUtil().map(
+            nc -> nc.getAugmentation(FlowCapableNodeConnector.class)).map(FlowCapableNodeConnector::getName).orElse(
+                null);
     }
 
     public static NodeConnectorId getNodeConnectorId(DataBroker dataBroker,
             NodeConnectorRef ref) {
-        return ((Optional<NodeConnector>) read(dataBroker, LogicalDatastoreType.OPERATIONAL, ref.getValue())).transform(
-                NodeConnector::getId).orNull();
+        return ((Optional<NodeConnector>) read(dataBroker, LogicalDatastoreType.OPERATIONAL,
+                ref.getValue())).toJavaUtil().map(NodeConnector::getId).orElse(null);
     }
 
     public static Action createNxOfInPortAction(final int actionKey, final int inPortVal) {
@@ -642,5 +677,6 @@ public class MDSALUtil {
                new PopVlanActionCaseBuilder().setPopVlanAction(new PopVlanActionBuilder().build()).build())
                 .setKey(new ActionKey(actionKey)).setOrder(actionKey).build();
     }
+
 }