aboutsummaryrefslogtreecommitdiffstats
path: root/datarouter-prov/src/main/java/org/onap/dmaap/datarouter/provisioning/beans/Parameters.java
blob: fff10ac7a45e45065f269c0d3f0b21992b201a5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*******************************************************************************
 * ============LICENSE_START==================================================
 * * org.onap.dmaap
 * * ===========================================================================
 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * * ===========================================================================
 * * Licensed under the Apache License, Version 2.0 (the "License");
 * * you may not use this file except in compliance with the License.
 * * You may obtain a copy of the License at
 * *
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 * *
 *  * Unless required by applicable law or agreed to in writing, software
 * * distributed under the License is distributed on an "AS IS" BASIS,
 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * * See the License for the specific language governing permissions and
 * * limitations under the License.
 * * ============LICENSE_END====================================================
 * *
 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * *
 ******************************************************************************/

package org.onap.dmaap.datarouter.provisioning.beans;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;

import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import org.json.JSONObject;
import org.onap.dmaap.datarouter.provisioning.utils.DB;

/**
 * Methods to provide access to Provisioning parameters in the DB. This class also provides constants of the standard
 * parameters used by the Data Router.
 *
 * @author Robert Eby
 * @version $Id: Parameters.java,v 1.11 2014/03/12 19:45:41 eby Exp $
 */
public class Parameters extends Syncable {

    public static final String PROV_REQUIRE_SECURE = "PROV_REQUIRE_SECURE";
    public static final String PROV_REQUIRE_CERT = "PROV_REQUIRE_CERT";
    public static final String PROV_AUTH_ADDRESSES = "PROV_AUTH_ADDRESSES";
    public static final String PROV_AUTH_SUBJECTS = "PROV_AUTH_SUBJECTS";
    public static final String PROV_NAME = "PROV_NAME";
    public static final String PROV_ACTIVE_NAME = "PROV_ACTIVE_NAME";
    public static final String PROV_DOMAIN = "PROV_DOMAIN";
    public static final String PROV_MAXFEED_COUNT = "PROV_MAXFEED_COUNT";
    public static final String PROV_MAXSUB_COUNT = "PROV_MAXSUB_COUNT";
    public static final String PROV_POKETIMER1 = "PROV_POKETIMER1";
    public static final String PROV_POKETIMER2 = "PROV_POKETIMER2";
    public static final String PROV_SPECIAL_SUBNET = "PROV_SPECIAL_SUBNET";
    public static final String PROV_LOG_RETENTION = "PROV_LOG_RETENTION";
    public static final String NODES = "NODES";
    public static final String ACTIVE_POD = "ACTIVE_POD";
    public static final String STANDBY_POD = "STANDBY_POD";
    public static final String LOGROLL_INTERVAL = "LOGROLL_INTERVAL";
    public static final String DELIVERY_INIT_RETRY_INTERVAL = "DELIVERY_INIT_RETRY_INTERVAL";
    public static final String DELIVERY_MAX_RETRY_INTERVAL = "DELIVERY_MAX_RETRY_INTERVAL";
    public static final String DELIVERY_RETRY_RATIO = "DELIVERY_RETRY_RATIO";
    public static final String DELIVERY_MAX_AGE = "DELIVERY_MAX_AGE";
    public static final String THROTTLE_FILTER = "THROTTLE_FILTER";
    public static final String STATIC_ROUTING_NODES = "STATIC_ROUTING_NODES"; //Adding new param for static Routing - Rally:US664862-1610

    private static EELFLogger intlogger = EELFManager.getInstance().getLogger("InternalLog");
    private static final String SQLEXCEPTION = "SQLException: ";

    private String keyname;
    private String value;

    /**
     * Get all parameters in the DB as a Map.
     *
     * @return the Map of keynames/values from the DB.
     */
    public static Map<String, String> getParameters() {
        Map<String, String> props = new HashMap<String, String>();
        for (Parameters p : getParameterCollection()) {
            props.put(p.getKeyname(), p.getValue());
        }
        return props;
    }

