e29920ec6ad3e152a49ca17867a66cc34e0857be
[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  * 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         log.info("vnfType = " + vnfType + " protocol = " + protocol + " user=" + user + " password=" + password
53                 + " action=" + action + " port=" + port + " url=" + url);
54
55         if (StringUtils.isBlank(user)) {
56             log.info("ERROR-USER can not be null");
57             return;
58         }
59         if (StringUtils.isBlank(password)) {
60             log.info("ERROR-PASSWORD can not be null");
61             return;
62         }
63         if (StringUtils.isBlank(protocol) || StringUtils.isBlank(vnfType) || StringUtils.isBlank(action)) {
64             log.info("ERROR-PROTOCOL ,Action and VNF-TYPE both can not be null");
65             return;
66         }
67         if (protocol.equalsIgnoreCase("ansible") && (StringUtils.isBlank(url))) {
68             log.info("URL cannot be null for Ansible");
69             return;
70         }
71         EncryptionTool et = EncryptionTool.getInstance();
72         String enPass = et.encrypt(password);
73         log.info("enPass =" + enPass);
74         if (protocol.equalsIgnoreCase("ansible"))
75             updatePropertiesAnsible(user, vnfType, enPass, action, port, url, protocol);
76         else
77             updateProperties(user, vnfType, enPass, action, port, url, protocol);
78     }
79
80     public static void updatePropertiesAnsible(String user, String vnfType, String enPass, String action, String port,
81             String url, String protocol) {
82         DBResourceManager dbResourceManager = null;
83         ArrayList<String> getList = new ArrayList<>();
84         getList.add(vnfType);
85         getList.add(protocol);
86         getList.add(action);
87         getList.add(url);
88         String whereClause = " VNF_TYPE = ? AND  PROTOCOL = ?  AND ACTION = ? AND URL = ? ";
89         String setClause = " USER_NAME = ?, PASSWORD = ?, PORT_NUMBER = ? ";
90         String insertClause = " USER_NAME,PASSWORD,PORT_NUMBER,URL,VNF_TYPE,PROTOCOL,ACTION";
91         String insertsetClause = " ?,?,?,?,?,?,?";
92         try {
93             dbResourceManager = DbServiceUtil.initDbLibService();
94             CachedRowSet data = DbServiceUtil.getData(Constants.DEVICE_AUTHENTICATION, getList, Constants.SCHEMA_SDNCTL,
95                     "*", whereClause);
96             int rowCount = 0;
97             if (data.first()) {
98                 rowCount++;
99                 log.info(rowCount + "rowcount");
100             }
101             getList.clear();
102             getList.add(user);
103             getList.add(enPass);
104             getList.add(port);
105             getList.add(url);
106             getList.add(vnfType);
107             getList.add(protocol);
108             getList.add(action);
109             if (rowCount == 1) {
110                 DbServiceUtil.updateDB(Constants.DEVICE_AUTHENTICATION, getList, whereClause, setClause);
111                 log.info("APPC-MESSAGE: Password Updated Successfully");
112             } else {
113                 DbServiceUtil.insertDB(Constants.DEVICE_AUTHENTICATION, getList, insertClause, insertsetClause);
114                 log.info("APPC-MESSAGE: password  Inserted Successfully");
115             }
116         } catch (Exception e) {
117             log.debug("Caught Exception", e);
118             log.info("Caught exception", e);
119             log.info("APPC-MESSAGE:" + e.getMessage());
120
121         } finally {
122             dbResourceManager.cleanUp();
123         }
124     }
125
126     public static void updateProperties(String user, String vnfType, String enPass, String action, String port,
127             String url, String protocol) {
128         DBResourceManager dbResourceManager = null;
129         ArrayList<String> getList = new ArrayList<>();
130         getList.add(vnfType);
131         getList.add(protocol);
132         getList.add(action);
133         String whereClause = " VNF_TYPE = ? AND  PROTOCOL = ?  AND ACTION = ? ";
134         String setClause = " USER_NAME = ?, PASSWORD = ?, PORT_NUMBER = ?,URL = ? ";
135         String insertClause = " USER_NAME,PASSWORD,PORT_NUMBER,URL,VNF_TYPE,PROTOCOL,ACTION";
136         String insertsetClause = " ?,?,?,?,?,?,?";
137         try {
138             dbResourceManager = DbServiceUtil.initDbLibService();
139             CachedRowSet data = DbServiceUtil.getData(Constants.DEVICE_AUTHENTICATION, getList, Constants.SCHEMA_SDNCTL,
140                     "*", whereClause);
141             int rowCount = 0;
142             if (data.first()) {
143                 rowCount++;
144                 log.info(rowCount + "rowcount");
145             }
146             getList.clear();
147             getList.add(user);
148             getList.add(enPass);
149             getList.add(port);
150             getList.add(url);
151             getList.add(vnfType);
152             getList.add(protocol);
153             getList.add(action);
154             if (rowCount == 1) {
155                 DbServiceUtil.updateDB(Constants.DEVICE_AUTHENTICATION, getList, whereClause, setClause);
156                 log.info("APPC-MESSAGE: Password Updated Successfully");
157             } else {
158                 DbServiceUtil.insertDB(Constants.DEVICE_AUTHENTICATION, getList, insertClause, insertsetClause);
159                 log.info("APPC-MESSAGE: password  Inserted Successfully");
160             }
161         } catch (Exception e) {
162             log.debug("Caught Exception", e);
163             log.info("Caught exception", e);
164             log.info("APPC-MESSAGE:" + e.getMessage());
165
166         } finally {
167             dbResourceManager.cleanUp();
168         }
169     }
170
171 }