Update license header in REST and SSH adapter file
[appc.git] / appc-adapters / appc-ssh-adapter / appc-ssh-adapter-sshd / src / main / java / org / onap / appc / adapter / ssh / sshd / SshdDataAccessService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.adapter.ssh.sshd;
25
26 import javax.sql.rowset.CachedRowSet;
27
28 import org.onap.appc.adapter.ssh.Constants;
29 import org.onap.appc.adapter.ssh.SshConnectionDetails;
30 import org.onap.appc.adapter.ssh.SshDataAccessException;
31 import org.onap.appc.adapter.ssh.SshDataAccessService;
32 import org.onap.ccsdk.sli.core.dblib.DbLibService;
33
34 import java.sql.SQLException;
35 import java.util.ArrayList;
36
37 public class SshdDataAccessService implements SshDataAccessService {
38
39     private String schema = Constants.NETCONF_SCHEMA;
40     private DbLibService dbLibService;
41
42     @Override
43     public void setSchema(String schema) {
44         this.schema = schema;
45     }
46
47     @Override
48     public void setDbLibService(DbLibService dbLibService) {
49         this.dbLibService = dbLibService;
50     }
51
52     @Override
53     public boolean retrieveConnectionDetails(String vnfType, SshConnectionDetails connectionDetails) throws SshDataAccessException {
54
55         boolean recordFound = false;
56
57         String queryString = "select " + Constants.USER_NAME_TABLE_FIELD_NAME + "," + Constants.PASSWORD_TABLE_FIELD_NAME + "," + Constants.PORT_NUMBER_TABLE_FIELD_NAME + " " +
58                 "from " + Constants.DEVICE_AUTHENTICATION_TABLE_NAME + " " +
59                 "where " + Constants.VNF_TYPE_TABLE_FIELD_NAME + " = ?";
60
61         ArrayList<String> argList = new ArrayList<>();
62         argList.add(vnfType);
63
64         try {
65
66             final CachedRowSet data = dbLibService.getData(queryString, argList, schema);
67             if (data.first()) {
68                 recordFound = true;
69                 connectionDetails.setUsername(data.getString(Constants.USER_NAME_TABLE_FIELD_NAME));
70                 connectionDetails.setPassword(data.getString(Constants.PASSWORD_TABLE_FIELD_NAME));
71                 connectionDetails.setPort(data.getInt(Constants.PORT_NUMBER_TABLE_FIELD_NAME));
72             }
73
74         } catch (SQLException e) {
75             throw new SshDataAccessException(e);
76         }
77
78         return recordFound;
79     }
80
81     @Override
82     public String retrieveConfigFileName(String xmlID) throws SshDataAccessException {
83         String fileContent;
84
85         String queryString = "select " + Constants.FILE_CONTENT_TABLE_FIELD_NAME + " " +
86                 "from " + Constants.CONFIGFILES_TABLE_NAME + " " +
87                 "where " + Constants.FILE_NAME_TABLE_FIELD_NAME + " = ?";
88
89         ArrayList<String> argList = new ArrayList<>();
90         argList.add(xmlID);
91
92         try {
93
94             final CachedRowSet data = dbLibService.getData(queryString, argList, schema);
95             fileContent = data.getString(Constants.FILE_CONTENT_TABLE_FIELD_NAME);
96
97         } catch (SQLException e) {
98             throw new SshDataAccessException(e);
99         }
100
101         return fileContent;
102     }
103
104
105 }