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