Update project structure to org.onap
[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 \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          * @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                 int nextid = 0;\r
65                 for (Integer n : map.values()) {\r
66                         if (n >= nextid)\r
67                                 nextid = n+1;\r
68                 }\r
69                 // take | separated list, add domain if needed.\r
70                 Logger intlogger = Logger.getLogger("org.onap.dmaap.datarouter.provisioning.internal");\r
71                 for (String node : nodes) {\r
72                         node = normalizeNodename(node);\r
73                         if (!map.containsKey(node)) {\r
74                                 intlogger.info("..adding "+node+" to NODES with index "+nextid);\r
75                                 map.put(node, nextid);\r
76                                 PreparedStatement ps = null;\r
77                                 try {\r
78                                         DB db = new DB();\r
79                                         @SuppressWarnings("resource")\r
80                                         Connection conn = db.getConnection();\r
81                                         ps = conn.prepareStatement("insert into NODES (NODEID, NAME, ACTIVE) values (?, ?, 1)");\r
82                                         ps.setInt(1, nextid);\r
83                                         ps.setString(2, node);\r
84                                         ps.execute();\r
85                                         ps.close();\r
86                                         db.release(conn);\r
87                                 } catch (SQLException e) {\r
88                                         intlogger.warn("PROV0005 doInsert: "+e.getMessage());\r
89                                         e.printStackTrace();\r
90                                 } finally {\r
91                                         try {\r
92                                                 ps.close();\r
93                                         } catch (SQLException e) {\r
94                                                 e.printStackTrace();\r
95                                         }\r
96                                 }\r
97                                 nextid++;\r
98                         }\r
99                 }\r
100         }\r
101 \r
102         public static void reload() {\r
103                 Map<String, Integer> m = new HashMap<String, Integer>();\r
104                 PreparedStatement ps = null;\r
105                 try {\r
106                         DB db = new DB();\r
107                         @SuppressWarnings("resource")\r
108                         Connection conn = db.getConnection();\r
109                         String sql = "select NODEID, NAME from NODES";\r
110                         ps = conn.prepareStatement(sql);\r
111                         ResultSet rs = ps.executeQuery();\r
112                         while (rs.next()) {\r
113                                 int id = rs.getInt("NODEID");\r
114                                 String name = rs.getString("NAME");\r
115                                 m.put(name, id);\r
116                         }\r
117                         rs.close();\r
118                         ps.close();\r
119                         db.release(conn);\r
120                 } catch (SQLException e) {\r
121                         e.printStackTrace();\r
122                 } finally {\r
123                         try {\r
124                                 ps.close();\r
125                         } catch (SQLException e) {\r
126                                 e.printStackTrace();\r
127                         }\r
128                 }\r
129                 map = m;\r
130         }\r
131 \r
132         public static Integer lookupNodeName(final String name) throws IllegalArgumentException {\r
133                 Integer n = map.get(name);\r
134                 if (n == null)\r
135                         throw new IllegalArgumentException("Invalid node name: "+name);\r
136                 return n;\r
137         }\r
138 \r
139         public static Collection<String> lookupNodeNames(String patt) throws IllegalArgumentException {\r
140                 Collection<String> coll = new TreeSet<String>();\r
141                 final Set<String> keyset = map.keySet();\r
142                 for (String s : patt.toLowerCase().split(",")) {\r
143                         if (s.endsWith("*")) {\r
144                                 s = s.substring(0, s.length()-1);\r
145                                 for (String s2 : keyset) {\r
146                                         if (s2.startsWith(s))\r
147                                                 coll.add(s2);\r
148                                 }\r
149                         } else if (keyset.contains(s)) {\r
150                                 coll.add(s);\r
151                         } else if (keyset.contains(normalizeNodename(s))) {\r
152                                 coll.add(normalizeNodename(s));\r
153                         } else {\r
154                                 throw new IllegalArgumentException("Invalid node name: "+s);\r
155                         }\r
156                 }\r
157                 return coll;\r
158         }\r
159 \r
160         protected String lookupNodeID(int n) {\r
161                 for (String s : map.keySet()) {\r
162                         if (map.get(s) == n)\r
163                                 return s;\r
164                 }\r
165                 return null;\r
166         }\r
167 \r
168         public static String normalizeNodename(String s) {\r
169                 if (s != null && s.indexOf('.') <= 0) {\r
170                         Parameters p = Parameters.getParameter(Parameters.PROV_DOMAIN);\r
171                         if (p != null) {\r
172                                 String domain = p.getValue();\r
173                                 s += "." + domain;\r
174                         }\r
175                 }\r
176                 return s.toLowerCase();\r
177         }\r
178 }\r