Convert itm-impl to use mdsal-binding-util
[genius.git] / itm / itm-impl / src / main / java / org / opendaylight / genius / itm / confighelpers / ItmInternalTunnelAddWorker.java
index a43cf405159d94d84cfc0552949b1bfc9bff0358..4eb7376f589b552e696687e5f32cef2d847c8f07 100644 (file)
@@ -8,29 +8,19 @@
 package org.opendaylight.genius.itm.confighelpers;
 
 import static java.util.Collections.singletonList;
-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.mdsal.binding.util.Datastore.CONFIGURATION;
 
-import com.google.common.base.Optional;
 import com.google.common.util.concurrent.ListenableFuture;
-import java.math.BigInteger;
 import java.time.Duration;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
 import java.util.Objects;
+import java.util.Optional;
 import java.util.concurrent.Callable;
 import java.util.concurrent.ExecutionException;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
 import org.opendaylight.genius.datastoreutils.listeners.DataTreeEventCallbackRegistrar;
-import org.opendaylight.genius.infra.Datastore.Configuration;
-import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
-import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
-import org.opendaylight.genius.infra.TypedReadWriteTransaction;
-import org.opendaylight.genius.infra.TypedWriteTransaction;
 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
 import org.opendaylight.genius.itm.cache.OfEndPointCache;
 import org.opendaylight.genius.itm.cache.OvsBridgeRefEntryCache;
@@ -41,6 +31,14 @@ import org.opendaylight.genius.itm.impl.TunnelMonitoringConfig;
 import org.opendaylight.genius.itm.itmdirecttunnels.renderer.ovs.utilities.DirectTunnelUtils;
 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.binding.util.Datastore.Configuration;
+import org.opendaylight.mdsal.binding.util.ManagedNewTransactionRunner;
+import org.opendaylight.mdsal.binding.util.ManagedNewTransactionRunnerImpl;
+import org.opendaylight.mdsal.binding.util.TypedReadWriteTransaction;
+import org.opendaylight.mdsal.binding.util.TypedWriteTransaction;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
+import org.opendaylight.mdsal.common.api.ReadFailedException;
 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.inet.types.rev130715.IpAddressBuilder;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
@@ -75,6 +73,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.tun
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.common.OperationFailedException;
+import org.opendaylight.yangtools.yang.common.Uint64;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -118,8 +117,9 @@ public final class ItmInternalTunnelAddWorker {
         monitorInterval = tunnelMonitoringConfig.getMonitorInterval();
     }
 
