Second part of onap rename
[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 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             final CachedRowSet data = dbLibService.getData(queryString, argList, schema);
67             if (data.first()) {
68                 fileContent = data.getString(Constants.FILE_CONTENT_TABLE_FIELD_NAME);
69             }
70         } catch (Exception e) {
71             logger.error("Error Accessing Database " + e);
72             throw new DataAccessException(e);
73         }
74
75         return fileContent;
76     }
77
78     @Override
79     public boolean retrieveConnectionDetails(String vnfType, ConnectionDetails connectionDetails) throws
80                     DataAccessException {
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             throws DataAccessException {
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             throws DataAccessException {
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 }