65cdbf26d9a7196b5ef90532ef7f24c777073555
[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  * APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Amdocs
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.openecomp.appc.adapter.ssh.sshd;
24
25 import javax.sql.rowset.CachedRowSet;
26
27 import org.openecomp.appc.adapter.ssh.Constants;
28 import org.openecomp.appc.adapter.ssh.SshConnectionDetails;
29 import org.openecomp.appc.adapter.ssh.SshDataAccessException;
30 import org.openecomp.appc.adapter.ssh.SshDataAccessService;
31 import org.openecomp.sdnc.sli.resource.dblib.DbLibService;
32
33 import java.sql.SQLException;
34 import java.util.ArrayList;
35
36 public class SshdDataAccessService implements SshDataAccessService {
37
38     private String schema = Constants.NETCONF_SCHEMA;
39     private DbLibService dbLibService;
40
41     @Override
42     public void setSchema(String schema) {
43         this.schema = schema;
44     }
45
46     @Override
47     public void setDbLibService(DbLibService dbLibService) {
48         this.dbLibService = dbLibService;
49     }
50
51     @Override
52     public boolean retrieveConnectionDetails(String vnfType, SshConnectionDetails connectionDetails) throws SshDataAccessException {
53
54         boolean recordFound = false;
55
56         String queryString = "select " + Constants.USER_NAME_TABLE_FIELD_NAME + "," + Constants.PASSWORD_TABLE_FIELD_NAME + "," + Constants.PORT_NUMBER_TABLE_FIELD_NAME + " " +
57                 "from " + Constants.DEVICE_AUTHENTICATION_TABLE_NAME + " " +
58                 "where " + Constants.VNF_TYPE_TABLE_FIELD_NAME + " = ?";
59
60         ArrayList<String> argList = new ArrayList<>();
61         argList.add(vnfType);
62
63         try {
64
65             final CachedRowSet data = dbLibService.getData(queryString, argList, schema);
66             if (data.first()) {
67                 recordFound = true;
68                 connectionDetails.setUsername(data.getString(Constants.USER_NAME_TABLE_FIELD_NAME));
69                 connectionDetails.setPassword(data.getString(Constants.PASSWORD_TABLE_FIELD_NAME));
70                 connectionDetails.setPort(data.getInt(Constants.PORT_NUMBER_TABLE_FIELD_NAME));
71             }
72
73         } catch (SQLException e) {
74             throw new SshDataAccessException(e);
75         }
76
77         return recordFound;
78     }
79
80     @Override
81     public String retrieveConfigFileName(String xmlID) throws SshDataAccessException {
82         String fileContent;
83
84         String queryString = "select " + Constants.FILE_CONTENT_TABLE_FIELD_NAME + " " +
85                 "from " + Constants.CONFIGFILES_TABLE_NAME + " " +
86                 "where " + Constants.FILE_NAME_TABLE_FIELD_NAME + " = ?";
87
88         ArrayList<String> argList = new ArrayList<>();
89         argList.add(xmlID);
90
91         try {
92
93             final CachedRowSet data = dbLibService.getData(queryString, argList, schema);
94             fileContent = data.getString(Constants.FILE_CONTENT_TABLE_FIELD_NAME);
95
96         } catch (SQLException e) {
97             throw new SshDataAccessException(e);
98         }
99
100         return fileContent;
101     }
102
103
104 }