Test coverage in encryptiontool package
[appc.git] / appc-config / appc-encryption-tool / provider / src / main / java / org / onap / appc / encryptiontool / wrapper / LoadFromDB.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  * Modifications Copyright (C) 2019 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  *
23  * ============LICENSE_END=========================================================
24  */
25 package org.onap.appc.encryptiontool.wrapper;
26
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.Map;
30 import java.io.File;
31 import javax.sql.rowset.CachedRowSet;
32
33 import org.apache.commons.configuration.PropertiesConfiguration;
34 import org.onap.ccsdk.sli.core.dblib.DBResourceManager;
35 import org.slf4j.*;
36
37 public class LoadFromDB {
38
39     private static final Logger log = LoggerFactory
40             .getLogger(WrapperEncryptionTool.class);
41     public static void main(String[] args) {
42         int rowCount =0;
43         ArrayList argList=null;
44
45         String getselectData = " DA.VNF_TYPE, PR.PROTOCOL, PR.ACTION ,DA.USER_NAME,DA.PASSWORD,DA.PORT_NUMBER ";
46
47         String clause = "  DA.VNF_TYPE=PR.VNF_TYPE group by PR.ACTION ";
48         String tableName ="DEVICE_AUTHENTICATION DA , PROTOCOL_REFERENCE PR";
49         DBResourceManager dbResourceManager = null;
50         try {
51
52             dbResourceManager = DbServiceUtil.initDbLibService();
53             CachedRowSet data = DbServiceUtil.getData(tableName, argList, Constants.SCHEMA_SDNCTL, getselectData, clause );
54
55             Map <String, String> mp = new HashMap<>();
56             while (data.next()) {
57
58               mp.put(data.getString(1) + "." + data.getString(2) + "."+data.getString(3) + "."+"user", data.getString(4));
59               mp.put(data.getString(1) + "." + data.getString(2) + "."+data.getString(3) + "."+"password", data.getString(5));
60               mp.put(data.getString(1) + "." + data.getString(2) + "."+data.getString(3) + "."+"port", data.getString(6));
61               mp.put(data.getString(1) + "." + data.getString(2) + "."+data.getString(3) + "."+"url", "");
62               rowCount++;
63             }
64
65
66             log.info("Size of Map data:"+mp.size());
67                 File file  = new File(Constants.APPC_CONFIG_DIR );
68                 file.mkdir();
69                 file  = new File(Constants.APPC_CONFIG_DIR + "/appc_southbound.properties");
70                 if(file.exists())
71                 {
72                      log.info("APPC-MESSAGE:" + " File already Exists");
73                 }
74                 else
75                 {
76                     file.createNewFile();
77                     log.info("APPC-MESSAGE:" + " New  File is created");
78                 }
79             if (rowCount == 0)
80                 log.info("APPC-MESSAGE: ERROR - No record Found ");
81             else {
82
83
84                 log.info("Size of Map file:"+mp.size());
85                 PropertiesConfiguration conf = new PropertiesConfiguration(
86                         Constants.APPC_CONFIG_DIR + "/appc_southbound.properties");
87
88
89                 for (Map.Entry<String, String> key : mp.entrySet()) {
90                            log.debug(key.getKey() + ":" + key.getValue());
91                          if(key.getValue()==null)
92                     {
93                         key.setValue("");
94                     }
95                     conf.setProperty(key.getKey(), key.getValue());
96               }
97
98
99                 conf.save();
100                 log.info("APPC-MESSAGE:" + "properties updated successfully");
101
102             }
103         } catch (Exception e) {
104             log.info("Caught exception", e);
105             log.info("APPC-MESSAGE:" + e.getMessage());
106         } finally {
107             if (dbResourceManager != null) {
108                 dbResourceManager.cleanUp();
109                  System.exit(0);
110             }
111         }
112     }
113
114 }