    public static Collection<Parameters> getParameterCollection() {
        Collection<Parameters> coll = new ArrayList<Parameters>();
        try {
            DB db = new DB();
            @SuppressWarnings("resource")
            Connection conn = db.getConnection();
            try (Statement stmt = conn.createStatement()) {
                String sql = "select * from PARAMETERS";
                try (ResultSet rs = stmt.executeQuery(sql)) {
                    while (rs.next()) {
                        Parameters p = new Parameters(rs);
                        coll.add(p);
                    }
                }
            }
            db.release(conn);
        } catch (SQLException e) {
            intlogger.error(SQLEXCEPTION + e.getMessage());
        }
        return coll;
    }

    /**
     * Get a specific parameter value from the DB.
     *
     * @param k the key to lookup
     * @return the value, or null if non-existant
     */
    public static Parameters getParameter(String k) {
        Parameters v = null;
        try {
            DB db = new DB();
            @SuppressWarnings("resource")
            Connection conn = db.getConnection();
            try (PreparedStatement stmt = conn
                    .prepareStatement("select KEYNAME, VALUE from PARAMETERS where KEYNAME = ?")) {
                stmt.setString(1, k);
                try (ResultSet rs = stmt.executeQuery()) {
                    if (rs.next()) {
                        v = new Parameters(rs);
                    }
                }
            }
            db.release(conn);
        } catch (SQLException e) {
            intlogger.error(SQLEXCEPTION + e.getMessage());
        }
        return v;
    }

    public Parameters() {
        this("", "");
    }

    public Parameters(String k, String v) {
        this.keyname = k;
        this.value = v;
    }

    public Parameters(ResultSet rs) throws SQLException {
        this.keyname = rs.getString("KEYNAME");
        this.value = rs.getString("VALUE");
    }

    public String getKeyname() {
        return keyname;
    }

    public void setKeyname(String keyname) {
        this.keyname = keyname;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    @Override
    public JSONObject asJSONObject() {
        JSONObject jo = new JSONObject();
        jo.put("keyname", keyname);
        jo.put("value", value);
        return jo;
    }

    @Override
    public boolean doInsert(Connection c) {
        boolean rv = true;
        PreparedStatement ps = null;
        try {
            // Create the SUBSCRIPTIONS row
            String sql = "insert into PARAMETERS values (?, ?)";
            ps = c.prepareStatement(sql);
            ps.setString(1, getKeyname());
            ps.setString(2, getValue());
            ps.execute();
        } catch (SQLException e) {
            rv = false;
            intlogger.warn("PROV0005 doInsert: " + e.getMessage(), e);
        } finally {
            try {
                if (ps != null) {
                    ps.close();
                }
            } catch (SQLException e) {
                intlogger.error(SQLEXCEPTION + e.getMessage());
            }
        }
        return rv;
    }

    @Override
    public boolean doUpdate(Connection c) {
        boolean rv = true;
        PreparedStatement ps = null;
        try {
            // Update the PARAMETERS row
            String sql = "update PARAMETERS set VALUE = ? where KEYNAME = ?";
            ps = c.prepareStatement(sql);
            ps.setString(1, getValue());
            ps.setString(2, getKeyname());
            ps.executeUpdate();
        } catch (SQLException e) {
            rv = false;
            intlogger.warn("PROV0006 doUpdate: " + e.getMessage(),e);
        } finally {
            try {
                if (ps != null) {
                    ps.close();
                }
            } catch (SQLException e) {
                intlogger.error(SQLEXCEPTION + e.getMessage(), e);
            }
        }
        return rv;
    }

    @Override
    public boolean doDelete(Connection c) {
        boolean rv = true;
        PreparedStatement ps = null;
        try {
            // Create the SUBSCRIPTIONS row
            String sql = "delete from PARAMETERS where KEYNAME = ?";
            ps = c.prepareStatement(sql);
            ps.setString(1, getKeyname());
            ps.execute();
        } catch (SQLException e) {
            rv = false;
            intlogger.warn("PROV0007 doDelete: " + e.getMessage(), e);
        } finally {
            try {
                if (ps != null) {
                    ps.close();
                }
            } catch (SQLException e) {
                intlogger.error(SQLEXCEPTION + e.getMessage(), e);
            }
        }
        return rv;
    }

    @Override
    public String getKey() {
        return getKeyname();
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof Parameters)) {
            return false;
        }
        Parameters of = (Parameters) obj;
        if (!keyname.equals(of.keyname)) {
            return false;
        }
        if (!value.equals(of.value)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        return Objects.hash(keyname, value);
    }

    @Override
    public String toString() {
        return "PARAM: keyname=" + keyname + ", value=" + value;
    }
}