37e362511341a47429e285928485179c2dfeb7e1
[appc.git] / appc-config / appc-encryption-tool / provider / src / main / java / org / openecomp / appc / encryptiontool / wrapper / WrapperEncryptionTool.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.encryptiontool.wrapper;
26
27 import java.util.ArrayList;
28
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.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class WrapperEncryptionTool {
37
38     private static final Logger log = LoggerFactory.getLogger(WrapperEncryptionTool.class);
39
40     public static void main(String[] args) {
41         int rowCount = 0;
42         String vnfType = args[0];
43         String user = args[1];
44         String password = args[2];
45         String action = args[3];
46         String port = args[4];
47         String url = args[5];
48
49         if ("".equals(vnfType)) {
50             log.info("ERROR-VNF_TYPE can not be null");
51             return;
52         }
53         if ("".equals(user)) {
54             log.info("ERROR-USER can not be null");
55             return;
56         }
57         if ("".equals(password)) {
58             log.info("ERROR-PASSWORD can not be null");
59             return;
60         }
61
62         EncryptionTool et = EncryptionTool.getInstance();
63         String enPass = et.encrypt(password);
64
65         if (action != null && !action.isEmpty()) {
66             updateProperties(user, vnfType, enPass, action, port, url);
67             return;
68         }
69
70         ArrayList<String> argList = new ArrayList<>();
71         argList.add(vnfType);
72         argList.add(user);
73         String clause = " vnfType = ? and user_name = ? ";
74         String setClause = " password = ? ";
75         String getselectData = " * ";
76         DBResourceManager dbResourceManager = null;
77         try {
78             dbResourceManager = DbServiceUtil.initDbLibService();
79             CachedRowSet data = DbServiceUtil.getData(Constants.DEVICE_AUTHENTICATION, argList,Constants.SCHEMA_SDNCTL, getselectData, clause);
80
81             while (data.next()) {
82                 rowCount++;
83             }
84             if (rowCount == 0)
85                 log.info("APPC-MESSAGE: ERROR - No record Found for VNF_TYPE: %, User % ", vnfType, user);
86             else {
87                 argList.clear();
88                 argList.add(enPass);
89                 argList.add(vnfType);
90                 argList.add(user);
91                 DbServiceUtil.updateDB(Constants.DEVICE_AUTHENTICATION, argList, Constants.SCHEMA_SDNCTL, clause,
92                         setClause);
93                 log.info("APPC-MESSAGE: Password Updated Successfully");
94             }
95         } catch (Exception e) {
96             log.info("Caught exception", e);
97             log.info("APPC-MESSAGE:" + e.getMessage());
98         } finally {
99             if (dbResourceManager != null) {
100                 dbResourceManager.cleanUp();
101             }
102         }
103     }
104
105     private static void updateProperties(String user, String vnfType, String password, String action, String port,
106             String url) {
107
108         log.info("Received Inputs User:%s vnfType:%s action:%s", user, vnfType, action);
109         String property = vnfType + "." + action + ".";
110
111
112         try {
113             PropertiesConfiguration conf =
114                     new PropertiesConfiguration(Constants.APPC_CONFIG_DIR + "/appc_southbound.properties");
115             conf.setProperty(property + "user", user);
116             if (port != null && !port.isEmpty())
117                 conf.setProperty(property + "port", port);
118             if (password != null && !password.isEmpty())
119                 conf.setProperty(property + "password", password);
120             if (url != null && !url.isEmpty())
121                 conf.setProperty(property + "url", url);
122
123             conf.save();
124
125         } catch (Exception e) {
126             log.info("Caught Exception", e);
127         }
128     }
129 }