[CCSDK-245] RA: Refactor RA to make it generic
[ccsdk/sli/adaptors.git] / resource-assignment / provider / src / main / java / org / onap / ccsdk / sli / adaptors / lock / dao / ResourceLockDaoImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
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.adaptors.lock.dao;
23
24 import java.sql.ResultSet;
25 import java.sql.SQLException;
26 import java.util.Date;
27 import java.util.List;
28 import org.onap.ccsdk.sli.adaptors.lock.data.ResourceLock;
29 import org.onap.ccsdk.sli.adaptors.util.db.CachedDataSourceWrap;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.jdbc.core.JdbcTemplate;
33 import org.springframework.jdbc.core.RowMapper;
34
35 public class ResourceLockDaoImpl implements ResourceLockDao {
36
37     @SuppressWarnings("unused")
38     private static final Logger log = LoggerFactory.getLogger(ResourceLockDaoImpl.class);
39
40     private JdbcTemplate jdbcTemplate;
41
42     @Override
43     public void add(ResourceLock l) {
44         jdbcTemplate.update(
45                 "INSERT INTO RESOURCE_LOCK (resource_name, lock_holder, lock_count, lock_time, expiration_time)\n" +
46                         "VALUES (?, ?, ?, ?, ?)",
47                 new Object[] { l.resourceName, l.lockHolder, l.lockCount, l.lockTime, l.expirationTime });
48     }
49
50     @Override
51     public void update(long id, Date lockTime, Date expirationTime, int lockCount) {
52         jdbcTemplate.update(
53                 "UPDATE RESOURCE_LOCK SET lock_time = ?, expiration_time = ?, lock_count = ? WHERE resource_lock_id = ?",
54                 new Object[] { lockTime, expirationTime, lockCount, id });
55     }
56
57     @Override
58     public ResourceLock getByResourceName(String resourceName) {
59         List<ResourceLock> ll = jdbcTemplate.query("SELECT * FROM RESOURCE_LOCK WHERE resource_name = ?",
60                 new Object[] { resourceName }, new RowMapper<ResourceLock>() {
61
62                     @Override
63                     public ResourceLock mapRow(ResultSet rs, int rowNum) throws SQLException {
64                         ResourceLock rl = new ResourceLock();
65                         rl.id = rs.getLong("resource_lock_id");
66                         rl.resourceName = rs.getString("resource_name");
67                         rl.lockHolder = rs.getString("lock_holder");
68                         rl.lockCount = rs.getInt("lock_count");
69                         rl.lockTime = rs.getTimestamp("lock_time");
70                         rl.expirationTime = rs.getTimestamp("expiration_time");
71                         return rl;
72                     }
73                 });
74         return ll != null && !ll.isEmpty() ? ll.get(0) : null;
75     }
76
77     @Override
78     public void delete(long id) {
79         jdbcTemplate.update("DELETE FROM RESOURCE_LOCK WHERE resource_lock_id = ?", new Object[] { id });
80     }
81
82     @Override
83     public void decrementLockCount(long id) {
84         jdbcTemplate.update("UPDATE RESOURCE_LOCK SET lock_count = lock_count - 1 WHERE resource_lock_id = ?",
85                 new Object[] { id });
86     }
87
88     public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
89         this.jdbcTemplate = jdbcTemplate;
90     }
91
92     @Override
93     public void commit() {
94         if (jdbcTemplate.getDataSource() instanceof CachedDataSourceWrap) {
95             CachedDataSourceWrap ds = (CachedDataSourceWrap) jdbcTemplate.getDataSource();
96             ds.commit();
97             ds.releaseConnection();
98         }
99     }
100
101     @Override
102     public void rollback() {
103         if (jdbcTemplate.getDataSource() instanceof CachedDataSourceWrap) {
104             CachedDataSourceWrap ds = (CachedDataSourceWrap) jdbcTemplate.getDataSource();
105             ds.rollback();
106             ds.releaseConnection();
107         }
108     }
109 }