fcde0f8f12f25a506733049ec0d088d573349fc9
[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.sql.Timestamp;
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 AllocationItemJdbcDaoImpl implements AllocationItemJdbcDao {
39
40     @SuppressWarnings("unused")
41     private static final Logger log = LoggerFactory.getLogger(ResourceJdbcDaoImpl.class);
42
43     private static final String INSERT_SQL = "INSERT INTO ALLOCATION_ITEM (\n"
44             + "  resource_id, application_id, resource_set_id, resource_union_id, resource_share_group_list,\n"
45             + "  lt_used, ll_label, rr_used, allocation_time)\nVALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
46
47     private static final String UPDATE_SQL = "UPDATE ALLOCATION_ITEM SET\n"
48             + "  resource_share_group_list = ?, lt_used = ?, ll_label = ?, rr_used = ?, allocation_time = ?\n"
49             + "WHERE allocation_item_id = ?";
50
51     private static final String DELETE_SQL = "DELETE FROM ALLOCATION_ITEM WHERE allocation_item_id = ?";
52
53     private static final String GET_SQL = "SELECT * FROM ALLOCATION_ITEM WHERE resource_id = ?";
54
55     private JdbcTemplate jdbcTemplate;
56     private AllocationItemRowMapper allocationItemRowMapper = new AllocationItemRowMapper();
57
58     @Override
59     public void add(final AllocationItem ai) {
60         PreparedStatementCreator psc = dbc -> {
61             PreparedStatement ps = dbc.prepareStatement(INSERT_SQL, new String[] { "allocation_item_id" });
62             ps.setLong(1, ai.resourceId);
63             ps.setString(2, ai.applicationId);
64             ps.setString(3, ai.resourceSetId);
65             ps.setString(4, ai.resourceUnionId);
66             ps.setString(5, ai.resourceShareGroupList);
67             ps.setLong(6, ai.ltUsed);
68             ps.setString(7, ai.llLabel);
69             ps.setString(8, ai.rrUsed);
70             ps.setTimestamp(9, new Timestamp(ai.allocationTime.getTime()));
71             return ps;
72         };
73         KeyHolder keyHolder = new GeneratedKeyHolder();
74         jdbcTemplate.update(psc, keyHolder);
75         ai.id = keyHolder.getKey().longValue();
76     }
77
78     @Override
79     public void update(AllocationItem ai) {
80         Long ltUsed = ai.ltUsed <= 0 ? null : ai.ltUsed;
81         jdbcTemplate.update(UPDATE_SQL, ai.resourceShareGroupList, ltUsed, ai.llLabel, ai.rrUsed, ai.allocationTime,
82                 ai.id);
83     }
84
85     @Override
86     public void delete(long id) {
87         jdbcTemplate.update(DELETE_SQL, id);
88     }
89
90     @Override
91     public List<AllocationItem> getAllocationItems(long resourceId) {
92         if (resourceId <= 0) {
93             return Collections.emptyList();
94         }
95
96         return jdbcTemplate.query(GET_SQL, new Object[] { resourceId }, allocationItemRowMapper);
97     }
98
99     @Override
100     public List<AllocationItem> queryAllocationItems(long resourceId, String resourceUnionFilter,
101             String resourceShareGroupFilter) {
102         if (resourceId <= 0) {
103             return Collections.emptyList();
104         }
105
106         String sql = GET_SQL;
107
108         if (resourceUnionFilter != null) {
109             sql += " AND resource_union_id LIKE '" + resourceUnionFilter + "'";
110         }
111
112         if (resourceShareGroupFilter != null) {
113             if (resourceShareGroupFilter.equalsIgnoreCase("null")) {
114                 sql += " AND resource_share_group_list IS NULL";
115             } else {
116                 sql += " AND resource_share_group_list LIKE '" + resourceShareGroupFilter + "'";
117             }
118         }
119
120         return jdbcTemplate.query(sql, new Object[] { resourceId }, allocationItemRowMapper);
121     }
122
123     private static class AllocationItemRowMapper implements RowMapper<AllocationItem> {
124
125         @Override
126         public AllocationItem mapRow(ResultSet rs, int n) throws SQLException {
127             AllocationItem ai = new AllocationItem();
128             ai.id = rs.getLong("allocation_item_id");
129             ai.resourceId = rs.getLong("resource_id");
130             ai.applicationId = rs.getString("application_id");
131             ai.resourceSetId = rs.getString("resource_set_id");
132             ai.resourceUnionId = rs.getString("resource_union_id");
133             ai.resourceShareGroupList = rs.getString("resource_share_group_list");
134             ai.ltUsed = rs.getLong("lt_used");
135             ai.llLabel = rs.getString("ll_label");
136             ai.rrUsed = rs.getString("rr_used");
137             ai.allocationTime = rs.getTimestamp("allocation_time");
138             return ai;
139         }
140     }
141
142     public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
143         this.jdbcTemplate = jdbcTemplate;
144     }
145 }