0df2dc5d607a4de3ded9fefb76588cd1fe021e3e
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2021 highstreet technologies GmbH Intellectual Property.
6  * All rights 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.features.sdnr.wt.dataprovider.database.sqldb.database;
23
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.SqlDBClient;
26 import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query.DeleteQuery;
27 import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query.InsertQuery;
28 import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query.UpdateQuery;
29 import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query.UpsertQuery;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Entity;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.entity.input.Filter;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.entity.input.FilterBuilder;
33 import org.opendaylight.yangtools.yang.binding.DataObject;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 import java.sql.Connection;
38 import java.sql.PreparedStatement;
39 import java.sql.SQLException;
40 import java.util.Arrays;
41 import java.util.List;
42
43 public class SqlDBReaderWriter<T extends DataObject> extends SqlDBReader<T> {
44
45     private static final Logger LOG = LoggerFactory.getLogger(SqlDBReaderWriter.class);
46
47     public SqlDBReaderWriter(SqlDBClient dbService, Entity e, String dbSuffix, Class<T> clazz,
48             String controllerId) {
49         super(dbService, e, dbSuffix, clazz, controllerId);
50     }
51
52     public SqlDBReaderWriter(SqlDBClient dbService, Entity e, String dbSuffix, Class<T> clazz,
53             String controllerId, boolean ignoreControllerId) {
54         super(dbService, e, dbSuffix, clazz, controllerId, ignoreControllerId);
55     }
56
57     public <S extends DataObject> String write(S object, String id) {
58         if (id == null) {
59             return this.writeWithoutId(object);
60         }
61         InsertQuery<S> query = new InsertQuery<S>(this.entity, object, this.controllerId, this.ignoreControllerId);
62         query.setId(id);
63         if (LOG.isTraceEnabled()) {
64             LOG.trace("query={}", query.toSql());
65         }
66         boolean success = false;
67         try {
68             success = this.dbService.write(query.toSql());
69         } catch (SQLException e) {
70             LOG.warn("problem writing data into db: ", e);
71         }
72
73         return success ? id : null;
74     }
75
76     private <S extends DataObject> String writeWithoutId(S object) {
77
78         InsertQuery<S> query =
79                 new InsertQuery<S>(this.entity, object, this.controllerId, this.ignoreControllerId, true);
80         if (LOG.isTraceEnabled()) {
81             LOG.trace("query={}", query.toSql());
82         }
83         try {
84             return this.dbService.writeAndReturnId(query.toSql());
85         } catch (SQLException e) {
86             LOG.warn("problem writing data into db: ", e);
87         }
88         return null;
89     }
90
91     public <S extends DataObject> String update(S object, String id) {
92         UpdateQuery<S> query = new UpdateQuery<S>(this.entity, object, this.controllerId, this.ignoreControllerId, true);
93         query.setId(id);
94         if (LOG.isTraceEnabled()) {
95             LOG.trace("query={}", query.toSql());
96         }
97         String insertedId = null;
98         PreparedStatement stmt = null;
99         Connection connection = null;
100         try {
101             connection = this.dbService.getConnection();
102             stmt = connection.prepareStatement(query.toSql());
103             stmt.execute();
104
105             int affectedRows = stmt.getUpdateCount();
106             connection.close();
107             if (affectedRows > 0) {
108                 insertedId = id;
109             }
110             if (LOG.isTraceEnabled()) {
111                 LOG.trace("insertedid={}", insertedId);
112             }
113         } catch (SQLException e) {
114             LOG.warn("problem writing data into db: ", e);
115         } finally {
116             if (stmt != null) {
117                 try {
118                     stmt.close();
119                 } catch (SQLException e) {
120                     LOG.warn("problem closing sql statement: ", e);
121                 }
122             }
123             if (connection != null) {
124                 try {
125                     connection.close();
126                 } catch (SQLException e) {
127                     LOG.warn("problem closing sql connection: ", e);
128                 }
129             }
130         }
131
132         return insertedId;
133     }
134
135     public <S extends DataObject> String updateOrInsert(S object, String id) {
136         UpsertQuery<S> query = new UpsertQuery<S>(this.entity, object, this.controllerId, this.ignoreControllerId, true);
137         query.setId(id);
138         String insertedId = null;
139         if (LOG.isTraceEnabled()) {
140             LOG.trace("query={}", query.toSql());
141         }
142         PreparedStatement stmt = null;
143         Connection connection = null;
144         try {
145             connection = this.dbService.getConnection();
146             stmt = connection.prepareStatement(query.toSql());
147             stmt.execute();
148
149             int affectedRows = stmt.getUpdateCount();
150             connection.close();
151             if (affectedRows > 0) {
152                 insertedId = id;
153             }
154         } catch (SQLException e) {
155             LOG.warn("problem writing data into db: ", e);
156         } finally {
157             if (stmt != null) {
158                 try {
159                     stmt.close();
160                 } catch (SQLException e) {
161                     LOG.warn("problem closing sql statement: ", e);
162                 }
163             }
164             if (connection != null) {
165                 try {
166                     connection.close();
167                 } catch (SQLException e) {
168                     LOG.warn("problem closing sql connection: ", e);
169                 }
170             }
171         }
172         return insertedId;
173     }
174
175     public SqlDBReaderWriter<T> setWriteInterface(Class<? extends DataObject> writeInterfaceClazz) {
176         LOG.debug("Set write interface to {}", writeInterfaceClazz);
177         if (writeInterfaceClazz == null) {
178             throw new IllegalArgumentException("Null not allowed here.");
179         }
180
181         //      this.writeInterfaceClazz = writeInterfaceClazz;
182         return this;
183     }
184
185     public int remove(List<Filter> filters) {
186         DeleteQuery query = new DeleteQuery(this.entity, filters);
187         if (LOG.isTraceEnabled()) {
188             LOG.trace("query={}", query.toSql());
189         }
190         int affectedRows = 0;
191         PreparedStatement stmt = null;
192         Connection connection = null;
193         try {
194             connection = this.dbService.getConnection();
195             stmt = connection.prepareStatement(query.toSql());
196             stmt.execute();
197             affectedRows = stmt.getUpdateCount();
198             connection.close();
199         } catch (SQLException e) {
200             LOG.warn("problem execute delete query: ", e);
201         } finally {
202             if (stmt != null) {
203                 try {
204                     stmt.close();
205                 } catch (SQLException e) {
206                     LOG.warn("problem closing sql statement: ", e);
207                 }
208             }
209             if (connection != null) {
210                 try {
211                     connection.close();
212                 } catch (SQLException e) {
213                     LOG.warn("problem closing sql connection: ", e);
214                 }
215             }
216         }
217         return affectedRows;
218     }
219
220     public int remove(@Nullable String id) {
221         return this.remove(Arrays.asList(new FilterBuilder().setProperty("id").setFiltervalue(id).build()));
222     }
223 }