Remove GENIUS UTIL references in Elanmanager Module
[netvirt.git] / elanmanager / impl / src / main / java / org / opendaylight / netvirt / elan / l2gw / ha / HwvtepHAUtil.java
index 8f05dd0eb355a6d98f129822c76c12f58f63f7a0..df4b325621b907201fbe443da065ce7cf64bd06c 100644 (file)
@@ -7,10 +7,7 @@
  */
 package org.opendaylight.netvirt.elan.l2gw.ha;
 
-import static org.opendaylight.controller.md.sal.binding.api.WriteTransaction.CREATE_MISSING_PARENTS;
-import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.OPERATIONAL;
 
-import com.google.common.base.Optional;
 import com.google.common.base.Strings;
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
@@ -18,19 +15,18 @@ import java.util.Collection;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
+import java.util.Optional;
 import java.util.Set;
 import java.util.concurrent.ExecutionException;
 import java.util.stream.Collectors;
-import javax.annotation.Nullable;
-import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
-import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
-import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
-import org.opendaylight.genius.infra.Datastore;
-import org.opendaylight.genius.infra.Datastore.Configuration;
-import org.opendaylight.genius.infra.Datastore.Operational;
-import org.opendaylight.genius.infra.TypedReadWriteTransaction;
-import org.opendaylight.genius.infra.TypedWriteTransaction;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.genius.utils.hwvtep.HwvtepNodeHACache;
+import org.opendaylight.mdsal.binding.api.DataObjectModification;
+import org.opendaylight.mdsal.binding.util.Datastore;
+import org.opendaylight.mdsal.binding.util.Datastore.Configuration;
+import org.opendaylight.mdsal.binding.util.TypedReadWriteTransaction;
+import org.opendaylight.mdsal.binding.util.TypedWriteTransaction;
 import org.opendaylight.netvirt.elan.l2gw.ha.commands.SwitchesCmd;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
