2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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.sli.adaptors.rm.dao.jdbc;
24 import java.sql.PreparedStatement;
25 import java.sql.ResultSet;
26 import java.sql.SQLException;
27 import java.util.Collections;
28 import java.util.List;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.jdbc.core.JdbcTemplate;
32 import org.springframework.jdbc.core.PreparedStatementCreator;
33 import org.springframework.jdbc.core.RowMapper;
34 import org.springframework.jdbc.support.GeneratedKeyHolder;
35 import org.springframework.jdbc.support.KeyHolder;
37 public class ResourceJdbcDaoImpl implements ResourceJdbcDao {
39 @SuppressWarnings("unused")
40 private static final Logger log = LoggerFactory.getLogger(ResourceJdbcDaoImpl.class);
42 private static final String RESOURCE_SQL = "SELECT * FROM RESOURCE WHERE asset_id = ? AND resource_name = ?";
44 private static final String RESOURCE_QUERY_1_SQL =
45 "SELECT * FROM RESOURCE WHERE asset_id LIKE ? AND resource_name = ?";
47 private static final String RESOURCE_SET_SQL = "SELECT * FROM RESOURCE WHERE resource_id IN (\n"
48 + "SELECT DISTINCT resource_id FROM ALLOCATION_ITEM WHERE resource_set_id = ?)";
50 private static final String RESOURCE_UNION_SQL = "SELECT * FROM RESOURCE WHERE resource_id IN (\n"
51 + "SELECT DISTINCT resource_id FROM ALLOCATION_ITEM WHERE resource_union_id = ?)";
53 private static final String RESOURCE_SET_FOR_ASSET_SQL = "SELECT * FROM RESOURCE WHERE resource_id IN (\n"
54 + "SELECT DISTINCT resource_id FROM ALLOCATION_ITEM WHERE resource_set_id = ?) AND asset_id = ?";
56 private static final String RESOURCE_UNION_FOR_ASSET_SQL = "SELECT * FROM RESOURCE WHERE resource_id IN (\n"
57 + "SELECT DISTINCT resource_id FROM ALLOCATION_ITEM WHERE resource_union_id = ?) AND asset_id = ?";
59 private static final String INSERT_SQL = "INSERT INTO RESOURCE (\n"
60 + " asset_id, resource_name, resource_type, lt_used, ll_label, ll_reference_count, rr_used)\n"
61 + "VALUES (?, ?, ?, ?, ?, ?, ?)";
63 private static final String UPDATE_SQL = "UPDATE RESOURCE SET\n"
64 + " lt_used = ?, ll_label = ?, ll_reference_count = ?, rr_used = ?\nWHERE resource_id = ?";
66 private static final String DELETE_SQL = "DELETE FROM RESOURCE WHERE resource_id = ?";
68 private JdbcTemplate jdbcTemplate;
69 private ResourceRowMapper resourceRowMapper = new ResourceRowMapper();
72 public Resource getResource(String assetId, String resourceName) {
73 if (assetId == null || assetId.trim().length() == 0 || resourceName == null
74 || resourceName.trim().length() == 0) {
78 List<Resource> ll = jdbcTemplate.query(RESOURCE_SQL, new Object[] {assetId, resourceName}, resourceRowMapper);
79 return ll.isEmpty() ? null : ll.get(0);
83 public List<Resource> queryResources(String assetIdFilter, String resourceName) {
84 if (assetIdFilter == null || assetIdFilter.trim().length() == 0 || resourceName == null
85 || resourceName.trim().length() == 0) {
86 return Collections.emptyList();
90 return jdbcTemplate.query(RESOURCE_QUERY_1_SQL, new Object[] {assetIdFilter, resourceName}, resourceRowMapper);
94 public List<Resource> getResourceSet(String resourceSetId) {
95 if (resourceSetId == null) {
96 return Collections.emptyList();
99 return jdbcTemplate.query(RESOURCE_SET_SQL, new Object[] {resourceSetId}, resourceRowMapper);
103 public List<Resource> getResourceUnion(String resourceUnionId) {
104 if (resourceUnionId == null) {
105 return Collections.emptyList();
108 return jdbcTemplate.query(RESOURCE_UNION_SQL, new Object[] {resourceUnionId}, resourceRowMapper);
112 public List<Resource> getResourceSetForAsset(String resourceSetId, String assetId) {
113 if (resourceSetId == null) {
114 return Collections.emptyList();
117 return jdbcTemplate.query(RESOURCE_SET_FOR_ASSET_SQL, new Object[] {resourceSetId, assetId}, resourceRowMapper);
121 public List<Resource> getResourceUnionForAsset(String resourceUnionId, String assetId) {
122 if (resourceUnionId == null) {
123 return Collections.emptyList();
126 return jdbcTemplate.query(RESOURCE_UNION_FOR_ASSET_SQL, new Object[] {resourceUnionId, assetId},
131 public void add(final Resource r) {
132 PreparedStatementCreator psc = dbc -> {
133 PreparedStatement ps = dbc.prepareStatement(INSERT_SQL, new String[] {"resource_id"});
134 ps.setString(1, r.assetId);
135 ps.setString(2, r.name);
136 ps.setString(3, r.type);
137 ps.setLong(4, r.ltUsed);
138 ps.setString(5, r.llLabel);
139 ps.setInt(6, r.llReferenceCount);
140 ps.setString(7, r.rrUsed);
143 KeyHolder keyHolder = new GeneratedKeyHolder();
144 jdbcTemplate.update(psc, keyHolder);
145 r.id = keyHolder.getKey().longValue();
149 public void update(Resource r) {
150 Long ltUsed = r.ltUsed <= 0 ? null : r.ltUsed;
151 Integer llRefCount = r.llReferenceCount <= 0 ? null : r.llReferenceCount;
152 jdbcTemplate.update(UPDATE_SQL, ltUsed, r.llLabel, llRefCount, r.rrUsed, r.id);
156 public void delete(long id) {
157 jdbcTemplate.update(DELETE_SQL, id);
160 private static class ResourceRowMapper implements RowMapper<Resource> {
163 public Resource mapRow(ResultSet rs, int arg1) throws SQLException {
164 Resource r = new Resource();
165 r.id = rs.getLong("resource_id");
166 r.assetId = rs.getString("asset_id");
167 r.name = rs.getString("resource_name");
168 r.type = rs.getString("resource_type");
169 r.ltUsed = rs.getLong("lt_used");
170 r.llLabel = rs.getString("ll_label");
171 r.llReferenceCount = rs.getInt("ll_reference_count");
172 r.rrUsed = rs.getString("rr_used");
177 public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
178 this.jdbcTemplate = jdbcTemplate;