Change to CCSDK and ODL Carbon
[appc.git] / appc-config / appc-encryption-tool / provider / src / main / java / org / openecomp / appc / encryptiontool / wrapper / WrapperEncryptionTool.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property.  All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.appc.encryptiontool.wrapper;
22
23 import java.io.File;
24 import java.io.FileOutputStream;
25 import java.io.OutputStream;
26 import java.util.ArrayList;
27 import java.util.Properties;
28
29 import javax.sql.rowset.CachedRowSet;
30
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.apache.commons.configuration.PropertiesConfiguration;
34 import org.onap.ccsdk.sli.core.dblib.DBResourceManager;
35
36 public class WrapperEncryptionTool {
37
38     private static final Logger log = LoggerFactory
39             .getLogger(WrapperEncryptionTool.class);
40
41     public static void main(String[] args)
42     {
43         int rowCount = 0;
44         String vnf_type=args[0];
45         String user = args[1];
46         String password = args[2];
47         String action = args[3];
48         String port = args[4];
49         String url = args[5];
50
51         if("".equals(vnf_type))
52         {
53             System.out.println("ERROR-VNF_TYPE can not be null");
54             return;
55         }
56         if("".equals(user))
57         {
58             System.out.println("ERROR-USER can not be null");
59             return;
60         }
61         if("".equals(password))
62         {
63             System.out.println("ERROR-PASSWORD can not be null");
64             return;
65         }
66
67         EncryptionTool encryptionTool = EncryptionTool.getInstance();
68         String enPass = encryptionTool.encrypt(password);
69
70         if(action != null && !action.isEmpty()){
71             updateProperties(user,vnf_type , enPass, action, port, url);
72             return ;
73         }
74
75         ArrayList<String> argList = new ArrayList<>();
76         argList.add(vnf_type);
77         argList.add(user);
78         String clause = " vnf_type = ? and user_name = ? ";
79         String setClause = " password = ? ";
80         String getselectData = " * ";
81         DBResourceManager dbResourceManager = null;
82         try
83         {
84             dbResourceManager = DbServiceUtil.initDbLibService();
85             CachedRowSet data = DbServiceUtil.getData(Constants.DEVICE_AUTHENTICATION, 
86                 argList, Constants.SCHEMA_SDNCTL, getselectData,clause );
87             while(data.next())
88             {
89                 rowCount ++;
90             }
91             if(rowCount == 0)
92                 log.info("APPC-MESSAGE: ERROR - No record Found for VNF_TYPE: " + vnf_type + ", User " + user );
93             else
94             {
95                 argList.clear();
96                 argList.add(enPass);
97                 argList.add(vnf_type);
98                 argList.add(user);
99                 DbServiceUtil.updateDB(Constants.DEVICE_AUTHENTICATION, argList, 
100                     Constants.SCHEMA_SDNCTL, clause, setClause);
101                 log.info("APPC-MESSAGE: Password Updated Successfully");
102             }
103         }
104         catch (Exception e)
105         {
106             e.printStackTrace();
107             log.info("APPC-MESSAGE:" + e.getMessage());
108         }
109         finally
110         {
111             dbResourceManager.cleanUp();
112             System.exit(0);
113         }
114     }
115
116     private static void updateProperties(String user, String vnf_type, String password, 
117             String action, String port, String url) {
118
119         log.info("Received Inputs User:" + user + " vnf_type:"  + vnf_type + " action:" + action );
120
121         String property =  vnf_type + "." + action + ".";
122
123         try {
124             PropertiesConfiguration conf = new PropertiesConfiguration(Constants.APPC_CONFIG_DIR  + "/appc_southbound.properties");
125             conf.setProperty(property + "user",  user);
126             if(port != null && !port.isEmpty() )
127                 conf.setProperty(property + "port",  port);
128             if(password != null && !password.isEmpty() )
129                 conf.setProperty(property + "password",  password);
130             if(url != null && !url.isEmpty() )
131             conf.setProperty(property + "url",  url);
132
133             conf.save();
134
135         }
136         catch (Exception e ) {
137             e.printStackTrace();
138             log.info("APPC-MESSAGE:" + e.getMessage());
139         }
140
141     }
142 }