b0cc7d2cc8be84e4ac2694018ff6ee1a3863d576
[ccsdk/sli/adaptors.git] / resource-assignment / provider / src / main / java / org / openecomp / sdnc / ra / rule / dao / MaxPortSpeedDaoImpl.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.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 MaxPortSpeedDaoImpl implements MaxPortSpeedDao {
35
36     @SuppressWarnings("unused")
37     private static final Logger log = LoggerFactory.getLogger(MaxPortSpeedDaoImpl.class);
38
39     private final static String GET_SQL =
40             "SELECT * FROM MAX_PORT_SPEED WHERE image_file_name = ? AND end_point_position = ? AND interface_name = ?";
41
42     private JdbcTemplate jdbcTemplate;
43     private long defaultMaxPortSpeed = 5000000;
44     private SpeedUtil speedUtil;
45
46     @Override
47     public long getMaxPortSpeed(String imageFile, String endPointPosition, String interfaceName) {
48         List<MaxPortSpeed> maxPortSpeedList =
49                 jdbcTemplate.query(GET_SQL, new Object[] { imageFile, endPointPosition, interfaceName },
50                         new RowMapper<MaxPortSpeed>() {
51
52                             @Override
53                             public MaxPortSpeed mapRow(ResultSet rs, int rowNum) throws SQLException {
54                                 MaxPortSpeed mps = new MaxPortSpeed();
55                                 mps.maxSpeed = rs.getLong("max_speed");
56                                 mps.unit = rs.getString("unit");
57                                 return mps;
58                             }
59                         });
60
61         if (maxPortSpeedList.isEmpty())
62             return defaultMaxPortSpeed;
63
64         MaxPortSpeed mps = maxPortSpeedList.get(0);
65         return speedUtil.convertToKbps(mps.maxSpeed, mps.unit);
66     }
67
68     private static class MaxPortSpeed {
69
70         public long maxSpeed;
71         public String unit;
72     }
73
74     public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
75         this.jdbcTemplate = jdbcTemplate;
76     }
77
78     public void setDefaultMaxPortSpeed(long defaultMaxPortSpeed) {
79         this.defaultMaxPortSpeed = defaultMaxPortSpeed;
80     }
81
82     public void setSpeedUtil(SpeedUtil speedUtil) {
83         this.speedUtil = speedUtil;
84     }
85 }