Adjust to odlparent 3 Checkstyle settings
[genius.git] / mdsalutil / mdsalutil-api / src / main / java / org / opendaylight / genius / mdsalutil / MDSALUtil.java
index 5e8329b51cf9863f77f671d65a358f71bdd60132..e87d0f8a89ce819c268c45fc3c789252b060e350 100644 (file)
@@ -9,20 +9,22 @@
 package org.opendaylight.genius.mdsalutil;
 
 import com.google.common.base.Optional;
-import com.google.common.util.concurrent.CheckedFuture;
+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;
-import java.util.concurrent.ExecutionException;
 import org.opendaylight.controller.liblldp.HexEncode;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
+import org.opendaylight.genius.mdsalutil.actions.ActionDrop;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PopVlanActionCaseBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetFieldCaseBuilder;
@@ -93,9 +95,12 @@ 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);
+
     public enum MdsalOp {  CREATION_OP, UPDATE_OP, REMOVAL_OP }
 
     public static final String NODE_PREFIX = "openflow";
@@ -103,34 +108,39 @@ public class MDSALUtil {
     public static final long WATCH_PORT = 0xffffffffL;
     public static final long WATCH_GROUP = 0xffffffffL;
     public static final String SEPARATOR = ":";
-    private static final Buckets EMPTY_Buckets = new BucketsBuilder().build();
-    private static final Instructions EMPTY_Instructions = new InstructionsBuilder().setInstruction(
+    private static final Buckets EMPTY_BUCKETS = new BucketsBuilder().build();
+    private static final Instructions EMPTY_INSTRUCTIONS = new InstructionsBuilder().setInstruction(
             new ArrayList<>()).build();
-    private static final Match EMPTY_Matches = new MatchBuilder().build();
-    private static final Logger logger = LoggerFactory.getLogger(MDSALUtil.class);
-
-    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;
+    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) {
+
+        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
     public static Flow buildFlow(short tableId, String flowId, int priority, String flowName, int idleTimeOut,
-            int hardTimeOut, BigInteger cookie, List<? extends MatchInfoBase> listMatchInfoBase, List<InstructionInfo> listInstructionInfo) {
+            int hardTimeOut, BigInteger cookie, List<? extends MatchInfoBase> listMatchInfoBase,
+            List<InstructionInfo> listInstructionInfo) {
         return MDSALUtil.buildFlow(tableId, flowId, priority, flowName, idleTimeOut, hardTimeOut, cookie,
                 listMatchInfoBase, listInstructionInfo, true);
     }
@@ -151,7 +161,8 @@ public class MDSALUtil {
     }
 
     public static Flow buildFlowNew(short tableId, String flowId, int priority, String flowName, int idleTimeOut,
-                                 int hardTimeOut, BigInteger cookie, List<? extends MatchInfoBase>  listMatchInfoBase, List<Instruction> listInstructionInfo) {
+            int hardTimeOut, BigInteger cookie, List<? extends MatchInfoBase> listMatchInfoBase,
+            List<Instruction> listInstructionInfo) {
         return MDSALUtil.buildFlowNew(tableId, flowId, priority, flowName, idleTimeOut, hardTimeOut, cookie,
                 listMatchInfoBase, listInstructionInfo, true);
     }
@@ -161,7 +172,8 @@ public class MDSALUtil {
                                   List<Instruction> listInstructionInfo, boolean isStrict) {
         FlowKey key = new FlowKey(new FlowId(flowId));
         return new FlowBuilder().setMatch(buildMatches(listMatchInfoBase)).setKey(key)
-                .setPriority(priority).setInstructions(new InstructionsBuilder().setInstruction(listInstructionInfo).build())
+                .setPriority(priority)
+                .setInstructions(new InstructionsBuilder().setInstruction(listInstructionInfo).build())
                 .setBarrier(false).setInstallHw(true).setHardTimeout(hardTimeOut).setIdleTimeout(idleTimeOut)
                 .setFlowName(flowName).setTableId(tableId).setStrict(isStrict)
                 .setCookie(new FlowCookie(cookie)).build();
@@ -170,23 +182,23 @@ 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) {
         GroupId groupIdentifier = new GroupId(groupId);
-        return new GroupBuilder().setGroupId(groupIdentifier).setKey(new GroupKey(groupIdentifier)).setGroupName(groupName)
-                .setGroupType(groupType).setBuckets(buckets).build();
+        return new GroupBuilder().setGroupId(groupIdentifier).setKey(new GroupKey(groupIdentifier))
+                .setGroupName(groupName).setGroupType(groupType).setBuckets(buckets).build();
     }
 
-    public static TransmitPacketInput getPacketOutDefault(List<ActionInfo> actionInfos, byte[] payload, BigInteger dpnId) {
+    public static TransmitPacketInput getPacketOutDefault(List<ActionInfo> actionInfos, byte[] payload,
+            BigInteger dpnId) {
         return new TransmitPacketInputBuilder()
                 .setAction(buildActions(actionInfos))
                 .setPayload(payload)
@@ -218,6 +230,23 @@ public class MDSALUtil {
                 .setIngress(ingress).setEgress(ingress).build();
     }
 
+    public static TransmitPacketInput getPacketOut(List<ActionInfo> actionInfos, byte[] payload, BigInteger dpnId,
+            NodeConnectorRef nodeConnRef) {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    public static TransmitPacketInput getPacketOut(List<Action> actions, byte[] payload, BigInteger dpnId) {
+        NodeConnectorRef ncRef = getDefaultNodeConnRef(dpnId);
+        return new TransmitPacketInputBuilder()
+                .setAction(actions)
+                .setPayload(payload)
+                .setNode(
+                        new NodeRef(InstanceIdentifier.builder(Nodes.class)
+                                .child(Node.class, new NodeKey(new NodeId("openflow:" + dpnId))).toInstance()))
+                .setIngress(ncRef).setEgress(ncRef).build();
+    }
+
     public static Action retrieveSetTunnelIdAction(BigInteger tunnelId, int actionKey) {
         return new ActionBuilder().setAction(
                 new SetFieldCaseBuilder().setSetField(new SetFieldBuilder().setTunnel(new TunnelBuilder()
@@ -234,18 +263,25 @@ public class MDSALUtil {
     }
 
     public static String longToIp(long ip, long mask) {
-        return ((ip & 0xFF000000) >> 3 * 8) + "." +
-               ((ip & 0x00FF0000) >> 2 * 8) + "." +
-               ((ip & 0x0000FF00) >>     8) + "." +
-                (ip & 0x000000FF) +
-                (mask == 0 ? "" : "/" + mask);
+        return ((ip & 0xFF000000) >> 3 * 8) + "."
+               + ((ip & 0x00FF0000) >> 2 * 8) + "."
+               + ((ip & 0x0000FF00) >>     8) + "."
+               + (ip & 0x000000FF)
+               + (mask == 0 ? "" : "/" + mask);
     }
 
+    public static BigInteger getBigIntIpFromIpAddress(IpAddress ipAddr) {
+        String ipString = ipAddr.getIpv4Address().getValue();
+        int ipInt = InetAddresses.coerceToInteger(InetAddresses.forString(ipString));
+        return BigInteger.valueOf(ipInt & 0xffffffffL);
+    }
 
-    public static Bucket buildBucket(List<Action> actionsList, int weight, int bucketId, long watchPort, long watchGroup) {
-        return  new BucketBuilder().setAction(actionsList).setWeight(weight)
-                .setWatchGroup(watchGroup).setWatchPort(watchPort).setBucketId(new BucketId(Long.valueOf(bucketId))).setKey(new BucketKey(new BucketId(Long.valueOf(bucketId)))).build();
 
+    public static Bucket buildBucket(List<Action> actionsList, int weight, int bucketId, long watchPort,
+            long watchGroup) {
+        return new BucketBuilder().setAction(actionsList).setWeight(weight).setWatchGroup(watchGroup)
+                .setWatchPort(watchPort).setBucketId(new BucketId(Long.valueOf(bucketId)))
+                .setKey(new BucketKey(new BucketId(Long.valueOf(bucketId)))).build();
     }
 
     public static Buckets buildBucketLists(List<Bucket> bucketList) {
@@ -253,7 +289,7 @@ public class MDSALUtil {
     }
 
     protected static Buckets buildBuckets(List<BucketInfo> listBucketInfo) {
-        long i = 0;
+        long index = 0;
         if (listBucketInfo != null) {
             BucketsBuilder bucketsBuilder = new BucketsBuilder();
             List<Bucket> bucketList = new ArrayList<>();
@@ -262,7 +298,7 @@ public class MDSALUtil {
                 BucketBuilder bucketBuilder = new BucketBuilder();
                 bucketBuilder.setAction(bucketInfo.buildActions());
                 bucketBuilder.setWeight(bucketInfo.getWeight());
-                bucketBuilder.setBucketId(new BucketId(i++));
+                bucketBuilder.setBucketId(new BucketId(index++));
                 bucketBuilder.setWeight(bucketInfo.getWeight()).setWatchPort(bucketInfo.getWatchPort())
                         .setWatchGroup(bucketInfo.getWatchGroup());
                 bucketList.add(bucketBuilder.build());
@@ -272,7 +308,7 @@ public class MDSALUtil {
             return bucketsBuilder.build();
         }
 
-        return EMPTY_Buckets;
+        return EMPTY_BUCKETS;
     }
 
     public static Instructions buildInstructions(List<InstructionInfo> listInstructionInfo) {
@@ -288,7 +324,7 @@ public class MDSALUtil {
             return new InstructionsBuilder().setInstruction(instructions).build();
         }
 
-        return EMPTY_Instructions;
+        return EMPTY_INSTRUCTIONS;
     }
 
     public static Match buildMatches(List<? extends MatchInfoBase> listMatchInfoBase) {
@@ -296,61 +332,46 @@ public class MDSALUtil {
             MatchBuilder matchBuilder = new MatchBuilder();
             Map<Class<?>, Object> mapMatchBuilder = new HashMap<>();
 
-            for (MatchInfoBase MatchInfoBase : listMatchInfoBase) {
-                MatchInfoBase.createInnerMatchBuilder(mapMatchBuilder);
+            for (MatchInfoBase matchInfoBase : listMatchInfoBase) {
+                matchInfoBase.createInnerMatchBuilder(mapMatchBuilder);
             }
 
-            for (MatchInfoBase MatchInfoBase : listMatchInfoBase) {
-                MatchInfoBase.setMatch(matchBuilder, mapMatchBuilder);
+            for (MatchInfoBase matchInfoBase : listMatchInfoBase) {
+                matchInfoBase.setMatch(matchBuilder, mapMatchBuilder);
             }
 
             return matchBuilder.build();
         }
 
-        return EMPTY_Matches;
+        return EMPTY_MATCHES;
     }
 
     // TODO: Check the port const
-    public static NodeConnectorRef getDefaultNodeConnRef(BigInteger nDpId) {
-        return getNodeConnRef(NODE_PREFIX + SEPARATOR + nDpId, "0xfffffffd");
-    }
-
-    public static NodeConnectorRef getNodeConnRef(BigInteger nDpId, String port) {
-        return getNodeConnRef(NODE_PREFIX + SEPARATOR + nDpId, port);
-    }
-
-    public static NodeConnectorRef getNodeConnRef(String sNodeId, String port) {
-        String sNodeConnectorKey;
-        StringBuilder sbTmp;
-        NodeId nodeId;
-        NodeKey nodeKey;
-        NodeConnectorId nodeConnectorId;
-        NodeConnectorKey nodeConnectorKey;
-        InstanceIdentifierBuilder<Nodes> nodesInstanceIdentifierBuilder;
-        InstanceIdentifierBuilder<Node> nodeInstanceIdentifierBuilder;
-        InstanceIdentifierBuilder<NodeConnector> nodeConnectorInstanceIdentifierBuilder;
-        InstanceIdentifier<NodeConnector> nodeConnectorInstanceIdentifier;
-        NodeConnectorRef nodeConnectorRef;
-
-        sbTmp = new StringBuilder();
-
-        sbTmp.append(sNodeId);
-        sbTmp.append(SEPARATOR);
-        sbTmp.append(port);
-
-        sNodeConnectorKey = sbTmp.toString();
-        nodeConnectorId = new NodeConnectorId(sNodeConnectorKey);
-        nodeConnectorKey = new NodeConnectorKey(nodeConnectorId);
-
-        nodeId = new NodeId(sNodeId);
-        nodeKey = new NodeKey(nodeId);
-
-        nodesInstanceIdentifierBuilder = InstanceIdentifier.builder(Nodes.class);
-        nodeInstanceIdentifierBuilder = nodesInstanceIdentifierBuilder.child(Node.class, nodeKey);
-        nodeConnectorInstanceIdentifierBuilder = nodeInstanceIdentifierBuilder.child(
-                NodeConnector.class, nodeConnectorKey);
-        nodeConnectorInstanceIdentifier = nodeConnectorInstanceIdentifierBuilder.toInstance();
-        nodeConnectorRef = new NodeConnectorRef(nodeConnectorInstanceIdentifier);
+    public static NodeConnectorRef getDefaultNodeConnRef(BigInteger dpId) {
+        return getNodeConnRef(NODE_PREFIX + SEPARATOR + dpId, "0xfffffffd");
+    }
+
+    public static NodeConnectorRef getNodeConnRef(BigInteger dpId, String port) {
+        return getNodeConnRef(NODE_PREFIX + SEPARATOR + dpId, port);
+    }
+
+    public static NodeConnectorRef getNodeConnRef(String nodeId, String port) {
+        StringBuilder sb = new StringBuilder();
+        sb.append(nodeId);
+        sb.append(SEPARATOR);
+        sb.append(port);
+        String nodeConnectorKeyAsString = sb.toString();
+        NodeConnectorId nodeConnectorId = new NodeConnectorId(nodeConnectorKeyAsString);
+        NodeConnectorKey nodeConnectorKey = new NodeConnectorKey(nodeConnectorId);
+
+        NodeKey nodeKey = new NodeKey(new NodeId(nodeId));
+        InstanceIdentifierBuilder<Node> nodeInstanceIdentifierBuilder
+            = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey);
+        InstanceIdentifierBuilder<NodeConnector> nodeConnectorInstanceIdentifierBuilder
+            = nodeInstanceIdentifierBuilder.child(NodeConnector.class, nodeConnectorKey);
+        InstanceIdentifier<NodeConnector> nodeConnectorInstanceIdentifier
+            = nodeConnectorInstanceIdentifierBuilder.toInstance();
+        NodeConnectorRef nodeConnectorRef = new NodeConnectorRef(nodeConnectorInstanceIdentifier);
         return nodeConnectorRef;
     }
 
@@ -358,34 +379,40 @@ public class MDSALUtil {
         return getDpnIdFromNodeName(nodeId.getValue());
     }
 
-    public static BigInteger getDpnIdFromNodeName(String sMdsalNodeName) {
-        String sDpId = sMdsalNodeName.substring(sMdsalNodeName.lastIndexOf(":") + 1);
-        return new BigInteger(sDpId);
+    public static BigInteger getDpnIdFromNodeName(String mdsalNodeName) {
+        String dpId = mdsalNodeName.substring(mdsalNodeName.lastIndexOf(':') + 1);
+        return new BigInteger(dpId);
     }
 
     public static long getOfPortNumberFromPortName(NodeConnectorId nodeConnectorId) {
         return getOfPortNumberFromPortName(nodeConnectorId.getValue());
     }
 
+    public static long getOfPortNumberFromPortName(String mdsalPortName) {
+        String portNumber = mdsalPortName.substring(mdsalPortName.lastIndexOf(':') + 1);
+        return Long.parseLong(portNumber);
+    }
+
     public static long getDpnIdFromPortName(NodeConnectorId nodeConnectorId) {
+        if (nodeConnectorId == null || nodeConnectorId.getValue() == null) {
+            return -1;
+        }
         try {
             String ofPortName = nodeConnectorId.getValue();
-            return Long.parseLong(ofPortName.substring(ofPortName.indexOf(":")+1,
-                    ofPortName.lastIndexOf(":")));
-        } catch (Exception e) {
-            logger.error("NodeConnectorId not of expected format openflow:dpnid:portnum");
+            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;
         }
     }
 
-    public static long getOfPortNumberFromPortName(String sMdsalPortName) {
-        String sPortNumber = sMdsalPortName.substring(sMdsalPortName.lastIndexOf(":") + 1);
-        return Long.parseLong(sPortNumber);
-    }
-
-    public static TransmitPacketInput getPacketOut(List<ActionInfo> actionInfos, byte[] payload, BigInteger dpnId,
-                    NodeConnectorRef nodeConnRef) {
-        // TODO Auto-generated method stub
+    public static BigInteger getDpnId(String datapathId) {
+        if (datapathId != null) {
+            String dpIdStr = datapathId.replace(":", "");
+            BigInteger dpnId =  new BigInteger(dpIdStr, 16);
+            return dpnId;
+        }
         return null;
     }
 
@@ -393,13 +420,22 @@ public class MDSALUtil {
         Action popVlanAction = new ActionBuilder().setAction(
                 new PopVlanActionCaseBuilder().setPopVlanAction(new PopVlanActionBuilder().build()).build())
                 .setKey(new ActionKey(actionKey)).build();
-        List<Action> listAction = new ArrayList<> ();
+        List<Action> listAction = new ArrayList<>();
         listAction.add(popVlanAction);
         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())
@@ -409,64 +445,81 @@ public class MDSALUtil {
         nxRegLoadBuilder.setDst(dst);
         nxRegLoadBuilder.setValue(new BigInteger(Long.toString(value)));
         ActionBuilder ab = new ActionBuilder();
-        ab.setAction(new NxActionRegLoadNodesNodeTableFlowApplyActionsCaseBuilder().setNxRegLoad(nxRegLoadBuilder.build()).build());
+        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) {
         return buildApplyActionsInstruction(actions, 0);
     }
 
-    public static Instruction buildWriteActionsInstruction(List<Action> actions) {
-        WriteActions writeActions = new WriteActionsBuilder().setAction(actions).build();
-        WriteActionsCase writeActionsCase = new WriteActionsCaseBuilder().setWriteActions(writeActions).build();
+    public static Instruction buildApplyActionsInstruction(List<Action> listAction, int instructionKey) {
+        ApplyActions applyActions = new ApplyActionsBuilder().setAction(listAction).build();
+        ApplyActionsCase applyActionsCase = new ApplyActionsCaseBuilder().setApplyActions(applyActions).build();
         InstructionBuilder instructionBuilder = new InstructionBuilder();
 
-        instructionBuilder.setInstruction(writeActionsCase);
-        instructionBuilder.setKey(new InstructionKey(0));
+        instructionBuilder.setInstruction(applyActionsCase);
+        instructionBuilder.setKey(new InstructionKey(instructionKey));
         return instructionBuilder.build();
     }
 
-    public static Instruction buildApplyActionsInstruction(List<Action> listAction, int instructionKey) {
-        ApplyActions applyActions = new ApplyActionsBuilder().setAction(listAction).build();
-        ApplyActionsCase applyActionsCase = new ApplyActionsCaseBuilder().setApplyActions(applyActions).build();
+    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(applyActionsCase);
+        instructionBuilder.setInstruction(writeActionsCase);
         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);
     }
 
     public static List<Instruction> buildInstructionsDrop(int instructionKey) {
         List<Instruction> mkInstructions = new ArrayList<>();
-        List <Action> actionsInfos = new ArrayList <> ();
-        actionsInfos.add(new ActionInfo(ActionType.drop_action, new String[]{}).buildAction());
+        List<Action> actionsInfos = new ArrayList<>();
+        actionsInfos.add(new ActionDrop().buildAction());
         mkInstructions.add(getWriteActionsInstruction(actionsInfos, instructionKey));
         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,
@@ -513,52 +566,68 @@ public class MDSALUtil {
         }
     }
 
+    /**
+     * Deprecated write.
+     *
+     * @deprecated Use
+     *             {@link SingleTransactionDataBroker#syncWrite(
+     *                     DataBroker, LogicalDatastoreType, InstanceIdentifier, DataObject)}
+     */
+    @Deprecated
     public static <T extends DataObject> void syncWrite(DataBroker broker,
                                                         LogicalDatastoreType datastoreType, InstanceIdentifier<T> path,
                                                         T data) {
-        WriteTransaction tx = broker.newWriteOnlyTransaction();
-        tx.put(datastoreType, path, data, true);
-        CheckedFuture<Void, TransactionCommitFailedException> futures = tx.submit();
         try {
-            futures.get();
-        } catch (InterruptedException | ExecutionException e) {
-            logger.error("Error writing to datastore (path, data) : ({}, {})", path, data);
-            throw new RuntimeException(e.getMessage());
+            SingleTransactionDataBroker.syncWrite(broker, datastoreType, path, data);
+        } catch (TransactionCommitFailedException e) {
+            LOG.error("Error writing to datastore (path, data) : ({}, {})", path, data);
+            throw new RuntimeException(e);
         }
     }
 
+    /**
+     * Deprecated update.
+     *
+     * @deprecated Use
+     *             {@link SingleTransactionDataBroker#syncUpdate(
+     *                          DataBroker, LogicalDatastoreType, InstanceIdentifier, DataObject)}
+     */
+    @Deprecated
     public static <T extends DataObject> void syncUpdate(DataBroker broker,
                                                          LogicalDatastoreType datastoreType, InstanceIdentifier<T> path,
                                                          T data) {
-        WriteTransaction tx = broker.newWriteOnlyTransaction();
-        tx.merge(datastoreType, path, data, true);
-        CheckedFuture<Void, TransactionCommitFailedException> futures = tx.submit();
         try {
-            futures.get();
-        } catch (InterruptedException | ExecutionException e) {
-            logger.error("Error writing to datastore (path, data) : ({}, {})", path, data);
-            throw new RuntimeException(e.getMessage());
+            SingleTransactionDataBroker.syncUpdate(broker, datastoreType, path, data);
+        } catch (TransactionCommitFailedException e) {
+            LOG.error("Error writing to datastore (path, data) : ({}, {})", path, data);
+            throw new RuntimeException(e);
         }
     }
 
+    /**
+     * Deprecated delete.
+     *
+     * @deprecated Use
+     *             {@link SingleTransactionDataBroker#syncDelete(DataBroker, LogicalDatastoreType, InstanceIdentifier)}
+     */
+    @Deprecated
     public static <T extends DataObject> void syncDelete(DataBroker broker,
-                                                         LogicalDatastoreType datastoreType, InstanceIdentifier<T> obj) {
-        WriteTransaction tx = broker.newWriteOnlyTransaction();
-        tx.delete(datastoreType, obj);
-        CheckedFuture<Void, TransactionCommitFailedException> futures = tx.submit();
+            LogicalDatastoreType datastoreType, InstanceIdentifier<T> path) {
         try {
-            futures.get();
-        } catch (InterruptedException | ExecutionException e) {
-            logger.error("Error deleting from datastore (path) : ({})", obj);
-            throw new RuntimeException(e.getMessage());
+            SingleTransactionDataBroker.syncDelete(broker, datastoreType, path);
+        } catch (TransactionCommitFailedException e) {
+            LOG.error("Error deleting from datastore (path) : ({})", path, e);
+            throw new RuntimeException(e);
         }
     }
 
+    // "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,
                 LogicalDatastoreType.OPERATIONAL, nodeConnectorId);
-        if(optNc.isPresent()) {
+        if (optNc.isPresent()) {
             NodeConnector nc = optNc.get();
             FlowCapableNodeConnector fcnc = nc.getAugmentation(FlowCapableNodeConnector.class);
             MacAddress macAddress = fcnc.getHardwareAddress();
@@ -569,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) {
@@ -580,35 +649,26 @@ 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();
-    }
-
-    public static TransmitPacketInput getPacketOut(List<Action> actions, byte[] payload, BigInteger dpnId) {
-        NodeConnectorRef ncRef = getDefaultNodeConnRef(dpnId);
-        return new TransmitPacketInputBuilder()
-                .setAction(actions)
-                .setPayload(payload)
-                .setNode(
-                        new NodeRef(InstanceIdentifier.builder(Nodes.class)
-                                .child(Node.class, new NodeKey(new NodeId("openflow:" + dpnId))).toInstance()))
-                .setIngress(ncRef).setEgress(ncRef).build();
+        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) {
-
-        NxRegLoad r = new NxRegLoadBuilder().setDst(new DstBuilder().setDstChoice(new DstNxOfInPortCaseBuilder().setOfInPort(Boolean.TRUE).build())
-                .setStart(0).setEnd(15).build()).setValue(BigInteger.valueOf(inPortVal)).build();
+        NxRegLoad regLoad = new NxRegLoadBuilder()
+                .setDst(new DstBuilder().setDstChoice(new DstNxOfInPortCaseBuilder().setOfInPort(Boolean.TRUE).build())
+                        .setStart(0).setEnd(15).build())
+                .setValue(BigInteger.valueOf(inPortVal)).build();
         ActionBuilder abExt = new ActionBuilder();
         abExt.setKey(new ActionKey(actionKey));
         abExt.setOrder(actionKey);
-        abExt.setAction(new NxActionRegLoadNodesNodeTableFlowApplyActionsCaseBuilder().setNxRegLoad(r).build());
+        abExt.setAction(new NxActionRegLoadNodesNodeTableFlowApplyActionsCaseBuilder().setNxRegLoad(regLoad).build());
         return abExt.build();
     }
 
@@ -617,5 +677,6 @@ public class MDSALUtil {
                new PopVlanActionCaseBuilder().setPopVlanAction(new PopVlanActionBuilder().build()).build())
                 .setKey(new ActionKey(actionKey)).setOrder(actionKey).build();
     }
+
 }