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.rule.dao;
24 import java.sql.ResultSet;
25 import java.sql.SQLException;
26 import java.util.List;
27 import org.onap.ccsdk.sli.adaptors.ra.rule.data.ResourceRule;
28 import org.onap.ccsdk.sli.adaptors.ra.rule.data.ResourceThreshold;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.jdbc.core.JdbcTemplate;
32 import org.springframework.jdbc.core.RowMapper;
34 public class ResourceRuleDaoImpl implements ResourceRuleDao {
36 @SuppressWarnings("unused")
37 private static final Logger log = LoggerFactory.getLogger(ResourceRuleDaoImpl.class);
39 private static final String GET1_SQL =
40 "SELECT * FROM RESOURCE_RULE WHERE service_model = ? AND equipment_level = ?";
41 private static final String GET2_SQL =
42 "SELECT * FROM RESOURCE_RULE WHERE service_model = ? AND end_point_position = ? AND equipment_level = ? AND resource_name = ?";
43 private static final String THRESHOLD_SQL = "SELECT * FROM RESOURCE_THRESHOLD WHERE resource_rule_id = ?";
45 private JdbcTemplate jdbcTemplate;
46 ResourceRuleRowMapper resourceRuleRowMapper = new ResourceRuleRowMapper();
47 ResourceThresholdRowMapper resourceThresholdRowMapper = new ResourceThresholdRowMapper();
50 public List<ResourceRule> getResourceRules(String serviceModel, String equipLevel) {
51 List<ResourceRule> resourceRuleList =
52 jdbcTemplate.query(GET1_SQL, new Object[] {serviceModel, equipLevel}, resourceRuleRowMapper);
54 for (ResourceRule rr : resourceRuleList) {
55 rr.thresholdList = jdbcTemplate.query(THRESHOLD_SQL, new Object[] {rr.id}, resourceThresholdRowMapper);
58 return resourceRuleList;
62 public ResourceRule getResourceRule(String serviceModel, String endPointPosition, String equipLevel,
63 String resourceName) {
64 List<ResourceRule> resourceRuleList = jdbcTemplate.query(GET2_SQL,
65 new Object[] {serviceModel, endPointPosition, equipLevel, resourceName}, resourceRuleRowMapper);
67 if (resourceRuleList == null || resourceRuleList.isEmpty()) {
71 ResourceRule rr = resourceRuleList.get(0);
72 rr.thresholdList = jdbcTemplate.query(THRESHOLD_SQL, new Object[] {rr.id}, resourceThresholdRowMapper);
77 private static class ResourceRuleRowMapper implements RowMapper<ResourceRule> {
80 public ResourceRule mapRow(ResultSet rs, int rowNum) throws SQLException {
81 ResourceRule rl = new ResourceRule();
82 rl.id = rs.getLong("resource_rule_id");
83 rl.resourceName = rs.getString("resource_name");
84 rl.serviceModel = rs.getString("service_model");
85 rl.endPointPosition = rs.getString("end_point_position");
86 rl.serviceExpression = rs.getString("service_expression");
87 rl.equipmentLevel = rs.getString("equipment_level");
88 rl.equipmentExpression = rs.getString("equipment_expression");
89 rl.allocationExpression = rs.getString("allocation_expression");
90 rl.softLimitExpression = rs.getString("soft_limit_expression");
91 rl.hardLimitExpression = rs.getString("hard_limit_expression");
96 private static class ResourceThresholdRowMapper implements RowMapper<ResourceThreshold> {
99 public ResourceThreshold mapRow(ResultSet rs, int rowNum) throws SQLException {
100 ResourceThreshold th = new ResourceThreshold();
101 th.expression = rs.getString("threshold_expression");
102 th.message = rs.getString("threshold_message");
107 public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
108 this.jdbcTemplate = jdbcTemplate;