[CCSDK-245] RA: Refactor RA to make it generic
[ccsdk/sli/adaptors.git] / resource-assignment / provider / src / main / java / org / onap / ccsdk / sli / adaptors / ra / rule / dao / ResourceRuleDaoImpl.java
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.ra.rule.dao;
23
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;
33
34 public class ResourceRuleDaoImpl implements ResourceRuleDao {
35
36     @SuppressWarnings("unused")
37     private static final Logger log = LoggerFactory.getLogger(ResourceRuleDaoImpl.class);
38
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 = ?";
44
45     private JdbcTemplate jdbcTemplate;
46     ResourceRuleRowMapper resourceRuleRowMapper = new ResourceRuleRowMapper();
47     ResourceThresholdRowMapper resourceThresholdRowMapper = new ResourceThresholdRowMapper();
48
49     @Override
50     public List<ResourceRule> getResourceRules(String serviceModel, String equipLevel) {
51         List<ResourceRule> resourceRuleList =
52                 jdbcTemplate.query(GET1_SQL, new Object[] {serviceModel, equipLevel}, resourceRuleRowMapper);
53
54         for (ResourceRule rr : resourceRuleList) {
55             rr.thresholdList = jdbcTemplate.query(THRESHOLD_SQL, new Object[] {rr.id}, resourceThresholdRowMapper);
56         }
57
58         return resourceRuleList;
59     }
60
61     @Override
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);
66
67         if (resourceRuleList == null || resourceRuleList.isEmpty()) {
68             return null;
69         }
70
71         ResourceRule rr = resourceRuleList.get(0);
72         rr.thresholdList = jdbcTemplate.query(THRESHOLD_SQL, new Object[] {rr.id}, resourceThresholdRowMapper);
73
74         return rr;
75     }
76
77     private static class ResourceRuleRowMapper implements RowMapper<ResourceRule> {
78
79         @Override
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");
92             return rl;
93         }
94     }
95
96     private static class ResourceThresholdRowMapper implements RowMapper<ResourceThreshold> {
97
98         @Override
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");
103             return th;
104         }
105     }
106
107     public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
108         this.jdbcTemplate = jdbcTemplate;
109     }
110 }