2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.encryptiontool.wrapper;
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;
33 public class WrapperEncryptionTool {
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";
41 private WrapperEncryptionTool() {
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];
53 if (StringUtils.isNotBlank(user)) {
54 log.info("ERROR-USER can not be null");
57 if (StringUtils.isNotBlank(password)) {
58 log.info("ERROR-PASSWORD can not be null");
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");
66 EncryptionTool et = EncryptionTool.getInstance();
67 String enPass = et.encrypt(password);
69 if (StringUtils.isNotBlank(protocol)) {
70 updateProperties(user, vnfType, enPass, action, port, url, protocol);
74 public static void updateProperties(String user, String vnfType, String password, String action, String port,
75 String url, String protocol) {
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)){
86 } else if (StringUtils.isBlank(protocol)){
90 PropertiesConfiguration conf = new PropertiesConfiguration(
91 System.getenv("APPC_CONFIG_DIR") + "/appc_southbound.properties");
93 if (conf.subset(property) != null) {
95 Iterator<String> it = conf.subset(property).getKeys();
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);
104 resolvePropertyAction(user, password, port, url, property, conf);
108 } catch (Exception e) {
109 log.debug("Caught Exception", e);
110 log.info("Caught exception", e);
111 log.info("APPC-MESSAGE:" + e.getMessage());
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);
123 conf.addProperty(property + "." + USER_PARAM, user);
125 if (containsParam(user, property, conf, PASS_PARAM)) {
126 conf.setProperty(property + "." + PASS_PARAM, password);
128 conf.addProperty(property + "." + PASS_PARAM, password);
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);
135 if (containsParam(user, property, conf, URL_PARAM)) {
136 conf.setProperty(property + "." + URL_PARAM, url);
138 conf.addProperty(property + "." + URL_PARAM, url);
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);
147 if (contains(user, property, key, PASS_PARAM)) {
148 conf.setProperty(property + "." + key, password);
150 if (contains(user, property, key, PORT_PARAM)) {
151 conf.setProperty(property + "." + key, port);
153 if (contains(user, property, key, URL_PARAM)) {
154 conf.setProperty(property + "." + key, url);
158 private static boolean containsParam(String var, String property, PropertiesConfiguration conf, String param) {
159 return StringUtils.isNotBlank(var) && conf.containsKey(property + "." + param);
162 private static boolean contains(String var, String property, String key, String param) {
163 return StringUtils.isNotBlank(var) && (property + "." + key).contains(param);