Mocked test cases for NetconfDataAccessServiceImpl
[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     @Override
56     public String retrieveConfigFileName(String xmlID) {
57         String fileContent = "";
58
59         String queryString = "select " + Constants.FILE_CONTENT_TABLE_FIELD_NAME + " " +
60                 "from " + Constants.CONFIGFILES_TABLE_NAME + " " +
61                 "where " + Constants.FILE_NAME_TABLE_FIELD_NAME + " = ?";
62
63         ArrayList<String> argList = new ArrayList<>();
64         argList.add(xmlID);
65
66         try {
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         } catch (Exception e) {
72             logger.error("Error Accessing Database " + e);
73             throw new DataAccessException(e);
74         }
75
76         return fileContent;
77     }
78
79     @Override
80     public boolean retrieveConnectionDetails(String vnfType, ConnectionDetails connectionDetails) {
81         boolean recordFound = false;
82
83         String queryString = "select " + Constants.USER_NAME_TABLE_FIELD_NAME + "," +
84                 Constants.PASSWORD_TABLE_FIELD_NAME + "," + Constants.PORT_NUMBER_TABLE_FIELD_NAME + " " +
85                 "from " + Constants.DEVICE_AUTHENTICATION_TABLE_NAME + " " +
86                 "where " + Constants.VNF_TYPE_TABLE_FIELD_NAME + " = ?";
87
88         ArrayList<String> argList = new ArrayList<>();
89         argList.add(vnfType);
90
91         try {
92             final CachedRowSet data = dbLibService.getData(queryString, argList, schema);
93             if (data.first()) {
94                 connectionDetails.setUsername(data.getString(Constants.USER_NAME_TABLE_FIELD_NAME));
95                 connectionDetails.setPassword(data.getString(Constants.PASSWORD_TABLE_FIELD_NAME));
96                 connectionDetails.setPort(data.getInt(Constants.PORT_NUMBER_TABLE_FIELD_NAME));
97                 recordFound = true;
98             }
99         } catch (SQLException e) {
100             logger.error("Error Accessing Database " + e);
101             throw new DataAccessException(e);
102         }
103
104         return recordFound;
105     }
106
107     @Override
108     public boolean retrieveNetconfConnectionDetails(String vnfType, NetconfConnectionDetails connectionDetails) {
109         ConnectionDetails connDetails = new ConnectionDetails();
110         if(this.retrieveConnectionDetails(vnfType, connDetails))
111         {
112             connectionDetails.setHost(connDetails.getHost());
113             connectionDetails.setPort(connDetails.getPort());
114             connectionDetails.setUsername(connDetails.getUsername());
115             connectionDetails.setPassword(connDetails.getPassword());
116         }
117         return true;
118     }
119
120     @Override
121     public boolean logDeviceInteraction(String instanceId, String requestId, String creationDate, String logText) {
122         String queryString = "INSERT INTO "+ Constants.DEVICE_INTERFACE_LOG_TABLE_NAME+"("+
123                 Constants.SERVICE_INSTANCE_ID_FIELD_NAME+","+
124                 Constants.REQUEST_ID_FIELD_NAME+","+
125                 Constants.CREATION_DATE_FIELD_NAME+","+
126                 Constants.LOG_FIELD_NAME+") ";
127         queryString += "values(?,?,?,?)";
128
129         ArrayList<String> argList = new ArrayList<>();
130         argList.add(instanceId);
131         argList.add(requestId);
132         argList.add(creationDate);
133         argList.add(logText);
134
135         try {
136             dbLibService.writeData(queryString, argList, schema);
137         } catch (SQLException e) {
138             logger.error("Logging Device interaction failed - "+ queryString);
139             throw new DataAccessException(e);
140         }
141
142         return true;
143     }
144
145 }