Convert itm-impl to use mdsal-binding-util
[genius.git] / itm / itm-impl / src / main / java / org / opendaylight / genius / itm / confighelpers / OvsdbTepRemoveConfigHelper.java
index b57cf984b3bd508d39ea3490673d0d6bf7cbccaf..07880c57f6b442cbeab1626ca131871177aefbc0 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,79 +7,93 @@
  */
 package org.opendaylight.genius.itm.confighelpers;
 
-import java.math.BigInteger;
+import com.google.common.util.concurrent.ListenableFuture;
+import java.util.ArrayList;
 import java.util.List;
-
-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 java.util.Map;
+import java.util.Objects;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.genius.itm.globals.ITMConstants;
 import org.opendaylight.genius.itm.impl.ItmUtils;
 import org.opendaylight.genius.mdsalutil.MDSALUtil;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.binding.util.Datastore;
+import org.opendaylight.mdsal.binding.util.ManagedNewTransactionRunner;
+import org.opendaylight.mdsal.binding.util.TypedWriteTransaction;
 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.IpPrefix;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddressBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.NotHostedTransportZones;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.TransportZones;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.*;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.tepsnothostedintransportzone.UnknownVteps;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.tepsnothostedintransportzone.UnknownVtepsKey;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.Subnets;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.SubnetsKey;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.subnets.Vteps;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.subnets.VtepsKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.not.hosted.transport.zones.TepsInNotHostedTransportZone;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.not.hosted.transport.zones.TepsInNotHostedTransportZoneKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.not.hosted.transport.zones.tepsinnothostedtransportzone.UnknownVteps;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.not.hosted.transport.zones.tepsinnothostedtransportzone.UnknownVtepsKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.TransportZone;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.TransportZoneKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.Vteps;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.VtepsKey;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.common.Uint64;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class OvsdbTepRemoveConfigHelper {
+public final class OvsdbTepRemoveConfigHelper {
 
     private static final Logger LOG = LoggerFactory.getLogger(OvsdbTepRemoveConfigHelper.class);
 
+    private OvsdbTepRemoveConfigHelper() {
+
+    }
+
     /**
-     * Removes the TEP from ITM configuration Datastore in one of the following cases:
+     * Removes the TEP from ITM configuration/operational Datastore in one of the following cases.
      * 1) default transport zone
      * 2) Configured transport zone
      * 3) Unhosted transport zone
      * Function checks for above three cases and calls other sub-function to remove the TEP
      *
      * @param tepIp TEP-IP address in string
-     * @param strDpid bridge datapath ID in string
+     * @param strDpnId bridge datapath ID in string
      * @param tzName transport zone name in string
-     * @param dataBroker data broker handle to perform operations on config datastore
-     * @param wrTx WriteTransaction object
+     * @param dataBroker data broker handle to perform operations on config/operational datastore
+     * @param txRunner ManagedNewTransactionRunner object
      */
-    public static void removeTepReceivedFromOvsdb(String tepIp, String strDpid, String tzName,
-        DataBroker dataBroker, WriteTransaction wrTx) {
-        BigInteger dpid = BigInteger.valueOf(0);
-
-        LOG.trace("Remove TEP: TEP-IP: {}, TZ name: {}, DPID: {}", tepIp, tzName, strDpid);
-
-        if (strDpid != null && !strDpid.isEmpty()) {
-            dpid = MDSALUtil.getDpnId(strDpid);
+    public static List<? extends ListenableFuture<?>> removeTepReceivedFromOvsdb(String tepIp, String strDpnId,
+                                                                                 String tzName, DataBroker dataBroker,
+                                                                                 ManagedNewTransactionRunner txRunner) {
+        List<ListenableFuture<?>> futures = new ArrayList<>();
+        Uint64 dpnId = Uint64.ZERO;
+        LOG.trace("Remove TEP: TEP-IP: {}, TZ name: {}, DPID: {}", tepIp, tzName, strDpnId);
+
+        if (strDpnId != null && !strDpnId.isEmpty()) {
+            dpnId = MDSALUtil.getDpnId(strDpnId);
         }
-
         // Get tep IP
-        IpAddress tepIpAddress = new IpAddress(tepIp.toCharArray());
-        TransportZone tZone = null;
+        IpAddress tepIpAddress = IpAddressBuilder.getDefaultInstance(tepIp);
+        TransportZone transportZone;
 
         // Case: TZ name is not given from OVS's other_config parameters.
         if (tzName == null) {
             tzName = ITMConstants.DEFAULT_TRANSPORT_ZONE;
             // add TEP into default-TZ
-            tZone = ItmUtils.getTransportZoneFromConfigDS(tzName, dataBroker);
-            if (tZone == null) {
+            transportZone = ItmUtils.getTransportZoneFromConfigDS(tzName, dataBroker);
+            if (transportZone == null) {
                 LOG.error("Error: default-transport-zone is not yet created.");
-                return;
+                return futures;
             }
             LOG.trace("Remove TEP from default-transport-zone.");
         } else {
             // Case: Add TEP into corresponding TZ created from Northbound.
-            tZone = ItmUtils.getTransportZoneFromConfigDS(tzName, dataBroker);
-            if (tZone == null) {
+            transportZone = ItmUtils.getTransportZoneFromConfigDS(tzName, dataBroker);
+            String name = tzName;
+            Uint64 id = dpnId;
+            if (transportZone == null) {
                 // Case: TZ is not configured from Northbound, then add TEP into
-                // "teps-not-hosted-in-transport-zone"
-                LOG.trace("Removing TEP from unknown TZ into teps-not-hosted-in-transport-zone.");
-                removeUnknownTzTepFromTepsNotHosted(tzName, tepIpAddress, dpid, dataBroker, wrTx);
-                return;
+                // "teps-in-not-hosted-transport-zone"
+                LOG.trace("Removing TEP from teps-in-not-hosted-transport-zone list.");
+                futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(Datastore.OPERATIONAL,
+                    tx -> removeUnknownTzTepFromTepsNotHosted(name, tepIpAddress, id, dataBroker, tx)));
+                return futures;
             } else {
                 LOG.trace("Remove TEP from transport-zone already configured by Northbound.");
             }
@@ -87,132 +101,105 @@ public class OvsdbTepRemoveConfigHelper {
 
         // Remove TEP from (default transport-zone) OR (transport-zone already configured by Northbound)
 
-        // Get subnet list of corresponding TZ created from Northbound.
-        List<Subnets> subnetList = tZone.getSubnets();
-
-        if (subnetList == null || subnetList.isEmpty()) {
-            LOG.trace("No subnet list in transport-zone. Nothing to do.");
+        @Nullable Map<VtepsKey, Vteps> vtepList = transportZone.getVteps();
+        if (vtepList == null || vtepList.isEmpty()) {
+            //  case: vtep list does not exist or it has no elements
+            LOG.trace("No vtep list in subnet list of transport-zone. Nothing to do.");
         } else {
-            String portName = ITMConstants.DUMMY_PORT;
-            IpPrefix subnetMaskObj = ItmUtils.getDummySubnet();
-
-            List<Vteps> vtepList = null;
-
-            // subnet list already exists case; check for dummy-subnet
-            for (Subnets subnet : subnetList) {
-                if (subnet.getKey().getPrefix().equals(subnetMaskObj)) {
-                    LOG.trace("Subnet exists in the subnet list of transport-zone {}.", tzName);
-                    // get vtep list of existing subnet
-                    vtepList = subnet.getVteps();
+            //  case: vtep list has elements
+            boolean vtepFound = false;
+            Vteps oldVtep = null;
+
+            for (Vteps vtep : vtepList.values()) {
+                if (Objects.equals(vtep.getDpnId(), dpnId)) {
+                    vtepFound = true;
+                    oldVtep = vtep;
                     break;
                 }
             }
-
-            if (vtepList == null || vtepList.isEmpty()) {
-                //  case: vtep list does not exist or it has no elements
-                LOG.trace("No vtep list in subnet list of transport-zone. Nothing to do.");
+            if (vtepFound) {
+                // vtep is found, update it with tep-ip
+                LOG.trace("Remove TEP from vtep list in subnet list of transport-zone.");
+                dpnId = oldVtep.getDpnId();
+                String name = tzName;
+                Uint64 id = dpnId;
+                futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(Datastore.CONFIGURATION,
+                    tx -> removeVtepFromTZConfig(name, id, tx)));
             } else {
-                //  case: vtep list has elements
-                boolean vtepFound = false;
-                Vteps oldVtep = null;
-
-                for (Vteps vtep : vtepList) {
-                    if (vtep.getDpnId().equals(dpid)) {
-                        vtepFound = true;
-                        oldVtep = vtep;
-                        // get portName of existing vtep
-                        portName = vtep.getPortname();
-                        break;
-                    }
-                }
-                if (vtepFound) {
-                    // vtep is found, update it with tep-ip
-                    LOG.trace("Remove TEP from vtep list in subnet list of transport-zone.");
-                    dpid = oldVtep.getDpnId();
-                    portName = oldVtep.getPortname();
-                    removeVtepFromTZConfig(subnetMaskObj, tzName, dpid, portName, wrTx);
-                } else {
-                    LOG.trace(
+                LOG.trace(
                         "TEP is not found in the vtep list in subnet list of transport-zone. Nothing to do.");
-                }
             }
         }
+        return futures;
     }
 
     /**
      * Removes the TEP from subnet list in the transport zone list
      * from ITM configuration Datastore by delete operation with write transaction.
      *
-     * @param subnetMaskObj subnet mask in IpPrefix object
-     * @param dpid bridge datapath ID in BigInteger
+     * @param dpnId bridge datapath ID
      * @param tzName transport zone name in string
-     * @param portName port name as a part of VtepsKey
-     * @param wrTx WriteTransaction object
+     * @param tx TypedWriteTransaction object
      */
-    protected static void removeVtepFromTZConfig(IpPrefix subnetMaskObj, String tzName, BigInteger dpid,
-        String portName, WriteTransaction wrTx) {
-        SubnetsKey subnetsKey = new SubnetsKey(subnetMaskObj);
-        VtepsKey vtepkey = new VtepsKey(dpid, portName);
+    private static void removeVtepFromTZConfig(String tzName, Uint64 dpnId,
+                                               TypedWriteTransaction<Datastore.Configuration> tx) {
+
+        VtepsKey vtepkey = new VtepsKey(dpnId);
 
-        InstanceIdentifier<Vteps> vTepPath = InstanceIdentifier.builder(TransportZones.class)
-            .child(TransportZone.class, new TransportZoneKey(tzName))
-            .child(Subnets.class, subnetsKey).child(Vteps.class, vtepkey).build();
+        InstanceIdentifier<Vteps> vtepPath = InstanceIdentifier.builder(TransportZones.class)
+                .child(TransportZone.class, new TransportZoneKey(tzName))
+                .child(Vteps.class, vtepkey).build();
 
-        LOG.trace("Removing TEP (TZ: {} Subnet: {} DPID: {}) in ITM Config DS.", tzName,
-            subnetMaskObj.getValue().toString(), dpid);
+        LOG.trace("Removing TEP from (TZ: {} DPN-ID: {}) inside ITM Config DS.",
+                tzName, dpnId);
         // remove vtep
-        wrTx.delete(LogicalDatastoreType.CONFIGURATION, vTepPath);
+        tx.delete(vtepPath);
     }
 
     /**
      * Removes the TEP from the not-hosted transport zone in the TepsNotHosted list
-     * from ITM configuration Datastore
+     * from ITM Operational Datastore.
      *
      * @param tzName transport zone name in string
      * @param tepIpAddress TEP IP address in IpAddress object
-     * @param dpid bridge datapath ID in BigInteger
-     * @param dataBroker data broker handle to perform operations on config datastore
-     * @param wrTx WriteTransaction object
+     * @param dpnId bridge datapath ID
+     * @param dataBroker data broker handle to perform operations on operational datastore
+     * @param tx TypedWriteTransaction object
      */
     public static void removeUnknownTzTepFromTepsNotHosted(String tzName, IpAddress tepIpAddress,
-        BigInteger dpid, DataBroker dataBroker, WriteTransaction wrTx) {
-        List<UnknownVteps> vtepList = null;
-
-        TepsNotHostedInTransportZone unknownTz =
-            ItmUtils.getUnknownTransportZoneFromITMConfigDS(tzName, dataBroker);
-        if (unknownTz == null) {
-            LOG.trace("Unhosted TransportZone does not exist. Nothing to do for TEP removal.");
+                                                           Uint64 dpnId, DataBroker dataBroker,
+                                                           TypedWriteTransaction<Datastore.Operational> tx) {
+        Map<UnknownVtepsKey, UnknownVteps> vtepList;
+        TepsInNotHostedTransportZone tepsInNotHostedTransportZone =
+                ItmUtils.getUnknownTransportZoneFromITMOperDS(tzName, dataBroker);
+        if (tepsInNotHostedTransportZone == null) {
+            LOG.trace("Unhosted TransportZone ({}) does not exist in OperDS. Nothing to do for TEP removal.", tzName);
             return;
         } else {
-            vtepList = unknownTz.getUnknownVteps();
+            vtepList = tepsInNotHostedTransportZone.getUnknownVteps();
             if (vtepList == null || vtepList.isEmpty()) {
                 //  case: vtep list does not exist or it has no elements
-                LOG.trace(
-                    "Remove TEP in unhosted TZ ({}) when no vtep-list in the TZ. Nothing to do.",
-                    tzName);
+                LOG.trace("Remove TEP from unhosted TZ ({}) when no vtep-list in the TZ. Nothing to do.", tzName);
             } else {
                 //  case: vtep list has elements
                 boolean vtepFound = false;
-                UnknownVteps foundVtep = null;
 
-                for (UnknownVteps vtep : vtepList) {
-                    if (vtep.getDpnId().equals(dpid)) {
+                for (UnknownVteps vtep : vtepList.values()) {
+                    if (Objects.equals(vtep.getDpnId(), dpnId)) {
                         vtepFound = true;
-                        foundVtep = vtep;
                         break;
                     }
                 }
                 if (vtepFound) {
                     // vtep is found, update it with tep-ip
                     LOG.trace(
-                        "Remove TEP with IP ({}) from unhosted TZ ({}) in TepsNotHosted list.",
-                        tepIpAddress, tzName);
+                            "Remove TEP with IP ({}) from unhosted TZ ({}) inside not-hosted-transport-zones list.",
+                            tepIpAddress, tzName);
                     if (vtepList.size() == 1) {
-                        removeTzFromTepsNotHosted(tzName, wrTx);
+                        removeTzFromTepsNotHosted(tzName, tx);
                     } else {
-                        removeVtepFromTepsNotHosted(tzName, dpid, wrTx);
+                        removeVtepFromTepsNotHosted(tzName, dpnId, tx);
                     }
-                    vtepList.remove(foundVtep);
                 }
             }
         }
@@ -220,41 +207,33 @@ public class OvsdbTepRemoveConfigHelper {
 
     /**
      * Removes the TEP from unknown vtep list under the transport zone in the TepsNotHosted list
-     * from ITM configuration Datastore by delete operation with write transaction.
+     * from ITM operational Datastore by delete operation with write transaction.
      *
      * @param tzName transport zone name in string
-     * @param dpid bridge datapath ID in BigInteger
-     * @param wrTx WriteTransaction object
+     * @param dpnId bridge datapath ID
+     * @param tx TypedWriteTransaction object
      */
-    protected static void removeVtepFromTepsNotHosted(String tzName, BigInteger dpid,
-        WriteTransaction wrTx) {
-
-        UnknownVtepsKey unknownVtepkey = new UnknownVtepsKey(dpid);
-        InstanceIdentifier<UnknownVteps> vTepPath = InstanceIdentifier.builder(TransportZones.class)
-            .child(TepsNotHostedInTransportZone.class, new TepsNotHostedInTransportZoneKey(tzName))
-            .child(UnknownVteps.class, unknownVtepkey).build();
-
-        LOG.trace("Removing TEP from unhosted (TZ: {}, DPID: {}) from ITM Config DS.", tzName,
-            dpid);
-        // remove vtep
-        wrTx.delete(LogicalDatastoreType.CONFIGURATION, vTepPath);
+    private static void removeVtepFromTepsNotHosted(String tzName, Uint64 dpnId,
+                                                    TypedWriteTransaction<Datastore.Operational> tx) {
+        InstanceIdentifier<UnknownVteps> vtepPath = InstanceIdentifier.builder(NotHostedTransportZones.class)
+                .child(TepsInNotHostedTransportZone.class, new TepsInNotHostedTransportZoneKey(tzName))
+                .child(UnknownVteps.class, new UnknownVtepsKey(dpnId)).build();
+        LOG.trace("Removing TEP from unhosted (TZ: {}, DPID: {}) inside ITM Oper DS.", tzName, dpnId);
+        tx.delete(vtepPath);
     }
 
     /**
      * Removes the transport zone in the TepsNotHosted list
-     * from ITM configuration Datastore by delete operation with write transaction.
+     * from ITM operational Datastore by delete operation with write transaction.
      *
      * @param tzName transport zone name in string
-     * @param wrTx WriteTransaction object
+     * @param tx TypedWriteTransaction object
      */
-    protected static void removeTzFromTepsNotHosted(String tzName, WriteTransaction wrTx) {
-        InstanceIdentifier<TepsNotHostedInTransportZone> tzTepsNotHostedTepPath =
-            InstanceIdentifier.builder(TransportZones.class)
-                .child(TepsNotHostedInTransportZone.class,
-                    new TepsNotHostedInTransportZoneKey(tzName)).build();
-
-        LOG.trace("Removing TZ ({})from TepsNotHosted list  from ITM Config DS.", tzName);
-        // remove TZ from TepsNotHosted list
-        wrTx.delete(LogicalDatastoreType.CONFIGURATION, tzTepsNotHostedTepPath);
+    private static void removeTzFromTepsNotHosted(String tzName, TypedWriteTransaction<Datastore.Operational> tx) {
+        InstanceIdentifier<TepsInNotHostedTransportZone> tepsInNotHostedTransportZoneIid =
+                InstanceIdentifier.builder(NotHostedTransportZones.class).child(TepsInNotHostedTransportZone.class,
+                        new TepsInNotHostedTransportZoneKey(tzName)).build();
+        LOG.trace("Removing TZ ({})from not-hosted-transport-zones list inside ITM Oper DS.", tzName);
+        tx.delete(tepsInNotHostedTransportZoneIid);
     }
 }