78c4578f1d7e5948fefd9630f15eec5bb9734b81
[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 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 package org.onap.appc.encryptiontool.wrapper;
25
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.Map;
29 import java.io.File;
30 import javax.sql.rowset.CachedRowSet;
31
32 import org.apache.commons.configuration.PropertiesConfiguration;
33 import org.onap.ccsdk.sli.core.dblib.DBResourceManager;
34 import org.slf4j.*;
35
36 public class LoadFromDB {
37
38     private static final Logger log = LoggerFactory
39             .getLogger(WrapperEncryptionTool.class);
40     public static void main(String[] args) {
41         int rowCount =0;
42         ArrayList argList=null;
43
44         String getselectData = " DA.VNF_TYPE, PR.PROTOCOL, PR.ACTION ,DA.USER_NAME,DA.PASSWORD,DA.PORT_NUMBER ";
45
46         String clause = "  DA.VNF_TYPE=PR.VNF_TYPE group by PR.ACTION ";
47         String tableName ="DEVICE_AUTHENTICATION DA , PROTOCOL_REFERENCE PR";
48         DBResourceManager dbResourceManager = null;
49         try {
50
51             dbResourceManager = DbServiceUtil.initDbLibService();
52             CachedRowSet data = DbServiceUtil.getData(tableName, argList, Constants.SCHEMA_SDNCTL, getselectData,clause );
53
54             Map <String,String> mp = new HashMap<String,String>();
55             while (data.next()) {
56
57               mp.put(data.getString(1)+"."+data.getString(2)+"."+data.getString(3)+"."+"user",data.getString(4));
58               mp.put(data.getString(1)+"."+data.getString(2)+"."+data.getString(3)+"."+"password",data.getString(5));
59               mp.put(data.getString(1)+"."+data.getString(2)+"."+data.getString(3)+"."+"port",data.getString(6));
60               mp.put(data.getString(1)+"."+data.getString(2)+"."+data.getString(3)+"."+"url","");
61               rowCount++;
62             }
63
64
65             log.info("Size of Map data:"+mp.size());
66                 File file  = new File(System.getenv("APPC_CONFIG_DIR"));
67                 file.mkdir();
68                 file  = new File(System.getenv("APPC_CONFIG_DIR")+"/appc_southbound.properties");
69                 if(file.exists())
70                 {
71                      log.info("APPC-MESSAGE:" + " File already Exists");
72                 }
73                 else
74                 {
75                     file.createNewFile();
76                     log.info("APPC-MESSAGE:" + " New  File is created");
77                 }
78             if (rowCount == 0)
79                 log.info("APPC-MESSAGE: ERROR - No record Found ");
80             else {
81
82
83                 log.info("Size of Map file:"+mp.size());
84                 PropertiesConfiguration conf = new PropertiesConfiguration(
85                        System.getenv("APPC_CONFIG_DIR")+"/appc_southbound.properties");
86
87
88                 for (Map.Entry<String, String> key : mp.entrySet()) {
89                            log.debug(key.getKey() + ":" + key.getValue());
90                          if(key.getValue()==null)
91                     {
92                         key.setValue("");
93                     }
94                     conf.setProperty(key.getKey(), key.getValue());
95               }
96
97
98                 conf.save();
99                 log.info("APPC-MESSAGE:" + "properties updated successfully");
100
101             }
102         } catch (Exception e) {
103             log.info("Caught exception", e);
104             log.info("APPC-MESSAGE:" + e.getMessage());
105         } finally {
106             if (dbResourceManager != null) {
107                 dbResourceManager.cleanUp();
108                  System.exit(0);
109             }
110         }
111     }
112
113 }