* * org.onap.dmaap\r
* * ===========================================================================\r
* * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
- * * ===========================================================================\r
+ * * ===========================================================================\r
* * Licensed under the Apache License, Version 2.0 (the "License");\r
* * you may not use this file except in compliance with the License.\r
* * You may obtain a copy of the License at\r
import java.util.Map;\r
import java.util.Set;\r
import java.util.TreeSet;\r
-import org.onap.dmaap.datarouter.provisioning.utils.DB;\r
+import org.onap.dmaap.datarouter.provisioning.utils.ProvDbUtils;\r
\r
/**\r
* This class is used to aid in the mapping of node names from/to node IDs.\r
public abstract class NodeClass extends Syncable {\r
\r
private static final String PROV_0005_DO_INSERT = "PROV0005 doInsert: ";\r
- private static Map<String, Integer> map;\r
+ private static Map<String, Integer> nodesMap;\r
private static EELFLogger intLogger = EELFManager.getInstance().getLogger("InternalLog");\r
\r
NodeClass() {\r
// init on first use\r
- if (map == null) {\r
+ if (nodesMap == null) {\r
reload();\r
}\r
}\r
* @param nodes a pipe separated list of the current nodes\r
*/\r
public static void setNodes(String[] nodes) {\r
- if (map == null) {\r
+ if (nodesMap == null) {\r
reload();\r
}\r
int nextid = 0;\r
- for (Integer n : map.values()) {\r
+ for (Integer n : nodesMap.values()) {\r
if (n >= nextid) {\r
nextid = n + 1;\r
}\r
\r
for (String node : nodes) {\r
node = normalizeNodename(node);\r
- if (!map.containsKey(node)) {\r
+ if (!nodesMap.containsKey(node)) {\r
intLogger.info("..adding " + node + " to NODES with index " + nextid);\r
- map.put(node, nextid);\r
+ nodesMap.put(node, nextid);\r
insertNodesToTable(nextid, node);\r
nextid++;\r
}\r
}\r
\r
private static void insertNodesToTable(int nextid, String node) {\r
- DB db = new DB();\r
- try (Connection conn = db.getConnection()) {\r
- try (PreparedStatement ps = conn\r
- .prepareStatement("insert into NODES (NODEID, NAME, ACTIVE) values (?, ?, 1)")) {\r
- ps.setInt(1, nextid);\r
- ps.setString(2, node);\r
- ps.execute();\r
- } finally {\r
- db.release(conn);\r
- }\r
+ try (Connection conn = ProvDbUtils.getInstance().getConnection();\r
+ PreparedStatement ps = conn.prepareStatement(\r
+ "insert into NODES (NODEID, NAME, ACTIVE) values (?, ?, 1)")) {\r
+ ps.setInt(1, nextid);\r
+ ps.setString(2, node);\r
+ ps.execute();\r
} catch (SQLException e) {\r
intLogger.error(PROV_0005_DO_INSERT + e.getMessage(), e);\r
}\r
}\r
\r
private static void reload() {\r
- Map<String, Integer> hmap = new HashMap<>();\r
- String sql = "select NODEID, NAME from NODES";\r
- DB db = new DB();\r
- try (Connection conn = db.getConnection();\r
- PreparedStatement ps = conn.prepareStatement(sql)) {\r
- try (ResultSet rs = ps.executeQuery()) {\r
- while (rs.next()) {\r
- int id = rs.getInt("NODEID");\r
- String name = rs.getString("NAME");\r
- hmap.put(name, id);\r
- }\r
- } finally {\r
- db.release(conn);\r
+ Map<String, Integer> tmpNodesMap = new HashMap<>();\r
+ try (Connection conn = ProvDbUtils.getInstance().getConnection();\r
+ PreparedStatement ps = conn.prepareStatement("select NODEID, NAME from NODES");\r
+ ResultSet rs = ps.executeQuery()) {\r
+ while (rs.next()) {\r
+ int id = rs.getInt("NODEID");\r
+ String name = rs.getString("NAME");\r
+ tmpNodesMap.put(name, id);\r
}\r
} catch (SQLException e) {\r
intLogger.error(PROV_0005_DO_INSERT + e.getMessage(),e);\r
}\r
- map = hmap;\r
+ nodesMap = tmpNodesMap;\r
}\r
\r
static Integer lookupNodeName(final String name) {\r
- Integer nodeName = map.get(name);\r
+ Integer nodeName = nodesMap.get(name);\r
if (nodeName == null) {\r
throw new IllegalArgumentException("Invalid node name: " + name);\r
}\r
*/\r
public static Collection<String> lookupNodeNames(String patt) {\r
Collection<String> coll = new TreeSet<>();\r
- final Set<String> keyset = map.keySet();\r
+ final Set<String> keyset = nodesMap.keySet();\r
for (String s : patt.toLowerCase().split(",")) {\r
if (s.endsWith("*")) {\r
addNodeToCollection(coll, keyset, s);\r
}\r
\r
String lookupNodeID(int node) {\r
- for (Map.Entry<String, Integer> entry : map.entrySet()) {\r
+ for (Map.Entry<String, Integer> entry : nodesMap.entrySet()) {\r
if (entry.getValue() == node) {\r
return entry.getKey();\r
}\r