2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.ccsdk.sli.adaptors.ra.service.dao;
24 import java.sql.ResultSet;
25 import java.sql.SQLException;
26 import java.util.List;
28 import org.onap.ccsdk.sli.adaptors.ra.service.data.ServiceResource;
29 import org.onap.ccsdk.sli.adaptors.ra.service.data.ServiceStatus;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.jdbc.core.JdbcTemplate;
33 import org.springframework.jdbc.core.RowMapper;
35 public class ServiceResourceDaoImpl implements ServiceResourceDao {
37 @SuppressWarnings("unused")
38 private static final Logger log = LoggerFactory.getLogger(ServiceResourceDaoImpl.class);
40 private static final String GET_SQL =
41 "SELECT * FROM SERVICE_RESOURCE WHERE service_instance_id = ? AND service_status = ?";
43 private static final String ADD_SQL = "INSERT INTO SERVICE_RESOURCE (\n"
44 + " service_instance_id, service_status, service_change_number, resource_set_id, resource_union_id)\n"
45 + "VALUES (?, ?, ?, ?, ?)";
47 private static final String UPDATE_SQL =
48 "UPDATE SERVICE_RESOURCE SET service_change_number = ?, resource_set_id = ?\n"
49 + "WHERE service_instance_id = ? AND service_status = ?";
51 private static final String DELETE_SQL =
52 "DELETE FROM SERVICE_RESOURCE WHERE service_instance_id = ? AND service_status = ?";
54 private static final String UPDATE_STATUS_SQL =
55 "UPDATE SERVICE_RESOURCE SET service_status = ? WHERE service_instance_id = ? AND service_status = ?";
57 private JdbcTemplate jdbcTemplate;
60 public ServiceResource getServiceResource(final String serviceInstanceId, final ServiceStatus serviceStatus) {
61 List<ServiceResource> serviceResourceList =
62 jdbcTemplate.query(GET_SQL, new Object[] { serviceInstanceId, serviceStatus.toString() },
63 new RowMapper<ServiceResource>() {
66 public ServiceResource mapRow(ResultSet rs, int rowNum) throws SQLException {
67 ServiceResource sr = new ServiceResource();
68 sr.id = rs.getLong("service_resource_id");
69 sr.serviceInstanceId = serviceInstanceId;
70 sr.serviceStatus = serviceStatus;
71 sr.serviceChangeNumber = rs.getInt("service_change_number");
72 sr.resourceSetId = rs.getString("resource_set_id");
73 sr.resourceUnionId = rs.getString("resource_union_id");
77 if (serviceResourceList.isEmpty())
79 return serviceResourceList.get(0);
83 public void addServiceResource(ServiceResource serviceResource) {
84 jdbcTemplate.update(ADD_SQL, serviceResource.serviceInstanceId, serviceResource.serviceStatus.toString(),
85 serviceResource.serviceChangeNumber, serviceResource.resourceSetId, serviceResource.resourceUnionId);
89 public void updateServiceResource(ServiceResource serviceResource) {
90 jdbcTemplate.update(UPDATE_SQL, serviceResource.serviceChangeNumber, serviceResource.resourceSetId,
91 serviceResource.serviceInstanceId, serviceResource.serviceStatus.toString());
95 public void deleteServiceResource(String serviceInstanceId, ServiceStatus serviceStatus) {
96 jdbcTemplate.update(DELETE_SQL, serviceInstanceId, serviceStatus.toString());
100 public void updateServiceStatus(
101 String serviceInstanceId,
102 ServiceStatus serviceStatus,
103 ServiceStatus newServiceStatus) {
104 jdbcTemplate.update(UPDATE_STATUS_SQL, newServiceStatus.toString(), serviceInstanceId, serviceStatus.toString());
107 public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
108 this.jdbcTemplate = jdbcTemplate;