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