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