update DR logging to log under one system
[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 com.att.eelf.configuration.EELFLogger;\r
37 import com.att.eelf.configuration.EELFManager;\r
38 import org.onap.dmaap.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 \r
48     private static Map<String, Integer> map;\r
49     private static EELFLogger intLogger = EELFManager.getInstance().getLogger("InternalLog");\r
50     public NodeClass() {\r
51         // init on first use\r
52         if (map == null) {\r
53             reload();\r
54         }\r
55     }\r
56 \r
57     /**\r
58      * Add nodes to the NODES table, when the NODES parameter value is changed. Nodes are only added to the table, they\r
59      * are never deleted.  The node name is normalized to contain the domain (if missing).\r
60      *\r
61      * @param nodes a pipe separated list of the current nodes\r
62      */\r
63     public static void setNodes(String[] nodes) {\r
64         if (map == null) {\r
65             reload();\r
66         }\r
67         int nextid = 0;\r
68         for (Integer n : map.values()) {\r
69             if (n >= nextid) {\r
70                 nextid = n + 1;\r
71             }\r
72         }\r
73         // take | separated list, add domain if needed.\r
74 \r
75         for (String node : nodes) {\r
76             node = normalizeNodename(node);\r
77             if (!map.containsKey(node)) {\r
78                 intLogger.info("..adding " + node + " to NODES with index " + nextid);\r
79                 map.put(node, nextid);\r
80                 PreparedStatement ps = null;\r
81                 try {\r
82                     DB db = new DB();\r
83                     @SuppressWarnings("resource")\r
84                     Connection conn = db.getConnection();\r
85                     ps = conn.prepareStatement("insert into NODES (NODEID, NAME, ACTIVE) values (?, ?, 1)");\r
86                     ps.setInt(1, nextid);\r
87                     ps.setString(2, node);\r
88                     ps.execute();\r
89                     ps.close();\r
90                     db.release(conn);\r
91                 } catch (SQLException e) {\r
92                     intLogger.error("PROV0005 doInsert: " + e.getMessage(),e);\r
93                 } finally {\r
94                     try {\r
95                         if(ps!=null){\r
96                             ps.close();\r
97                         }\r
98                     } catch (SQLException e) {\r
99                         intLogger.error("Error in closing PreparedStatement: " + e.getMessage(),e);\r
100                     }\r
101                 }\r
102                 nextid++;\r
103             }\r
104         }\r
105     }\r
106 \r
107     public static void reload() {\r
108         Map<String, Integer> m = new HashMap<String, Integer>();\r
109         PreparedStatement ps = null;\r
110 \r
111         try {\r
112             DB db = new DB();\r
113             @SuppressWarnings("resource")\r
114             Connection conn = db.getConnection();\r
115             String sql = "select NODEID, NAME from NODES";\r
116             ps = conn.prepareStatement(sql);\r
117             try(ResultSet rs = ps.executeQuery()) {\r
118                 while (rs.next()) {\r
119                     int id = rs.getInt("NODEID");\r
120                     String name = rs.getString("NAME");\r
121                     m.put(name, id);\r
122                 }\r
123             }\r
124             ps.close();\r
125             db.release(conn);\r
126         } catch (SQLException e) {\r
127             intLogger.error("PROV0005 doInsert: " + e.getMessage(),e);\r
128         } finally {\r
129             try {\r
130                 if(ps!=null){\r
131                     ps.close();\r
132                 }\r
133             } catch (SQLException e) {\r
134                 intLogger.error("PROV0005 doInsert: " + e.getMessage(),e);\r
135             }\r
136         }\r
137         map = m;\r
138     }\r
139 \r
140     public static Integer lookupNodeName(final String name) {\r
141         Integer n = map.get(name);\r
142         if (n == null) {\r
143             throw new IllegalArgumentException("Invalid node name: " + name);\r
144         }\r
145         return n;\r
146     }\r
147 \r
148     public static Collection<String> lookupNodeNames(String patt) {\r
149         Collection<String> coll = new TreeSet<String>();\r
150         final Set<String> keyset = map.keySet();\r
151         for (String s : patt.toLowerCase().split(",")) {\r
152             if (s.endsWith("*")) {\r
153                 s = s.substring(0, s.length() - 1);\r
154                 for (String s2 : keyset) {\r
155                     if (s2.startsWith(s)) {\r
156                         coll.add(s2);\r
157                     }\r
158                 }\r
159             } else if (keyset.contains(s)) {\r
160                 coll.add(s);\r
161             } else if (keyset.contains(normalizeNodename(s))) {\r
162                 coll.add(normalizeNodename(s));\r
163             } else {\r
164                 throw new IllegalArgumentException("Invalid node name: " + s);\r
165             }\r
166         }\r
167         return coll;\r
168     }\r
169 \r
170     public static String normalizeNodename(String s) {\r
171         if (s != null && s.indexOf('.') <= 0) {\r
172             Parameters p = Parameters.getParameter(Parameters.PROV_DOMAIN);\r
173             if (p != null) {\r
174                 String domain = p.getValue();\r
175                 s += "." + domain;\r
176             }\r
177             return s.toLowerCase();\r
178         }\r
179         else{\r
180             return s;\r
181         }\r
182 \r
183     }\r
184 \r
185     protected String lookupNodeID(int n) {\r
186         for (String s : map.keySet()) {\r
187             if (map.get(s) == n) {\r
188                 return s;\r
189             }\r
190         }\r
191         return null;\r
192     }\r
193 }\r