NETVIRT-1630 migrate to md-sal APIs
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / evpn / utils / ElanEvpnFlowUtils.java
index 7b85af93fe01300bc2674cfc54605daaebc9b7f0..ce51c4b6b6effe569c1f9f205939cb9dfc970412 100644 (file)
@@ -15,10 +15,15 @@ import java.util.Collections;
 import java.util.List;
 import javax.inject.Inject;
 import javax.inject.Singleton;
+import org.opendaylight.genius.infra.Datastore;
+import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
+import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
 import org.opendaylight.genius.mdsalutil.MDSALUtil;
 import org.opendaylight.genius.mdsalutil.MatchInfo;
 import org.opendaylight.genius.mdsalutil.NwConstants;
 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
+import org.opendaylight.infrautils.utils.concurrent.NamedSimpleReentrantLock.Acquired;
+import org.opendaylight.mdsal.binding.api.DataBroker;
 import org.opendaylight.netvirt.elan.utils.ElanConstants;
 import org.opendaylight.netvirt.elan.utils.ElanEtreeUtils;
 import org.opendaylight.netvirt.elan.utils.ElanItmUtils;
@@ -29,6 +34,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.ta
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeLeafTagName;
+import org.opendaylight.yangtools.yang.common.Uint64;
 
 
 @Singleton
