Fix license headers
[ccsdk/sli/adaptors.git] / resource-assignment / provider / src / main / java / org / openecomp / sdnc / ra / rule / dao / MaxServerSpeedDaoImpl.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.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.util.speed.SpeedUtil;
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 MaxServerSpeedDaoImpl implements MaxServerSpeedDao {
35
36     @SuppressWarnings("unused")
37     private static final Logger log = LoggerFactory.getLogger(MaxServerSpeedDaoImpl.class);
38
39     private final static String GET_SQL =
40             "SELECT * FROM MAX_SERVER_SPEED\n" +
41             "WHERE (server_model = ? OR server_model = 'ALL') AND evc_count >= ?\n" +
42             "ORDER BY evc_count";
43
44     private JdbcTemplate jdbcTemplate;
45     private long defaultMaxServerSpeed = 1600000;
46     private SpeedUtil speedUtil;
47
48     @Override
49     public long getMaxServerSpeed(String serverModel, int evcCount) {
50         List<MaxServerSpeed> maxServerSpeedList =
51                 jdbcTemplate.query(GET_SQL, new Object[] { serverModel, evcCount }, new RowMapper<MaxServerSpeed>() {
52
53                     @Override
54                     public MaxServerSpeed mapRow(ResultSet rs, int rowNum) throws SQLException {
55                         MaxServerSpeed mps = new MaxServerSpeed();
56                         mps.maxSpeed = rs.getLong("max_speed");
57                         mps.unit = rs.getString("unit");
58                         return mps;
59                     }
60                 });
61
62         if (maxServerSpeedList.isEmpty())
63             return defaultMaxServerSpeed;
64
65         MaxServerSpeed mps = maxServerSpeedList.get(0);
66         return speedUtil.convertToKbps(mps.maxSpeed, mps.unit);
67     }
68
69     private static class MaxServerSpeed {
70
71         public long maxSpeed;
72         public String unit;
73     }
74
75     public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
76         this.jdbcTemplate = jdbcTemplate;
77     }
78
79     public void setDefaultMaxServerSpeed(long defaultMaxServerSpeed) {
80         this.defaultMaxServerSpeed = defaultMaxServerSpeed;
81     }
82
83     public void setSpeedUtil(SpeedUtil speedUtil) {
84         this.speedUtil = speedUtil;
85     }
86 }