2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2021 highstreet technologies GmbH Intellectual Property.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.database;
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;
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;
43 public class SqlDBReaderWriter<T extends DataObject> extends SqlDBReader<T> {
45 private static final Logger LOG = LoggerFactory.getLogger(SqlDBReaderWriter.class);
47 public SqlDBReaderWriter(SqlDBClient dbService, Entity e, String dbSuffix, Class<T> clazz,
48 String controllerId) {
49 super(dbService, e, dbSuffix, clazz, controllerId);
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);
57 public <S extends DataObject> String write(S object, String id) {
59 return this.writeWithoutId(object);
61 InsertQuery<S> query = new InsertQuery<S>(this.entity, object, this.controllerId, this.ignoreControllerId);
63 if (LOG.isTraceEnabled()) {
64 LOG.trace("query={}", query.toSql());
66 boolean success = false;
68 success = this.dbService.write(query.toSql());
69 } catch (SQLException e) {
70 LOG.warn("problem writing data into db: ", e);
73 return success ? id : null;
76 private <S extends DataObject> String writeWithoutId(S object) {
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());
84 return this.dbService.writeAndReturnId(query.toSql());
85 } catch (SQLException e) {
86 LOG.warn("problem writing data into db: ", e);
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);
94 if (LOG.isTraceEnabled()) {
95 LOG.trace("query={}", query.toSql());
97 String insertedId = null;
98 PreparedStatement stmt = null;
99 Connection connection = null;
101 connection = this.dbService.getConnection();
102 stmt = connection.prepareStatement(query.toSql());
105 int affectedRows = stmt.getUpdateCount();
107 if (affectedRows > 0) {
110 if (LOG.isTraceEnabled()) {
111 LOG.trace("insertedid={}", insertedId);
113 } catch (SQLException e) {
114 LOG.warn("problem writing data into db: ", e);
119 } catch (SQLException e) {
120 LOG.warn("problem closing sql statement: ", e);
123 if (connection != null) {
126 } catch (SQLException e) {
127 LOG.warn("problem closing sql connection: ", e);
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);
138 String insertedId = null;
139 if (LOG.isTraceEnabled()) {
140 LOG.trace("query={}", query.toSql());
142 PreparedStatement stmt = null;
143 Connection connection = null;
145 connection = this.dbService.getConnection();
146 stmt = connection.prepareStatement(query.toSql());
149 int affectedRows = stmt.getUpdateCount();
151 if (affectedRows > 0) {
154 } catch (SQLException e) {
155 LOG.warn("problem writing data into db: ", e);
160 } catch (SQLException e) {
161 LOG.warn("problem closing sql statement: ", e);
164 if (connection != null) {
167 } catch (SQLException e) {
168 LOG.warn("problem closing sql connection: ", e);
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.");
181 // this.writeInterfaceClazz = writeInterfaceClazz;
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());
190 int affectedRows = 0;
191 PreparedStatement stmt = null;
192 Connection connection = null;
194 connection = this.dbService.getConnection();
195 stmt = connection.prepareStatement(query.toSql());
197 affectedRows = stmt.getUpdateCount();
199 } catch (SQLException e) {
200 LOG.warn("problem execute delete query: ", e);
205 } catch (SQLException e) {
206 LOG.warn("problem closing sql statement: ", e);
209 if (connection != null) {
212 } catch (SQLException e) {
213 LOG.warn("problem closing sql connection: ", e);
220 public int remove(@Nullable String id) {
221 return this.remove(Arrays.asList(new FilterBuilder().setProperty("id").setFiltervalue(id).build()));