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=28c3ec2839187b77e7e73746bcaae0f9524b5c18;hp=e3dd85245befbbde09b693af426d50385ca6c5ab;hb=4261823d84c2b911b68cdf4cb4dc3be429ebe285;hpb=13639e1b05d8c8b5b1e9efd543573834501aefaa 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 e3dd8524..28c3ec28 100644 --- 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 @@ -7,9 +7,9 @@ * * 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 - * * + * * * * http://www.apache.org/licenses/LICENSE-2.0 - * * + * * * * Unless required by applicable law or agreed to in writing, software * * distributed under the License is distributed on an "AS IS" BASIS, * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -43,136 +43,137 @@ import org.onap.dmaap.datarouter.provisioning.utils.DB; * @version $Id: NodeClass.java,v 1.2 2014/01/15 16:08:43 eby Exp $ */ public abstract class NodeClass extends Syncable { - private static Map map; + private static Map map; - public NodeClass() { - // init on first use - if (map == null) { - reload(); - } - } + public NodeClass() { + // init on first use + if (map == null) { + reload(); + } + } - /** - * 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) - reload(); - int nextid = 0; - for (Integer n : map.values()) { - if (n >= nextid) - nextid = n+1; - } - // take | separated list, add domain if needed. - Logger intlogger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal"); - for (String node : nodes) { - node = normalizeNodename(node); - 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.warn("PROV0005 doInsert: "+e.getMessage()); - e.printStackTrace(); - } finally { - try { - ps.close(); - } catch (SQLException e) { - e.printStackTrace(); - } - } - nextid++; - } - } - } + /** + * 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) + reload(); + int nextid = 0; + for (Integer n : map.values()) { + if (n >= nextid) + nextid = n + 1; + } + // take | separated list, add domain if needed. + Logger intlogger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal"); + for (String node : nodes) { + node = normalizeNodename(node); + 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.warn("PROV0005 doInsert: " + e.getMessage()); + e.printStackTrace(); + } finally { + try { + ps.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + 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); - } - rs.close(); - ps.close(); - db.release(conn); - } catch (SQLException e) { - e.printStackTrace(); - } finally { - try { - ps.close(); - } catch (SQLException e) { - e.printStackTrace(); - } - } - map = m; - } + 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); + } + rs.close(); + ps.close(); + db.release(conn); + } catch (SQLException e) { + e.printStackTrace(); + } finally { + try { + ps.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + map = m; + } - public static Integer lookupNodeName(final String name) throws IllegalArgumentException { - Integer n = map.get(name); - if (n == null) - throw new IllegalArgumentException("Invalid node name: "+name); - return n; - } + public static Integer lookupNodeName(final String name) throws IllegalArgumentException { + Integer n = map.get(name); + if (n == null) + throw new IllegalArgumentException("Invalid node name: " + name); + return n; + } - public static Collection lookupNodeNames(String patt) throws IllegalArgumentException { - 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); - } - } else if (keyset.contains(s)) { - coll.add(s); - } else if (keyset.contains(normalizeNodename(s))) { - coll.add(normalizeNodename(s)); - } else { - throw new IllegalArgumentException("Invalid node name: "+s); - } - } - return coll; - } + public static Collection lookupNodeNames(String patt) throws IllegalArgumentException { + 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); + } + } else if (keyset.contains(s)) { + coll.add(s); + } else if (keyset.contains(normalizeNodename(s))) { + coll.add(normalizeNodename(s)); + } else { + throw new IllegalArgumentException("Invalid node name: " + s); + } + } + return coll; + } - protected String lookupNodeID(int n) { - for (String s : map.keySet()) { - if (map.get(s) == n) - return s; - } - return null; - } + protected String lookupNodeID(int n) { + for (String s : map.keySet()) { + if (map.get(s) == n) + return s; + } + 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; - } - } - return s.toLowerCase(); - } + 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; + } + } + return s.toLowerCase(); + } }