remove ODL/karaf from common
[ccsdk/sli/core.git] / sli / common / src / main / java / org / onap / ccsdk / sli / core / sli / SvcLogicDblibStore.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.ccsdk.sli.core.sli;
23
24 import java.io.ByteArrayInputStream;
25 import java.io.ByteArrayOutputStream;
26 import java.io.ObjectInputStream;
27 import java.io.ObjectOutputStream;
28 import java.sql.Blob;
29 import java.sql.Connection;
30 import java.sql.PreparedStatement;
31 import java.sql.ResultSet;
32 import java.sql.SQLException;
33 import java.util.ArrayList;
34 import java.util.Properties;
35 import javax.sql.rowset.CachedRowSet;
36 import org.onap.ccsdk.sli.core.dblib.DbLibService;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 public class SvcLogicDblibStore implements SvcLogicStore {
41         private static final Logger LOG = LoggerFactory.getLogger(SvcLogicDblibStore.class);
42         private DbLibService dbSvc;
43
44         public SvcLogicDblibStore(DbLibService dbsvc) {
45                 this.dbSvc = dbsvc;
46         }
47
48         public Connection getConnection() throws SQLException {
49             return(dbSvc.getConnection());
50         }
51
52         @Override
53         public void init(Properties props) throws ConfigurationException {
54                 if(dbSvc == null) {
55                         LOG.error("SvcLogic cannot acquire DBLIB_SERVICE");
56                         return;
57                 }
58                 try {
59                         dbSvc.getData("select 1 from DUAL", new ArrayList<String>(), null);
60                         LOG.debug("SQL test was successful");
61                 } catch (SQLException e) {
62                         LOG.error("Failed SQL test", e);
63                 }
64         }
65
66         @Override
67         public boolean hasGraph(String module, String rpc, String version,
68                         String mode) throws SvcLogicException {
69                 boolean retval = false;
70                 CachedRowSet results = null;
71                 String hasVersionGraphSql = "SELECT count(*) FROM SVC_LOGIC"
72                                 + " WHERE module = ? AND rpc = ? AND mode = ? AND version = ?";
73
74                 String hasActiveGraphSql = "SELECT count(*) FROM SVC_LOGIC"
75                                 + " WHERE module = ? AND rpc = ? AND mode = ? AND active = 'Y'";
76
77                 ArrayList<String> args = new ArrayList<>();
78                 args.add(module);
79                 args.add(rpc);
80                 args.add(mode);
81
82                 try {
83
84                         if (version == null) {
85                                 results = dbSvc.getData(hasActiveGraphSql, args, null);
86                         } else {
87                                 args.add(version);
88                                 results = dbSvc.getData(hasVersionGraphSql, args, null);
89                         }
90
91                         if (results.next()) {
92                                 int cnt = results.getInt(1);
93
94                                 if (cnt > 0) {
95                                         retval = true;
96                                 }
97                         }
98                 } catch (Exception e) {
99                         throw new ConfigurationException("SQL query failed", e);
100                 } finally {
101                         if (results != null) {
102                                 try {
103                                         results.close();
104                                 } catch (SQLException x) {
105                                         LOG.error("Failed to close CachedRowSet", x);
106                                 }
107                         }
108
109                 }
110
111                 return retval;
112         }
113
114         public SvcLogicGraph fetch(String module, String rpc, String version,
115                         String mode) throws SvcLogicException {
116
117                 PreparedStatement fetchGraphStmt = null;
118                 Connection dbConn = null;
119                 SvcLogicGraph retval = null;
120                 ResultSet results = null;
121
122                 String fetchVersionGraphSql = "SELECT graph FROM SVC_LOGIC"
123                                 + " WHERE module = ? AND rpc = ? AND mode = ? AND version = ?";
124
125                 String fetchActiveGraphSql = "SELECT graph FROM SVC_LOGIC"
126                                 + " WHERE module = ? AND rpc = ? AND mode = ? AND active = 'Y'";
127
128
129                 try {
130                         dbConn = dbSvc.getConnection();
131
132                         if (version == null) {
133                                 fetchGraphStmt = dbConn.prepareStatement(fetchActiveGraphSql);
134                         } else {
135                                 fetchGraphStmt = dbConn.prepareStatement(fetchVersionGraphSql);
136                         }
137
138                         fetchGraphStmt.setString(1, module);
139                         fetchGraphStmt.setString(2,  rpc);
140                         fetchGraphStmt.setString(3, mode);
141                         if (version != null) {
142                                 fetchGraphStmt.setString(4,version);
143                         }
144
145                         results = fetchGraphStmt.executeQuery();
146
147                         if (results.next()) {
148                                 Blob graphBlob = results.getBlob("graph");
149
150                                 ObjectInputStream gStream = new ObjectInputStream(graphBlob.getBinaryStream());
151
152                                 Object graphObj = gStream.readObject();
153                                 gStream.close();
154
155                                 if (graphObj instanceof SvcLogicGraph) {
156                                         retval = (SvcLogicGraph) graphObj;
157                                 } else {
158                                         throw new ConfigurationException("invalid type for graph ("
159                                                         + graphObj.getClass().getName());
160
161                                 }
162
163                         } else {
164                                 return null;
165                         }
166                 } catch (Exception e) {
167                     LOG.error("Graph processing failed", e);
168                         throw new ConfigurationException("Graph processing failed: " + e.getMessage());
169                 } finally {
170                         try {
171                                 if (fetchGraphStmt != null) {
172                                         fetchGraphStmt.close();
173                                 }
174                         } catch (SQLException e) {
175                                 LOG.error("PreparedStatement close error", e);
176                         }
177                         if (results != null) {
178                                 try {
179                                         results.close();
180                                 } catch (SQLException x) {
181                                         LOG.error("ResultSet close error", x);
182                                 }
183                         }
184                         try {
185                                 if (dbConn != null && !dbConn.isClosed()) {
186                                         dbConn.close();
187                                 }
188                         } catch (Exception exc) {
189                                 LOG.error("dbConn close error", exc);
190                         } finally {
191                                 dbConn = null;
192                         }
193
194                 }
195
196                 return retval;
197         }
198
199         public void store(SvcLogicGraph graph) throws SvcLogicException {
200
201
202
203                 String storeGraphSql = "INSERT INTO SVC_LOGIC (module, rpc, version, mode, active, graph, md5sum)"
204                                 + " VALUES(?, ?, ?, ?, ?, ?, ?)";
205
206                 if (graph == null) {
207                         throw new SvcLogicException("graph cannot be null");
208                 }
209
210                 byte[] graphBytes = null;
211
212                 try (ByteArrayOutputStream byteStr = new ByteArrayOutputStream();
213                         ObjectOutputStream goutStr = new ObjectOutputStream(byteStr)) {
214
215                         goutStr.writeObject(graph);
216
217                         graphBytes = byteStr.toByteArray();
218
219                 } catch (Exception e) {
220                         throw new SvcLogicException("could not serialize graph", e);
221                 }
222
223                 // If object already stored in database, delete it
224                 if (hasGraph(graph.getModule(), graph.getRpc(), graph.getVersion(),
225                                 graph.getMode())) {
226                         delete(graph.getModule(), graph.getRpc(), graph.getVersion(),
227                                         graph.getMode());
228                 }
229
230                 Connection dbConn = null;
231         PreparedStatement storeGraphStmt = null;
232                 try {
233             dbConn = dbSvc.getConnection();
234             boolean oldAutoCommit = dbConn.getAutoCommit();
235                         dbConn.setAutoCommit(false);
236             storeGraphStmt = dbConn.prepareStatement(storeGraphSql);
237                         storeGraphStmt.setString(1, graph.getModule());
238                         storeGraphStmt.setString(2, graph.getRpc());
239                         storeGraphStmt.setString(3, graph.getVersion());
240                         storeGraphStmt.setString(4, graph.getMode());
241                         storeGraphStmt.setString(5, "N");
242                         storeGraphStmt.setBlob(6, new ByteArrayInputStream(graphBytes));
243                         storeGraphStmt.setString(7, graph.getMd5sum());
244                         storeGraphStmt.executeUpdate();
245                         dbConn.commit();
246             dbConn.setAutoCommit(oldAutoCommit);
247                 } catch (Exception e) {
248                         throw new SvcLogicException("Could not write object to database", e);
249                 } finally {
250                         try {
251                                 if (storeGraphStmt != null) {
252                                         storeGraphStmt.close();
253                                 }
254                         } catch (SQLException e) {
255                                 LOG.error("PreparedStatement close error", e);
256                         }
257                         try {
258                                 if (dbConn != null && !dbConn.isClosed()) {
259                                         dbConn.close();
260                                 }
261                         } catch (Exception exc) {
262                                 LOG.error("dbConn close error", exc);
263                         } finally {
264                                 dbConn = null;
265                         }
266
267                 }
268         }
269
270         public void delete(String module, String rpc, String version, String mode)
271                         throws SvcLogicException {
272                 String deleteGraphSql = "DELETE FROM SVC_LOGIC WHERE module = ? AND rpc = ? AND version = ? AND mode = ?";
273
274                 ArrayList<String> args = new ArrayList<>();
275
276                 args.add(module);
277                 args.add(rpc);
278                 args.add(version);
279                 args.add(mode);
280
281                 try {
282                         dbSvc.writeData(deleteGraphSql, args, null);
283                 } catch (Exception e) {
284                         throw new SvcLogicException("Could not delete object from database", e);
285                 }
286         }
287
288         public void activate(SvcLogicGraph graph) throws SvcLogicException {
289                 String deactivateSql = "UPDATE SVC_LOGIC SET active = 'N' WHERE module = ? AND rpc = ? AND mode = ?";
290                 String activateSql = "UPDATE SVC_LOGIC SET active = 'Y' WHERE module = ? AND rpc = ? AND mode = ? AND version = ?";
291
292                 ArrayList<String> args = new ArrayList<>();
293
294                 args.add(graph.getModule());
295                 args.add(graph.getRpc());
296                 args.add(graph.getMode());
297
298                 try {
299                         dbSvc.writeData(deactivateSql, args, null);
300                         args.add(graph.getVersion());
301                         dbSvc.writeData(activateSql, args, null);
302                 } catch (Exception e) {
303                         throw new SvcLogicException("Could not activate graph", e);
304                 }
305         }
306
307     @Override
308     public void activate(String module, String rpc, String version, String mode) throws SvcLogicException {
309
310         String deactivateSql = "UPDATE SVC_LOGIC SET active = 'N' WHERE module = ? AND rpc = ? AND mode = ?";
311
312         String activateSql = "UPDATE SVC_LOGIC SET active = 'Y' WHERE module = ? AND rpc = ? AND mode = ? AND version = ?";
313
314         ArrayList<String> args = new ArrayList<>();
315
316         args.add(module);
317         args.add(rpc);
318         args.add(mode);
319
320         try {
321
322             dbSvc.writeData(deactivateSql, args, null);
323
324             args.add(version);
325             dbSvc.writeData(activateSql, args, null);
326
327         } catch (Exception e) {
328             throw new SvcLogicException("Could not activate graph", e);
329         }        
330     }
331
332 }