Make nullToEmpty return immutable empty lists
[genius.git] / interfacemanager / interfacemanager-impl / src / main / java / org / opendaylight / genius / interfacemanager / IfmUtil.java
index 107c30e3dc3709bb093537b7560d21250a654a4c..92d2ce123d11db13f36b183b6416799d08c7c7d6 100755 (executable)
@@ -7,6 +7,9 @@
  */
 package org.opendaylight.genius.interfacemanager;
 
+import static java.util.Collections.emptyList;
+import static org.opendaylight.controller.md.sal.binding.api.WriteTransaction.CREATE_MISSING_PARENTS;
+import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
 import static org.opendaylight.genius.interfacemanager.globals.InterfaceInfo.InterfaceType.GRE_TRUNK_INTERFACE;
 import static org.opendaylight.genius.interfacemanager.globals.InterfaceInfo.InterfaceType.LOGICAL_GROUP_INTERFACE;
 import static org.opendaylight.genius.interfacemanager.globals.InterfaceInfo.InterfaceType.MPLS_OVER_GRE;
@@ -16,23 +19,26 @@ import static org.opendaylight.genius.interfacemanager.globals.InterfaceInfo.Int
 import com.google.common.base.Optional;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.util.concurrent.ListenableFuture;
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import java.math.BigInteger;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
 import org.apache.commons.lang3.BooleanUtils;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
-import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.genius.datastoreutils.DataStoreJobCoordinator;
+import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
+import org.opendaylight.genius.infra.Datastore.Configuration;
+import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
+import org.opendaylight.genius.infra.TypedWriteTransaction;
 import org.opendaylight.genius.interfacemanager.commons.InterfaceManagerCommonUtils;
-import org.opendaylight.genius.interfacemanager.commons.InterfaceMetaUtils;
 import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
 import org.opendaylight.genius.interfacemanager.globals.VlanInterfaceInfo;
-import org.opendaylight.genius.interfacemanager.servicebindings.flowbased.utilities.FlowBasedServicesUtils;
 import org.opendaylight.genius.mdsalutil.ActionInfo;
 import org.opendaylight.genius.mdsalutil.MDSALUtil;
 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
@@ -46,8 +52,9 @@ import org.opendaylight.genius.mdsalutil.actions.ActionSetFieldTunnelId;
 import org.opendaylight.genius.mdsalutil.actions.ActionSetFieldVlanVid;
 import org.opendaylight.genius.mdsalutil.actions.ActionSetTunnelDestinationIp;
 import org.opendaylight.genius.mdsalutil.actions.ActionSetTunnelSourceIp;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.L2vlan;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.Tunnel;
