Applying license changes to 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  * 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.openecomp.appc.adapter.netconf.internal;
26
27 import javax.sql.rowset.CachedRowSet;
28
29 import org.openecomp.appc.adapter.netconf.ConnectionDetails;
30 import org.openecomp.appc.adapter.netconf.NetconfConnectionDetails;
31 import org.openecomp.appc.adapter.netconf.NetconfDataAccessService;
32 import org.openecomp.appc.adapter.netconf.exception.DataAccessException;
33 import org.openecomp.appc.adapter.netconf.util.Constants;
34 import org.openecomp.appc.exceptions.APPCException;
35 import com.att.eelf.configuration.EELFLogger;
36 import com.att.eelf.configuration.EELFManager;
37 import org.openecomp.sdnc.sli.resource.dblib.DbLibService;
38
39 import java.sql.SQLException;
40 import java.util.ArrayList;
41
42
43 public class NetconfDataAccessServiceImpl implements NetconfDataAccessService {
44
45     private static EELFLogger logger = EELFManager.getInstance().getLogger(NetconfDataAccessServiceImpl.class);
46
47     public void setSchema(String schema) {
48         this.schema = schema;
49     }
50
51     private String schema;
52
53     public void setDbLibService(DbLibService service) {dbLibService = service;}
54
55     private DbLibService dbLibService;
56
57     @Override
58     public String retrieveConfigFileName(String xmlID) throws DataAccessException {
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
70             final CachedRowSet data = dbLibService.getData(queryString, argList, schema);
71             if (data.first()) {
72                 fileContent = data.getString(Constants.FILE_CONTENT_TABLE_FIELD_NAME);
73             }
74
75         } catch (Throwable e) {
76             logger.error("Error Accessing Database " + e);
77             throw new DataAccessException(e);
78         }
79
80         return fileContent;
81     }
82
83     @Override
84     public boolean retrieveConnectionDetails(String vnfType, ConnectionDetails connectionDetails) throws
85                     DataAccessException {
86         boolean recordFound = false;
87
88         String queryString = "select " + Constants.USER_NAME_TABLE_FIELD_NAME + "," + Constants.PASSWORD_TABLE_FIELD_NAME + "," + Constants.PORT_NUMBER_TABLE_FIELD_NAME + " " +
89                 "from " + Constants.DEVICE_AUTHENTICATION_TABLE_NAME + " " +
90                 "where " + Constants.VNF_TYPE_TABLE_FIELD_NAME + " = ?";
91
92         ArrayList<String> argList = new ArrayList<>();
93         argList.add(vnfType);
94
95         try {
96
97             final CachedRowSet data = dbLibService.getData(queryString, argList, schema);
98             if (data.first()) {
99                 connectionDetails.setUsername(data.getString(Constants.USER_NAME_TABLE_FIELD_NAME));
100                 connectionDetails.setPassword(data.getString(Constants.PASSWORD_TABLE_FIELD_NAME));
101                 connectionDetails.setPort(data.getInt(Constants.PORT_NUMBER_TABLE_FIELD_NAME));
102                 recordFound = true;
103             }
104
105         } catch (SQLException e) {
106             logger.error("Error Accessing Database " + e);
107             throw new DataAccessException(e);
108         }
109
110         return recordFound;
111     }
112
113     @Override
114     public boolean retrieveNetconfConnectionDetails(String vnfType, NetconfConnectionDetails connectionDetails) throws
115                     DataAccessException
116     {
117         ConnectionDetails connDetails = new ConnectionDetails();
118         if(this.retrieveConnectionDetails(vnfType, connDetails))
119         {
120             connectionDetails.setHost(connDetails.getHost());
121             connectionDetails.setPort(connDetails.getPort());
122             connectionDetails.setUsername(connDetails.getUsername());
123             connectionDetails.setPassword(connDetails.getPassword());
124         }
125         return true;
126     }
127
128     @Override
129     public boolean logDeviceInteraction(String instanceId, String requestId, String creationDate, String logText) throws
130                     DataAccessException {
131
132         String queryString = "INSERT INTO "+ Constants.DEVICE_INTERFACE_LOG_TABLE_NAME+"("+
133                 Constants.SERVICE_INSTANCE_ID_FIELD_NAME+","+
134                 Constants.REQUEST_ID_FIELD_NAME+","+
135                 Constants.CREATION_DATE_FIELD_NAME+","+
136                 Constants.LOG_FIELD_NAME+") ";
137         queryString += "values(?,?,?,?)";
138
139         ArrayList<String> argList = new ArrayList<>();
140         argList.add(instanceId);
141         argList.add(requestId);
142         argList.add(creationDate);
143         argList.add(logText);
144
145         try {
146
147             dbLibService.writeData(queryString, argList, schema);
148
149         } catch (SQLException e) {
150             logger.error("Logging Device interaction failed - "+ queryString);
151             throw new DataAccessException(e);
152         }
153
154         return true;
155     }
156
157 }