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=19cbf55c9a858190323f7012703272d2fe8be3fb;hp=d11c20fbf2c1aaf190e5c6fe1f0357edfb820a8a;hb=68a9ca240970fceaf12bbe91b7bad8e1d98ecd93;hpb=f20778ffa99aa9c6f30a0f84112a5392b259ea63 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 index d11c20fb..19cbf55c 100755 --- 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 @@ -3,7 +3,7 @@ * * org.onap.dmaap * * =========================================================================== * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * * =========================================================================== + * * =========================================================================== * * Licensed under the Apache License, Version 2.0 (the "License"); * * you may not use this file except in compliance with the License. * * You may obtain a copy of the License at @@ -34,7 +34,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.TreeSet; -import org.onap.dmaap.datarouter.provisioning.utils.DB; +import org.onap.dmaap.datarouter.provisioning.utils.ProvDbUtils; /** * This class is used to aid in the mapping of node names from/to node IDs. @@ -42,15 +42,16 @@ 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 Map nodesMap; private static EELFLogger intLogger = EELFManager.getInstance().getLogger("InternalLog"); NodeClass() { // init on first use - if (map == null) { + if (nodesMap == null) { reload(); } } @@ -62,11 +63,11 @@ public abstract class NodeClass extends Syncable { * @param nodes a pipe separated list of the current nodes */ public static void setNodes(String[] nodes) { - if (map == null) { + if (nodesMap == null) { reload(); } int nextid = 0; - for (Integer n : map.values()) { + for (Integer n : nodesMap.values()) { if (n >= nextid) { nextid = n + 1; } @@ -75,9 +76,9 @@ public abstract class NodeClass extends Syncable { for (String node : nodes) { node = normalizeNodename(node); - if (!map.containsKey(node)) { + if (!nodesMap.containsKey(node)) { intLogger.info("..adding " + node + " to NODES with index " + nextid); - map.put(node, nextid); + nodesMap.put(node, nextid); insertNodesToTable(nextid, node); nextid++; } @@ -85,53 +86,49 @@ public abstract class NodeClass extends Syncable { } 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); - } + try (Connection conn = ProvDbUtils.getInstance().getConnection(); + PreparedStatement ps = conn.prepareStatement( + "insert into NODES (NODEID, NAME, ACTIVE) values (?, ?, 1)")) { + ps.setInt(1, nextid); + ps.setString(2, node); + ps.execute(); } catch (SQLException e) { intLogger.error(PROV_0005_DO_INSERT + e.getMessage(), e); } } private static void reload() { - Map m = 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"); - m.put(name, id); - } - } finally { - db.release(conn); + Map tmpNodesMap = new HashMap<>(); + try (Connection conn = ProvDbUtils.getInstance().getConnection(); + PreparedStatement ps = conn.prepareStatement("select NODEID, NAME from NODES"); + ResultSet rs = ps.executeQuery()) { + while (rs.next()) { + int id = rs.getInt("NODEID"); + String name = rs.getString("NAME"); + tmpNodesMap.put(name, id); } } catch (SQLException e) { intLogger.error(PROV_0005_DO_INSERT + e.getMessage(),e); } - map = m; + nodesMap = tmpNodesMap; } static Integer lookupNodeName(final String name) { - Integer n = map.get(name); - if (n == null) { + Integer nodeName = nodesMap.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<>(); - final Set keyset = map.keySet(); + final Set keyset = nodesMap.keySet(); for (String s : patt.toLowerCase().split(",")) { if (s.endsWith("*")) { addNodeToCollection(coll, keyset, s); @@ -146,32 +143,37 @@ public abstract class NodeClass extends Syncable { return coll; } - private static void addNodeToCollection(Collection coll, Set keyset, String s) { - s = s.substring(0, s.length() - 1); + private static void addNodeToCollection(Collection coll, Set keyset, String str) { + str = str.substring(0, str.length() - 1); for (String s2 : keyset) { - if (s2.startsWith(s)) { + if (s2.startsWith(str)) { coll.add(s2); } } } - 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(); + return str.toLowerCase(); } else { - return s; + return str; } } - String lookupNodeID(int n) { - for (Map.Entry entry : map.entrySet()) { - if (entry.getValue() == n) { + String lookupNodeID(int node) { + for (Map.Entry entry : nodesMap.entrySet()) { + if (entry.getValue() == node) { return entry.getKey(); } }