Changes to encryption tool bundle
[appc.git] / appc-config / appc-encryption-tool / provider / src / main / java / org / onap / appc / encryptiontool / wrapper / WrapperEncryptionTool.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
24 package org.onap.appc.encryptiontool.wrapper;
25
26 import java.util.ArrayList;
27 import javax.sql.rowset.CachedRowSet;
28 import org.apache.commons.lang.StringUtils;
29 import org.onap.ccsdk.sli.core.dblib.DBResourceManager;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class WrapperEncryptionTool {
34
35     private static final Logger log = LoggerFactory.getLogger(WrapperEncryptionTool.class);
36     private static final String USER_PARAM = "user";
37     private static final String PASS_PARAM = "password";
38     private static final String URL_PARAM = "url";
39     private static final String PORT_PARAM = "port";
40
41     private WrapperEncryptionTool() {
42     }
43
44     public static void main(String[] args) {
45           String vnfType = args[0];
46           String protocol = args[1];
47           String user = args[2];
48           String password = args[3];
49           String action = args[4];
50           String port = args[5];
51           String url = args[6];
52         if (StringUtils.isBlank(user)) {
53             log.info("ERROR-USER can not be null");
54             return;
55         }
56         if (StringUtils.isBlank(password)) {
57             log.info("ERROR-PASSWORD can not be null");
58             return;
59         }
60         if (StringUtils.isBlank(protocol) || StringUtils.isBlank(vnfType) || StringUtils.isBlank(action)) {
61             log.info("ERROR-PROTOCOL ,Action and VNF-TYPE both can not be null");
62             return;
63         }
64         EncryptionTool et = EncryptionTool.getInstance();
65         String enPass = et.encrypt(password);
66         updateProperties(user, vnfType, enPass, action, port, url, protocol);
67     }
68
69     public static void updateProperties(String user, String vnfType, String enPass, String action, String port,
70             String url, String protocol) {
71         DBResourceManager dbResourceManager = null;
72         ArrayList<String> getList = new ArrayList<>();
73         getList.add(vnfType);
74         getList.add(protocol);
75         getList.add(action);
76         String whereClause = " VNF_TYPE = ? AND  PROTOCOL = ?  AND ACTION = ? ";
77         String setClause = " USER_NAME = ?, PASSWORD = ?, PORT_NUMBER = ?,URL = ? ";
78         String insertClause = " USER_NAME,PASSWORD,PORT_NUMBER,URL,VNF_TYPE,PROTOCOL,ACTION";
79         String insertsetClause = " ?,?,?,?,?,?,?";
80         try {
81             dbResourceManager = DbServiceUtil.initDbLibService();
82             CachedRowSet data = DbServiceUtil.getData(Constants.DEVICE_AUTHENTICATION, getList, Constants.SCHEMA_SDNCTL,
83                     "*", whereClause);
84             int rowCount = 0;
85             if (data.first()) {
86                 rowCount++;
87                    log.info(rowCount + "rowcount");
88             }
89             getList.clear();
90             getList.add(user);
91             getList.add(enPass);
92             getList.add(port);
93             getList.add(url);
94             getList.add(vnfType);
95             getList.add(protocol);
96             getList.add(action);
97             if (rowCount == 1) {
98                 DbServiceUtil.updateDB(Constants.DEVICE_AUTHENTICATION, getList, whereClause, setClause);
99                 log.info("APPC-MESSAGE: Password Updated Successfully");
100             } else {
101                 DbServiceUtil.insertDB(Constants.DEVICE_AUTHENTICATION, getList, insertClause, insertsetClause);
102                 log.info("APPC-MESSAGE: password  Inserted Successfully");
103             }
104         } catch (Exception e) {
105             log.debug("Caught Exception", e);
106             log.info("Caught exception", e);
107             log.info("APPC-MESSAGE:" + e.getMessage());
108             dbResourceManager.cleanUp();
109
110         } finally {
111             dbResourceManager.cleanUp();
112         }
113     }
114
115 }