NETVIRT-1630 migrate to md-sal APIs
[netvirt.git] / qosservice / impl / src / main / java / org / opendaylight / netvirt / qosservice / QosNeutronUtils.java
index 878b2c863dd5a6fdaba4e8b9899e7bc825cd9714..7ffe635a1eebb60fccb3f0a43933b41df3ec1adc 100644 (file)
@@ -10,13 +10,12 @@ package org.opendaylight.netvirt.qosservice;
 import static java.util.Collections.emptyList;
 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
 
-import com.google.common.base.Optional;
-import java.math.BigInteger;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.CopyOnWriteArraySet;
@@ -26,8 +25,7 @@ import javax.inject.Inject;
 import javax.inject.Singleton;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
 import org.opendaylight.genius.infra.Datastore;
 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
@@ -47,6 +45,8 @@ import org.opendaylight.genius.mdsalutil.matches.MatchEthernetType;
 import org.opendaylight.genius.mdsalutil.matches.MatchMetadata;
 import org.opendaylight.genius.utils.ServiceIndex;
 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.netvirt.neutronvpn.interfaces.INeutronVpnManager;
 import org.opendaylight.ovsdb.utils.southbound.utils.SouthboundUtils;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
@@ -102,6 +102,7 @@ import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.common.RpcResult;
+import org.opendaylight.yangtools.yang.common.Uint64;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -257,8 +258,8 @@ public class QosNeutronUtils {
                             && !qosPolicyOld.getBandwidthLimitRules().isEmpty()) {
                         BandwidthLimitRulesBuilder bwLimitBuilder = new BandwidthLimitRulesBuilder();
                         setPortBandwidthLimits(port, bwLimitBuilder
-                                .setMaxBurstKbps(BigInteger.ZERO)
-                                .setMaxKbps(BigInteger.ZERO).build(), tx);
+                                .setMaxBurstKbps(Uint64.ZERO)
+                                .setMaxKbps(Uint64.ZERO).build(), tx);
                     }
                 }
                 //handle DSCP Mark Rules update
