9283df9eaa4596ee031bd86cc416da4dce6b575c
[ccsdk/sli/adaptors.git] /
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.rm.dao.jdbc;
23
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;
36
37 public class ResourceJdbcDaoImpl implements ResourceJdbcDao {
38
39     @SuppressWarnings("unused")
40     private static final Logger log = LoggerFactory.getLogger(ResourceJdbcDaoImpl.class);
41
42     private static final String RESOURCE_SQL = "SELECT * FROM RESOURCE WHERE asset_id = ? AND resource_name = ?";
43
44     private static final String RESOURCE_QUERY_1_SQL =
45             "SELECT * FROM RESOURCE WHERE asset_id LIKE ? AND resource_name = ?";
46
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 = ?)";
49
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 = ?)";
52
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 (?, ?, ?, ?, ?, ?, ?)";
56
57     private static final String UPDATE_SQL = "UPDATE RESOURCE SET\n"
58             + "  lt_used = ?, ll_label = ?, ll_reference_count = ?, rr_used = ?\nWHERE resource_id = ?";
59
60     private static final String DELETE_SQL = "DELETE FROM RESOURCE WHERE resource_id = ?";
61
62     private JdbcTemplate jdbcTemplate;
63     private ResourceRowMapper resourceRowMapper = new ResourceRowMapper();
64
65     @Override
66     public Resource getResource(String assetId, String resourceName) {
67         if (assetId == null || assetId.trim().length() == 0 || resourceName == null
68                 || resourceName.trim().length() == 0) {
69             return null;
70         }
71
72         List<Resource> ll = jdbcTemplate.query(RESOURCE_SQL, new Object[] {assetId, resourceName}, resourceRowMapper);
73         return ll.isEmpty() ? null : ll.get(0);
74     }
75
76     @Override
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();
81         }
82
83
84         return jdbcTemplate.query(RESOURCE_QUERY_1_SQL, new Object[] {assetIdFilter, resourceName}, resourceRowMapper);
85     }
86
87     @Override
88     public List<Resource> getResourceSet(String resourceSetId) {
89         if (resourceSetId == null) {
90             return Collections.emptyList();
91         }
92
93         return jdbcTemplate.query(RESOURCE_SET_SQL, new Object[] {resourceSetId}, resourceRowMapper);
94     }
95
96     @Override
97     public List<Resource> getResourceUnion(String resourceUnionId) {
98         if (resourceUnionId == null) {
99             return Collections.emptyList();
100         }
101
102         return jdbcTemplate.query(RESOURCE_UNION_SQL, new Object[] {resourceUnionId}, resourceRowMapper);
103     }
104
105     @Override
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);
116             return ps;
117         };
118         KeyHolder keyHolder = new GeneratedKeyHolder();
119         jdbcTemplate.update(psc, keyHolder);
120         r.id = keyHolder.getKey().longValue();
121     }
122
123     @Override
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);
128     }
129
130     @Override
131     public void delete(long id) {
132         jdbcTemplate.update(DELETE_SQL, id);
133     }
134
135     private static class ResourceRowMapper implements RowMapper<Resource> {
136
137         @Override
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");
148             return r;
149         }
150     }
151
152     public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
153         this.jdbcTemplate = jdbcTemplate;
154     }
155 }