Updated aai-path.properties with AAI API'S
[ccsdk/sli/adaptors.git] / resource-assignment / provider / src / main / java / org / onap / ccsdk / sli / adaptors / rm / dao / jdbc / ResourceJdbcDaoImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                         reserved.
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  */
22
23 package org.onap.ccsdk.sli.adaptors.rm.dao.jdbc;
24
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;
37
38 public class ResourceJdbcDaoImpl implements ResourceJdbcDao {
39
40     private static final String baseSelectResourceQuery = "SELECT * FROM RESOURCE WHERE resource_id IN (\n";
41
42     @SuppressWarnings("unused")
43     private static final Logger log = LoggerFactory.getLogger(ResourceJdbcDaoImpl.class);
44
45     private static final String RESOURCE_SQL = "SELECT * FROM RESOURCE WHERE asset_id = ? AND resource_name = ?";
46
47     private static final String RESOURCE_QUERY_1_SQL =
48             "SELECT * FROM RESOURCE WHERE asset_id LIKE ? AND resource_name = ?";
49
50     private static final String RESOURCE_SET_SQL = baseSelectResourceQuery
51             + "SELECT DISTINCT resource_id FROM ALLOCATION_ITEM WHERE resource_set_id = ?)";
52
53     private static final String RESOURCE_UNION_SQL = baseSelectResourceQuery
54             + "SELECT DISTINCT resource_id FROM ALLOCATION_ITEM WHERE resource_union_id = ?)";
55
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 = ?";
58
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 = ?";
61
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 (?, ?, ?, ?, ?, ?, ?)";
65
66     private static final String UPDATE_SQL = "UPDATE RESOURCE SET\n"
67             + "  lt_used = ?, ll_label = ?, ll_reference_count = ?, rr_used = ?\nWHERE resource_id = ?";
68
69     private static final String DELETE_SQL = "DELETE FROM RESOURCE WHERE resource_id = ?";
70
71     private JdbcTemplate jdbcTemplate;
72     private ResourceRowMapper resourceRowMapper = new ResourceRowMapper();
73
74     @Override
75     public Resource getResource(String assetId, String resourceName) {
76         if (assetId == null || assetId.trim().length() == 0 || resourceName == null
77                 || resourceName.trim().length() == 0) {
78             return null;
79         }
80
81         List<Resource> ll = jdbcTemplate.query(RESOURCE_SQL, new Object[] {assetId, resourceName}, resourceRowMapper);
82         return ll.isEmpty() ? null : ll.get(0);
83     }
84
85     @Override
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();
90         }
91
92
93         return jdbcTemplate.query(RESOURCE_QUERY_1_SQL, new Object[] {assetIdFilter, resourceName}, resourceRowMapper);
94     }
95
96     @Override
97     public List<Resource> getResourceSet(String resourceSetId) {
98         if (resourceSetId == null) {
99             return Collections.emptyList();
100         }
101
102         return jdbcTemplate.query(RESOURCE_SET_SQL, new Object[] {resourceSetId}, resourceRowMapper);
103     }
104
105     @Override
106     public List<Resource> getResourceUnion(String resourceUnionId) {
107         if (resourceUnionId == null) {
108             return Collections.emptyList();
109         }
110
111         return jdbcTemplate.query(RESOURCE_UNION_SQL, new Object[] {resourceUnionId}, resourceRowMapper);
112     }
113
114     @Override
115     public List<Resource> getResourceSetForAsset(String resourceSetId, String assetId) {
116         if (resourceSetId == null) {
117             return Collections.emptyList();
118         }
119
120         return jdbcTemplate.query(RESOURCE_SET_FOR_ASSET_SQL, new Object[] {resourceSetId, assetId}, resourceRowMapper);
121     }
122
123     @Override
124     public List<Resource> getResourceUnionForAsset(String resourceUnionId, String assetId) {
125         if (resourceUnionId == null) {
126             return Collections.emptyList();
127         }
128
129         return jdbcTemplate.query(RESOURCE_UNION_FOR_ASSET_SQL, new Object[] {resourceUnionId, assetId},
130                 resourceRowMapper);
131     }
132
133     @Override
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);
144             return ps;
145         };
146         KeyHolder keyHolder = new GeneratedKeyHolder();
147         jdbcTemplate.update(psc, keyHolder);
148         r.id = keyHolder.getKey().longValue();
149     }
150
151     @Override
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);
156     }
157
158     @Override
159     public void delete(long id) {
160         jdbcTemplate.update(DELETE_SQL, id);
161     }
162
163     private static class ResourceRowMapper implements RowMapper<Resource> {
164
165         @Override
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");
176             return r;
177         }
178     }
179
180     public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
181         this.jdbcTemplate = jdbcTemplate;
182     }
183 }