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