@@ -294,8 +295,8 @@ public class QosNeutronUtils {
                             && !qosPolicy.getBandwidthLimitRules().isEmpty()) {
                         BandwidthLimitRulesBuilder bwLimitBuilder = new BandwidthLimitRulesBuilder();
                         setPortBandwidthLimits(port, bwLimitBuilder
-                                .setMaxBurstKbps(BigInteger.ZERO)
-                                .setMaxKbps(BigInteger.ZERO).build(), tx);
+                                .setMaxBurstKbps(Uint64.ZERO)
+                                .setMaxKbps(Uint64.ZERO).build(), tx);
                     }
                     // handle DSCP MArk Rules removal
                     if (qosPolicy != null && qosPolicy.getDscpmarkingRules() != null
@@ -388,8 +389,8 @@ public class QosNeutronUtils {
                                         && !qosPolicy.getBandwidthLimitRules().isEmpty()) {
                                     BandwidthLimitRulesBuilder bwLimitBuilder = new BandwidthLimitRulesBuilder();
                                     setPortBandwidthLimits(port, bwLimitBuilder
-                                            .setMaxBurstKbps(BigInteger.ZERO)
-                                            .setMaxKbps(BigInteger.ZERO).build(), tx);
+                                            .setMaxBurstKbps(Uint64.ZERO)
+                                            .setMaxKbps(Uint64.ZERO).build(), tx);
                                 }
                                 if (qosPolicy != null && qosPolicy.getDscpmarkingRules() != null
                                         && !qosPolicy.getDscpmarkingRules().isEmpty()) {
@@ -448,8 +449,8 @@ public class QosNeutronUtils {
             LOG.debug("Not Qos Cluster Owner. Ignoring setting bandwidth limits");
             return;
         }
-        BigInteger dpId = getDpnForInterface(port.getUuid().getValue());
-        if (dpId.equals(BigInteger.ZERO)) {
+        Uint64 dpId = getDpnForInterface(port.getUuid().getValue());
+        if (dpId.equals(Uint64.ZERO)) {
             LOG.info("DPN ID for interface {} not found", port.getUuid().getValue());
             return;
         }
@@ -508,98 +509,110 @@ public class QosNeutronUtils {
     }
 
     public void setPortDscpMarking(Port port, DscpmarkingRules dscpMark) {
-        if (!qosEosHandler.isQosClusterOwner()) {
-            LOG.trace("Not Qos Cluster Owner. Ignoring setting DSCP marking");
-            return;
-        }
 
-        BigInteger dpnId = getDpnForInterface(port.getUuid().getValue());
+        Uint64 dpnId = getDpnForInterface(port.getUuid().getValue());
         String ifName = port.getUuid().getValue();
-        Interface ifState = getInterfaceStateFromOperDS(ifName);
-        Short dscpValue = dscpMark.getDscpMark();
 
-        if (dpnId.equals(BigInteger.ZERO)) {
+        if (dpnId.equals(Uint64.ZERO)) {
             LOG.info("DPN ID for interface {} not found. Cannot set dscp value {} on port {}",
                     port.getUuid().getValue(), dscpMark, port.getUuid().getValue());
             return;
         }
-        int ipVersions = getIpVersions(port);
-        //1. OF rules
-        if (hasIpv4Addr(ipVersions)) {
-            LOG.trace("setting ipv4 flow for port: {}, dscp: {}", ifName, dscpValue);
-            addFlow(dpnId, dscpValue, ifName, NwConstants.ETHTYPE_IPV4, ifState);
-        }
-        if (hasIpv6Addr(ipVersions)) {
-            LOG.trace("setting ipv6 flow for port: {}, dscp: {}", ifName, dscpValue);
-            addFlow(dpnId, dscpValue, ifName, NwConstants.ETHTYPE_IPV6, ifState);
-        }
 
-        if (qosServiceConfiguredPorts.add(port.getUuid())) {
-            // bind qos service to interface
-            bindservice(ifName);
+        if (!qosEosHandler.isQosClusterOwner()) {
+            qosServiceConfiguredPorts.add(port.getUuid());
+            LOG.trace("Not Qos Cluster Owner. Ignoring setting DSCP marking");
+            return;
+        } else {
+            Interface ifState = getInterfaceStateFromOperDS(ifName);
+            Short dscpValue = dscpMark.getDscpMark().toJava();
+            int ipVersions = getIpVersions(port);
+            //1. OF rules
+            if (hasIpv4Addr(ipVersions)) {
+                LOG.trace("setting ipv4 flow for port: {}, dscp: {}", ifName, dscpValue);
+                addFlow(dpnId, dscpValue, ifName, NwConstants.ETHTYPE_IPV4, ifState);
+            }
+            if (hasIpv6Addr(ipVersions)) {
+                LOG.trace("setting ipv6 flow for port: {}, dscp: {}", ifName, dscpValue);
+                addFlow(dpnId, dscpValue, ifName, NwConstants.ETHTYPE_IPV6, ifState);
+            }
+
+            if (qosServiceConfiguredPorts.add(port.getUuid())) {
+                // bind qos service to interface
+                bindservice(ifName);
+            }
+
         }
     }
 
     public void unsetPortDscpMark(Port port) {
-        if (!qosEosHandler.isQosClusterOwner()) {
-            LOG.debug("Not Qos Cluster Owner. Ignoring unsetting DSCP marking");
-            return;
-        }
 
-        BigInteger dpnId = getDpnForInterface(port.getUuid().getValue());
+        Uint64 dpnId = getDpnForInterface(port.getUuid().getValue());
         String ifName = port.getUuid().getValue();
 
-        if (dpnId.equals(BigInteger.ZERO)) {
+        if (dpnId.equals(Uint64.ZERO)) {
             LOG.debug("DPN ID for port {} not found. Cannot unset dscp value", port.getUuid().getValue());
             return;
         }
-        LOG.trace("Removing dscp marking rule from Port {}", port.getUuid().getValue());
-        Interface intf = getInterfaceStateFromOperDS(ifName);
-        //unbind service from interface
-        unbindservice(ifName);
-        // 1. OF
-        int ipVersions = getIpVersions(port);
-        if (hasIpv4Addr(ipVersions)) {
-            removeFlow(dpnId, ifName, NwConstants.ETHTYPE_IPV4, intf);
-        }
-        if (hasIpv6Addr(ipVersions)) {
-            removeFlow(dpnId, ifName, NwConstants.ETHTYPE_IPV6, intf);
-        }
-        qosServiceConfiguredPorts.remove(port.getUuid());
-    }
 
-    public void unsetPortDscpMark(Port port, Interface intrf) {
         if (!qosEosHandler.isQosClusterOwner()) {
+            qosServiceConfiguredPorts.remove(port.getUuid());
+            LOG.debug("Not Qos Cluster Owner. Ignoring unsetting DSCP marking");
             return;
+        } else {
+            LOG.trace("Removing dscp marking rule from Port {}", port.getUuid().getValue());
+            Interface intf = getInterfaceStateFromOperDS(ifName);
+            //unbind service from interface
+            unbindservice(ifName);
+            // 1. OF
+            int ipVersions = getIpVersions(port);
+            if (hasIpv4Addr(ipVersions)) {
+                removeFlow(dpnId, ifName, NwConstants.ETHTYPE_IPV4, intf);
+            }
+            if (hasIpv6Addr(ipVersions)) {
+                removeFlow(dpnId, ifName, NwConstants.ETHTYPE_IPV6, intf);
+            }
+            qosServiceConfiguredPorts.remove(port.getUuid());
         }
-        BigInteger dpnId = getDpIdFromInterface(intrf);
+    }
+
+    public void unsetPortDscpMark(Port port, Interface intrf) {
+
+        Uint64 dpnId = getDpIdFromInterface(intrf);
         String ifName = port.getUuid().getValue();
 
-        if (dpnId.equals(BigInteger.ZERO)) {
+        if (dpnId.equals(Uint64.ZERO)) {
             LOG.error("Unable to retrieve DPN Id for interface {}. Cannot unset dscp value on port", ifName);
             return;
         }
-        LOG.trace("Removing dscp marking rule from Port {}", port.getUuid().getValue());
-        unbindservice(ifName);
-        int ipVersions = getIpVersions(port);
-        if (hasIpv4Addr(ipVersions)) {
-            removeFlow(dpnId, ifName, NwConstants.ETHTYPE_IPV4, intrf);
-        }
-        if (hasIpv6Addr(ipVersions)) {
-            removeFlow(dpnId, ifName, NwConstants.ETHTYPE_IPV6, intrf);
+        if (!qosEosHandler.isQosClusterOwner()) {
+            qosServiceConfiguredPorts.remove(port.getUuid());
+            return;
+        } else {
+            LOG.trace("Removing dscp marking rule from Port {}", port.getUuid().getValue());
+            unbindservice(ifName);
+            int ipVersions = getIpVersions(port);
+            if (hasIpv4Addr(ipVersions)) {
+                removeFlow(dpnId, ifName, NwConstants.ETHTYPE_IPV4, intrf);
+            }
+            if (hasIpv6Addr(ipVersions)) {
+                removeFlow(dpnId, ifName, NwConstants.ETHTYPE_IPV6, intrf);
+            }
+            qosServiceConfiguredPorts.remove(port.getUuid());
         }
-        qosServiceConfiguredPorts.remove(port.getUuid());
     }
 
-    private static BigInteger getDpIdFromInterface(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf
+    private static Uint64 getDpIdFromInterface(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf
                                                            .interfaces.rev140508.interfaces.state.Interface ifState) {
         String lowerLayerIf = ifState.getLowerLayerIf().get(0);
         NodeConnectorId nodeConnectorId = new NodeConnectorId(lowerLayerIf);
-        return BigInteger.valueOf(MDSALUtil.getDpnIdFromPortName(nodeConnectorId));
+        Long dpIdLong = MDSALUtil.getDpnIdFromPortName(nodeConnectorId);
+        Uint64 dpnId = dpIdLong < 0 ? Uint64.ZERO : Uint64.valueOf(dpIdLong);
+        return dpnId;
     }
 
-    public BigInteger getDpnForInterface(String ifName) {
-        BigInteger nodeId = BigInteger.ZERO;
+    public Uint64 getDpnForInterface(String ifName) {
+        Uint64 nodeId = Uint64.ZERO;
         try {
             GetDpidFromInterfaceInput
                     dpIdInput = new GetDpidFromInterfaceInputBuilder().setIntfName(ifName).build();
@@ -622,7 +635,7 @@ public class QosNeutronUtils {
     }
 
     @Nullable
-    private BridgeEntry getBridgeEntryFromConfigDS(BigInteger dpnId) {
+    private BridgeEntry getBridgeEntryFromConfigDS(Uint64 dpnId) {
         BridgeEntryKey bridgeEntryKey = new BridgeEntryKey(dpnId);
         InstanceIdentifier<BridgeEntry> bridgeEntryInstanceIdentifier = getBridgeEntryIdentifier(bridgeEntryKey);
         LOG.debug("Trying to retrieve bridge entry from config for Id: {}", bridgeEntryInstanceIdentifier);
@@ -631,16 +644,17 @@ public class QosNeutronUtils {
 
     @Nullable
     private BridgeEntry getBridgeEntryFromConfigDS(InstanceIdentifier<BridgeEntry> bridgeEntryInstanceIdentifier) {
-        return MDSALUtil.read(LogicalDatastoreType.CONFIGURATION, bridgeEntryInstanceIdentifier, dataBroker).orNull();
+        return MDSALUtil.read(LogicalDatastoreType.CONFIGURATION, bridgeEntryInstanceIdentifier, dataBroker)
+                .orElse(null);
     }
 
     @Nullable
     private BridgeRefEntry getBridgeRefEntryFromOperDS(InstanceIdentifier<BridgeRefEntry> dpnBridgeEntryIid) {
-        return MDSALUtil.read(LogicalDatastoreType.OPERATIONAL, dpnBridgeEntryIid, dataBroker).orNull();
+        return MDSALUtil.read(LogicalDatastoreType.OPERATIONAL, dpnBridgeEntryIid, dataBroker).orElse(null);
     }
 
     @Nullable
-    private OvsdbBridgeRef getBridgeRefEntryFromOperDS(BigInteger dpId) {
+    private OvsdbBridgeRef getBridgeRefEntryFromOperDS(Uint64 dpId) {
         BridgeRefEntryKey bridgeRefEntryKey = new BridgeRefEntryKey(dpId);
         InstanceIdentifier<BridgeRefEntry> bridgeRefEntryIid = getBridgeRefEntryIdentifier(bridgeRefEntryKey);
         BridgeRefEntry bridgeRefEntry = getBridgeRefEntryFromOperDS(bridgeRefEntryIid);
@@ -669,7 +683,7 @@ public class QosNeutronUtils {
     public void removeStaleFlowEntry(Interface intrf, int ethType) {
         List<MatchInfo> matches = new ArrayList<>();
 
-        BigInteger dpnId = getDpIdFromInterface(intrf);
+        Uint64 dpnId = getDpIdFromInterface(intrf);
 
         Integer ifIndex = intrf.getIfIndex();
         matches.add(new MatchMetadata(MetaDataUtil.getLportTagMetaData(ifIndex), MetaDataUtil.METADATA_MASK_LPORT_TAG));
@@ -680,7 +694,7 @@ public class QosNeutronUtils {
         mdsalUtils.removeFlow(flowEntity);
     }
 
-    public void addFlow(BigInteger dpnId, Short dscpValue, String ifName, int ethType, Interface ifState) {
+    public void addFlow(Uint64 dpnId, Short dscpValue, String ifName, int ethType, Interface ifState) {
         if (ifState == null) {
             LOG.debug("Could not find the ifState for interface {}", ifName);
             return;
@@ -703,7 +717,7 @@ public class QosNeutronUtils {
         mdsalUtils.installFlow(flowEntity);
     }
 
-    public void removeFlow(BigInteger dpnId, String ifName, int ethType, Interface ifState) {
+    public void removeFlow(Uint64 dpnId, String ifName, int ethType, Interface ifState) {
         if (ifState == null) {
             LOG.debug("Could not find the ifState for interface {}", ifName);
             return;
@@ -716,8 +730,14 @@ public class QosNeutronUtils {
 
     public org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state
             .@Nullable Interface getInterfaceStateFromOperDS(String interfaceName) {
-        return MDSALUtil.read(dataBroker, LogicalDatastoreType.OPERATIONAL,
-                createInterfaceStateInstanceIdentifier(interfaceName)).orNull();
+        try {
+            return SingleTransactionDataBroker.syncReadOptional(dataBroker, LogicalDatastoreType.OPERATIONAL,
+                    createInterfaceStateInstanceIdentifier(interfaceName)).orElse(null);
+        } catch (ExecutionException | InterruptedException e) {
+            LOG.error("getInterfaceStateFromOperDS: Exception while reading interface DS for the interface {}",
+                    interfaceName, e);
+        }
+        return null;
     }
 
     @NonNull
@@ -761,7 +781,7 @@ public class QosNeutronUtils {
     }
 
     private static BoundServices getBoundServices(String serviceName, short qosServiceIndex, int priority,
-                                                  BigInteger cookieQosTable, List<Instruction> instructions) {
+                                                  Uint64 cookieQosTable, List<Instruction> instructions) {
         StypeOpenflowBuilder augBuilder = new StypeOpenflowBuilder().setFlowCookie(cookieQosTable)
                 .setFlowPriority(priority).setInstruction(instructions);
         return new BoundServicesBuilder().withKey(new BoundServicesKey(qosServiceIndex)).setServiceName(serviceName)
@@ -770,7 +790,7 @@ public class QosNeutronUtils {
     }
 
     @NonNull
-    public static String getQosFlowId(short tableId, BigInteger dpId, int lportTag, int ethType) {
+    public static String getQosFlowId(short tableId, Uint64 dpId, int lportTag, int ethType) {
         return new StringBuilder().append(tableId).append(NwConstants.FLOWID_SEPARATOR).append(dpId)
                 .append(NwConstants.FLOWID_SEPARATOR).append(lportTag)
                 .append(NwConstants.FLOWID_SEPARATOR).append(ethType).toString();
@@ -796,7 +816,7 @@ public class QosNeutronUtils {
         } else {
             Network network = neutronVpnManager.getNeutronNetwork(port.getNetworkId());
 
-            if (network.augmentation(QosNetworkExtension.class) != null) {
+            if (network != null && network.augmentation(QosNetworkExtension.class) != null) {
                 qosUuid = network.augmentation(QosNetworkExtension.class).getQosPolicyId();
             }
         }
@@ -844,9 +864,9 @@ public class QosNeutronUtils {
     }
 
     @Nullable
-    public static BigInteger getDpnIdFromLowerLayerIf(String lowerLayerIf) {
+    public static Uint64 getDpnIdFromLowerLayerIf(String lowerLayerIf) {
         try {
-            return new BigInteger(lowerLayerIf.substring(lowerLayerIf.indexOf(":") + 1, lowerLayerIf.lastIndexOf(":")));
+            return Uint64.valueOf(lowerLayerIf.substring(lowerLayerIf.indexOf(":") + 1, lowerLayerIf.lastIndexOf(":")));
         } catch (NullPointerException e) {
             return null;
         }
@@ -880,4 +900,17 @@ public class QosNeutronUtils {
     public boolean hasIpv6Addr(int versions) {
         return (versions & (1 << QosConstants.IPV6_ADDR_MASK_BIT)) != 0;
     }
+
+    public boolean isBindServiceDone(Optional<Uuid> uuid) {
+        if (uuid != null) {
+            return qosServiceConfiguredPorts.contains(uuid.get());
+        }
+        return false;
+    }
+
+    public void removeInterfaceInQosConfiguredPorts(Optional<Uuid> uuid) {
+        if (uuid != null) {
+            qosServiceConfiguredPorts.remove(uuid.get());
+        }
+    }
 }