X-Git-Url: https://gerrit.onap.org/r/gitweb?p=dmaap%2Fdatarouter.git;a=blobdiff_plain;f=datarouter-prov%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fdmaap%2Fdatarouter%2Fprovisioning%2Fbeans%2FNodeClass.java;h=ef491cab1f10cd2ffae99b2c12727d9362a5ad77;hp=4c140490621710bcc84f7c88517d402253f125f0;hb=bc1df610cddfb558cf6bde90c269b4af59768648;hpb=d8563ed0441c02a81185e2be29afa3d1ba219c2a diff --git a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/NodeClass.java b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/NodeClass.java old mode 100644 new mode 100755 index 4c140490..ef491cab --- a/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/NodeClass.java +++ b/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/NodeClass.java @@ -23,6 +23,8 @@ package org.onap.dmaap.datarouter.provisioning.beans; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -32,8 +34,6 @@ import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.TreeSet; - -import org.apache.log4j.Logger; import org.onap.dmaap.datarouter.provisioning.utils.DB; /** @@ -42,10 +42,14 @@ import org.onap.dmaap.datarouter.provisioning.utils.DB; * @author Robert P. Eby * @version $Id: NodeClass.java,v 1.2 2014/01/15 16:08:43 eby Exp $ */ + public abstract class NodeClass extends Syncable { + + private static final String PROV_0005_DO_INSERT = "PROV0005 doInsert: "; private static Map map; - private static Logger intLogger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal"); - public NodeClass() { + private static EELFLogger intLogger = EELFManager.getInstance().getLogger("InternalLog"); + + NodeClass() { // init on first use if (map == null) { reload(); @@ -53,19 +57,20 @@ public abstract class NodeClass extends Syncable { } /** - * Add nodes to the NODES table, when the NODES parameter value is changed. - * Nodes are only added to the table, they are never deleted. The node name is normalized - * to contain the domain (if missing). + * Add nodes to the NODES table, when the NODES parameter value is changed. Nodes are only added to the table, they + * are never deleted. The node name is normalized to contain the domain (if missing). * * @param nodes a pipe separated list of the current nodes */ public static void setNodes(String[] nodes) { - if (map == null) + if (map == null) { reload(); + } int nextid = 0; for (Integer n : map.values()) { - if (n >= nextid) + if (n >= nextid) { nextid = n + 1; + } } // take | separated list, add domain if needed. @@ -74,83 +79,68 @@ public abstract class NodeClass extends Syncable { if (!map.containsKey(node)) { intLogger.info("..adding " + node + " to NODES with index " + nextid); map.put(node, nextid); - PreparedStatement ps = null; - try { - DB db = new DB(); - @SuppressWarnings("resource") - Connection conn = db.getConnection(); - ps = conn.prepareStatement("insert into NODES (NODEID, NAME, ACTIVE) values (?, ?, 1)"); - ps.setInt(1, nextid); - ps.setString(2, node); - ps.execute(); - ps.close(); - db.release(conn); - } catch (SQLException e) { - intLogger.error("PROV0005 doInsert: " + e.getMessage(),e); - } finally { - try { - if(ps!=null){ - ps.close(); - } - } catch (SQLException e) { - intLogger.error("Error in closing PreparedStatement: " + e.getMessage(),e); - } - } + insertNodesToTable(nextid, node); nextid++; } } } - public static void reload() { - Map m = new HashMap(); - PreparedStatement ps = null; - try { - DB db = new DB(); - @SuppressWarnings("resource") - Connection conn = db.getConnection(); - String sql = "select NODEID, NAME from NODES"; - ps = conn.prepareStatement(sql); - ResultSet rs = ps.executeQuery(); - while (rs.next()) { - int id = rs.getInt("NODEID"); - String name = rs.getString("NAME"); - m.put(name, id); + private static void insertNodesToTable(int nextid, String node) { + DB db = new DB(); + try (Connection conn = db.getConnection()) { + try (PreparedStatement ps = conn + .prepareStatement("insert into NODES (NODEID, NAME, ACTIVE) values (?, ?, 1)")) { + ps.setInt(1, nextid); + ps.setString(2, node); + ps.execute(); + } finally { + db.release(conn); } - rs.close(); - ps.close(); - db.release(conn); } catch (SQLException e) { - intLogger.error("PROV0005 doInsert: " + e.getMessage(),e); - } finally { - try { - if(ps!=null){ - ps.close(); - } + intLogger.error(PROV_0005_DO_INSERT + e.getMessage(), e); + } + } - } catch (SQLException e) { - intLogger.error("PROV0005 doInsert: " + e.getMessage(),e); + private static void reload() { + Map hmap = new HashMap<>(); + String sql = "select NODEID, NAME from NODES"; + DB db = new DB(); + try (Connection conn = db.getConnection(); + PreparedStatement ps = conn.prepareStatement(sql)) { + try (ResultSet rs = ps.executeQuery()) { + while (rs.next()) { + int id = rs.getInt("NODEID"); + String name = rs.getString("NAME"); + hmap.put(name, id); + } + } finally { + db.release(conn); } + } catch (SQLException e) { + intLogger.error(PROV_0005_DO_INSERT + e.getMessage(),e); } - map = m; + map = hmap; } - public static Integer lookupNodeName(final String name) { - Integer n = map.get(name); - if (n == null) + static Integer lookupNodeName(final String name) { + Integer nodeName = map.get(name); + if (nodeName == null) { throw new IllegalArgumentException("Invalid node name: " + name); - return n; + } + return nodeName; } + /** + * Get node names. + * @param patt pattern to search + * @return collection of node names + */ public static Collection lookupNodeNames(String patt) { - Collection coll = new TreeSet(); + Collection coll = new TreeSet<>(); final Set keyset = map.keySet(); for (String s : patt.toLowerCase().split(",")) { if (s.endsWith("*")) { - s = s.substring(0, s.length() - 1); - for (String s2 : keyset) { - if (s2.startsWith(s)) - coll.add(s2); - } + addNodeToCollection(coll, keyset, s); } else if (keyset.contains(s)) { coll.add(s); } else if (keyset.contains(normalizeNodename(s))) { @@ -162,26 +152,40 @@ public abstract class NodeClass extends Syncable { return coll; } - protected String lookupNodeID(int n) { - for (String s : map.keySet()) { - if (map.get(s) == n) - return s; + private static void addNodeToCollection(Collection coll, Set keyset, String str) { + str = str.substring(0, str.length() - 1); + for (String s2 : keyset) { + if (s2.startsWith(str)) { + coll.add(s2); + } } - return null; } - public static String normalizeNodename(String s) { - if (s != null && s.indexOf('.') <= 0) { - Parameters p = Parameters.getParameter(Parameters.PROV_DOMAIN); - if (p != null) { - String domain = p.getValue(); - s += "." + domain; + /** + * Method to add domain name. + * @param str nde name string + * @return normalized node name + */ + public static String normalizeNodename(String str) { + if (str != null && str.indexOf('.') <= 0) { + Parameters param = Parameters.getParameter(Parameters.PROV_DOMAIN); + if (param != null) { + String domain = param.getValue(); + str += "." + domain; } - return s.toLowerCase(); - } - else{ - return s; + return str.toLowerCase(); + } else { + return str; } } + + String lookupNodeID(int node) { + for (Map.Entry entry : map.entrySet()) { + if (entry.getValue() == node) { + return entry.getKey(); + } + } + return null; + } }