WrapperEncryptionTool sonar fixes
[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 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.onap.appc.encryptiontool.wrapper;
26
27 import java.util.Iterator;
28 import org.apache.commons.configuration.PropertiesConfiguration;
29 import org.apache.commons.lang.StringUtils;
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
53         if (StringUtils.isNotBlank(user)) {
54             log.info("ERROR-USER can not be null");
55             return;
56         }
57         if (StringUtils.isNotBlank(password)) {
58             log.info("ERROR-PASSWORD can not be null");
59             return;
60         }
61         if (StringUtils.isNotBlank(protocol) || StringUtils.isNotBlank(vnfType) || StringUtils.isNotBlank(action)) {
62             log.info("ERROR-PROTOCOL ,Action and VNF-TYPE both can not be null");
63             return;
64         }
65
66         EncryptionTool et = EncryptionTool.getInstance();
67         String enPass = et.encrypt(password);
68
69         if (StringUtils.isNotBlank(protocol)) {
70             updateProperties(user, vnfType, enPass, action, port, url, protocol);
71         }
72     }
73
74     public static void updateProperties(String user, String vnfType, String password, String action, String port,
75         String url, String protocol) {
76         try {
77             log.info("Received Inputs protocol:%s User:%s vnfType:%s action:%surl:%s port:%s ", protocol, user,
78                 vnfType, action, url, port);
79             String property = protocol;
80             if (StringUtils.isBlank(vnfType)) {
81                 if (StringUtils.isBlank(protocol) && StringUtils.isBlank(action)) {
82                     property = vnfType + "." + protocol + "." + action;
83                 } else if (StringUtils.isNotBlank(protocol)){
84                     property = vnfType;
85                 }
86             } else if (StringUtils.isBlank(protocol)){
87                 property = protocol;
88             }
89
90             PropertiesConfiguration conf = new PropertiesConfiguration(
91                 System.getenv("APPC_CONFIG_DIR") + "/appc_southbound.properties");
92
93             if (conf.subset(property) != null) {
94
95                 Iterator<String> it = conf.subset(property).getKeys();
96                 if (it.hasNext()) {
97                     while (it.hasNext()) {
98                         String key = it.next();
99                         log.info("key---value pairs");
100                         log.info(property + "." + key + "------" + conf.getProperty(property + "." + key));
101                         resolveProperty(user, password, port, url, property, conf, key);
102                     }
103                 } else {
104                     resolvePropertyAction(user, password, port, url, property, conf);
105                 }
106             }
107             conf.save();
108         } catch (Exception e) {
109             log.debug("Caught Exception", e);
110             log.info("Caught exception", e);
111             log.info("APPC-MESSAGE:" + e.getMessage());
112
113         } finally {
114             System.exit(0);
115         }
116     }
117
118     private static void resolvePropertyAction(String user, String password, String port, String url, String property,
119         PropertiesConfiguration conf) {
120         if (containsParam(user, property, conf, USER_PARAM)) {
121             conf.setProperty(property + "." + USER_PARAM, user);
122         } else {
123             conf.addProperty(property + "." + USER_PARAM, user);
124         }
125         if (containsParam(user, property, conf, PASS_PARAM)) {
126             conf.setProperty(property + "." + PASS_PARAM, password);
127         } else {
128             conf.addProperty(property + "." + PASS_PARAM, password);
129         }
130         if (containsParam(user, property, conf, PORT_PARAM)) {
131             conf.setProperty(property + "." + PORT_PARAM, port);
132         } else if (port != null && !port.isEmpty()) {
133             conf.addProperty(property + "." + PORT_PARAM, port);
134         }
135         if (containsParam(user, property, conf, URL_PARAM)) {
136             conf.setProperty(property + "." + URL_PARAM, url);
137         } else {
138             conf.addProperty(property + "." + URL_PARAM, url);
139         }
140     }
141
142     private static void resolveProperty(String user, String password, String port, String url, String property,
143         PropertiesConfiguration conf, String key) {
144         if (contains(user, property, key, USER_PARAM)) {
145             conf.setProperty(property + "." + key, user);
146         }
147         if (contains(user, property, key, PASS_PARAM)) {
148             conf.setProperty(property + "." + key, password);
149         }
150         if (contains(user, property, key, PORT_PARAM)) {
151             conf.setProperty(property + "." + key, port);
152         }
153         if (contains(user, property, key, URL_PARAM)) {
154             conf.setProperty(property + "." + key, url);
155         }
156     }
157
158     private static boolean containsParam(String var, String property, PropertiesConfiguration conf, String param) {
159         return StringUtils.isNotBlank(var) && conf.containsKey(property + "." + param);
160     }
161
162     private static boolean contains(String var, String property, String key, String param) {
163         return StringUtils.isNotBlank(var) && (property + "." + key).contains(param);
164     }
165 }