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 INSERT_SQL = "INSERT INTO RESOURCE (\n"
54 + " asset_id, resource_name, resource_type, lt_used, ll_label, ll_reference_count, rr_used)\n"
55 + "VALUES (?, ?, ?, ?, ?, ?, ?)";
57 private static final String UPDATE_SQL = "UPDATE RESOURCE SET\n"
58 + " lt_used = ?, ll_label = ?, ll_reference_count = ?, rr_used = ?\nWHERE resource_id = ?";
60 private static final String DELETE_SQL = "DELETE FROM RESOURCE WHERE resource_id = ?";
62 private JdbcTemplate jdbcTemplate;
63 private ResourceRowMapper resourceRowMapper = new ResourceRowMapper();
66 public Resource getResource(String assetId, String resourceName) {
67 if (assetId == null || assetId.trim().length() == 0 || resourceName == null
68 || resourceName.trim().length() == 0) {
72 List<Resource> ll = jdbcTemplate.query(RESOURCE_SQL, new Object[] {assetId, resourceName}, resourceRowMapper);
73 return ll.isEmpty() ? null : ll.get(0);
77 public List<Resource> queryResources(String assetIdFilter, String resourceName) {
78 if (assetIdFilter == null || assetIdFilter.trim().length() == 0 || resourceName == null
79 || resourceName.trim().length() == 0) {
80 return Collections.emptyList();
84 return jdbcTemplate.query(RESOURCE_QUERY_1_SQL, new Object[] {assetIdFilter, resourceName}, resourceRowMapper);
88 public List<Resource> getResourceSet(String resourceSetId) {
89 if (resourceSetId == null) {
90 return Collections.emptyList();
93 return jdbcTemplate.query(RESOURCE_SET_SQL, new Object[] {resourceSetId}, resourceRowMapper);
97 public List<Resource> getResourceUnion(String resourceUnionId) {
98 if (resourceUnionId == null) {
99 return Collections.emptyList();
102 return jdbcTemplate.query(RESOURCE_UNION_SQL, new Object[] {resourceUnionId}, resourceRowMapper);
106 public void add(final Resource r) {
107 PreparedStatementCreator psc = dbc -> {
108 PreparedStatement ps = dbc.prepareStatement(INSERT_SQL, new String[] {"resource_id"});
109 ps.setString(1, r.assetId);
110 ps.setString(2, r.name);
111 ps.setString(3, r.type);
112 ps.setLong(4, r.ltUsed);
113 ps.setString(5, r.llLabel);
114 ps.setInt(6, r.llReferenceCount);
115 ps.setString(7, r.rrUsed);
118 KeyHolder keyHolder = new GeneratedKeyHolder();
119 jdbcTemplate.update(psc, keyHolder);
120 r.id = keyHolder.getKey().longValue();
124 public void update(Resource r) {
125 Long ltUsed = r.ltUsed <= 0 ? null : r.ltUsed;
126 Integer llRefCount = r.llReferenceCount <= 0 ? null : r.llReferenceCount;
127 jdbcTemplate.update(UPDATE_SQL, ltUsed, r.llLabel, llRefCount, r.rrUsed, r.id);
131 public void delete(long id) {
132 jdbcTemplate.update(DELETE_SQL, id);
135 private static class ResourceRowMapper implements RowMapper<Resource> {
138 public Resource mapRow(ResultSet rs, int arg1) throws SQLException {
139 Resource r = new Resource();
140 r.id = rs.getLong("resource_id");
141 r.assetId = rs.getString("asset_id");
142 r.name = rs.getString("resource_name");
143 r.type = rs.getString("resource_type");
144 r.ltUsed = rs.getLong("lt_used");
145 r.llLabel = rs.getString("ll_label");
146 r.llReferenceCount = rs.getInt("ll_reference_count");
147 r.rrUsed = rs.getString("rr_used");
152 public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
153 this.jdbcTemplate = jdbcTemplate;