[CCSDK-6] Populate seed code
[ccsdk/sli/adaptors.git] / resource-assignment / provider / src / main / java / org / openecomp / sdnc / ra / rule / dao / ResourceRuleDaoImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 ONAP 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.openecomp.sdnc.ra.rule.dao;
23
24 import java.sql.ResultSet;
25 import java.sql.SQLException;
26 import java.util.List;
27
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;
35
36 public class ResourceRuleDaoImpl implements ResourceRuleDao {
37
38         @SuppressWarnings("unused")
39         private static final Logger log = LoggerFactory.getLogger(ResourceRuleDaoImpl.class);
40
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 = ?";
46
47         private JdbcTemplate jdbcTemplate;
48         ResourceRuleRowMapper resourceRuleRowMapper = new ResourceRuleRowMapper();
49         ResourceThresholdRowMapper resourceThresholdRowMapper = new ResourceThresholdRowMapper();
50
51         @Override
52         public List<ResourceRule> getResourceRules(
53                 String serviceModel,
54                 String endPointPosition,
55                 EquipmentLevel equipLevel) {
56                 List<ResourceRule> resourceRuleList = jdbcTemplate.query(GET1_SQL,
57                         new Object[] { serviceModel, endPointPosition, equipLevel.toString() }, resourceRuleRowMapper);
58
59                 for (ResourceRule rr : resourceRuleList)
60                         rr.thresholdList = jdbcTemplate.query(THRESHOLD_SQL, new Object[] { rr.id }, resourceThresholdRowMapper);
61
62                 return resourceRuleList;
63         }
64
65         @Override
66         public ResourceRule getResourceRule(
67                 String serviceModel,
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);
74
75                 if (resourceRuleList == null || resourceRuleList.isEmpty())
76                         return null;
77
78                 ResourceRule rr = resourceRuleList.get(0);
79                 rr.thresholdList = jdbcTemplate.query(THRESHOLD_SQL, new Object[] { rr.id }, resourceThresholdRowMapper);
80
81                 return rr;
82         }
83
84         private static class ResourceRuleRowMapper implements RowMapper<ResourceRule> {
85
86                 @Override
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");
99                         return rl;
100                 }
101         }
102
103         private static class ResourceThresholdRowMapper implements RowMapper<ResourceThreshold> {
104
105                 @Override
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");
110                         return th;
111                 }
112         }
113
114         public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
115                 this.jdbcTemplate = jdbcTemplate;
116         }
117 }