[CCSDK-6] Populate seed code
[ccsdk/sli/adaptors.git] / resource-assignment / provider / src / main / java / org / openecomp / sdnc / rm / dao / jdbc / ResourceJdbcDaoImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 ONAP 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.openecomp.sdnc.rm.dao.jdbc;
23
24 import java.sql.Connection;
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
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.jdbc.core.JdbcTemplate;
34 import org.springframework.jdbc.core.PreparedStatementCreator;
35 import org.springframework.jdbc.core.RowMapper;
36 import org.springframework.jdbc.support.GeneratedKeyHolder;
37 import org.springframework.jdbc.support.KeyHolder;
38
39 public class ResourceJdbcDaoImpl implements ResourceJdbcDao {
40
41         @SuppressWarnings("unused")
42         private static final Logger log = LoggerFactory.getLogger(ResourceJdbcDaoImpl.class);
43
44         private static final String RESOURCE_SQL = "SELECT * FROM RESOURCE WHERE asset_id = ? AND resource_name = ?";
45
46         private static final String RESOURCE_SET_SQL = "SELECT * FROM RESOURCE WHERE resource_id IN (\n"
47                 + "SELECT DISTINCT resource_id FROM ALLOCATION_ITEM WHERE resource_set_id = ?)";
48
49         private static final String RESOURCE_UNION_SQL = "SELECT * FROM RESOURCE WHERE resource_id IN (\n"
50                 + "SELECT DISTINCT resource_id FROM ALLOCATION_ITEM WHERE resource_union_id = ?)";
51
52         private static final String INSERT_SQL = "INSERT INTO RESOURCE (\n"
53                 + "  asset_id, resource_name, resource_type, lt_used, ll_label, ll_reference_count, rr_used)\n"
54                 + "VALUES (?, ?, ?, ?, ?, ?, ?)";
55
56         private static final String UPDATE_SQL = "UPDATE RESOURCE SET\n"
57                 + "  lt_used = ?, ll_label = ?, ll_reference_count = ?, rr_used = ?\nWHERE resource_id = ?";
58
59         private static final String DELETE_SQL = "DELETE FROM RESOURCE WHERE resource_id = ?";
60
61         private JdbcTemplate jdbcTemplate;
62         private ResourceRowMapper resourceRowMapper = new ResourceRowMapper();
63
64         @Override
65         public Resource getResource(String assetId, String resourceName) {
66                 if (assetId == null || assetId.trim().length() == 0 || resourceName == null ||
67                         resourceName.trim().length() == 0)
68                         return null;
69
70                 List<Resource> ll = jdbcTemplate.query(RESOURCE_SQL, new Object[] { assetId, resourceName }, resourceRowMapper);
71                 return ll.isEmpty() ? null : ll.get(0);
72         }
73
74         @Override
75         public List<Resource> getResourceSet(String resourceSetId) {
76                 if (resourceSetId == null)
77                         return Collections.emptyList();
78
79                 return jdbcTemplate.query(RESOURCE_SET_SQL, new Object[] { resourceSetId }, resourceRowMapper);
80         }
81
82         @Override
83         public List<Resource> getResourceUnion(String resourceUnionId) {
84                 if (resourceUnionId == null)
85                         return Collections.emptyList();
86
87                 return jdbcTemplate.query(RESOURCE_UNION_SQL, new Object[] { resourceUnionId }, resourceRowMapper);
88         }
89
90         @Override
91         public void add(final Resource r) {
92                 PreparedStatementCreator psc = new PreparedStatementCreator() {
93
94                         @Override
95                         public PreparedStatement createPreparedStatement(Connection dbc) throws SQLException {
96                                 PreparedStatement ps = dbc.prepareStatement(INSERT_SQL, new String[] { "resource_id" });
97                                 ps.setString(1, r.assetId);
98                                 ps.setString(2, r.name);
99                                 ps.setString(3, r.type);
100                                 ps.setLong(4, r.ltUsed);
101                                 ps.setString(5, r.llLabel);
102                                 ps.setInt(6, r.llReferenceCount);
103                                 ps.setString(7, r.rrUsed);
104                                 return ps;
105                         }
106                 };
107                 KeyHolder keyHolder = new GeneratedKeyHolder();
108                 jdbcTemplate.update(psc, keyHolder);
109                 r.id = keyHolder.getKey().longValue();
110         }
111
112         @Override
113     public void update(Resource r) {
114                 Long ltUsed = r.ltUsed <= 0 ? null : r.ltUsed;
115                 Integer llRefCount = r.llReferenceCount <= 0 ? null : r.llReferenceCount;
116                 jdbcTemplate.update(UPDATE_SQL, ltUsed, r.llLabel, llRefCount, r.rrUsed, r.id);
117         }
118
119         @Override
120         public void delete(long id) {
121                 jdbcTemplate.update(DELETE_SQL, id);
122         }
123
124         private static class ResourceRowMapper implements RowMapper<Resource> {
125
126                 @Override
127                 public Resource mapRow(ResultSet rs, int arg1) throws SQLException {
128                         Resource r = new Resource();
129                         r.id = rs.getLong("resource_id");
130                         r.assetId = rs.getString("asset_id");
131                         r.name = rs.getString("resource_name");
132                         r.type = rs.getString("resource_type");
133                         r.ltUsed = rs.getLong("lt_used");
134                         r.llLabel = rs.getString("ll_label");
135                         r.llReferenceCount = rs.getInt("ll_reference_count");
136                         r.rrUsed = rs.getString("rr_used");
137                         return r;
138                 }
139         }
140
141         public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
142                 this.jdbcTemplate = jdbcTemplate;
143         }
144 }