2 * ============LICENSE_START=======================================================
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
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 * ============LICENSE_END=========================================================
24 package org.onap.appc.encryptiontool.wrapper;
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;
32 public class WrapperEncryptionTool {
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";
40 private WrapperEncryptionTool() {
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];
52 if (StringUtils.isBlank(user)) {
53 log.info("ERROR-USER can not be null");
56 if (StringUtils.isBlank(password)) {
57 log.info("ERROR-PASSWORD can not be null");
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");
65 EncryptionTool et = EncryptionTool.getInstance();
66 String enPass = et.encrypt(password);
68 if (StringUtils.isBlank(protocol)) {
69 updateProperties(user, vnfType, enPass, action, port, url, protocol);
73 public static void updateProperties(String user, String vnfType, String password, String action, String port,
74 String url, String protocol) {
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)){
85 } else if (StringUtils.isNotBlank(protocol)){
89 PropertiesConfiguration conf = new PropertiesConfiguration(
90 System.getenv("APPC_CONFIG_DIR") + "/appc_southbound.properties");
92 if (conf.subset(property) != null) {
94 Iterator<String> it = conf.subset(property).getKeys();
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);
103 resolvePropertyAction(user, password, port, url, property, conf);
107 } catch (Exception e) {
108 log.debug("Caught Exception", e);
109 log.info("Caught exception", e);
110 log.info("APPC-MESSAGE:" + e.getMessage());
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);
122 conf.addProperty(property + "." + USER_PARAM, user);
124 if (containsParam(user, property, conf, PASS_PARAM)) {
125 conf.setProperty(property + "." + PASS_PARAM, password);
127 conf.addProperty(property + "." + PASS_PARAM, password);
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);
134 if (containsParam(user, property, conf, URL_PARAM)) {
135 conf.setProperty(property + "." + URL_PARAM, url);
137 conf.addProperty(property + "." + URL_PARAM, url);
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);
146 if (contains(user, property, key, PASS_PARAM)) {
147 conf.setProperty(property + "." + key, password);
149 if (contains(user, property, key, PORT_PARAM)) {
150 conf.setProperty(property + "." + key, port);
152 if (contains(user, property, key, URL_PARAM)) {
153 conf.setProperty(property + "." + key, url);
157 private static boolean containsParam(String var, String property, PropertiesConfiguration conf, String param) {
158 return StringUtils.isNotBlank(var) && conf.containsKey(property + "." + param);
161 private static boolean contains(String var, String property, String key, String param) {
162 return StringUtils.isNotBlank(var) && (property + "." + key).contains(param);