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