@@ -36,13 +42,15 @@ public class ElanEvpnFlowUtils {
     private final IMdsalApiManager mdsalManager;
     private final ElanItmUtils elanItmUtils;
     private final ElanEtreeUtils elanEtreeUtils;
+    private final ManagedNewTransactionRunner txRunner;
 
     @Inject
     public ElanEvpnFlowUtils(final IMdsalApiManager mdsalManager, final ElanItmUtils elanItmUtils,
-            final ElanEtreeUtils elanEtreeUtils) {
+            final ElanEtreeUtils elanEtreeUtils, final DataBroker dataBroker) {
         this.mdsalManager = mdsalManager;
         this.elanItmUtils = elanItmUtils;
         this.elanEtreeUtils = elanEtreeUtils;
+        this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
     }
 
     public Flow evpnBuildDmacFlowForExternalRemoteMac(EvpnDmacFlow evpnDmacFlow) {
@@ -56,15 +64,16 @@ public class ElanEvpnFlowUtils {
                 ElanUtils.getKnownDynamicmacFlowRef(NwConstants.ELAN_DMAC_TABLE, evpnDmacFlow.getDpId(),
                         evpnDmacFlow.getNexthopIP(), evpnDmacFlow.getDstMacAddress(), evpnDmacFlow.getElanTag(), false),
                 20, evpnDmacFlow.getElanName(), 0, 0,
-                ElanConstants.COOKIE_ELAN_KNOWN_DMAC.add(BigInteger.valueOf(evpnDmacFlow.getElanTag())), mkMatches,
-                mkInstructions);
+                Uint64.valueOf(ElanConstants.COOKIE_ELAN_KNOWN_DMAC.toJava()
+                    .add(BigInteger.valueOf(evpnDmacFlow.getElanTag()))),
+                mkMatches, mkInstructions);
 
         return flow;
     }
 
     public List<ListenableFuture<Void>> evpnDeleteDmacFlowsToExternalMac(EvpnDmacFlow evpnDmacFlow) {
         List<ListenableFuture<Void>> futures = new ArrayList<>();
-        synchronized (ElanUtils.getElanMacDPNKey(evpnDmacFlow.getElanTag(), evpnDmacFlow.getDstMacAddress(),
+        try (Acquired lock = ElanUtils.lockElanMacDPN(evpnDmacFlow.getElanTag(), evpnDmacFlow.getDstMacAddress(),
                 evpnDmacFlow.getDpId())) {
             futures.addAll(
                     evpnRemoveFlowThatSendsThePacketOnAnExternalTunnel(evpnDmacFlow.getElanTag(), evpnDmacFlow.dpId,
@@ -75,43 +84,47 @@ public class ElanEvpnFlowUtils {
         return futures;
     }
 
-    private List<ListenableFuture<Void>> evpnDeleteEtreeDmacFlowsToExternalMac(long elanTag, BigInteger dpId,
+    private List<ListenableFuture<Void>> evpnDeleteEtreeDmacFlowsToExternalMac(long elanTag, Uint64 dpId,
             String nexthopIp, String macToRemove) {
         List<ListenableFuture<Void>> futures = new ArrayList<>();
         EtreeLeafTagName etreeLeafTag = elanEtreeUtils.getEtreeLeafTagByElanTag(elanTag);
         if (etreeLeafTag != null) {
             futures.addAll(
-                    evpnRemoveFlowThatSendsThePacketOnAnExternalTunnel(etreeLeafTag.getEtreeLeafTag().getValue(), dpId,
+                    evpnRemoveFlowThatSendsThePacketOnAnExternalTunnel(
+                            etreeLeafTag.getEtreeLeafTag().getValue().toJava(), dpId,
                             nexthopIp, macToRemove));
             futures.addAll(
-                    evpnRemoveTheDropFlow(etreeLeafTag.getEtreeLeafTag().getValue(), dpId, nexthopIp, macToRemove));
+                    evpnRemoveTheDropFlow(etreeLeafTag.getEtreeLeafTag().getValue().toJava(),
+                            dpId, nexthopIp, macToRemove));
         }
         return futures;
     }
 
-    static String evpnGetKnownDynamicmacFlowRef(short elanDmacTable, BigInteger dpId, String nexthopIp,
+    static String evpnGetKnownDynamicmacFlowRef(short elanDmacTable, Uint64 dpId, String nexthopIp,
                                                 String dstMacAddress, long elanTag, boolean shFlag) {
         return String.valueOf(elanDmacTable) + elanTag + dpId + nexthopIp + dstMacAddress + shFlag;
     }
 
-    private List<ListenableFuture<Void>> evpnRemoveTheDropFlow(long elanTag, BigInteger dpId, String nexthopIp,
+    private List<ListenableFuture<Void>> evpnRemoveTheDropFlow(long elanTag, Uint64 dpId, String nexthopIp,
             String macToRemove) {
         String flowId = ElanEvpnFlowUtils.evpnGetKnownDynamicmacFlowRef(NwConstants.ELAN_DMAC_TABLE, dpId, nexthopIp,
                 macToRemove, elanTag, true);
         Flow flowToRemove = new FlowBuilder().setId(new FlowId(flowId)).setTableId(NwConstants.ELAN_DMAC_TABLE).build();
-        return Collections.singletonList(mdsalManager.removeFlow(dpId, flowToRemove));
+        return Collections.singletonList(txRunner.callWithNewReadWriteTransactionAndSubmit(Datastore.CONFIGURATION,
+            tx -> mdsalManager.removeFlow(tx, dpId, flowToRemove)));
     }
 
     private List<ListenableFuture<Void>> evpnRemoveFlowThatSendsThePacketOnAnExternalTunnel(long elanTag,
-            BigInteger dpId, String nexthopIp, String macToRemove) {
+            Uint64 dpId, String nexthopIp, String macToRemove) {
         String flowId = ElanEvpnFlowUtils.evpnGetKnownDynamicmacFlowRef(NwConstants.ELAN_DMAC_TABLE, dpId, nexthopIp,
                 macToRemove, elanTag, false);
         Flow flowToRemove = new FlowBuilder().setId(new FlowId(flowId)).setTableId(NwConstants.ELAN_DMAC_TABLE).build();
-        return Collections.singletonList(mdsalManager.removeFlow(dpId, flowToRemove));
+        return Collections.singletonList(txRunner.callWithNewReadWriteTransactionAndSubmit(Datastore.CONFIGURATION,
+            tx -> mdsalManager.removeFlow(tx, dpId, flowToRemove)));
     }
 
     public static class EvpnDmacFlowBuilder {
-        private BigInteger dpId;
+        private Uint64 dpId;
         private String nexthopIP;
         private long elanTag;
         private Long vni;
@@ -121,7 +134,7 @@ public class ElanEvpnFlowUtils {
         public EvpnDmacFlowBuilder() {
         }
 
-        public EvpnDmacFlowBuilder setDpId(BigInteger dpId) {
+        public EvpnDmacFlowBuilder setDpId(Uint64 dpId) {
             this.dpId = dpId;
             return this;
         }
@@ -157,14 +170,14 @@ public class ElanEvpnFlowUtils {
     }
 
     static class EvpnDmacFlow {
-        private BigInteger dpId;
-        private String nexthopIP;
-        private long elanTag;
-        private Long vni;
-        private String dstMacAddress;
-        private String elanName;
-
-        EvpnDmacFlow(BigInteger dpId, String nexthopIP, long elanTag, Long vni, String dstMacAddress,
+        private final Uint64 dpId;
+        private final String nexthopIP;
+        private final long elanTag;
+        private final Long vni;
+        private final String dstMacAddress;
+        private final String elanName;
+
+        EvpnDmacFlow(Uint64 dpId, String nexthopIP, long elanTag, Long vni, String dstMacAddress,
                             String elanName) {
             this.dpId = dpId;
             this.nexthopIP = nexthopIP;
@@ -174,7 +187,7 @@ public class ElanEvpnFlowUtils {
             this.elanName = elanName;
         }
 
-        public BigInteger getDpId() {
+        public Uint64 getDpId() {
             return dpId;
         }