Merge "Add LOGJSONObject Unit Tests Issue-ID: DMAAP-101"
[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 \r
36 import org.apache.log4j.Logger;\r
37 import org.onap.dmaap.datarouter.provisioning.utils.DB;\r
38 \r
39 /**\r
40  * This class is used to aid in the mapping of node names from/to node IDs.\r
41  *\r
42  * @author Robert P. Eby\r
43  * @version $Id: NodeClass.java,v 1.2 2014/01/15 16:08:43 eby Exp $\r
44  */\r
45 public abstract class NodeClass extends Syncable {\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.\r
57      * Nodes are only added to the table, they are never deleted.  The node name is normalized\r
58      * to contain the domain (if missing).\r
59      *\r
60      * @param nodes a pipe separated list of the current nodes\r
61      */\r
62     public static void setNodes(String[] nodes) {\r
63         if (map == null)\r
64             reload();\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         // take | separated list, add domain if needed.\r
71 \r
72         for (String node : nodes) {\r
73             node = normalizeNodename(node);\r
74             if (!map.containsKey(node)) {\r
75                 intLogger.info("..adding " + node + " to NODES with index " + nextid);\r
76                 map.put(node, nextid);\r
77                 PreparedStatement ps = null;\r
78                 try {\r
79                     DB db = new DB();\r
80                     @SuppressWarnings("resource")\r
81                     Connection conn = db.getConnection();\r
82                     ps = conn.prepareStatement("insert into NODES (NODEID, NAME, ACTIVE) values (?, ?, 1)");\r
83                     ps.setInt(1, nextid);\r
84                     ps.setString(2, node);\r
85                     ps.execute();\r
86                     ps.close();\r
87                     db.release(conn);\r
88                 } catch (SQLException e) {\r
89                     intLogger.error("PROV0005 doInsert: " + e.getMessage(),e);\r
90                 } finally {\r
91                     try {\r
92                         if(ps!=null){\r
93                             ps.close();\r
94                         }\r
95                     } catch (SQLException e) {\r
96                         intLogger.error("Error in closing PreparedStatement: " + e.getMessage(),e);\r
97                     }\r
98                 }\r
99                 nextid++;\r
100             }\r
101         }\r
102     }\r
103 \r
104     public static void reload() {\r
105         Map<String, Integer> m = new HashMap<String, Integer>();\r
106         PreparedStatement ps = null;\r
107         try {\r
108             DB db = new DB();\r
109             @SuppressWarnings("resource")\r
110             Connection conn = db.getConnection();\r
111             String sql = "select NODEID, NAME from NODES";\r
112             ps = conn.prepareStatement(sql);\r
113             ResultSet rs = ps.executeQuery();\r
114             while (rs.next()) {\r
115                 int id = rs.getInt("NODEID");\r
116                 String name = rs.getString("NAME");\r
117                 m.put(name, id);\r
118             }\r
119             rs.close();\r
120             ps.close();\r
121             db.release(conn);\r
122         } catch (SQLException e) {\r
123             intLogger.error("PROV0005 doInsert: " + e.getMessage(),e);\r
124         } finally {\r
125             try {\r
126                 if(ps!=null){\r
127                     ps.close();\r
128                 }\r
129 \r
130             } catch (SQLException e) {\r
131                 intLogger.error("PROV0005 doInsert: " + e.getMessage(),e);\r
132             }\r
133         }\r
134         map = m;\r
135     }\r
136 \r
137     public static Integer lookupNodeName(final String name) {\r
138         Integer n = map.get(name);\r
139         if (n == null)\r
140             throw new IllegalArgumentException("Invalid node name: " + name);\r
141         return n;\r
142     }\r
143 \r
144     public static Collection<String> lookupNodeNames(String patt) {\r
145         Collection<String> coll = new TreeSet<String>();\r
146         final Set<String> keyset = map.keySet();\r
147         for (String s : patt.toLowerCase().split(",")) {\r
148             if (s.endsWith("*")) {\r
149                 s = s.substring(0, s.length() - 1);\r
150                 for (String s2 : keyset) {\r
151                     if (s2.startsWith(s))\r
152                         coll.add(s2);\r
153                 }\r
154             } else if (keyset.contains(s)) {\r
155                 coll.add(s);\r
156             } else if (keyset.contains(normalizeNodename(s))) {\r
157                 coll.add(normalizeNodename(s));\r
158             } else {\r
159                 throw new IllegalArgumentException("Invalid node name: " + s);\r
160             }\r
161         }\r
162         return coll;\r
163     }\r
164 \r
165     protected String lookupNodeID(int n) {\r
166         for (String s : map.keySet()) {\r
167             if (map.get(s) == n)\r
168                 return s;\r
169         }\r
170         return null;\r
171     }\r
172 \r
173     public static String normalizeNodename(String s) {\r
174         if (s != null && s.indexOf('.') <= 0) {\r
175             Parameters p = Parameters.getParameter(Parameters.PROV_DOMAIN);\r
176             if (p != null) {\r
177                 String domain = p.getValue();\r
178                 s += "." + domain;\r
179             }\r
180             return s.toLowerCase();\r
181         }\r
182         else{\r
183             return s;\r
184         }\r
185 \r
186     }\r
187 }\r