2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * Modifications Copyright (C) 2018 IBM.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.ccsdk.sli.adaptors.rm.dao.jdbc;
25 import java.sql.PreparedStatement;
26 import java.sql.ResultSet;
27 import java.sql.SQLException;
28 import java.util.Collections;
29 import java.util.List;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.jdbc.core.JdbcTemplate;
33 import org.springframework.jdbc.core.PreparedStatementCreator;
34 import org.springframework.jdbc.core.RowMapper;
35 import org.springframework.jdbc.support.GeneratedKeyHolder;
36 import org.springframework.jdbc.support.KeyHolder;
38 public class ResourceJdbcDaoImpl implements ResourceJdbcDao {
40 private static final String baseSelectResourceQuery = "SELECT * FROM RESOURCE WHERE resource_id IN (\n";
42 @SuppressWarnings("unused")
43 private static final Logger log = LoggerFactory.getLogger(ResourceJdbcDaoImpl.class);
45 private static final String RESOURCE_SQL = "SELECT * FROM RESOURCE WHERE asset_id = ? AND resource_name = ?";
47 private static final String RESOURCE_QUERY_1_SQL =
48 "SELECT * FROM RESOURCE WHERE asset_id LIKE ? AND resource_name = ?";
50 private static final String RESOURCE_SET_SQL = baseSelectResourceQuery
51 + "SELECT DISTINCT resource_id FROM ALLOCATION_ITEM WHERE resource_set_id = ?)";
53 private static final String RESOURCE_UNION_SQL = baseSelectResourceQuery
54 + "SELECT DISTINCT resource_id FROM ALLOCATION_ITEM WHERE resource_union_id = ?)";
56 private static final String RESOURCE_SET_FOR_ASSET_SQL = baseSelectResourceQuery
57 + "SELECT DISTINCT resource_id FROM ALLOCATION_ITEM WHERE resource_set_id = ?) AND asset_id = ?";
59 private static final String RESOURCE_UNION_FOR_ASSET_SQL = baseSelectResourceQuery
60 + "SELECT DISTINCT resource_id FROM ALLOCATION_ITEM WHERE resource_union_id = ?) AND asset_id = ?";
62 private static final String INSERT_SQL = "INSERT INTO RESOURCE (\n"
63 + " asset_id, resource_name, resource_type, lt_used, ll_label, ll_reference_count, rr_used)\n"
64 + "VALUES (?, ?, ?, ?, ?, ?, ?)";
66 private static final String UPDATE_SQL = "UPDATE RESOURCE SET\n"
67 + " lt_used = ?, ll_label = ?, ll_reference_count = ?, rr_used = ?\nWHERE resource_id = ?";
69 private static final String DELETE_SQL = "DELETE FROM RESOURCE WHERE resource_id = ?";
71 private JdbcTemplate jdbcTemplate;
72 private ResourceRowMapper resourceRowMapper = new ResourceRowMapper();
75 public Resource getResource(String assetId, String resourceName) {
76 if (assetId == null || assetId.trim().length() == 0 || resourceName == null
77 || resourceName.trim().length() == 0) {
81 List<Resource> ll = jdbcTemplate.query(RESOURCE_SQL, new Object[] {assetId, resourceName}, resourceRowMapper);
82 return ll.isEmpty() ? null : ll.get(0);
86 public List<Resource> queryResources(String assetIdFilter, String resourceName) {
87 if (assetIdFilter == null || assetIdFilter.trim().length() == 0 || resourceName == null
88 || resourceName.trim().length() == 0) {
89 return Collections.emptyList();
93 return jdbcTemplate.query(RESOURCE_QUERY_1_SQL, new Object[] {assetIdFilter, resourceName}, resourceRowMapper);
97 public List<Resource> getResourceSet(String resourceSetId) {
98 if (resourceSetId == null) {
99 return Collections.emptyList();
102 return jdbcTemplate.query(RESOURCE_SET_SQL, new Object[] {resourceSetId}, resourceRowMapper);
106 public List<Resource> getResourceUnion(String resourceUnionId) {
107 if (resourceUnionId == null) {
108 return Collections.emptyList();
111 return jdbcTemplate.query(RESOURCE_UNION_SQL, new Object[] {resourceUnionId}, resourceRowMapper);
115 public List<Resource> getResourceSetForAsset(String resourceSetId, String assetId) {
116 if (resourceSetId == null) {
117 return Collections.emptyList();
120 return jdbcTemplate.query(RESOURCE_SET_FOR_ASSET_SQL, new Object[] {resourceSetId, assetId}, resourceRowMapper);
124 public List<Resource> getResourceUnionForAsset(String resourceUnionId, String assetId) {
125 if (resourceUnionId == null) {
126 return Collections.emptyList();
129 return jdbcTemplate.query(RESOURCE_UNION_FOR_ASSET_SQL, new Object[] {resourceUnionId, assetId},
134 public void add(final Resource r) {
135 PreparedStatementCreator psc = dbc -> {
136 PreparedStatement ps = dbc.prepareStatement(INSERT_SQL, new String[] {"resource_id"});
137 ps.setString(1, r.assetId);
138 ps.setString(2, r.name);
139 ps.setString(3, r.type);
140 ps.setLong(4, r.ltUsed);
141 ps.setString(5, r.llLabel);
142 ps.setInt(6, r.llReferenceCount);
143 ps.setString(7, r.rrUsed);
146 KeyHolder keyHolder = new GeneratedKeyHolder();
147 jdbcTemplate.update(psc, keyHolder);
148 r.id = keyHolder.getKey().longValue();
152 public void update(Resource r) {
153 Long ltUsed = r.ltUsed <= 0 ? null : r.ltUsed;
154 Integer llRefCount = r.llReferenceCount <= 0 ? null : r.llReferenceCount;
155 jdbcTemplate.update(UPDATE_SQL, ltUsed, r.llLabel, llRefCount, r.rrUsed, r.id);
159 public void delete(long id) {
160 jdbcTemplate.update(DELETE_SQL, id);
163 private static class ResourceRowMapper implements RowMapper<Resource> {
166 public Resource mapRow(ResultSet rs, int arg1) throws SQLException {
167 Resource r = new Resource();
168 r.id = rs.getLong("resource_id");
169 r.assetId = rs.getString("asset_id");
170 r.name = rs.getString("resource_name");
171 r.type = rs.getString("resource_type");
172 r.ltUsed = rs.getLong("lt_used");
173 r.llLabel = rs.getString("ll_label");
174 r.llReferenceCount = rs.getInt("ll_reference_count");
175 r.rrUsed = rs.getString("rr_used");
180 public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
181 this.jdbcTemplate = jdbcTemplate;