Changed to unmaintained
[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  * Modifications Copyright (C) 2019 Ericsson
10  * Modifications Copyright (C) 2019 IBM
11  * =============================================================================
12  * Licensed under the Apache License, Version 2.0 (the "License");
13  * you may not use this file except in compliance with the License.
14  * You may obtain a copy of the License at
15  *
16  *      http://www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an "AS IS" BASIS,
20  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  *
24  * ============LICENSE_END=========================================================
25  */
26
27 package org.onap.appc.encryptiontool.wrapper;
28
29 import java.util.ArrayList;
30 import javax.sql.rowset.CachedRowSet;
31 import org.apache.commons.lang.StringUtils;
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     private static final String USER_PARAM = "user";
40     private static final String PASS_PARAM = "password";
41     private static final String URL_PARAM = "url";
42     private static final String PORT_PARAM = "port";
43
44     private WrapperEncryptionTool() {
45     }
46
47     public static void main(String[] args) {
48         String vnfType = args[0];
49         String protocol = args[1];
50         String user = args[2];
51         String password = args[3];
52         String action = args[4];
53         String port = args[5];
54         String url = args[6];
55         //log.info("vnfType = " + vnfType + " protocol = " + protocol + " " + USER_PARAM + "=" + user + " " + PASS_PARAM
56         //      + "=" + password + " action=" + action + " " + PORT_PARAM + "=" + port + " " + URL_PARAM + "=" + url);
57
58         if (StringUtils.isBlank(user)) {
59             log.info("ERROR-USER can not be null");
60             return;
61         }
62         if (StringUtils.isBlank(password)) {
63             log.info("ERROR-PASSWORD can not be null");
64             return;
65         }
66         if (StringUtils.isBlank(protocol) || StringUtils.isBlank(vnfType) || StringUtils.isBlank(action)) {
67             log.info("ERROR-PROTOCOL ,Action and VNF-TYPE both can not be null");
68             return;
69         }
70         if (protocol.equalsIgnoreCase("ansible") && (StringUtils.isBlank(url))) {
71             log.info("URL cannot be null for Ansible");
72             return;
73         }
74         EncryptionTool et = EncryptionTool.getInstance();
75         String enPass = et.encrypt(password);
76         log.info("enPass =" + enPass);
77         if (protocol.equalsIgnoreCase("ansible"))
78             updatePropertiesAnsible(user, vnfType, enPass, action, port, url, protocol);
79         else
80             updateProperties(user, vnfType, enPass, action, port, url, protocol);
81     }
82
83     public static void updatePropertiesAnsible(String user, String vnfType, String enPass, String action, String port,
84             String url, String protocol) {
85         DBResourceManager dbResourceManager = null;
86         ArrayList<String> getList = new ArrayList<>();
87         getList.add(vnfType);
88         getList.add(protocol);
89         getList.add(action);
90         getList.add(url);
91         String whereClause = " VNF_TYPE = ? AND  PROTOCOL = ?  AND ACTION = ? AND URL = ? ";
92         String setClause = " USER_NAME = ?, PASSWORD = ?, PORT_NUMBER = ? ";
93         String insertClause = " USER_NAME,PASSWORD,PORT_NUMBER,URL,VNF_TYPE,PROTOCOL,ACTION";
94         String insertsetClause = " ?,?,?,?,?,?,?";
95         try {
96             dbResourceManager = DbServiceUtil.initDbLibService();
97             CachedRowSet data = DbServiceUtil.getData(Constants.DEVICE_AUTHENTICATION, getList, Constants.SCHEMA_SDNCTL,
98                     "*", whereClause);
99             int rowCount = 0;
100             if (data.first()) {
101                 rowCount++;
102                 log.info(rowCount + "rowcount");
103             }
104             getList.clear();
105             getList.add(user);
106             getList.add(enPass);
107             getList.add(port);
108             getList.add(url);
109             getList.add(vnfType);
110             getList.add(protocol);
111             getList.add(action);
112             if (rowCount == 1) {
113                 DbServiceUtil.updateDB(Constants.DEVICE_AUTHENTICATION, getList, whereClause, setClause);
114                 log.info("APPC-MESSAGE: Password Updated Successfully");
115             } else {
116                 DbServiceUtil.insertDB(Constants.DEVICE_AUTHENTICATION, getList, insertClause, insertsetClause);
117                 log.info("APPC-MESSAGE: password  Inserted Successfully");
118             }
119         } catch (Exception e) {
120             log.debug("Caught Exception", e);
121             log.info("Caught exception", e);
122             log.info("APPC-MESSAGE:" + e.getMessage());
123
124         } finally {
125           if(dbResourceManager!=null)
126             dbResourceManager.cleanUp();
127         }
128     }
129
130     public static void updateProperties(String user, String vnfType, String enPass, String action, String port,
131             String url, String protocol) {
132         DBResourceManager dbResourceManager = null;
133         ArrayList<String> getList = new ArrayList<>();
134         getList.add(vnfType);
135         getList.add(protocol);
136         getList.add(action);
137         String whereClause = " VNF_TYPE = ? AND  PROTOCOL = ?  AND ACTION = ? ";
138         String setClause = " USER_NAME = ?, PASSWORD = ?, PORT_NUMBER = ?,URL = ? ";
139         String insertClause = " USER_NAME,PASSWORD,PORT_NUMBER,URL,VNF_TYPE,PROTOCOL,ACTION";
140         String insertsetClause = " ?,?,?,?,?,?,?";
141         try {
142             dbResourceManager = DbServiceUtil.initDbLibService();
143             CachedRowSet data = DbServiceUtil.getData(Constants.DEVICE_AUTHENTICATION, getList, Constants.SCHEMA_SDNCTL,
144                     "*", whereClause);
145             int rowCount = 0;
146             if (data.first()) {
147                 rowCount++;
148                 log.info(rowCount + "rowcount");
149             }
150             getList.clear();
151             getList.add(user);
152             getList.add(enPass);
153             getList.add(port);
154             getList.add(url);
155             getList.add(vnfType);
156             getList.add(protocol);
157             getList.add(action);
158             if (rowCount == 1) {
159                 DbServiceUtil.updateDB(Constants.DEVICE_AUTHENTICATION, getList, whereClause, setClause);
160                 log.info("APPC-MESSAGE: Password Updated Successfully");
161             } else {
162                 DbServiceUtil.insertDB(Constants.DEVICE_AUTHENTICATION, getList, insertClause, insertsetClause);
163                 log.info("APPC-MESSAGE: password  Inserted Successfully");
164             }
165         } catch (Exception e) {
166             log.debug("Caught Exception", e);
167             log.info("Caught exception", e);
168             log.info("APPC-MESSAGE:" + e.getMessage());
169
170         } finally {
171           if(dbResourceManager!=null)
172             dbResourceManager.cleanUp();
173         }
174     }
175
176 }