Merge "Fixed Sonar issue in SubDelivery.Java"
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / provisioning / beans / NodeClass.java
1 /*******************************************************************************\r
2  * ============LICENSE_START==================================================\r
3  * * org.onap.dmaap\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6   * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * *\r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * *\r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 \r
24 package org.onap.dmaap.datarouter.provisioning.beans;\r
25 \r
26 import java.sql.Connection;\r
27 import java.sql.PreparedStatement;\r
28 import java.sql.ResultSet;\r
29 import java.sql.SQLException;\r
30 import java.util.Collection;\r
31 import java.util.HashMap;\r
32 import java.util.Map;\r
33 import java.util.Set;\r
34 import java.util.TreeSet;\r
35 import org.apache.log4j.Logger;\r
36 import org.onap.dmaap.datarouter.provisioning.utils.DB;\r
37 \r
38 /**\r
39  * This class is used to aid in the mapping of node names from/to node IDs.\r
40  *\r
41  * @author Robert P. Eby\r
42  * @version $Id: NodeClass.java,v 1.2 2014/01/15 16:08:43 eby Exp $\r
43  */\r
44 public abstract class NodeClass extends Syncable {\r
45 \r
46     private static Map<String, Integer> map;\r
47     private static Logger intLogger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal");\r
48     public NodeClass() {\r
49         // init on first use\r
50         if (map == null) {\r
51             reload();\r
52         }\r
53     }\r
54 \r
55     /**\r
56      * Add nodes to the NODES table, when the NODES parameter value is changed. Nodes are only added to the table, they\r
57      * are never deleted.  The node name is normalized to contain the domain (if missing).\r
58      *\r
59      * @param nodes a pipe separated list of the current nodes\r
60      */\r
61     public static void setNodes(String[] nodes) {\r
62         if (map == null) {\r
63             reload();\r
64         }\r
65         int nextid = 0;\r
66         for (Integer n : map.values()) {\r
67             if (n >= nextid) {\r
68                 nextid = n + 1;\r
69             }\r
70         }\r
71         // take | separated list, add domain if needed.\r
72 \r
73         for (String node : nodes) {\r
74             node = normalizeNodename(node);\r
75             if (!map.containsKey(node)) {\r
76                 intLogger.info("..adding " + node + " to NODES with index " + nextid);\r
77                 map.put(node, nextid);\r
78                 PreparedStatement ps = null;\r
79                 try {\r
80                     DB db = new DB();\r
81                     @SuppressWarnings("resource")\r
82                     Connection conn = db.getConnection();\r
83                     ps = conn.prepareStatement("insert into NODES (NODEID, NAME, ACTIVE) values (?, ?, 1)");\r
84                     ps.setInt(1, nextid);\r
85                     ps.setString(2, node);\r
86                     ps.execute();\r
87                     ps.close();\r
88                     db.release(conn);\r
89                 } catch (SQLException e) {\r
90                     intLogger.error("PROV0005 doInsert: " + e.getMessage(),e);\r
91                 } finally {\r
92                     try {\r
93                         if(ps!=null){\r
94                             ps.close();\r
95                         }\r
96                     } catch (SQLException e) {\r
97                         intLogger.error("Error in closing PreparedStatement: " + e.getMessage(),e);\r
98                     }\r
99                 }\r
100                 nextid++;\r
101             }\r
102         }\r
103     }\r
104 \r
105     public static void reload() {\r
106         Map<String, Integer> m = new HashMap<String, Integer>();\r
107         PreparedStatement ps = null;\r
108         ResultSet rs=null;\r
109         try {\r
110             DB db = new DB();\r
111             @SuppressWarnings("resource")\r
112             Connection conn = db.getConnection();\r
113             String sql = "select NODEID, NAME from NODES";\r
114             ps = conn.prepareStatement(sql);\r
115             rs = ps.executeQuery();\r
116             while (rs.next()) {\r
117                 int id = rs.getInt("NODEID");\r
118                 String name = rs.getString("NAME");\r
119                 m.put(name, id);\r
120             }\r
121             rs.close();\r
122             ps.close();\r
123             db.release(conn);\r
124         } catch (SQLException e) {\r
125             intLogger.error("PROV0005 doInsert: " + e.getMessage(),e);\r
126         } finally {\r
127             try {\r
128                 if(ps!=null){\r
129                     ps.close();\r
130                 }\r
131                 if(rs!=null){\r
132                     rs.close();\r
133                 }\r
134 \r
135             } catch (SQLException e) {\r
136                 intLogger.error("PROV0005 doInsert: " + e.getMessage(),e);\r
137             }\r
138         }\r
139         map = m;\r
140     }\r
141 \r
142     public static Integer lookupNodeName(final String name) {\r
143         Integer n = map.get(name);\r
144         if (n == null) {\r
145             throw new IllegalArgumentException("Invalid node name: " + name);\r
146         }\r
147         return n;\r
148     }\r
149 \r
150     public static Collection<String> lookupNodeNames(String patt) {\r
151         Collection<String> coll = new TreeSet<String>();\r
152         final Set<String> keyset = map.keySet();\r
153         for (String s : patt.toLowerCase().split(",")) {\r
154             if (s.endsWith("*")) {\r
155                 s = s.substring(0, s.length() - 1);\r
156                 for (String s2 : keyset) {\r
157                     if (s2.startsWith(s)) {\r
158                         coll.add(s2);\r
159                     }\r
160                 }\r
161             } else if (keyset.contains(s)) {\r
162                 coll.add(s);\r
163             } else if (keyset.contains(normalizeNodename(s))) {\r
164                 coll.add(normalizeNodename(s));\r
165             } else {\r
166                 throw new IllegalArgumentException("Invalid node name: " + s);\r
167             }\r
168         }\r
169         return coll;\r
170     }\r
171 \r
172     public static String normalizeNodename(String s) {\r
173         if (s != null && s.indexOf('.') <= 0) {\r
174             Parameters p = Parameters.getParameter(Parameters.PROV_DOMAIN);\r
175             if (p != null) {\r
176                 String domain = p.getValue();\r
177                 s += "." + domain;\r
178             }\r
179             return s.toLowerCase();\r
180         }\r
181         else{\r
182             return s;\r
183         }\r
184 \r
185     }\r
186 \r
187     protected String lookupNodeID(int n) {\r
188         for (String s : map.keySet()) {\r
189             if (map.get(s) == n) {\r
190                 return s;\r
191             }\r
192         }\r
193         return null;\r
194     }\r
195 }\r