Bug 8055: use method references instead of lambdas
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / HwvtepSouthboundUtil.java
index 8592e4286d1c76e01789d4318a7026aae5387f8b..d38363959660dff02c9dbf1d76e26f7129fb4b29 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
+ * Copyright (c) 2015, 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,
@@ -13,12 +13,16 @@ import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
-import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
+import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalRef;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepLogicalSwitchAttributes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalSwitchAttributes;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.ConnectionInfo;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
+import org.opendaylight.yangtools.yang.binding.Identifiable;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.impl.codec.DeserializationException;
 import org.slf4j.Logger;
@@ -28,9 +32,15 @@ import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.util.concurrent.CheckedFuture;
 
+import java.util.Collection;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
 public class HwvtepSouthboundUtil {
 
     private static final Logger LOG = LoggerFactory.getLogger(HwvtepSouthboundUtil.class);
+    private static final String SCHEMA_VERSION_MISMATCH =
+            "{} column for {} table is not supported by this version of the {} schema: {}";
 
     private static InstanceIdentifierCodec instanceIdentifierCodec;
 
@@ -71,20 +81,6 @@ public class HwvtepSouthboundUtil {
         return node;
     }
 
-    public static <D extends org.opendaylight.yangtools.yang.binding.DataObject> boolean deleteNode(
-                    ReadWriteTransaction transaction, final InstanceIdentifier<D> connectionIid) {
-        boolean result = false;
-        transaction.delete(LogicalDatastoreType.OPERATIONAL, connectionIid);
-        CheckedFuture<Void, TransactionCommitFailedException> future = transaction.submit();
-        try {
-            future.checkedGet();
-            result = true;
-        } catch (TransactionCommitFailedException e) {
-            LOG.warn("Failed to delete {} ", connectionIid, e);
-        }
-        return result;
-    }
-
     public static Optional<HwvtepGlobalAugmentation> getManagingNode(DataBroker db,
                     HwvtepPhysicalSwitchAttributes pNode) {
         Preconditions.checkNotNull(pNode);
@@ -102,25 +98,7 @@ public class HwvtepSouthboundUtil {
         return result;
     }
 
-    public static Optional<HwvtepGlobalAugmentation> getManagingNode(DataBroker db,
-                    HwvtepLogicalSwitchAttributes lNode) {
-        Preconditions.checkNotNull(lNode);
-        Optional<HwvtepGlobalAugmentation> result = null;
-        // TODO: Add managed-by to hwvtep-logical-switch-attributes in
-        // hwvtep.yang
-        /*
-         * HwvtepGlobalRef ref = lNode.getManagedBy(); if (ref != null &&
-         * ref.getValue() != null) { result = getManagingNode(db, ref); } else {
-         * LOG.warn(
-         * "Cannot find client for LogicalSwitch without a specified ManagedBy {}"
-         * , pNode); return Optional.absent(); } if(!result.isPresent()) {
-         * LOG.warn("Failed to find managing node for PhysicalSwitch {}",
-         * pNode); } return result;
-         */
-        return Optional.absent(); // TODO: Delete this once yang is updated
-    }
-
-    private static Optional<HwvtepGlobalAugmentation> getManagingNode(DataBroker db, HwvtepGlobalRef ref) {
+    public static Optional<HwvtepGlobalAugmentation> getManagingNode(DataBroker db, HwvtepGlobalRef ref) {
         try {
             ReadOnlyTransaction transaction = db.newReadOnlyTransaction();
             @SuppressWarnings("unchecked")
@@ -156,5 +134,75 @@ public class HwvtepSouthboundUtil {
             return Optional.absent();
         }
     }
+    public static String connectionInfoToString(final ConnectionInfo connectionInfo) {
+        return String.valueOf(
+                connectionInfo.getRemoteIp().getValue()) + ":" + connectionInfo.getRemotePort().getValue();
+    }
+
+
+    public static void schemaMismatchLog(String column, String table, SchemaVersionMismatchException ex) {
+        LOG.debug(SCHEMA_VERSION_MISMATCH, column, table, "hw_vtep", ex.getMessage());
+    }
+
+    public static <KeyType, D> void updateData(Map<Class<? extends Identifiable>, Map<KeyType, D>> map,
+                                               Class<? extends Identifiable> cls, KeyType key, D data) {
+        if (key == null) {
+            return;
+        }
+        if (!map.containsKey(cls)) {
+            map.put(cls, new ConcurrentHashMap<>());
+        }
+        map.get(cls).put(key, data);
+    }
+
+    public static <KeyType, D> D getData(Map<Class<? extends Identifiable>, Map<KeyType, D>> map,
+                                         Class<? extends Identifiable> cls, KeyType key) {
+        if (key == null) {
+            return null;
+        }
+        if (map.containsKey(cls)) {
+            return map.get(cls).get(key);
+        }
+        return null;
+    }
+
+    public static <KeyType, D> boolean containsKey(Map<Class<? extends Identifiable>, Map<KeyType, D>> map,
+                                                   Class<? extends Identifiable> cls, KeyType key) {
+        if (key == null) {
+            return false;
+        }
+        if (map.containsKey(cls)) {
+            return map.get(cls).containsKey(key);
+        }
+        return false;
+    }
+
+    public static <KeyType, D> void clearData(Map<Class<? extends Identifiable>, Map<KeyType, D>> map,
+                                              Class<? extends Identifiable> cls, KeyType key) {
+        if (key == null) {
+            return;
+        }
+        if (map.containsKey(cls)) {
+            map.get(cls).remove(key);
+        }
+    }
 
+    public static <T> boolean isEmpty(Collection<T> list) {
+        return list == null || list.isEmpty();
+    }
+
+    public static boolean isEmptyMap(Map map) {
+        return map == null || map.isEmpty();
+    }
+
+    public static InstanceIdentifier<Node> getGlobalNodeIid(final InstanceIdentifier<Node> physicalNodeIid) {
+        String nodeId = physicalNodeIid.firstKeyOf(Node.class).getNodeId().getValue();
+        int physicalSwitchIndex = nodeId.indexOf(HwvtepSouthboundConstants.PSWITCH_URI_PREFIX);
+        if (physicalSwitchIndex > 0) {
+            nodeId = nodeId.substring(0, physicalSwitchIndex - 1);
+        } else {
+            return null;
+        }
+        return physicalNodeIid.firstIdentifierOf(Topology.class).child(Node.class , new NodeKey(new NodeId(nodeId)));
+    }
 }