@@ -47,6 +43,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hw
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.ManagersBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.ManagersKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.Switches;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.SwitchesKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.managers.ManagerOtherConfigs;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.managers.ManagerOtherConfigsBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.managers.ManagerOtherConfigsKey;
@@ -82,7 +79,9 @@ public final class HwvtepHAUtil {
     public static final String MANAGER_KEY = "managerKey";
     public static final String L2GW_JOB_KEY = ":l2gw";
 
-    private HwvtepHAUtil() { }
+    private HwvtepHAUtil() {
+
+    }
 
     public static HwvtepPhysicalLocatorRef buildLocatorRef(InstanceIdentifier<Node> nodeIid, String tepIp) {
         InstanceIdentifier<TerminationPoint> tepId = buildTpId(nodeIid, tepIp);
@@ -109,21 +108,6 @@ public final class HwvtepHAUtil {
         return tpId.firstKeyOf(TerminationPoint.class).getTpId().getValue().substring("vxlan_over_ipv4:".length());
     }
 
-    public static String getLogicalSwitchSwitchName(HwvtepLogicalSwitchRef logicalSwitchRef) {
-        InstanceIdentifier<LogicalSwitches> id = (InstanceIdentifier<LogicalSwitches>) logicalSwitchRef.getValue();
-        return id.firstKeyOf(LogicalSwitches.class).getHwvtepNodeName().getValue();
-    }
-
-    public static String getNodeIdFromLocatorRef(HwvtepPhysicalLocatorRef locatorRef) {
-        InstanceIdentifier<TerminationPoint> tpId = (InstanceIdentifier<TerminationPoint>) locatorRef.getValue();
-        return tpId.firstKeyOf(Node.class).getNodeId().getValue();
-    }
-
-    public static String getNodeIdFromLogicalSwitches(HwvtepLogicalSwitchRef logicalSwitchRef) {
-        InstanceIdentifier<LogicalSwitches> id = (InstanceIdentifier<LogicalSwitches>) logicalSwitchRef.getValue();
-        return id.firstKeyOf(Node.class).getNodeId().getValue();
-    }
-
     public static InstanceIdentifier<Node> createInstanceIdentifierFromHAId(String haUUidVal) {
         String nodeString = HWVTEP_URI_PREFIX + "://"
             + UUID + "/" + java.util.UUID.nameUUIDFromBytes(haUUidVal.getBytes(StandardCharsets.UTF_8)).toString();
@@ -233,20 +217,13 @@ public final class HwvtepHAUtil {
 
     @Nullable
     public static Node getUpdated(DataObjectModification<Node> mod) {
-        Node node = null;
         switch (mod.getModificationType()) {
             case SUBTREE_MODIFIED:
-                node = mod.getDataAfter();
-                break;
             case WRITE:
-                if (mod.getDataAfter() !=  null) {
-                    node = mod.getDataAfter();
-                }
-                break;
+                return mod.getDataAfter();
             default:
-                break;
+                return null;
         }
-        return node;
     }
 
     @Nullable
@@ -267,19 +244,18 @@ public final class HwvtepHAUtil {
     }
 
     @Nullable
-    public static String getPsName(Node psNode) {
-        String psNodeId = psNode.getNodeId().getValue();
+    public static String getPsName(InstanceIdentifier<Node> psNodeIid) {
+        String psNodeId = psNodeIid.firstKeyOf(Node.class).getNodeId().getValue();
         if (psNodeId.contains(PHYSICALSWITCH)) {
-            return psNodeId.substring(psNodeId.indexOf(PHYSICALSWITCH) + PHYSICALSWITCH.length());
+            return psNodeId.substring(psNodeId.indexOf(PHYSICALSWITCH) +  PHYSICALSWITCH.length());
         }
         return null;
     }
 
     @Nullable
-    public static String getPsName(InstanceIdentifier<Node> psNodeIid) {
-        String psNodeId = psNodeIid.firstKeyOf(Node.class).getNodeId().getValue();
+    public static String getPsName(String psNodeId) {
         if (psNodeId.contains(PHYSICALSWITCH)) {
-            return psNodeId.substring(psNodeId.indexOf(PHYSICALSWITCH) +  PHYSICALSWITCH.length());
+            return psNodeId.substring(psNodeId.indexOf(PHYSICALSWITCH) + PHYSICALSWITCH.length());
         }
         return null;
     }
@@ -319,9 +295,9 @@ public final class HwvtepHAUtil {
         }
         HwvtepGlobalAugmentation globalAugmentation = node.augmentation(HwvtepGlobalAugmentation.class);
         if (globalAugmentation != null) {
-            List<Managers> managers = globalAugmentation.getManagers();
+            List<Managers> managers = new ArrayList<Managers>(globalAugmentation.nonnullManagers().values());
             if (managers != null && !managers.isEmpty() && managers.get(0).getManagerOtherConfigs() != null) {
-                for (ManagerOtherConfigs configs : managers.get(0).getManagerOtherConfigs()) {
+                for (ManagerOtherConfigs configs : managers.get(0).nonnullManagerOtherConfigs().values()) {
                     if (HA_ID.equals(configs.getOtherConfigKey())) {
                         return configs.getOtherConfigValue();
                     }
@@ -346,11 +322,11 @@ public final class HwvtepHAUtil {
                 haGlobalConfigNodeOptional.get().augmentation(HwvtepGlobalAugmentation.class);
         if (augmentation != null && augmentation.getManagers() != null
                 && augmentation.getManagers().size() > 0) {
-            Managers managers = augmentation.getManagers().get(0);
+            Managers managers = new ArrayList<Managers>(augmentation.nonnullManagers().values()).get(0);
             if (null == managers.getManagerOtherConfigs()) {
                 return childNodeIds;
             }
-            for (ManagerOtherConfigs otherConfigs : managers.getManagerOtherConfigs()) {
+            for (ManagerOtherConfigs otherConfigs : managers.nonnullManagerOtherConfigs().values()) {
                 if (HA_CHILDREN.equals(otherConfigs.getOtherConfigKey())) {
                     String nodeIdsVal = otherConfigs.getOtherConfigValue();
                     if (nodeIdsVal != null) {
@@ -441,7 +417,7 @@ public final class HwvtepHAUtil {
         if (!switchesAlreadyPresent) {
             HwvtepGlobalAugmentation augmentation = childNode.augmentation(HwvtepGlobalAugmentation.class);
             if (augmentation != null && augmentation.getSwitches() != null) {
-                List<Switches> src = augmentation.getSwitches();
+                List<Switches> src = new ArrayList<Switches>(augmentation.nonnullSwitches().values());
                 if (src != null && src.size() > 0) {
                     psList.add(new SwitchesCmd().transform(haNodePath, src.get(0)));
                 }
@@ -471,7 +447,7 @@ public final class HwvtepHAUtil {
         nodeBuilder.setNodeId(haNodePath.firstKeyOf(Node.class).getNodeId());
         nodeBuilder.addAugmentation(HwvtepGlobalAugmentation.class, hwvtepGlobalBuilder.build());
         Node configHANode = nodeBuilder.build();
-        tx.merge(haNodePath, configHANode, CREATE_MISSING_PARENTS);
+        tx.mergeParentStructureMerge(haNodePath, configHANode);
     }
 
     public static <D extends Datastore> void deleteNodeIfPresent(TypedReadWriteTransaction<D> tx,
@@ -497,9 +473,9 @@ public final class HwvtepHAUtil {
             return;
         }
         HashMap<InstanceIdentifier<Node>,Boolean> deleted = new HashMap<>();
-        List<Switches> switches = globalAugmentation.getSwitches();
+        Map<SwitchesKey, Switches> switches = globalAugmentation.nonnullSwitches();
         if (switches != null) {
-            for (Switches switche : switches) {
+            for (Switches switche : switches.values()) {
                 InstanceIdentifier<Node> psId = (InstanceIdentifier<Node>)switche.getSwitchRef().getValue();
                 deleteNodeIfPresent(tx, psId);
                 deleted.put(psId, Boolean.TRUE);
@@ -511,7 +487,7 @@ public final class HwvtepHAUtil {
         if (topologyOptional.isPresent()) {
             Topology topology = topologyOptional.get();
             if (topology.getNode() != null) {
-                for (Node psNode : topology.getNode()) {
+                for (Node psNode : topology.nonnullNode().values()) {
                     PhysicalSwitchAugmentation ps = psNode.augmentation(PhysicalSwitchAugmentation.class);
                     if (ps != null) {
                         InstanceIdentifier<Node> iid = (InstanceIdentifier<Node>)ps.getManagedBy().getValue();
@@ -529,51 +505,6 @@ public final class HwvtepHAUtil {
         }
     }
 
-    /**
-     * Delete switches from Node in Operational Data Tree .
-     *
-     * @param haPath HA node path from whih switches will be deleted
-     * @param tx  Transaction object
-     */
-    public static void deleteSwitchesManagedByNode(InstanceIdentifier<Node> haPath,
-            TypedReadWriteTransaction<Operational> tx) throws ExecutionException, InterruptedException {
-
-        Optional<Node> nodeOptional = tx.read(haPath).get();
-        if (!nodeOptional.isPresent()) {
-            return;
-        }
-        Node node = nodeOptional.get();
-        HwvtepGlobalAugmentation globalAugmentation = node.augmentation(HwvtepGlobalAugmentation.class);
-        if (globalAugmentation == null) {
-            return;
-        }
-        List<Switches> switches = globalAugmentation.getSwitches();
-        if (switches != null) {
-            for (Switches switche : switches) {
-                InstanceIdentifier<Node> id = (InstanceIdentifier<Node>)switche.getSwitchRef().getValue();
-                deleteNodeIfPresent(tx, id);
-            }
-        }
-    }
-
-    /**
-     * Returns true/false if all the childrens are deleted from Operational Data store.
-     *
-     * @param children IID for the child node to read from OP data tree
-     * @param tx Transaction
-     * @return true/false boolean
-     * @throws ReadFailedException Exception thrown if read fails
-     */
-    public static boolean areAllChildDeleted(Set<InstanceIdentifier<Node>> children,
-                                             ReadWriteTransaction tx) throws ReadFailedException {
-        for (InstanceIdentifier<Node> childId : children) {
-            if (tx.read(OPERATIONAL, childId).checkedGet().isPresent()) {
-                return false;
-            }
-        }
-        return true;
-    }
-
     public static void addToCacheIfHAChildNode(InstanceIdentifier<Node> childPath, Node childNode,
             HwvtepNodeHACache hwvtepNodeHACache) {
         String haId = HwvtepHAUtil.getHAIdFromManagerOtherConfig(childNode);