Updating licenses in all files
[appc.git] / 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  * APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Amdocs
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.openecomp.appc.adapter.netconf.internal;
24
25 import javax.sql.rowset.CachedRowSet;
26
27 import org.openecomp.appc.adapter.netconf.ConnectionDetails;
28 import org.openecomp.appc.adapter.netconf.NetconfConnectionDetails;
29 import org.openecomp.appc.adapter.netconf.NetconfDataAccessService;
30 import org.openecomp.appc.adapter.netconf.exception.DataAccessException;
31 import org.openecomp.appc.adapter.netconf.util.Constants;
32 import org.openecomp.appc.exceptions.APPCException;
33 import com.att.eelf.configuration.EELFLogger;
34 import com.att.eelf.configuration.EELFManager;
35 import org.openecomp.sdnc.sli.resource.dblib.DbLibService;
36
37 import java.sql.SQLException;
38 import java.util.ArrayList;
39
40
41 public class NetconfDataAccessServiceImpl implements NetconfDataAccessService {
42
43     private static EELFLogger logger = EELFManager.getInstance().getLogger(NetconfDataAccessServiceImpl.class);
44
45     public void setSchema(String schema) {
46         this.schema = schema;
47     }
48
49     private String schema;
50
51     public void setDbLibService(DbLibService service) {dbLibService = service;}
52
53     private DbLibService dbLibService;
54
55     @Override
56     public String retrieveConfigFileName(String xmlID) throws DataAccessException {
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
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
73         } catch (Throwable 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) throws
83                     DataAccessException {
84         boolean recordFound = false;
85
86         String queryString = "select " + Constants.USER_NAME_TABLE_FIELD_NAME + "," + 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
95             final CachedRowSet data = dbLibService.getData(queryString, argList, schema);
96             if (data.first()) {
97                 connectionDetails.setUsername(data.getString(Constants.USER_NAME_TABLE_FIELD_NAME));
98                 connectionDetails.setPassword(data.getString(Constants.PASSWORD_TABLE_FIELD_NAME));
99                 connectionDetails.setPort(data.getInt(Constants.PORT_NUMBER_TABLE_FIELD_NAME));
100                 recordFound = true;
101             }
102
103         } catch (SQLException e) {
104             logger.error("Error Accessing Database " + e);
105             throw new DataAccessException(e);
106         }
107
108         return recordFound;
109     }
110
111     @Override
112     public boolean retrieveNetconfConnectionDetails(String vnfType, NetconfConnectionDetails connectionDetails) throws
113                     DataAccessException
114     {
115         ConnectionDetails connDetails = new ConnectionDetails();
116         if(this.retrieveConnectionDetails(vnfType, connDetails))
117         {
118             connectionDetails.setHost(connDetails.getHost());
119             connectionDetails.setPort(connDetails.getPort());
120             connectionDetails.setUsername(connDetails.getUsername());
121             connectionDetails.setPassword(connDetails.getPassword());
122         }
123         return true;
124     }
125
126     @Override
127     public boolean logDeviceInteraction(String instanceId, String requestId, String creationDate, String logText) throws
128                     DataAccessException {
129
130         String queryString = "INSERT INTO "+ Constants.DEVICE_INTERFACE_LOG_TABLE_NAME+"("+
131                 Constants.SERVICE_INSTANCE_ID_FIELD_NAME+","+
132                 Constants.REQUEST_ID_FIELD_NAME+","+
133                 Constants.CREATION_DATE_FIELD_NAME+","+
134                 Constants.LOG_FIELD_NAME+") ";
135         queryString += "values(?,?,?,?)";
136
137         ArrayList<String> argList = new ArrayList<>();
138         argList.add(instanceId);
139         argList.add(requestId);
140         argList.add(creationDate);
141         argList.add(logText);
142
143         try {
144
145             dbLibService.writeData(queryString, argList, schema);
146
147         } catch (SQLException e) {
148             logger.error("Logging Device interaction failed - "+ queryString);
149             throw new DataAccessException(e);
150         }
151
152         return true;
153     }
154
155 }