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