1 /*******************************************************************************
\r
2 * ============LICENSE_START==================================================
\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
11 * * http://www.apache.org/licenses/LICENSE-2.0
\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
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
24 package org.onap.dmaap.datarouter.provisioning.beans;
\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
40 * This class is used to aid in the mapping of node names from/to node IDs.
\r
42 * @author Robert P. Eby
\r
43 * @version $Id: NodeClass.java,v 1.2 2014/01/15 16:08:43 eby Exp $
\r
46 public abstract class NodeClass extends Syncable {
\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
53 // init on first use
\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
63 * @param nodes a pipe separated list of the current nodes
\r
65 public static void setNodes(String[] nodes) {
\r
70 for (Integer n : map.values()) {
\r
75 // take | separated list, add domain if needed.
\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
88 private static void insertNodesToTable(int nextid, String node) {
\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
99 } catch (SQLException e) {
\r
100 intLogger.error(PROV_0005_DO_INSERT + e.getMessage(), e);
\r
104 private static void reload() {
\r
105 Map<String, Integer> hmap = new HashMap<>();
\r
106 String sql = "select NODEID, NAME from NODES";
\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
119 } catch (SQLException e) {
\r
120 intLogger.error(PROV_0005_DO_INSERT + e.getMessage(),e);
\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
135 * @param patt pattern to search
\r
136 * @return collection of node names
\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
146 } else if (keyset.contains(normalizeNodename(s))) {
\r
147 coll.add(normalizeNodename(s));
\r
149 throw new IllegalArgumentException("Invalid node name: " + s);
\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
165 * Method to add domain name.
\r
166 * @param str nde name string
\r
167 * @return normalized node name
\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
176 return str.toLowerCase();
\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