+import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev170119.L2vlan;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev170119.Tunnel;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
@@ -66,9 +73,9 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdPools;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.ReleaseIdInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.ReleaseIdInputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.ReleaseIdOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.IdPool;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.IdPoolKey;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406.bridge.ref.info.BridgeRefEntry;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfL2vlan;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfL2vlan.L2vlanMode;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfTunnel;
@@ -90,8 +97,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeCon
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg6;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathId;
-import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
-import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
@@ -99,10 +104,15 @@ import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class IfmUtil {
+public final class IfmUtil {
+
     private static final Logger LOG = LoggerFactory.getLogger(IfmUtil.class);
     private static final int INVALID_ID = 0;
 
+    private IfmUtil() {
+        throw new IllegalStateException("Utility class");
+    }
+
     private static final ImmutableMap<Class<? extends TunnelTypeBase>, InterfaceInfo.InterfaceType>
         TUNNEL_TYPE_MAP = new ImmutableMap.Builder<Class<? extends TunnelTypeBase>, InterfaceInfo.InterfaceType>()
             .put(TunnelTypeGre.class, GRE_TRUNK_INTERFACE).put(TunnelTypeMplsOverGre.class, MPLS_OVER_GRE)
@@ -111,11 +121,14 @@ public class IfmUtil {
             .build();
 
     public static BigInteger getDpnFromNodeConnectorId(NodeConnectorId portId) {
+        return new BigInteger(getDpnStringFromNodeConnectorId(portId));
+    }
+
+    public static String getDpnStringFromNodeConnectorId(NodeConnectorId portId) {
         /*
          * NodeConnectorId is of form 'openflow:dpnid:portnum'
          */
-        String[] split = portId.getValue().split(IfmConstants.OF_URI_SEPARATOR);
-        return new BigInteger(split[1]);
+        return portId.getValue().split(IfmConstants.OF_URI_SEPARATOR)[1];
     }
 
     public static BigInteger getDpnFromInterface(
@@ -170,10 +183,7 @@ public class IfmUtil {
                         new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
                             .ietf.interfaces.rev140508.interfaces.state.InterfaceKey(
                                 interfaceName));
-        InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
-            .ietf.interfaces.rev140508.interfaces.state.Interface> id = idBuilder
-                .build();
-        return id;
+        return idBuilder.build();
     }
 
     public static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
@@ -195,6 +205,14 @@ public class IfmUtil {
         return 0;
     }
 
+    /**
+     * Synchronous blocking read from data store.
+     *
+     * @deprecated Use
+     * {@link SingleTransactionDataBroker#syncReadOptional(DataBroker, LogicalDatastoreType, InstanceIdentifier)}
+     *             instead of this.
+     */
+    @Deprecated
     public static <T extends DataObject> Optional<T> read(LogicalDatastoreType datastoreType,
             InstanceIdentifier<T> path, DataBroker broker) {
         try (ReadOnlyTransaction tx = broker.newReadOnlyTransaction()) {
@@ -206,9 +224,9 @@ public class IfmUtil {
     }
 
     public static List<Action> getEgressActionsForInterface(String interfaceName, Long tunnelKey, Integer actionKey,
-            DataBroker dataBroker, Boolean isDefaultEgress) {
+            InterfaceManagerCommonUtils interfaceUtils, Boolean isDefaultEgress) {
         List<ActionInfo> listActionInfo = getEgressActionInfosForInterface(interfaceName, tunnelKey,
-                actionKey == null ? 0 : actionKey, dataBroker, isDefaultEgress);
+                actionKey == null ? 0 : actionKey, interfaceUtils, isDefaultEgress);
         List<Action> actionsList = new ArrayList<>();
         for (ActionInfo actionInfo : listActionInfo) {
             actionsList.add(actionInfo.buildAction());
@@ -217,10 +235,10 @@ public class IfmUtil {
     }
 
     public static List<Instruction> getEgressInstructionsForInterface(String interfaceName, Long tunnelKey,
-            DataBroker dataBroker, Boolean isDefaultEgress) {
+            InterfaceManagerCommonUtils interfaceUtils, Boolean isDefaultEgress) {
         List<Instruction> instructions = new ArrayList<>();
         List<Action> actionList = MDSALUtil.buildActions(
-                getEgressActionInfosForInterface(interfaceName, tunnelKey, 0, dataBroker, isDefaultEgress));
+                getEgressActionInfosForInterface(interfaceName, tunnelKey, 0, interfaceUtils, isDefaultEgress));
         instructions.add(MDSALUtil.buildApplyActionsInstruction(actionList));
         return instructions;
     }
@@ -238,8 +256,8 @@ public class IfmUtil {
     }
 
     public static List<ActionInfo> getEgressActionInfosForInterface(String interfaceName, int actionKeyStart,
-            DataBroker dataBroker, Boolean isDefaultEgress) {
-        return getEgressActionInfosForInterface(interfaceName, null, actionKeyStart, dataBroker, isDefaultEgress);
+            InterfaceManagerCommonUtils interfaceUtils, Boolean isDefaultEgress) {
+        return getEgressActionInfosForInterface(interfaceName, null, actionKeyStart, interfaceUtils, isDefaultEgress);
     }
 
     /**
@@ -252,20 +270,19 @@ public class IfmUtil {
      *            Optional.
      * @param actionKeyStart
      *            action key
-     * @param dataBroker
-     *            databroker
+     * @param interfaceUtils
+     *            InterfaceManagerCommonUtils
      * @return list of actions
      */
     public static List<ActionInfo> getEgressActionInfosForInterface(String interfaceName, Long tunnelKey,
-            int actionKeyStart, DataBroker dataBroker, Boolean isDefaultEgress) {
-        Interface interfaceInfo = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(new InterfaceKey(interfaceName),
-                dataBroker);
+            int actionKeyStart, InterfaceManagerCommonUtils interfaceUtils, Boolean isDefaultEgress) {
+        Interface interfaceInfo = interfaceUtils.getInterfaceFromConfigDS(new InterfaceKey(interfaceName));
         if (interfaceInfo == null) {
             throw new NullPointerException("Interface information not present in config DS for " + interfaceName);
         }
         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
-            .ietf.interfaces.rev140508.interfaces.state.Interface ifState = InterfaceManagerCommonUtils
-                .getInterfaceState(interfaceName, dataBroker);
+                .ietf.interfaces.rev140508.interfaces.state.Interface ifState =
+            interfaceUtils.getInterfaceState(interfaceName);
         if (ifState == null) {
             throw new NullPointerException("Interface information not present in oper DS for " + interfaceName);
         }
@@ -288,10 +305,12 @@ public class IfmUtil {
      * @param actionKeyStart the start for the first key assigned for the new actions
      * @param isDefaultEgress if it is the default egress
      * @param ifIndex interface index
+     * @param groupId group Id
      * @return list of actions for the interface
      */
     // The following suppression is for javac, not for checkstyle
     @SuppressWarnings("fallthrough")
+    @SuppressFBWarnings("SF_SWITCH_FALLTHROUGH")
     public static List<ActionInfo> getEgressActionInfosForInterface(Interface interfaceInfo, String portNo,
             InterfaceInfo.InterfaceType ifaceType, Long tunnelKey, int actionKeyStart, boolean isDefaultEgress,
             int ifIndex, long groupId) {
@@ -316,7 +335,7 @@ public class IfmUtil {
                     }
                 } else {
                     // For OF Tunnels default egress actions need to set tunnelIps
-                    IfTunnel ifTunnel = interfaceInfo.getAugmentation(IfTunnel.class);
+                    IfTunnel ifTunnel = interfaceInfo.augmentation(IfTunnel.class);
                     if (BooleanUtils.isTrue(ifTunnel.isTunnelRemoteIpFlow()
                             && ifTunnel.getTunnelDestination() != null)) {
                         result.add(new ActionSetTunnelDestinationIp(actionKeyStart++, ifTunnel.getTunnelDestination()));
@@ -329,8 +348,8 @@ public class IfmUtil {
                 // fall through
             case VLAN_INTERFACE:
                 if (isDefaultEgress) {
-                    IfL2vlan vlanIface = interfaceInfo.getAugmentation(IfL2vlan.class);
-                    LOG.trace("L2Vlan: {}", vlanIface);
+                    IfL2vlan vlanIface = interfaceInfo.augmentation(IfL2vlan.class);
+                    LOG.trace("get egress actions for l2vlan interface: {}", vlanIface);
                     boolean isVlanTransparent = false;
                     int vlanVid = 0;
                     if (vlanIface != null) {
@@ -365,11 +384,11 @@ public class IfmUtil {
         long regValue = MetaDataUtil.getReg6ValueForLPortDispatcher(ifIndex, NwConstants.DEFAULT_SERVICE_INDEX);
         result.add(new ActionRegLoad(actionKeyStart++, NxmNxReg6.class, IfmConstants.REG6_START_INDEX,
                 IfmConstants.REG6_END_INDEX, regValue));
-        result.add(new ActionNxResubmit(actionKeyStart++, NwConstants.EGRESS_LPORT_DISPATCHER_TABLE));
+        result.add(new ActionNxResubmit(actionKeyStart, NwConstants.EGRESS_LPORT_DISPATCHER_TABLE));
     }
 
     public static NodeId getNodeIdFromNodeConnectorId(NodeConnectorId ncId) {
-        return new NodeId(ncId.getValue().substring(0, ncId.getValue().lastIndexOf(":")));
+        return new NodeId(ncId.getValue().substring(0, ncId.getValue().lastIndexOf(':')));
     }
 
     public static BigInteger[] mergeOpenflowMetadataWriteInstructions(List<Instruction> instructions) {
@@ -411,10 +430,10 @@ public class IfmUtil {
     public static void releaseId(IdManagerService idManager, String poolName, String idKey) {
         ReleaseIdInput idInput = new ReleaseIdInputBuilder().setPoolName(poolName).setIdKey(idKey).build();
         try {
-            Future<RpcResult<Void>> result = idManager.releaseId(idInput);
-            RpcResult<Void> rpcResult = result.get();
+            ListenableFuture<RpcResult<ReleaseIdOutput>> result = idManager.releaseId(idInput);
+            RpcResult<ReleaseIdOutput> rpcResult = result.get();
             if (!rpcResult.isSuccessful()) {
-                LOG.warn("RPC Call to release Id {} with Key {} returned with Errors {}", idKey, rpcResult.getErrors());
+                LOG.warn("RPC Call to release Id with Key {} returned with Errors {}", idKey, rpcResult.getErrors());
             }
         } catch (InterruptedException | ExecutionException e) {
             LOG.warn("Exception when releasing Id for key {}", idKey, e);
@@ -431,10 +450,6 @@ public class IfmUtil {
         return null;
     }
 
-    public static NodeConnectorId getNodeConnectorIdFromInterface(String interfaceName, DataBroker dataBroker) {
-        return FlowBasedServicesUtils.getNodeConnectorIdFromInterface(interfaceName, dataBroker);
-    }
-
     public static NodeConnectorId getNodeConnectorIdFromInterface(
             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
                 .ietf.interfaces.rev140508.interfaces.state.Interface ifState) {
@@ -455,7 +470,7 @@ public class IfmUtil {
         if (ifType.isAssignableFrom(L2vlan.class)) {
             interfaceType = VLAN_INTERFACE;
         } else if (ifType.isAssignableFrom(Tunnel.class)) {
-            IfTunnel ifTunnel = iface.getAugmentation(IfTunnel.class);
+            IfTunnel ifTunnel = iface.augmentation(IfTunnel.class);
             Class<? extends org.opendaylight.yang.gen.v1.urn.opendaylight
                     .genius.interfacemanager.rev160406.TunnelTypeBase> tunnelType = ifTunnel
                     .getTunnelInterfaceType();
@@ -465,11 +480,11 @@ public class IfmUtil {
         return interfaceType;
     }
 
-    public static VlanInterfaceInfo getVlanInterfaceInfo(String interfaceName, Interface iface, BigInteger dpId) {
+    public static VlanInterfaceInfo getVlanInterfaceInfo(Interface iface, BigInteger dpId) {
         short vlanId = 0;
         String portName = null;
-        IfL2vlan vlanIface = iface.getAugmentation(IfL2vlan.class);
-        ParentRefs parentRefs = iface.getAugmentation(ParentRefs.class);
+        IfL2vlan vlanIface = iface.augmentation(IfL2vlan.class);
+        ParentRefs parentRefs = iface.augmentation(ParentRefs.class);
         if (parentRefs != null && parentRefs.getParentInterface() != null) {
             portName = parentRefs.getParentInterface();
         } else {
@@ -517,12 +532,12 @@ public class IfmUtil {
         return new PhysAddress(southboundMacAddress);
     }
 
-    public static void updateInterfaceParentRef(WriteTransaction writeTransaction, String interfaceName,
+    public static void updateInterfaceParentRef(TypedWriteTransaction<Configuration> tx, String interfaceName,
             String parentInterface) {
         InstanceIdentifier<ParentRefs> parentRefIdentifier = InstanceIdentifier.builder(Interfaces.class)
                 .child(Interface.class, new InterfaceKey(interfaceName)).augmentation(ParentRefs.class).build();
         ParentRefs parentRefs = new ParentRefsBuilder().setParentInterface(parentInterface).build();
-        writeTransaction.merge(LogicalDatastoreType.CONFIGURATION, parentRefIdentifier, parentRefs);
+        tx.merge(parentRefIdentifier, parentRefs);
         LOG.debug(
                 "Updating parentRefInterface for interfaceName {}. "
                         + "interfaceKey {}, with parentRef augmentation pointing to {}",
@@ -536,44 +551,34 @@ public class IfmUtil {
                 .child(BoundServices.class, new BoundServicesKey(servicePriority)).build();
     }
 
-    public static void bindService(WriteTransaction writeTransaction, String interfaceName, BoundServices serviceInfo,
-            Class<? extends ServiceModeBase> serviceMode) {
+    public static void bindService(TypedWriteTransaction<Configuration> tx, String interfaceName,
+        BoundServices serviceInfo, Class<? extends ServiceModeBase> serviceMode) {
         LOG.info("Binding Service {} for : {}", serviceInfo.getServiceName(), interfaceName);
         InstanceIdentifier<BoundServices> boundServicesInstanceIdentifier = buildBoundServicesIId(
-                serviceInfo.getServicePriority(), interfaceName, serviceMode);
-        writeTransaction.put(LogicalDatastoreType.CONFIGURATION, boundServicesInstanceIdentifier, serviceInfo, true);
+            serviceInfo.getServicePriority(), interfaceName, serviceMode);
+        tx.put(boundServicesInstanceIdentifier, serviceInfo, CREATE_MISSING_PARENTS);
+    }
+
+    public static void unbindService(ManagedNewTransactionRunner txRunner, JobCoordinator coordinator,
+            String interfaceName, InstanceIdentifier<BoundServices> boundServicesInstanceIdentifier) {
+        coordinator.enqueueJob(interfaceName, () -> Collections.singletonList(
+                txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
+                    tx -> unbindService(tx, interfaceName, boundServicesInstanceIdentifier))));
     }
 
-    public static void unbindService(DataBroker dataBroker, String interfaceName,
+    public static void unbindService(TypedWriteTransaction<Configuration> tx, String interfaceName,
             InstanceIdentifier<BoundServices> boundServicesInstanceIdentifier) {
         LOG.info("Unbinding Service from : {}", interfaceName);
-        DataStoreJobCoordinator dataStoreJobCoordinator = DataStoreJobCoordinator.getInstance();
-        dataStoreJobCoordinator.enqueueJob(interfaceName, () -> {
-            WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
-            writeTransaction.delete(LogicalDatastoreType.CONFIGURATION, boundServicesInstanceIdentifier);
-            List<ListenableFuture<Void>> futures = new ArrayList<>();
-            futures.add(writeTransaction.submit());
-            return futures;
-        });
-    }
-
-    public static List<TerminationPoint> getTerminationPointsOnBridge(DataBroker dataBroker, BigInteger dpnId) {
-        BridgeRefEntry bridgeRefEntry = InterfaceMetaUtils.getBridgeRefEntryFromOperDS(dpnId, dataBroker);
-        if (bridgeRefEntry == null || bridgeRefEntry.getBridgeReference() == null) {
-            LOG.debug("BridgeRefEntry for DPNID {} not found", dpnId);
-            return Collections.emptyList();
-        }
-        InstanceIdentifier<Node> nodeIid =
-                        bridgeRefEntry.getBridgeReference().getValue().firstIdentifierOf(Node.class);
-        com.google.common.base.Optional<Node> optNode =
-            IfmUtil.read(LogicalDatastoreType.OPERATIONAL, nodeIid,  dataBroker);
-        if (optNode.isPresent()) {
-            return optNode.get().getTerminationPoint();
-        }
-        return Collections.emptyList();
+        tx.delete(boundServicesInstanceIdentifier);
     }
 
     public static long getLogicalTunnelSelectGroupId(int lportTag) {
         return org.opendaylight.genius.interfacemanager.globals.IfmConstants.VXLAN_GROUPID_MIN + lportTag;
     }
+
+    // TODO Replace this with mdsal's DataObjectUtils.nullToEmpty when upgrading to mdsal 3.0.2
+    @Nonnull
+    public static <T> List<T> nullToEmpty(final @Nullable List<T> input) {
+        return input != null ? input : emptyList();
+    }
 }