2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 ONAP 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.openecomp.sdnc.ra.rule.dao;
24 import java.sql.ResultSet;
25 import java.sql.SQLException;
26 import java.util.List;
28 import org.openecomp.sdnc.ra.equip.data.EquipmentLevel;
29 import org.openecomp.sdnc.ra.rule.data.ResourceRule;
30 import org.openecomp.sdnc.ra.rule.data.ResourceThreshold;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.jdbc.core.JdbcTemplate;
34 import org.springframework.jdbc.core.RowMapper;
36 public class ResourceRuleDaoImpl implements ResourceRuleDao {
38 @SuppressWarnings("unused")
39 private static final Logger log = LoggerFactory.getLogger(ResourceRuleDaoImpl.class);
41 private static final String GET1_SQL =
42 "SELECT * FROM RESOURCE_RULE WHERE service_model = ? AND end_point_position = ? AND equipment_level = ?";
43 private static final String GET2_SQL =
44 "SELECT * FROM RESOURCE_RULE WHERE service_model = ? AND end_point_position = ? AND equipment_level = ? AND resource_name = ?";
45 private static final String THRESHOLD_SQL = "SELECT * FROM RESOURCE_THRESHOLD WHERE resource_rule_id = ?";
47 private JdbcTemplate jdbcTemplate;
48 ResourceRuleRowMapper resourceRuleRowMapper = new ResourceRuleRowMapper();
49 ResourceThresholdRowMapper resourceThresholdRowMapper = new ResourceThresholdRowMapper();
52 public List<ResourceRule> getResourceRules(
54 String endPointPosition,
55 EquipmentLevel equipLevel) {
56 List<ResourceRule> resourceRuleList = jdbcTemplate.query(GET1_SQL,
57 new Object[] { serviceModel, endPointPosition, equipLevel.toString() }, resourceRuleRowMapper);
59 for (ResourceRule rr : resourceRuleList)
60 rr.thresholdList = jdbcTemplate.query(THRESHOLD_SQL, new Object[] { rr.id }, resourceThresholdRowMapper);
62 return resourceRuleList;
66 public ResourceRule getResourceRule(
68 String endPointPosition,
69 EquipmentLevel equipLevel,
70 String resourceName) {
71 List<ResourceRule> resourceRuleList = jdbcTemplate.query(GET2_SQL,
72 new Object[] { serviceModel, endPointPosition, equipLevel.toString(), resourceName },
73 resourceRuleRowMapper);
75 if (resourceRuleList == null || resourceRuleList.isEmpty())
78 ResourceRule rr = resourceRuleList.get(0);
79 rr.thresholdList = jdbcTemplate.query(THRESHOLD_SQL, new Object[] { rr.id }, resourceThresholdRowMapper);
84 private static class ResourceRuleRowMapper implements RowMapper<ResourceRule> {
87 public ResourceRule mapRow(ResultSet rs, int rowNum) throws SQLException {
88 ResourceRule rl = new ResourceRule();
89 rl.id = rs.getLong("resource_rule_id");
90 rl.resourceName = rs.getString("resource_name");
91 rl.serviceModel = rs.getString("service_model");
92 rl.endPointPosition = rs.getString("end_point_position");
93 rl.serviceExpression = rs.getString("service_expression");
94 rl.equipmentLevel = rs.getString("equipment_level");
95 rl.equipmentExpression = rs.getString("equipment_expression");
96 rl.allocationExpression = rs.getString("allocation_expression");
97 rl.softLimitExpression = rs.getString("soft_limit_expression");
98 rl.hardLimitExpression = rs.getString("hard_limit_expression");
103 private static class ResourceThresholdRowMapper implements RowMapper<ResourceThreshold> {
106 public ResourceThreshold mapRow(ResultSet rs, int rowNum) throws SQLException {
107 ResourceThreshold th = new ResourceThreshold();
108 th.expression = rs.getString("threshold_expression");
109 th.message = rs.getString("threshold_message");
114 public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
115 this.jdbcTemplate = jdbcTemplate;