X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=datarouter-prov%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fdmaap%2Fdatarouter%2Fprovisioning%2Fbeans%2FNodeClass.java;h=9d43bd0aafdf77432cfb0538f91dc681b7e4119b;hb=28d544f0c3b997da5628d4dcdb8e6773a0440cb4;hp=28c3ec2839187b77e7e73746bcaae0f9524b5c18;hpb=8cbe8a88bc6dfe8673a33a017fe6a5a3e7ce86c3;p=dmaap%2Fdatarouter.git 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 28c3ec28..9d43bd0a --- 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 @@ -32,7 +32,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; @@ -43,8 +42,9 @@ 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; + private static Logger intLogger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal"); public NodeClass() { // init on first use if (map == null) { @@ -53,26 +53,27 @@ 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. - 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); + intLogger.info("..adding " + node + " to NODES with index " + nextid); map.put(node, nextid); PreparedStatement ps = null; try { @@ -86,13 +87,14 @@ public abstract class NodeClass extends Syncable { ps.close(); db.release(conn); } catch (SQLException e) { - intlogger.warn("PROV0005 doInsert: " + e.getMessage()); - e.printStackTrace(); + intLogger.error("PROV0005 doInsert: " + e.getMessage(),e); } finally { try { - ps.close(); + if(ps!=null){ + ps.close(); + } } catch (SQLException e) { - e.printStackTrace(); + intLogger.error("Error in closing PreparedStatement: " + e.getMessage(),e); } } nextid++; @@ -103,49 +105,54 @@ public abstract class NodeClass extends Syncable { 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); + try(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(); + intLogger.error("PROV0005 doInsert: " + e.getMessage(),e); } finally { try { - ps.close(); + if(ps!=null){ + ps.close(); + } } catch (SQLException e) { - e.printStackTrace(); + intLogger.error("PROV0005 doInsert: " + e.getMessage(),e); } } map = m; } - public static Integer lookupNodeName(final String name) throws IllegalArgumentException { + public static Integer lookupNodeName(final String name) { Integer n = map.get(name); - if (n == null) + if (n == null) { throw new IllegalArgumentException("Invalid node name: " + name); + } return n; } - public static Collection lookupNodeNames(String patt) throws IllegalArgumentException { + public static Collection lookupNodeNames(String patt) { 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)) + if (s2.startsWith(s)) { coll.add(s2); + } } } else if (keyset.contains(s)) { coll.add(s); @@ -158,14 +165,6 @@ 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; - } - return null; - } - public static String normalizeNodename(String s) { if (s != null && s.indexOf('.') <= 0) { Parameters p = Parameters.getParameter(Parameters.PROV_DOMAIN); @@ -173,7 +172,20 @@ public abstract class NodeClass extends Syncable { String domain = p.getValue(); s += "." + domain; } + return s.toLowerCase(); + } + else{ + return s; } - return s.toLowerCase(); + + } + + protected String lookupNodeID(int n) { + for (String s : map.keySet()) { + if (map.get(s) == n) { + return s; + } + } + return null; } }