[DMAAP-48] Initial code import
[dmaap/datarouter.git] / datarouter-prov / src / main / java / com / att / research / 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 com.att.research.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 \r
38 import com.att.research.datarouter.provisioning.utils.DB;\r
39 \r
40 /**\r
41  * This class is used to aid in the mapping of node names from/to node IDs.\r
42  *\r
43  * @author Robert P. Eby\r
44  * @version $Id: NodeClass.java,v 1.2 2014/01/15 16:08:43 eby Exp $\r
45  */\r
46 public abstract class NodeClass extends Syncable {\r
47         private static Map<String, Integer> map;\r
48 \r
49         public NodeClass() {\r
50                 // init on first use\r
51                 if (map == null) {\r
52                         reload();\r
53                 }\r
54         }\r
55 \r
56         /**\r
57          * Add nodes to the NODES table, when the NODES parameter value is changed.\r
58          * Nodes are only added to the table, they are never deleted.  The node name is normalized\r
59          * to contain the domain (if missing).\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                 Logger intlogger = Logger.getLogger("com.att.research.datarouter.provisioning.internal");\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.warn("PROV0005 doInsert: "+e.getMessage());\r
90                                         e.printStackTrace();\r
91                                 } finally {\r
92                                         try {\r
93                                                 ps.close();\r
94                                         } catch (SQLException e) {\r
95                                                 e.printStackTrace();\r
96                                         }\r
97                                 }\r
98                                 nextid++;\r
99                         }\r
100                 }\r
101         }\r
102 \r
103         public static void reload() {\r
104                 Map<String, Integer> m = new HashMap<String, Integer>();\r
105                 PreparedStatement ps = null;\r
106                 try {\r
107                         DB db = new DB();\r
108                         @SuppressWarnings("resource")\r
109                         Connection conn = db.getConnection();\r
110                         String sql = "select NODEID, NAME from NODES";\r
111                         ps = conn.prepareStatement(sql);\r
112                         ResultSet rs = ps.executeQuery();\r
113                         while (rs.next()) {\r
114                                 int id = rs.getInt("NODEID");\r
115                                 String name = rs.getString("NAME");\r
116                                 m.put(name, id);\r
117                         }\r
118                         rs.close();\r
119                         ps.close();\r
120                         db.release(conn);\r
121                 } catch (SQLException e) {\r
122                         e.printStackTrace();\r
123                 } finally {\r
124                         try {\r
125                                 ps.close();\r
126                         } catch (SQLException e) {\r
127                                 e.printStackTrace();\r
128                         }\r
129                 }\r
130                 map = m;\r
131         }\r
132 \r
133         public static Integer lookupNodeName(final String name) throws IllegalArgumentException {\r
134                 Integer n = map.get(name);\r
135                 if (n == null)\r
136                         throw new IllegalArgumentException("Invalid node name: "+name);\r
137                 return n;\r
138         }\r
139 \r
140         public static Collection<String> lookupNodeNames(String patt) throws IllegalArgumentException {\r
141                 Collection<String> coll = new TreeSet<String>();\r
142                 final Set<String> keyset = map.keySet();\r
143                 for (String s : patt.toLowerCase().split(",")) {\r
144                         if (s.endsWith("*")) {\r
145                                 s = s.substring(0, s.length()-1);\r
146                                 for (String s2 : keyset) {\r
147                                         if (s2.startsWith(s))\r
148                                                 coll.add(s2);\r
149                                 }\r
150                         } else if (keyset.contains(s)) {\r
151                                 coll.add(s);\r
152                         } else if (keyset.contains(normalizeNodename(s))) {\r
153                                 coll.add(normalizeNodename(s));\r
154                         } else {\r
155                                 throw new IllegalArgumentException("Invalid node name: "+s);\r
156                         }\r
157                 }\r
158                 return coll;\r
159         }\r
160 \r
161         protected String lookupNodeID(int n) {\r
162                 for (String s : map.keySet()) {\r
163                         if (map.get(s) == n)\r
164                                 return s;\r
165                 }\r
166                 return null;\r
167         }\r
168 \r
169         public static String normalizeNodename(String s) {\r
170                 if (s != null && s.indexOf('.') <= 0) {\r
171                         Parameters p = Parameters.getParameter(Parameters.PROV_DOMAIN);\r
172                         if (p != null) {\r
173                                 String domain = p.getValue();\r
174                                 s += "." + domain;\r
175                         }\r
176                 }\r
177                 return s.toLowerCase();\r
178         }\r
179 }\r