6091e4cf275eb217f5fe2f230a425c95e2159904
[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.Iterator;
27 import org.apache.commons.configuration.PropertiesConfiguration;
28 import org.apache.commons.lang.StringUtils;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class WrapperEncryptionTool {
33
34     private static final Logger log = LoggerFactory.getLogger(WrapperEncryptionTool.class);
35     private static final String USER_PARAM = "user";
36     private static final String PASS_PARAM = "password";
37     private static final String URL_PARAM = "url";
38     private static final String PORT_PARAM = "port";
39
40     private WrapperEncryptionTool() {
41     }
42
43     public static void main(String[] args) {
44         String vnfType = args[0];
45         String protocol = args[1];
46         String user = args[2];
47         String password = args[3];
48         String action = args[4];
49         String port = args[5];
50         String url = args[6];
51
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
65         EncryptionTool et = EncryptionTool.getInstance();
66         String enPass = et.encrypt(password);
67
68         if (StringUtils.isBlank(protocol)) {
69             updateProperties(user, vnfType, enPass, action, port, url, protocol);
70         }
71     }
72
73     public static void updateProperties(String user, String vnfType, String password, String action, String port,
74         String url, String protocol) {
75         try {
76             log.info("Received Inputs protocol:%s User:%s vnfType:%s action:%surl:%s port:%s ", protocol, user,
77                 vnfType, action, url, port);
78             String property = protocol;
79             if (StringUtils.isNotBlank(vnfType)) {
80                 if (StringUtils.isNotBlank(protocol) && StringUtils.isNotBlank(action)) {
81                     property = vnfType + "." + protocol + "." + action;
82                 } else if (StringUtils.isNotBlank(protocol)){
83                     property = vnfType;
84                 }
85             } else if (StringUtils.isNotBlank(protocol)){
86                 property = protocol;
87             }
88
89             PropertiesConfiguration conf = new PropertiesConfiguration(
90                 System.getenv("APPC_CONFIG_DIR") + "/appc_southbound.properties");
91
92             if (conf.subset(property) != null) {
93
94                 Iterator<String> it = conf.subset(property).getKeys();
95                 if (it.hasNext()) {
96                     while (it.hasNext()) {
97                         String key = it.next();
98                         log.info("key---value pairs");
99                         log.info(property + "." + key + "------" + conf.getProperty(property + "." + key));
100                         resolveProperty(user, password, port, url, property, conf, key);
101                     }
102                 } else {
103                     resolvePropertyAction(user, password, port, url, property, conf);
104                 }
105             }
106             conf.save();
107         } catch (Exception e) {
108             log.debug("Caught Exception", e);
109             log.info("Caught exception", e);
110             log.info("APPC-MESSAGE:" + e.getMessage());
111
112         } finally {
113             System.exit(0);
114         }
115     }
116
117     private static void resolvePropertyAction(String user, String password, String port, String url, String property,
118         PropertiesConfiguration conf) {
119         if (containsParam(user, property, conf, USER_PARAM)) {
120             conf.setProperty(property + "." + USER_PARAM, user);
121         } else {
122             conf.addProperty(property + "." + USER_PARAM, user);
123         }
124         if (containsParam(user, property, conf, PASS_PARAM)) {
125             conf.setProperty(property + "." + PASS_PARAM, password);
126         } else {
127             conf.addProperty(property + "." + PASS_PARAM, password);
128         }
129         if (containsParam(user, property, conf, PORT_PARAM)) {
130             conf.setProperty(property + "." + PORT_PARAM, port);
131         } else if (port != null && !port.isEmpty()) {
132             conf.addProperty(property + "." + PORT_PARAM, port);
133         }
134         if (containsParam(user, property, conf, URL_PARAM)) {
135             conf.setProperty(property + "." + URL_PARAM, url);
136         } else {
137             conf.addProperty(property + "." + URL_PARAM, url);
138         }
139     }
140
141     private static void resolveProperty(String user, String password, String port, String url, String property,
142         PropertiesConfiguration conf, String key) {
143         if (contains(user, property, key, USER_PARAM)) {
144             conf.setProperty(property + "." + key, user);
145         }
146         if (contains(user, property, key, PASS_PARAM)) {
147             conf.setProperty(property + "." + key, password);
148         }
149         if (contains(user, property, key, PORT_PARAM)) {
150             conf.setProperty(property + "." + key, port);
151         }
152         if (contains(user, property, key, URL_PARAM)) {
153             conf.setProperty(property + "." + key, url);
154         }
155     }
156
157     private static boolean containsParam(String var, String property, PropertiesConfiguration conf, String param) {
158         return StringUtils.isNotBlank(var) && conf.containsKey(property + "." + param);
159     }
160
161     private static boolean contains(String var, String property, String key, String param) {
162         return StringUtils.isNotBlank(var) && (property + "." + key).contains(param);
163     }
164 }