aboutsummaryrefslogtreecommitdiffstats
path: root/lib/doorman/src/main/java/org/onap/ccsdk/features/lib/doorman/dao/MessageDaoImpl.java
blob: f04ea625976bc083899cbd5574a9b648fb29084b (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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package org.onap.ccsdk.features.lib.doorman.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.onap.ccsdk.features.lib.doorman.data.ActionStatus;
import org.onap.ccsdk.features.lib.doorman.data.Message;
import org.onap.ccsdk.features.lib.doorman.data.MessageAction;
import org.onap.ccsdk.features.lib.doorman.data.MessageActionValue;
import org.onap.ccsdk.features.lib.doorman.data.MessageData;
import org.onap.ccsdk.features.lib.doorman.data.MessageStatus;
import org.onap.ccsdk.features.lib.doorman.data.MessageStatusValue;
import org.onap.ccsdk.features.lib.doorman.data.Queue;
import org.onap.ccsdk.features.lib.doorman.util.JsonUtil;

public class MessageDaoImpl implements MessageDao {

    private DataSource dataSource;

    @Override
    public long addArrivedMessage(String extMessageId, MessageData request, Queue queue, Date timestamp) {
        try (Connection con = dataSource.getConnection()) {
            try {
                con.setAutoCommit(false);
                long id = 0;
                String sql = "INSERT INTO message (\n"
                        + "  ext_message_id, request_param, request_body, arrived_timestamp, queue_type, queue_id)\n"
                        + "VALUES (?, ?, ?, ?, ?, ?)";
                try (PreparedStatement ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
                    ps.setString(1, extMessageId);
                    ps.setString(2, JsonUtil.dataToJson(request.getParam()));
                    ps.setString(3, request.getBody());
                    ps.setTimestamp(4, new Timestamp(timestamp.getTime()));
                    if (queue != null) {
                        ps.setString(5, queue.getType());
                        ps.setString(6, queue.getId());
                    } else {
                        ps.setNull(5, Types.VARCHAR);
                        ps.setNull(6, Types.VARCHAR);
                    }
                    ps.executeUpdate();
                    try (ResultSet rs = ps.getGeneratedKeys()) {
                        rs.next();
                        id = rs.getLong(1);
                    }
                }
                con.commit();
                return id;
            } catch (SQLException ex) {
                con.rollback();
                throw ex;
            }
        } catch (SQLException e) {
            throw new RuntimeException("Error inserting message to DB: " + e.getMessage(), e);
        }
    }

    @Override
    public void updateMessageStarted(long messageId, Date timestamp) {
        updateMessageStatus("started_timestamp", messageId, null, timestamp);
    }

    @Override
    public void updateMessageCompleted(long messageId, String resolution, Date timestamp) {
        updateMessageStatus("completed_timestamp", messageId, resolution, timestamp);
    }

    private void updateMessageStatus(String timestampColumn, long messageId, String resolution, Date timestamp) {
        try (Connection con = dataSource.getConnection()) {
            try {
                con.setAutoCommit(false);
                String sql = "UPDATE message SET " + timestampColumn + " = ? WHERE message_id = ?";
                try (PreparedStatement ps = con.prepareStatement(sql)) {
                    ps.setTimestamp(1, new Timestamp(timestamp.getTime()));
                    ps.setLong(2, messageId);
                    ps.executeUpdate();
                }
                con.commit();
            } catch (SQLException ex) {
                con.rollback();
                throw ex;
            }
        } catch (SQLException e) {
            throw new RuntimeException("Error updating message status in DB: " + e.getMessage(), e);
        }
    }

    @Override
    public void updateMessageResponse(long messageId, Date timestamp, MessageData response) {
        try (Connection con = dataSource.getConnection()) {
            try {
                con.setAutoCommit(false);
                String sql = "UPDATE message SET response_timestamp = ?, response_param = ?, response_body = ?\n"
                        + "WHERE message_id = ?";
                try (PreparedStatement ps = con.prepareStatement(sql)) {
                    ps.setTimestamp(1, new Timestamp(timestamp.getTime()));
                    ps.setString(2, JsonUtil.dataToJson(response.getParam()));
                    ps.setString(3, response.getBody());
                    ps.setLong(4, messageId);
                    ps.executeUpdate();
                }
                con.commit();
            } catch (SQLException ex) {
                con.rollback();
                throw ex;
            }
        } catch (SQLException e) {
            throw new RuntimeException("Error updating message response in DB: " + e.getMessage(), e);
        }
    }

    @Override
    public void addStatus(long messageId, MessageStatus status) {
        try (Connection con = dataSource.getConnection()) {
            try {
                con.setAutoCommit(false);
                String sql = "INSERT INTO message_status (message_id, status, status_timestamp) VALUES (?, ?, ?)";
                try (PreparedStatement ps = con.prepareStatement(sql)) {
                    ps.setLong(1, messageId);
                    ps.setString(2, status.getStatus().toString());
                    ps.setTimestamp(3, new Timestamp(status.getTimestamp().getTime()));
                    ps.executeUpdate();
                }
                con.commit();
            } catch (SQLException ex) {
                con.rollback();
                throw ex;
            }
        } catch (SQLException e) {
            throw new RuntimeException("Error inserting message status to DB: " + e.getMessage(), e);
        }
    }

    @Override
    public void addAction(long messageId, MessageAction action) {
        try (Connection con = dataSource.getConnection()) {
            try {
                con.setAutoCommit(false);
                String sql = "INSERT INTO message_action (\n"
                        + "  message_id, action, action_status, resolution, action_timestamp,\n"
                        + "  done_timestamp, hold_time, response_param, response_body)\n"
                        + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
                try (PreparedStatement ps = con.prepareStatement(sql)) {
                    ps.setLong(1, messageId);
                    ps.setString(2, action.getAction().toString());
                    ps.setString(3, action.getActionStatus().toString());
                    ps.setString(4, action.getResolution());
                    ps.setTimestamp(5, new Timestamp(action.getTimestamp().getTime()));
                    if (action.getDoneTimestamp() != null) {
                        ps.setTimestamp(6, new Timestamp(action.getDoneTimestamp().getTime()));
                    } else {
                        ps.setNull(6, Types.TIMESTAMP);
                    }
                    ps.setInt(7, action.getHoldTime());
                    if (action.getReturnResponse() != null) {
                        ps.setString(8, JsonUtil.dataToJson(action.getReturnResponse().getParam()));
                        ps.setString(9, action.getReturnResponse().getBody());
                    } else {
                        ps.setNull(8, Types.VARCHAR);
                        ps.setNull(9, Types.VARCHAR);
                    }
                    ps.executeUpdate();
                }
                con.commit();
            } catch (SQLException ex) {
                con.rollback();
                throw ex;
            }
        } catch (SQLException e) {
            throw new RuntimeException("Error inserting message action to DB: " + e.getMessage(), e);
        }
    }

    @Override
    public void updateActionDone(long actionId, Date now) {
        try (Connection con = dataSource.getConnection()) {
            try {
                con.setAutoCommit(false);
                String sql =
                        "UPDATE message_action SET action_status = ?, done_timestamp = ? WHERE message_action_id = ?";
                try (PreparedStatement ps = con.prepareStatement(sql)) {
                    ps.setString(1, ActionStatus.DONE.toString());
                    ps.setTimestamp(2, new Timestamp(now.getTime()));
                    ps.setLong(3, actionId);
                    ps.executeUpdate();
                }
                con.commit();
            } catch (SQLException ex) {
                con.rollback();
                throw ex;
            }
        } catch (SQLException e) {
            throw new RuntimeException("Error updating action in DB: " + e.getMessage(), e);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Message> readMessageQueue(Queue queue) {
        List<Message> messageList = new ArrayList<>();
        try (Connection con = dataSource.getConnection()) {
            String msql = "SELECT * FROM message WHERE queue_type = ? AND queue_id = ?";
            String ssql = "SELECT * FROM message_status WHERE message_id = ? ORDER BY message_status_id DESC";
            String asql = "SELECT * FROM message_action WHERE message_id = ? ORDER BY message_action_id DESC";
            try (PreparedStatement mps = con.prepareStatement(msql);
                    PreparedStatement sps = con.prepareStatement(ssql);
                    PreparedStatement aps = con.prepareStatement(asql)) {
                mps.setString(1, queue.getType());
                mps.setString(2, queue.getId());
                try (ResultSet mrs = mps.executeQuery()) {
                    while (mrs.next()) {
                        long messageId = mrs.getLong("message_id");
                        String extMessageId = mrs.getString("ext_message_id");

                        Map<String, Object> requestParam =
                                (Map<String, Object>) JsonUtil.jsonToData(mrs.getString("request_param"));
                        String requestBody = mrs.getString("request_body");
                        MessageData request = null;
                        if (requestParam != null || requestBody != null) {
                            request = new MessageData(requestParam, requestBody);
                        }

                        Map<String, Object> responseParam =
                                (Map<String, Object>) JsonUtil.jsonToData(mrs.getString("response_param"));
                        String responseBody = mrs.getString("response_body");
                        MessageData response = null;
                        if (responseParam != null || responseBody != null) {
                            response = new MessageData(responseParam, responseBody);
                        }

                        Date arrivedTimestamp = mrs.getTimestamp("arrived_timestamp");
                        Date startedTimestamp = mrs.getTimestamp("started_timestamp");
                        Date completedTimestamp = mrs.getTimestamp("completed_timestamp");
                        Date responseTimestamp = mrs.getTimestamp("response_timestamp");

                        List<MessageStatus> statusHistory = new ArrayList<>();
                        sps.setLong(1, messageId);
                        try (ResultSet srs = sps.executeQuery()) {
                            while (srs.next()) {
                                MessageStatusValue status = MessageStatusValue.valueOf(srs.getString("status"));
                                Date timestamp = srs.getTimestamp("status_timestamp");
                                statusHistory.add(new MessageStatus(status, timestamp));
                            }
                        }

                        List<MessageAction> actionHistory = new ArrayList<>();
                        aps.setLong(1, messageId);
                        try (ResultSet ars = aps.executeQuery()) {
                            while (ars.next()) {
                                long actionId = ars.getLong("message_action_id");
                                MessageActionValue action = MessageActionValue.valueOf(ars.getString("action"));
                                ActionStatus actionStatus = ActionStatus.valueOf(ars.getString("action_status"));
                                String resolution = ars.getString("resolution");
                                Date timestamp = ars.getTimestamp("action_timestamp");
                                Date doneTimestamp = ars.getTimestamp("done_timestamp");
                                Integer holdTimeO = ars.getInt("hold_time");
                                int holdTime = holdTimeO != null ? holdTimeO : 0;

                                Map<String, Object> returnResponseParam =
                                        (Map<String, Object>) JsonUtil.jsonToData(ars.getString("response_param"));
                                String returnResponseBody = ars.getString("response_body");
                                MessageData returnResponse = null;
                                if (returnResponseParam != null || returnResponseBody != null) {
                                    returnResponse = new MessageData(returnResponseParam, returnResponseBody);
                                }

                                MessageAction a = new MessageAction(actionId, action, actionStatus, resolution,
                                        timestamp, doneTimestamp, holdTime, returnResponse);
                                actionHistory.add(a);
                            }
                        }

                        Message m = new Message(messageId, extMessageId, request, response, arrivedTimestamp,
                                startedTimestamp, completedTimestamp, responseTimestamp, queue, statusHistory,
                                actionHistory);
                        messageList.add(m);
                    }
                }
            }
        } catch (SQLException e) {
            throw new RuntimeException("Error reading message action from DB: " + e.getMessage(), e);
        }
        return messageList;
    }

    @SuppressWarnings("unchecked")
    @Override
    public MessageAction getNextAction(long messageId) {
        try (Connection con = dataSource.getConnection()) {
            String sql = "SELECT * FROM message_action WHERE message_id = ? ORDER BY action_timestamp DESC";
            try (PreparedStatement ps = con.prepareStatement(sql)) {
                ps.setLong(1, messageId);
                try (ResultSet rs = ps.executeQuery()) {
                    if (rs.next()) {
                        long actionId = rs.getLong("message_action_id");
                        MessageActionValue action = MessageActionValue.valueOf(rs.getString("action"));
                        ActionStatus actionStatus = ActionStatus.valueOf(rs.getString("action_status"));
                        String resolution = rs.getString("resolution");
                        Date timestamp = rs.getTimestamp("action_timestamp");
                        Date doneTimestamp = rs.getTimestamp("done_timestamp");
                        Integer holdTimeO = rs.getInt("hold_time");
                        int holdTime = holdTimeO != null ? holdTimeO : 0;

                        Map<String, Object> returnResponseParam =
                                (Map<String, Object>) JsonUtil.jsonToData(rs.getString("response_param"));
                        String returnResponseBody = rs.getString("response_body");
                        MessageData returnResponse = null;
                        if (returnResponseParam != null || returnResponseBody != null) {
                            returnResponse = new MessageData(returnResponseParam, returnResponseBody);
                        }

                        MessageAction a = new MessageAction(actionId, action, actionStatus, resolution, timestamp,
                                doneTimestamp, holdTime, returnResponse);
                        return a;
                    }
                    return null;
                }
            }
        } catch (SQLException e) {
            throw new RuntimeException("Error reading message action from DB: " + e.getMessage(), e);
        }
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }
}