-    public List<ListenableFuture<Void>> buildAllTunnels(IMdsalApiManager mdsalManager, List<DPNTEPsInfo> cfgdDpnList,
-                                                        Collection<DPNTEPsInfo> meshedDpnList) {
+    public List<? extends ListenableFuture<?>> buildAllTunnels(IMdsalApiManager mdsalManager,
+                                                               List<DPNTEPsInfo> cfgdDpnList,
+                                                               Collection<DPNTEPsInfo> meshedDpnList) {
         LOG.trace("Building tunnels with DPN List {} " , cfgdDpnList);
         if (null == cfgdDpnList || cfgdDpnList.isEmpty()) {
             LOG.error(" Build Tunnels was invoked with empty list");
@@ -142,7 +142,7 @@ public final class ItmInternalTunnelAddWorker {
     private static void updateDpnTepInfoToConfig(TypedWriteTransaction<Configuration> tx, DPNTEPsInfo dpn,
         DirectTunnelUtils directTunnelUtils) throws ExecutionException, InterruptedException, OperationFailedException {
         LOG.debug("Updating CONFIGURATION datastore with DPN {} ", dpn);
-        InstanceIdentifier<DpnEndpoints> dep = InstanceIdentifier.builder(DpnEndpoints.class).build() ;
+        InstanceIdentifier<DpnEndpoints> dep = InstanceIdentifier.create(DpnEndpoints.class) ;
         List<DPNTEPsInfo> dpnList = new ArrayList<>() ;
         dpnList.add(new DPNTEPsInfoBuilder(dpn)
             .setDstId(directTunnelUtils.allocateId(ITMConstants.ITM_IDPOOL_NAME, dpn.getDPNID().toString())).build());
@@ -180,8 +180,7 @@ public final class ItmInternalTunnelAddWorker {
                     if (!ItmUtils.getIntersection(srcte.nonnullTzMembership(),
                             dstte.nonnullTzMembership()).isEmpty()) {
                         // wire them up
-                        wireUpBidirectionalTunnel(tx, srcte, dstte, srcDpn.getDPNID(), dstDpn.getDPNID(),
-                                mdsalManager);
+                        wireUpBidirectionalTunnel(tx, srcte, dstte, srcDpn.getDPNID(), dstDpn.getDPNID(), mdsalManager);
                         if (!ItmTunnelAggregationHelper.isTunnelAggregationEnabled()) {
                             // CHECK THIS -- Assumption -- One end point per Dpn per transport zone
                             break;
@@ -193,8 +192,8 @@ public final class ItmInternalTunnelAddWorker {
     }
 
     private void wireUpBidirectionalTunnel(TypedReadWriteTransaction<Configuration> tx, TunnelEndPoints srcte,
-        TunnelEndPoints dstte, BigInteger srcDpnId, BigInteger dstDpnId, IMdsalApiManager mdsalManager)
-        throws ExecutionException, InterruptedException, OperationFailedException {
+            TunnelEndPoints dstte, Uint64 srcDpnId, Uint64 dstDpnId, IMdsalApiManager mdsalManager)
+                    throws ExecutionException, InterruptedException, OperationFailedException {
         // Setup the flow for LLDP monitoring -- PUNT TO CONTROLLER
 
         if (monitorProtocol.isAssignableFrom(TunnelMonitoringTypeLldp.class)) {
@@ -214,8 +213,8 @@ public final class ItmInternalTunnelAddWorker {
     }
 
     private boolean wireUp(TypedWriteTransaction<Configuration> tx, TunnelEndPoints srcte, TunnelEndPoints dstte,
-        BigInteger srcDpnId, BigInteger dstDpnId)
-        throws ExecutionException, InterruptedException, OperationFailedException {
+            Uint64 srcDpnId, Uint64 dstDpnId)
+                    throws ExecutionException, InterruptedException, OperationFailedException {
         // Wire Up logic
         LOG.trace("Wiring between source tunnel end points {}, destination tunnel end points {}", srcte, dstte);
         String interfaceName = srcte.getInterfaceName();
@@ -225,6 +224,7 @@ public final class ItmInternalTunnelAddWorker {
 
         String trunkInterfaceName = ItmUtils.getTrunkInterfaceName(interfaceName,
                 srcte.getIpAddress().stringValue(), dstte.getIpAddress().stringValue(), tunTypeStr);
+
         String parentInterfaceName = null;
         if (tunType.isAssignableFrom(TunnelTypeVxlan.class)) {
             parentInterfaceName = createLogicalGroupTunnel(srcDpnId, dstDpnId);
@@ -240,12 +240,11 @@ public final class ItmInternalTunnelAddWorker {
         return true;
     }
 
-    private void createTunnelInterface(TunnelEndPoints srcte, TunnelEndPoints dstte, BigInteger srcDpnId,
+    private void createTunnelInterface(TunnelEndPoints srcte, TunnelEndPoints dstte, Uint64 srcDpnId,
             Class<? extends TunnelTypeBase> tunType, String trunkInterfaceName, String parentInterfaceName) {
         String gateway = srcte.getIpAddress().getIpv4Address() != null ? "0.0.0.0" : "::";
         IpAddress gatewayIpObj = IpAddressBuilder.getDefaultInstance(gateway);
-        IpAddress gwyIpAddress =
-                Objects.equals(srcte.getSubnetMask(), dstte.getSubnetMask()) ? gatewayIpObj : srcte.getGwIpAddress();
+        IpAddress gwyIpAddress = gatewayIpObj;
         LOG.debug(" Creating Trunk Interface with parameters trunk I/f Name - {}, parent I/f name - {}, "
                 + "source IP - {}, destination IP - {} gateway IP - {}",
                 trunkInterfaceName, srcte.getInterfaceName(), srcte.getIpAddress(), dstte.getIpAddress(), gwyIpAddress);
@@ -254,8 +253,8 @@ public final class ItmInternalTunnelAddWorker {
         List<TunnelOptions> tunOptions = ItmUtils.buildTunnelOptions(srcte, itmCfg);
         Boolean isMonitorEnabled = !tunType.isAssignableFrom(TunnelTypeLogicalGroup.class) && isTunnelMonitoringEnabled;
         Interface iface = ItmUtils.buildTunnelInterface(srcDpnId, trunkInterfaceName,
-                String.format("%s %s",ItmUtils.convertTunnelTypetoString(tunType), "Trunk Interface"),
-                true, tunType, srcte.getIpAddress(), dstte.getIpAddress(), gwyIpAddress, srcte.getVLANID(), true,
+                trunkInterfaceDecription(ItmUtils.convertTunnelTypetoString(tunType)),
+                true, tunType, srcte.getIpAddress(), dstte.getIpAddress(), true,
                 isMonitorEnabled, monitorProtocol, monitorInterval, useOfTunnel, parentInterfaceName, tunOptions);
         LOG.debug(" Trunk Interface builder - {} ", iface);
         InstanceIdentifier<Interface> trunkIdentifier = ItmUtils.buildId(trunkInterfaceName);
@@ -265,28 +264,23 @@ public final class ItmInternalTunnelAddWorker {
         ItmUtils.ITM_CACHE.addInterface(iface);
     }
 
-    private static void createLogicalTunnelInterface(BigInteger srcDpnId,
-            Class<? extends TunnelTypeBase> tunType, String interfaceName) {
-        Interface iface = ItmUtils.buildLogicalTunnelInterface(srcDpnId, interfaceName,
-                String.format("%s %s",ItmUtils.convertTunnelTypetoString(tunType), "Interface"), true);
-        InstanceIdentifier<Interface> trunkIdentifier = ItmUtils.buildId(interfaceName);
-        ITMBatchingUtils.update(trunkIdentifier, iface, ITMBatchingUtils.EntityType.DEFAULT_CONFIG);
-        ItmUtils.ITM_CACHE.addInterface(iface);
+    private static String trunkInterfaceDecription(String type) {
+        return type + " Trunk Interface";
     }
 
-    private static void createInternalTunnel(TypedWriteTransaction<Configuration> tx, BigInteger srcDpnId,
-        BigInteger dstDpnId, Class<? extends TunnelTypeBase> tunType, String trunkInterfaceName) {
+    private static void createInternalTunnel(TypedWriteTransaction<Configuration> tx, Uint64 srcDpnId,
+            Uint64 dstDpnId, Class<? extends TunnelTypeBase> tunType, String trunkInterfaceName) {
         InstanceIdentifier<InternalTunnel> path = InstanceIdentifier.create(TunnelList.class)
                 .child(InternalTunnel.class, new InternalTunnelKey(dstDpnId, srcDpnId, tunType));
         InternalTunnel tnl = ItmUtils.buildInternalTunnel(srcDpnId, dstDpnId, tunType, trunkInterfaceName);
         // Switching to individual transaction submit as batching latencies is causing ELAN failures.
         // Will revert when ELAN can handle this.
         // ITMBatchingUtils.update(path, tnl, ITMBatchingUtils.EntityType.DEFAULT_CONFIG);
-        tx.merge(path, tnl, CREATE_MISSING_PARENTS);
+        tx.mergeParentStructureMerge(path, tnl);
         ItmUtils.ITM_CACHE.addInternalTunnel(tnl);
     }
 
-    private String createLogicalGroupTunnel(BigInteger srcDpnId, BigInteger dstDpnId) {
+    private String createLogicalGroupTunnel(Uint64 srcDpnId, Uint64 dstDpnId) {
         boolean tunnelAggregationEnabled = ItmTunnelAggregationHelper.isTunnelAggregationEnabled();
         if (!tunnelAggregationEnabled) {
             return null;
@@ -298,14 +292,14 @@ public final class ItmInternalTunnelAddWorker {
         return logicTunnelGroupName;
     }
 
-    private static class ItmTunnelAggregationWorker implements Callable<List<ListenableFuture<Void>>> {
+    private static class ItmTunnelAggregationWorker implements Callable<List<? extends ListenableFuture<?>>> {
 
         private final String logicTunnelGroupName;
-        private final BigInteger srcDpnId;
-        private final BigInteger dstDpnId;
+        private final Uint64 srcDpnId;
+        private final Uint64 dstDpnId;
         private final ManagedNewTransactionRunner txRunner;
 
-        ItmTunnelAggregationWorker(String logicGroupName, BigInteger srcDpnId, BigInteger dstDpnId, DataBroker broker) {
+        ItmTunnelAggregationWorker(String logicGroupName, Uint64 srcDpnId, Uint64 dstDpnId, DataBroker broker) {
             this.logicTunnelGroupName = logicGroupName;
             this.srcDpnId = srcDpnId;
             this.dstDpnId = dstDpnId;
@@ -313,7 +307,7 @@ public final class ItmInternalTunnelAddWorker {
         }
 
         @Override
-        public List<ListenableFuture<Void>> call() {
+        public List<? extends ListenableFuture<?>> call() {
             return singletonList(txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION, tx -> {
                 //The logical tunnel interface be created only when the first tunnel interface on each OVS is created
                 InternalTunnel tunnel = ItmUtils.ITM_CACHE.getInternalTunnel(logicTunnelGroupName);
@@ -328,26 +322,34 @@ public final class ItmInternalTunnelAddWorker {
                 }
             }));
         }
+
+        private static void createLogicalTunnelInterface(Uint64 srcDpnId,
+                Class<? extends TunnelTypeBase> tunType, String interfaceName) {
+            Interface iface = ItmUtils.buildLogicalTunnelInterface(srcDpnId, interfaceName,
+                    String.format("%s %s",ItmUtils.convertTunnelTypetoString(tunType), "Interface"), true);
+            InstanceIdentifier<Interface> trunkIdentifier = ItmUtils.buildId(interfaceName);
+            ITMBatchingUtils.update(trunkIdentifier, iface, ITMBatchingUtils.EntityType.DEFAULT_CONFIG);
+            ItmUtils.ITM_CACHE.addInterface(iface);
+        }
     }
 
-    private void createInternalDirectTunnels(TunnelEndPoints srcte, TunnelEndPoints dstte, BigInteger srcDpnId,
-        BigInteger dstDpnId, Class<? extends TunnelTypeBase> tunType, String trunkInterfaceName,
-        String parentInterfaceName) throws ExecutionException, InterruptedException, OperationFailedException {
+    private void createInternalDirectTunnels(TunnelEndPoints srcte, TunnelEndPoints dstte, Uint64 srcDpnId,
+            Uint64 dstDpnId, Class<? extends TunnelTypeBase> tunType, String trunkInterfaceName,
+            String parentInterfaceName) throws OperationFailedException {
         IpAddress gatewayIpObj = IpAddressBuilder.getDefaultInstance("0.0.0.0");
-        IpAddress gwyIpAddress = Objects.equals(srcte.getSubnetMask(), dstte.getSubnetMask())
-                ? gatewayIpObj : srcte.getGwIpAddress() ;
+        IpAddress gwyIpAddress = gatewayIpObj;
         LOG.debug("Creating Trunk Interface with parameters trunk I/f Name - {}, parent I/f name - {}, source IP - {},"
                         + " destination IP - {} gateway IP - {}", trunkInterfaceName, parentInterfaceName,
                 srcte.getIpAddress(), dstte.getIpAddress(), gwyIpAddress) ;
 
-        boolean useOfTunnel = ItmUtils.falseIfNull(srcte.isOptionOfTunnel());
+        boolean useOfTunnel = itmCfg.isUseOfTunnels();
 
         List<TunnelOptions> tunOptions = ItmUtils.buildTunnelOptions(srcte, itmCfg);
         Boolean isMonitorEnabled = !tunType.isAssignableFrom(TunnelTypeLogicalGroup.class) && isTunnelMonitoringEnabled;
         Interface iface = ItmUtils.buildTunnelInterface(srcDpnId, trunkInterfaceName,
-                String.format("%s %s",ItmUtils.convertTunnelTypetoString(srcte.getTunnelType()), "Trunk Interface"),
-                true, tunType, srcte.getIpAddress(), dstte.getIpAddress(), gwyIpAddress, srcte.getVLANID(),
-                true, isMonitorEnabled, monitorProtocol, monitorInterval, useOfTunnel, parentInterfaceName, tunOptions);
+                trunkInterfaceDecription(ItmUtils.convertTunnelTypetoString(srcte.getTunnelType())),
+                true, tunType, srcte.getIpAddress(), dstte.getIpAddress(), true,
+                isMonitorEnabled, monitorProtocol, monitorInterval, useOfTunnel, parentInterfaceName, tunOptions);
         LOG.debug("Trunk Interface builder - {} ", iface);
 
         final DpnTepsStateBuilder dpnTepsStateBuilder = new DpnTepsStateBuilder();
@@ -358,12 +360,11 @@ public final class ItmInternalTunnelAddWorker {
         dpnsTepsBuilder.withKey(new DpnsTepsKey(srcDpnId));
         dpnsTepsBuilder.setTunnelType(srcte.getTunnelType());
         dpnsTepsBuilder.setSourceDpnId(srcDpnId);
-        if (itmCfg.isUseOfTunnels()) {
+        if (useOfTunnel) {
             String tunnelType = ItmUtils.convertTunnelTypetoString(srcte.getTunnelType());
-            ofTunnelPortName = directTunnelUtils.generateOfPortName(srcDpnId, tunnelType);
+            ofTunnelPortName = DirectTunnelUtils.generateOfPortName(srcDpnId, tunnelType);
             dpnsTepsBuilder.setOfTunnel(ofTunnelPortName);
         }
-
         RemoteDpnsBuilder remoteDpn = new RemoteDpnsBuilder();
         remoteDpn.withKey(new RemoteDpnsKey(dstDpnId));
         remoteDpn.setDestinationDpnId(dstDpnId);
@@ -381,8 +382,8 @@ public final class ItmInternalTunnelAddWorker {
 
     private static void updateDpnTepInterfaceInfoToConfig(DpnTepsState dpnTeps) {
         LOG.debug("Updating CONFIGURATION datastore with DPN-Teps {} ", dpnTeps);
-        InstanceIdentifier<DpnTepsState> dpnTepsII = InstanceIdentifier.builder(DpnTepsState.class).build() ;
-        ITMBatchingUtils.update(dpnTepsII, dpnTeps, ITMBatchingUtils.EntityType.DEFAULT_CONFIG);
+        InstanceIdentifier<DpnTepsState> dpnTepsII = InstanceIdentifier.create(DpnTepsState.class);
+        ITMBatchingUtils.updateContainer(dpnTepsII, dpnTeps, ITMBatchingUtils.EntityType.DEFAULT_CONFIG);
     }
 
     private void addTunnelConfiguration(Interface iface, String ofTunnelPortName)
@@ -395,7 +396,7 @@ public final class ItmInternalTunnelAddWorker {
             return;
         }
 
-        BigInteger dpId = parentRefs.getDatapathNodeIdentifier();
+        Uint64 dpId = parentRefs.getDatapathNodeIdentifier();
         if (dpId == null) {
             LOG.warn("dpid for interface: {} Not Found. No DPID provided. Creation of OF-Port not supported.",
                     iface.getName());
@@ -407,7 +408,7 @@ public final class ItmInternalTunnelAddWorker {
         LOG.info("adding tunnel port configuration for tunnelName: {}", tunnelName);
         if (createTunnelPort(dpId)) {
             LOG.debug("creating dpn tunnel mapping  for dpn: {} tunnelName: {}", dpId, tunnelName);
-            DirectTunnelUtils.createBridgeTunnelEntryInConfigDS(dpId, tunnelName);
+            DirectTunnelUtils.createBridgeTunnelEntryInConfigDS(dpId, iface.getName());
             if (ofTunnelPortName != null) {
                 ofEndPointCache.add(dpId, tunnelName);
             }
@@ -426,7 +427,7 @@ public final class ItmInternalTunnelAddWorker {
                                 .child(OvsBridgeRefEntry.class, new OvsBridgeRefEntryKey(dpId)).build();
 
                 eventCallbacks.onAdd(LogicalDatastoreType.OPERATIONAL, bridgeRefEntryFromDS, (refEntryIid) -> {
-                    addPortToBridgeOnCallback(iface, iface.getName(), refEntryIid);
+                    addPortToBridgeOnCallback(iface, tunnelName, refEntryIid);
                     return DataTreeEventCallbackRegistrar.NextAction.UNREGISTER;
                 }, Duration.ofMillis(5000), (id) -> {
                         try {
@@ -434,7 +435,7 @@ public final class ItmInternalTunnelAddWorker {
                             InstanceIdentifier<OvsdbBridgeAugmentation> bridgeIidOnCallback =
                                     (InstanceIdentifier<OvsdbBridgeAugmentation>) ovsBridgeRefEntryOnCallback.get()
                                             .getOvsBridgeReference().getValue();
-                            addPortToBridge(bridgeIidOnCallback, iface, iface.getName());
+                            addPortToBridge(bridgeIidOnCallback, iface, tunnelName);
                         }   catch (ReadFailedException e) {
                             LOG.error("Bridge not found in DS/cache for dpId {}", dpId);
                         }
@@ -450,11 +451,11 @@ public final class ItmInternalTunnelAddWorker {
         }
     }
 
-    private boolean createTunnelPort(BigInteger dpId) {
+    private boolean createTunnelPort(Uint64 dpId) {
         if (!itmCfg.isUseOfTunnels()) {
             return true;
         }
-        return (ofEndPointCache.get(dpId) == null);
+        return ofEndPointCache.get(dpId) == null;
     }
 
     private void addPortToBridgeOnCallback(Interface iface, String portName, OvsBridgeRefEntry bridgeRefEntry) {