Moving all files to root directory
[appc.git] / appc-adapters / appc-ssh-adapter / appc-ssh-adapter-sshd / src / main / java / org / openecomp / appc / adapter / ssh / sshd / SshdDataAccessService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-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.appc.adapter.ssh.sshd;
23
24 import javax.sql.rowset.CachedRowSet;
25
26 import org.openecomp.appc.adapter.ssh.Constants;
27 import org.openecomp.appc.adapter.ssh.SshConnectionDetails;
28 import org.openecomp.appc.adapter.ssh.SshDataAccessException;
29 import org.openecomp.appc.adapter.ssh.SshDataAccessService;
30 import org.openecomp.appc.exceptions.APPCException;
31 import org.openecomp.sdnc.sli.resource.dblib.DbLibService;
32
33 import java.sql.SQLException;
34 import java.util.ArrayList;
35
36
37
38 public class SshdDataAccessService implements SshDataAccessService {
39
40     private String schema = Constants.NETCONF_SCHEMA;
41     private DbLibService dbLibService;
42
43     @Override
44     public void setSchema(String schema) {
45         this.schema = schema;
46     }
47
48     @Override
49     public void setDbLibService(DbLibService dbLibService) {
50         this.dbLibService = dbLibService;
51     }
52
53     @Override
54     public boolean retrieveConnectionDetails(String vnfType, SshConnectionDetails connectionDetails) throws SshDataAccessException {
55
56         boolean recordFound = false;
57
58         String queryString = "select " + Constants.USER_NAME_TABLE_FIELD_NAME + "," + Constants.PASSWORD_TABLE_FIELD_NAME + "," + Constants.PORT_NUMBER_TABLE_FIELD_NAME + " " +
59                 "from " + Constants.DEVICE_AUTHENTICATION_TABLE_NAME + " " +
60                 "where " + Constants.VNF_TYPE_TABLE_FIELD_NAME + " = ?";
61
62         ArrayList<String> argList = new ArrayList<>();
63         argList.add(vnfType);
64
65         try {
66
67             final CachedRowSet data = dbLibService.getData(queryString, argList, schema);
68             if (data.first()) {
69                 recordFound = true;
70                 connectionDetails.setUsername(data.getString(Constants.USER_NAME_TABLE_FIELD_NAME));
71                 connectionDetails.setPassword(data.getString(Constants.PASSWORD_TABLE_FIELD_NAME));
72                 connectionDetails.setPort(data.getInt(Constants.PORT_NUMBER_TABLE_FIELD_NAME));
73             }
74
75         } catch (SQLException e) {
76             throw new SshDataAccessException(e);
77         }
78
79         return recordFound;
80     }
81
82     @Override
83     public String retrieveConfigFileName(String xmlID) throws SshDataAccessException {
84         String fileContent;
85
86         String queryString = "select " + Constants.FILE_CONTENT_TABLE_FIELD_NAME + " " +
87                 "from " + Constants.CONFIGFILES_TABLE_NAME + " " +
88                 "where " + Constants.FILE_NAME_TABLE_FIELD_NAME + " = ?";
89
90         ArrayList<String> argList = new ArrayList<>();
91         argList.add(xmlID);
92
93         try {
94
95             final CachedRowSet data = dbLibService.getData(queryString, argList, schema);
96             fileContent = data.getString(Constants.FILE_CONTENT_TABLE_FIELD_NAME);
97
98         } catch (SQLException e) {
99             throw new SshDataAccessException(e);
100         }
101
102         return fileContent;
103     }
104
105
106 }