Initial OpenECOMP APPC commit
[appc.git] / app-c / appc / appc-adapters / appc-netconf-adapter / appc-netconf-adapter-bundle / src / main / java / org / openecomp / appc / adapter / netconf / internal / NetconfDataAccessServiceImpl.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.netconf.internal;
23
24 import javax.sql.rowset.CachedRowSet;
25
26 import org.openecomp.appc.adapter.netconf.ConnectionDetails;
27 import org.openecomp.appc.adapter.netconf.NetconfConnectionDetails;
28 import org.openecomp.appc.adapter.netconf.NetconfDataAccessService;
29 import org.openecomp.appc.adapter.netconf.exception.DataAccessException;
30 import org.openecomp.appc.adapter.netconf.util.Constants;
31 import org.openecomp.appc.exceptions.APPCException;
32 import com.att.eelf.configuration.EELFLogger;
33 import com.att.eelf.configuration.EELFManager;
34 import org.openecomp.sdnc.sli.resource.dblib.DbLibService;
35
36 import java.sql.SQLException;
37 import java.util.ArrayList;
38
39
40 public class NetconfDataAccessServiceImpl implements NetconfDataAccessService {
41
42     private static EELFLogger logger = EELFManager.getInstance().getLogger(NetconfDataAccessServiceImpl.class);
43
44     public void setSchema(String schema) {
45         this.schema = schema;
46     }
47
48     private String schema;
49
50     public void setDbLibService(DbLibService service) {dbLibService = service;}
51
52     private DbLibService dbLibService;
53
54     @Override
55     public String retrieveConfigFileName(String xmlID) throws DataAccessException {
56         String fileContent = "";
57
58         String queryString = "select " + Constants.FILE_CONTENT_TABLE_FIELD_NAME + " " +
59                 "from " + Constants.CONFIGFILES_TABLE_NAME + " " +
60                 "where " + Constants.FILE_NAME_TABLE_FIELD_NAME + " = ?";
61
62         ArrayList<String> argList = new ArrayList<>();
63         argList.add(xmlID);
64
65         try {
66
67             final CachedRowSet data = dbLibService.getData(queryString, argList, schema);
68             if (data.first()) {
69                 fileContent = data.getString(Constants.FILE_CONTENT_TABLE_FIELD_NAME);
70             }
71
72         } catch (Throwable e) {
73             logger.error("Error Accessing Database " + e);
74             throw new DataAccessException(e);
75         }
76
77         return fileContent;
78     }
79
80     @Override
81     public boolean retrieveConnectionDetails(String vnfType, ConnectionDetails connectionDetails) throws
82                     DataAccessException {
83         boolean recordFound = false;
84
85         String queryString = "select " + Constants.USER_NAME_TABLE_FIELD_NAME + "," + Constants.PASSWORD_TABLE_FIELD_NAME + "," + Constants.PORT_NUMBER_TABLE_FIELD_NAME + " " +
86                 "from " + Constants.DEVICE_AUTHENTICATION_TABLE_NAME + " " +
87                 "where " + Constants.VNF_TYPE_TABLE_FIELD_NAME + " = ?";
88
89         ArrayList<String> argList = new ArrayList<>();
90         argList.add(vnfType);
91
92         try {
93
94             final CachedRowSet data = dbLibService.getData(queryString, argList, schema);
95             if (data.first()) {
96                 connectionDetails.setUsername(data.getString(Constants.USER_NAME_TABLE_FIELD_NAME));
97                 connectionDetails.setPassword(data.getString(Constants.PASSWORD_TABLE_FIELD_NAME));
98                 connectionDetails.setPort(data.getInt(Constants.PORT_NUMBER_TABLE_FIELD_NAME));
99                 recordFound = true;
100             }
101
102         } catch (SQLException e) {
103             logger.error("Error Accessing Database " + e);
104             throw new DataAccessException(e);
105         }
106
107         return recordFound;
108     }
109
110     @Override
111     public boolean retrieveNetconfConnectionDetails(String vnfType, NetconfConnectionDetails connectionDetails) throws
112                     DataAccessException
113     {
114         ConnectionDetails connDetails = new ConnectionDetails();
115         if(this.retrieveConnectionDetails(vnfType, connDetails))
116         {
117             connectionDetails.setHost(connDetails.getHost());
118             connectionDetails.setPort(connDetails.getPort());
119             connectionDetails.setUsername(connDetails.getUsername());
120             connectionDetails.setPassword(connDetails.getPassword());
121         }
122         return true;
123     }
124
125     @Override
126     public boolean logDeviceInteraction(String instanceId, String requestId, String creationDate, String logText) throws
127                     DataAccessException {
128
129         String queryString = "INSERT INTO "+ Constants.DEVICE_INTERFACE_LOG_TABLE_NAME+"("+
130                 Constants.SERVICE_INSTANCE_ID_FIELD_NAME+","+
131                 Constants.REQUEST_ID_FIELD_NAME+","+
132                 Constants.CREATION_DATE_FIELD_NAME+","+
133                 Constants.LOG_FIELD_NAME+") ";
134         queryString += "values(?,?,?,?)";
135
136         ArrayList<String> argList = new ArrayList<>();
137         argList.add(instanceId);
138         argList.add(requestId);
139         argList.add(creationDate);
140         argList.add(logText);
141
142         try {
143
144             dbLibService.writeData(queryString, argList, schema);
145
146         } catch (SQLException e) {
147             logger.error("Logging Device interaction failed - "+ queryString);
148             throw new DataAccessException(e);
149         }
150
151         return true;
152     }
153
154 }