Updates to config encryptiontool bundle
[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.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.apache.commons.lang.StringUtils;
32
33 public class WrapperEncryptionTool {
34
35     private static final Logger log = LoggerFactory.getLogger(WrapperEncryptionTool.class);
36
37     public static void main(String[] args) {
38         String vnf_type = args[0];
39         String protocol = args[1];
40         String user = args[2];
41         String password = args[3];
42         String action = args[4];
43         String port = args[5];
44         String url = args[6];
45
46         if (StringUtils.isNotBlank(user)) {
47             log.info("ERROR-USER can not be null");
48         return;
49         }
50         if (StringUtils.isNotBlank(password)) {
51             log.info("ERROR-PASSWORD can not be null");
52             return;
53         }
54         if (StringUtils.isNotBlank(protocol) || StringUtils.isNotBlank(vnf_type) || StringUtils.isNotBlank(action)) {
55             log.info("ERROR-PROTOCOL ,Action and VNF-TYPE both can not be null");
56             return;
57         }
58
59         EncryptionTool et = EncryptionTool.getInstance();
60         String enPass = et.encrypt(password);
61
62         if ((protocol != null && !protocol.isEmpty())) {
63             updateProperties(user, vnf_type, enPass, action, port, url, protocol);
64             return;
65         }
66
67     }
68
69     public static void updateProperties(String user, String vnf_type, String password, String action, String port,
70             String url, String protocol) {
71         try {
72             log.info("Received Inputs protocol:%s User:%s vnfType:%s action:%surl:%s port:%s ", protocol, user,
73                     vnf_type, action, url, port);
74             String property = protocol;
75             if (!StringUtils.isNotBlank(vnf_type)) {
76
77                 if (!StringUtils.isNotBlank(protocol)) {
78                     if (!StringUtils.isNotBlank(action)) {
79
80                         property = vnf_type + "." + protocol + "." + action;
81
82                     }
83
84                 } else {
85                     property = vnf_type;
86                 }
87
88             } else {
89
90                 if (!StringUtils.isNotBlank(protocol)) {
91                     property = protocol;
92
93                 }
94             }
95
96             PropertiesConfiguration conf = new PropertiesConfiguration(
97                     System.getenv("APPC_CONFIG_DIR")+"/appc_southbound.properties");
98
99             if (conf.subset(property) != null) {
100
101                 Iterator<String> it = conf.subset(property).getKeys();
102                 if (it.hasNext()) {
103                     while (it.hasNext()) {
104                         String key = it.next();
105                         log.info("key---value pairs");
106                         log.info(property + "." + key + "------" + conf.getProperty(property + "." + key));
107                         if ((property + "." + key).contains("user")) {
108                             if (user != null && !user.isEmpty())
109                             conf.setProperty(property + "." + key, user);
110                         }
111
112                         if ((property + "." + key).contains("password")) {
113                             if (password != null && !password.isEmpty())
114                                 conf.setProperty(property + "." + key, password);
115                         }
116
117                         if ((property + "." + key).contains("port")) {
118                             if (port != null && !port.isEmpty())
119                                 conf.setProperty(property + "." + key, port);
120                         }
121                         if ((property + "." + key).contains("url")) {
122                             if (url != null && !url.isEmpty())
123                                 conf.setProperty(property + "." + key, url);
124                         }
125
126                     }
127                 } else {
128                     if (conf.containsKey(property + "." + "user")) {
129                        if (user != null && !user.isEmpty())
130                         conf.setProperty(property + "." + "user", user);
131                     } else {
132                         conf.addProperty(property + "." + "user", user);
133                     }
134
135
136                     if (conf.containsKey(property + "." + "password")) {
137                         if (password != null && !password.isEmpty())
138                             conf.setProperty(property + "." + "password", password);
139                     } else {
140                         conf.addProperty(property + "." + "password", password);
141                     }
142
143
144                     if (conf.containsKey(property + "." + "port")) {
145                         if (port != null && !port.isEmpty())
146                             conf.setProperty(property + "." + "port", port);
147                     } else {
148                         if (port != null && !port.isEmpty())
149                             conf.addProperty(property + "." + "port", port);
150                     }
151
152                     if (conf.containsKey(property + "." + "url")) {
153                         if (url != null && !url.isEmpty())
154                             conf.setProperty(property + "." + "url", url);
155
156                     } else {
157
158                         conf.addProperty(property + "." + "url", url);
159                     }
160                 }
161
162             }
163             conf.save();
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         }
171          finally{
172     System.exit(0);
173         }
174
175     }
176 }