Merge of new rebased code
[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.sdnc.sli.resource.dblib.DbLibService;
31
32 import java.sql.SQLException;
33 import java.util.ArrayList;
34
35 public class SshdDataAccessService implements SshDataAccessService {
36
37     private String schema = Constants.NETCONF_SCHEMA;
38     private DbLibService dbLibService;
39
40     @Override
41     public void setSchema(String schema) {
42         this.schema = schema;
43     }
44
45     @Override
46     public void setDbLibService(DbLibService dbLibService) {
47         this.dbLibService = dbLibService;
48     }
49
50     @Override
51     public boolean retrieveConnectionDetails(String vnfType, SshConnectionDetails connectionDetails) throws SshDataAccessException {
52
53         boolean recordFound = false;
54
55         String queryString = "select " + Constants.USER_NAME_TABLE_FIELD_NAME + "," + Constants.PASSWORD_TABLE_FIELD_NAME + "," + Constants.PORT_NUMBER_TABLE_FIELD_NAME + " " +
56                 "from " + Constants.DEVICE_AUTHENTICATION_TABLE_NAME + " " +
57                 "where " + Constants.VNF_TYPE_TABLE_FIELD_NAME + " = ?";
58
59         ArrayList<String> argList = new ArrayList<>();
60         argList.add(vnfType);
61
62         try {
63
64             final CachedRowSet data = dbLibService.getData(queryString, argList, schema);
65             if (data.first()) {
66                 recordFound = true;
67                 connectionDetails.setUsername(data.getString(Constants.USER_NAME_TABLE_FIELD_NAME));
68                 connectionDetails.setPassword(data.getString(Constants.PASSWORD_TABLE_FIELD_NAME));
69                 connectionDetails.setPort(data.getInt(Constants.PORT_NUMBER_TABLE_FIELD_NAME));
70             }
71
72         } catch (SQLException e) {
73             throw new SshDataAccessException(e);
74         }
75
76         return recordFound;
77     }
78
79     @Override
80     public String retrieveConfigFileName(String xmlID) throws SshDataAccessException {
81         String fileContent;
82
83         String queryString = "select " + Constants.FILE_CONTENT_TABLE_FIELD_NAME + " " +
84                 "from " + Constants.CONFIGFILES_TABLE_NAME + " " +
85                 "where " + Constants.FILE_NAME_TABLE_FIELD_NAME + " = ?";
86
87         ArrayList<String> argList = new ArrayList<>();
88         argList.add(xmlID);
89
90         try {
91
92             final CachedRowSet data = dbLibService.getData(queryString, argList, schema);
93             fileContent = data.getString(Constants.FILE_CONTENT_TABLE_FIELD_NAME);
94
95         } catch (SQLException e) {
96             throw new SshDataAccessException(e);
97         }
98
99         return fileContent;
100     }
101
102
103 }