aboutsummaryrefslogtreecommitdiffstats
path: root/appc-config/appc-encryption-tool/provider/src/main/java/org/onap/appc/encryptiontool/wrapper/WrapperEncryptionTool.java
blob: 6091e4cf275eb217f5fe2f230a425c95e2159904 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*-
 * ============LICENSE_START=======================================================
 * ONAP : APPC
 * ================================================================================
 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Copyright (C) 2017 Amdocs
 * =============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * ============LICENSE_END=========================================================
 */

package org.onap.appc.encryptiontool.wrapper;

import java.util.Iterator;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class WrapperEncryptionTool {

    private static final Logger log = LoggerFactory.getLogger(WrapperEncryptionTool.class);
    private static final String USER_PARAM = "user";
    private static final String PASS_PARAM = "password";
    private static final String URL_PARAM = "url";
    private static final String PORT_PARAM = "port";

    private WrapperEncryptionTool() {
    }

    public static void main(String[] args) {
        String vnfType = args[0];
        String protocol = args[1];
        String user = args[2];
        String password = args[3];
        String action = args[4];
        String port = args[5];
        String url = args[6];

        if (StringUtils.isBlank(user)) {
            log.info("ERROR-USER can not be null");
            return;
        }
        if (StringUtils.isBlank(password)) {
            log.info("ERROR-PASSWORD can not be null");
            return;
        }
        if (StringUtils.isBlank(protocol) || StringUtils.isBlank(vnfType) || StringUtils.isBlank(action)) {
            log.info("ERROR-PROTOCOL ,Action and VNF-TYPE both can not be null");
            return;
        }

        EncryptionTool et = EncryptionTool.getInstance();
        String enPass = et.encrypt(password);

        if (StringUtils.isBlank(protocol)) {
            updateProperties(user, vnfType, enPass, action, port, url, protocol);
        }
    }

    public static void updateProperties(String user, String vnfType, String password, String action, String port,
        String url, String protocol) {
        try {
            log.info("Received Inputs protocol:%s User:%s vnfType:%s action:%surl:%s port:%s ", protocol, user,
                vnfType, action, url, port);
            String property = protocol;
            if (StringUtils.isNotBlank(vnfType)) {
                if (StringUtils.isNotBlank(protocol) && StringUtils.isNotBlank(action)) {
                    property = vnfType + "." + protocol + "." + action;
                } else if (StringUtils.isNotBlank(protocol)){
                    property = vnfType;
                }
            } else if (StringUtils.isNotBlank(protocol)){
                property = protocol;
            }

            PropertiesConfiguration conf = new PropertiesConfiguration(
                System.getenv("APPC_CONFIG_DIR") + "/appc_southbound.properties");

            if (conf.subset(property) != null) {

                Iterator<String> it = conf.subset(property).getKeys();
                if (it.hasNext()) {
                    while (it.hasNext()) {
                        String key = it.next();
                        log.info("key---value pairs");
                        log.info(property + "." + key + "------" + conf.getProperty(property + "." + key));
                        resolveProperty(user, password, port, url, property, conf, key);
                    }
                } else {
                    resolvePropertyAction(user, password, port, url, property, conf);
                }
            }
            conf.save();
        } catch (Exception e) {
            log.debug("Caught Exception", e);
            log.info("Caught exception", e);
            log.info("APPC-MESSAGE:" + e.getMessage());

        } finally {
            System.exit(0);
        }
    }

    private static void resolvePropertyAction(String user, String password, String port, String url, String property,
        PropertiesConfiguration conf) {
        if (containsParam(user, property, conf, USER_PARAM)) {
            conf.setProperty(property + "." + USER_PARAM, user);
        } else {
            conf.addProperty(property + "." + USER_PARAM, user);
        }
        if (containsParam(user, property, conf, PASS_PARAM)) {
            conf.setProperty(property + "." + PASS_PARAM, password);
        } else {
            conf.addProperty(property + "." + PASS_PARAM, password);
        }
        if (containsParam(user, property, conf, PORT_PARAM)) {
            conf.setProperty(property + "." + PORT_PARAM, port);
        } else if (port != null && !port.isEmpty()) {
            conf.addProperty(property + "." + PORT_PARAM, port);
        }
        if (containsParam(user, property, conf, URL_PARAM)) {
            conf.setProperty(property + "." + URL_PARAM, url);
        } else {
            conf.addProperty(property + "." + URL_PARAM, url);
        }
    }

    private static void resolveProperty(String user, String password, String port, String url, String property,
        PropertiesConfiguration conf, String key) {
        if (contains(user, property, key, USER_PARAM)) {
            conf.setProperty(property + "." + key, user);
        }
        if (contains(user, property, key, PASS_PARAM)) {
            conf.setProperty(property + "." + key, password);
        }
        if (contains(user, property, key, PORT_PARAM)) {
            conf.setProperty(property + "." + key, port);
        }
        if (contains(user, property, key, URL_PARAM)) {
            conf.setProperty(property + "." + key, url);
        }
    }

    private static boolean containsParam(String var, String property, PropertiesConfiguration conf, String param) {
        return StringUtils.isNotBlank(var) && conf.containsKey(property + "." + param);
    }

    private static boolean contains(String var, String property, String key, String param) {
        return StringUtils.isNotBlank(var) && (property + "." + key).contains(param);
    }
}