Handle nullable lists
[genius.git] / interfacemanager / interfacemanager-impl / src / main / java / org / opendaylight / genius / interfacemanager / renderer / ovs / statehelpers / OvsInterfaceStateUpdateHelper.java
index 74c7d6e05752b56484fc57dc1a45c40a64c9a0ed..db91e3940a9e22b40dbaa6ed638161dbb43f0d70 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
+ * Copyright (c) 2016, 2017 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -7,10 +7,19 @@
  */
 package org.opendaylight.genius.interfacemanager.renderer.ovs.statehelpers;
 
+import static org.opendaylight.genius.infra.Datastore.OPERATIONAL;
+
 import com.google.common.util.concurrent.ListenableFuture;
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+import javax.inject.Inject;
+import javax.inject.Singleton;
 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.genius.infra.Datastore.Operational;
+import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
+import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
+import org.opendaylight.genius.infra.TypedWriteTransaction;
 import org.opendaylight.genius.interfacemanager.IfmUtil;
 import org.opendaylight.genius.interfacemanager.commons.AlivenessMonitorUtils;
 import org.opendaylight.genius.interfacemanager.commons.InterfaceManagerCommonUtils;
@@ -20,61 +29,59 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.alivenessmonitor.rev160411.AlivenessMonitorService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfTunnel;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.util.ArrayList;
-import java.util.List;
-
+@Singleton
 public class OvsInterfaceStateUpdateHelper {
     private static final Logger LOG = LoggerFactory.getLogger(OvsInterfaceStateUpdateHelper.class);
 
-    public static List<ListenableFuture<Void>> updateState(InstanceIdentifier<FlowCapableNodeConnector> key,
-                                                           AlivenessMonitorService alivenessMonitorService,
-                                                           DataBroker dataBroker, String interfaceName,
-                                                           FlowCapableNodeConnector flowCapableNodeConnectorNew,
-                                                           FlowCapableNodeConnector flowCapableNodeConnectorOld) {
-        LOG.debug("Update of Interface State for port: {}", interfaceName);
-        List<ListenableFuture<Void>> futures = new ArrayList<>();
-        WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
+    private final ManagedNewTransactionRunner txRunner;
+    private final InterfaceManagerCommonUtils interfaceManagerCommonUtils;
+    private final AlivenessMonitorUtils alivenessMonitorUtils;
+
+    @Inject
+    public OvsInterfaceStateUpdateHelper(DataBroker dataBroker, AlivenessMonitorUtils alivenessMonitorUtils,
+            InterfaceManagerCommonUtils interfaceManagerCommonUtils) {
+        this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
+        this.interfaceManagerCommonUtils =  interfaceManagerCommonUtils;
+        this.alivenessMonitorUtils = alivenessMonitorUtils;
+    }
 
-        Interface.OperStatus operStatusNew = getOpState(flowCapableNodeConnectorNew);
+    public List<ListenableFuture<Void>> updateState(String interfaceName,
+            FlowCapableNodeConnector flowCapableNodeConnectorNew,
+            FlowCapableNodeConnector flowCapableNodeConnectorOld) {
+        LOG.debug("Updating interface state information for interface: {}", interfaceName);
+
+        Interface.OperStatus operStatusNew = InterfaceManagerCommonUtils.getOpState(flowCapableNodeConnectorNew);
         MacAddress macAddressNew = flowCapableNodeConnectorNew.getHardwareAddress();
 
-        Interface.OperStatus operStatusOld = getOpState(flowCapableNodeConnectorOld);
+        Interface.OperStatus operStatusOld = InterfaceManagerCommonUtils.getOpState(flowCapableNodeConnectorOld);
         MacAddress macAddressOld = flowCapableNodeConnectorOld.getHardwareAddress();
 
-        boolean opstateModified = false;
-        boolean hardwareAddressModified = false;
-        if (!operStatusNew.equals(operStatusOld)) {
-            opstateModified = true;
-        }
-        if (!macAddressNew.equals(macAddressOld)) {
-            hardwareAddressModified = true;
-        }
+        boolean opstateModified = !operStatusNew.equals(operStatusOld);
+        boolean hardwareAddressModified = !Objects.equals(macAddressNew, macAddressOld);
 
         if (!opstateModified && !hardwareAddressModified) {
             LOG.debug("If State entry for port: {} Not Modified.", interfaceName);
-            return futures;
+            return Collections.emptyList();
         }
 
-        org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface =
-                InterfaceManagerCommonUtils.getInterfaceFromConfigDS(interfaceName, dataBroker);
+        org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf
+            .interfaces.rev140508.interfaces.Interface iface = interfaceManagerCommonUtils
+                .getInterfaceFromConfigDS(interfaceName);
 
-        // For tunnels, derive the final opstate based on the bfd tunnel monitoring status
-        if(modifyTunnel(iface, opstateModified) && InterfaceManagerCommonUtils.checkIfBfdStateIsDown(iface.getName())){
-            operStatusNew = Interface.OperStatus.Down;
-            opstateModified = operStatusNew.equals(operStatusOld);
+        // For monitoring enabled tunnels, skip opstate update
+        if (isTunnelInterface(iface) && !modifyTunnelOpState(iface, opstateModified)) {
+            LOG.debug("skip interface-state updation for monitoring enabled tunnel interface {}", interfaceName);
+            opstateModified = false;
         }
 
         if (!opstateModified && !hardwareAddressModified) {
             LOG.debug("If State entry for port: {} Not Modified.", interfaceName);
-            return futures;
+            return Collections.emptyList();
         }
         InterfaceBuilder ifaceBuilder = new InterfaceBuilder();
         if (hardwareAddressModified) {
@@ -82,81 +89,103 @@ public class OvsInterfaceStateUpdateHelper {
             PhysAddress physAddress = new PhysAddress(macAddressNew.getValue());
             ifaceBuilder.setPhysAddress(physAddress);
         }
-        // modify the attributes in interface operational DS
-        handleInterfaceStateUpdates(iface, transaction, dataBroker,
-                        ifaceBuilder, opstateModified, interfaceName, flowCapableNodeConnectorNew.getName(), operStatusNew);
-
-        // start/stop monitoring based on opState
-        if(modifyTunnel(iface, opstateModified)){
-            handleTunnelMonitoringUpdates(alivenessMonitorService, dataBroker, iface.getAugmentation(IfTunnel.class),
-                    iface.getName(), operStatusNew);
-        }
 
-        futures.add(transaction.submit());
-        return futures;
+        if (opstateModified) {
+            return Collections.singletonList(txRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL, tx -> {
+                // modify the attributes in interface operational DS
+                handleInterfaceStateUpdates(iface, tx, ifaceBuilder, true, interfaceName,
+                        flowCapableNodeConnectorNew.getName(), operStatusNew);
+
+                // start/stop monitoring based on opState
+                if (isTunnelInterface(iface)) {
+                    handleTunnelMonitoringUpdates(iface.augmentation(IfTunnel.class), iface.getName(),
+                            operStatusNew);
+                }
+            }));
+        } else {
+            return Collections.singletonList(txRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL, tx -> {
+                // modify the attributes in interface operational DS
+                handleInterfaceStateUpdates(iface, tx, ifaceBuilder, false, interfaceName,
+                        flowCapableNodeConnectorNew.getName(), operStatusNew);
+            }));
+        }
     }
 
-    public static void updateInterfaceStateOnNodeRemove(String interfaceName, FlowCapableNodeConnector flowCapableNodeConnector,
-                                                        DataBroker dataBroker, AlivenessMonitorService alivenessMonitorService,
-                                                        WriteTransaction transaction){
+    public void updateInterfaceStateOnNodeRemove(String interfaceName,
+            FlowCapableNodeConnector flowCapableNodeConnector, TypedWriteTransaction<Operational> tx) {
         LOG.debug("Updating interface oper-status to UNKNOWN for : {}", interfaceName);
 
         InterfaceBuilder ifaceBuilder = new InterfaceBuilder();
-        org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface =
-                InterfaceManagerCommonUtils.getInterfaceFromConfigDS(interfaceName, dataBroker);
-        handleInterfaceStateUpdates(iface,transaction, dataBroker,
-                        ifaceBuilder, true, interfaceName, flowCapableNodeConnector.getName(),
-                        Interface.OperStatus.Unknown);
-        if (InterfaceManagerCommonUtils.isTunnelInterface(iface)){
-            handleTunnelMonitoringUpdates(alivenessMonitorService, dataBroker, iface.getAugmentation(IfTunnel.class),
-                    interfaceName, Interface.OperStatus.Unknown);
+        org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
+            .ietf.interfaces.rev140508.interfaces.Interface iface = interfaceManagerCommonUtils
+                .getInterfaceFromConfigDS(interfaceName);
+        handleInterfaceStateUpdates(iface, tx, ifaceBuilder, true, interfaceName,
+                flowCapableNodeConnector.getName(), Interface.OperStatus.Unknown);
+        if (InterfaceManagerCommonUtils.isTunnelInterface(iface)) {
+            handleTunnelMonitoringUpdates(iface.augmentation(IfTunnel.class), interfaceName,
+                    Interface.OperStatus.Unknown);
         }
     }
 
-    public static Interface.OperStatus getOpState(FlowCapableNodeConnector flowCapableNodeConnector){
-        Interface.OperStatus operStatus =
-                (flowCapableNodeConnector.getState().isLive() &&
-                        !flowCapableNodeConnector.getConfiguration().isPORTDOWN())
-                        ? Interface.OperStatus.Up: Interface.OperStatus.Down;
-        return operStatus;
-    }
-
-    public static void handleInterfaceStateUpdates(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface,
-                                                   WriteTransaction transaction, DataBroker dataBroker, InterfaceBuilder ifaceBuilder, boolean opStateModified,
-                                                   String interfaceName, String portName, Interface.OperStatus opState){
-        // if interface config DS is null, do the update only for the lower-layer-interfaces
+    private void handleInterfaceStateUpdates(
+            org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
+                .ietf.interfaces.rev140508.interfaces.Interface iface,
+            TypedWriteTransaction<Operational> tx, InterfaceBuilder ifaceBuilder, boolean opStateModified,
+            String interfaceName, String portName, Interface.OperStatus opState) {
+        // if interface config DS is null, do the update only for the
+        // lower-layer-interfaces
         // which have no corresponding config entries
-        if(iface == null && !interfaceName.equals(portName)) {
+        if (iface == null && !interfaceName.equals(portName)) {
+            return;
+        }
+
+        final Interface interfaceState = interfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceName);
+        if (interfaceState == null || (interfaceState.getOperStatus() == opState)) {
+            LOG.warn("Ignoring: updating interface state for interface {}",
+                    interfaceName);
             return;
         }
+
         LOG.debug("updating interface state entry for {}", interfaceName);
         InstanceIdentifier<Interface> ifStateId = IfmUtil.buildStateInterfaceId(interfaceName);
-        ifaceBuilder.setKey(new InterfaceKey(interfaceName));
+        ifaceBuilder.withKey(new InterfaceKey(interfaceName));
         if (modifyOpState(iface, opStateModified)) {
             LOG.debug("updating interface oper status as {} for {}", opState.name(), interfaceName);
             ifaceBuilder.setOperStatus(opState);
         }
-        transaction.merge(LogicalDatastoreType.OPERATIONAL, ifStateId, ifaceBuilder.build(), false);
+        tx.merge(ifStateId, ifaceBuilder.build());
     }
 
-    public static void handleTunnelMonitoringUpdates(AlivenessMonitorService alivenessMonitorService, DataBroker dataBroker,
-                                                     IfTunnel ifTunnel, String interfaceName,
-                                                     Interface.OperStatus operStatus){
-
+    public void handleTunnelMonitoringUpdates(IfTunnel ifTunnel,
+            String interfaceName, Interface.OperStatus operStatus) {
         LOG.debug("handling tunnel monitoring updates for {} due to opstate modification", interfaceName);
-        if (operStatus == Interface.OperStatus.Down || operStatus == Interface.OperStatus.Unknown)
-            AlivenessMonitorUtils.stopLLDPMonitoring(alivenessMonitorService, dataBroker, ifTunnel, interfaceName);
-        else
-            AlivenessMonitorUtils.startLLDPMonitoring(alivenessMonitorService, dataBroker, ifTunnel, interfaceName);
+        if (operStatus == Interface.OperStatus.Down || operStatus == Interface.OperStatus.Unknown) {
+            alivenessMonitorUtils.stopLLDPMonitoring(ifTunnel, interfaceName);
+        } else {
+            alivenessMonitorUtils.startLLDPMonitoring(ifTunnel, interfaceName);
+        }
     }
 
-    public static boolean modifyOpState(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface,
-                                        boolean opStateModified){
-        return (opStateModified && (iface == null || iface != null && iface.isEnabled()));
+    public static boolean modifyOpState(
+            org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf
+                .interfaces.rev140508.interfaces.Interface iface,
+            boolean opStateModified) {
+        return opStateModified && (iface == null || iface.isEnabled());
     }
 
-    public static boolean modifyTunnel(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface iface,
-                                       boolean opStateModified){
-        return modifyOpState(iface, opStateModified) && iface != null && iface.getAugmentation(IfTunnel.class) != null;
+    public static boolean isTunnelInterface(
+            org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
+                .ietf.interfaces.rev140508.interfaces.Interface iface) {
+        return iface != null && iface.augmentation(IfTunnel.class) != null;
+    }
+
+    public static boolean modifyTunnelOpState(
+            org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang
+                .ietf.interfaces.rev140508.interfaces.Interface iface,
+            boolean opStateModified) {
+        if (!iface.augmentation(IfTunnel.class).isMonitorEnabled()) {
+            return modifyOpState(iface, opStateModified);
+        }
+        return false;
     }
 }