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