aboutsummaryrefslogtreecommitdiffstats
path: root/appc-config/appc-encryption-tool/provider/src/main/java/org/onap/appc/encryptiontool/wrapper/EncryptionToolDGWrapper.java
blob: 0910a5ba0a7e55e6f7f05bfc593991ffe11ca89f (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*-
 * ============LICENSE_START=======================================================
 * ONAP : APPC
 * ================================================================================
 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Copyright (C) 2017 Amdocs
 * ================================================================================
 * Modifications Copyright (C) 2019 Ericsson
 * =============================================================================
 * 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.Map;

import org.apache.commons.lang.StringUtils;
import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
import org.onap.ccsdk.sli.core.sli.SvcLogicException;
import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource;
import org.onap.appc.encryptiontool.fqdn.ParseAdminArtifcat;

public class EncryptionToolDGWrapper implements SvcLogicJavaPlugin {
    private static final EELFLogger log = EELFManager.getInstance().getLogger(EncryptionToolDGWrapper.class);
    private SvcLogicResource serviceLogic;
    private static EncryptionToolDGWrapper dgGeneralDBService = null;
    ParseAdminArtifcat artifact = new ParseAdminArtifcat();

    public static EncryptionToolDGWrapper initialise() {
        dgGeneralDBService = new EncryptionToolDGWrapper();
        return dgGeneralDBService;
    }

    public EncryptionToolDGWrapper() {
        serviceLogic = new SqlResource();
    }

    protected EncryptionToolDGWrapper(SqlResource svcLogic) {
        serviceLogic = svcLogic;
    }

    public void runEncryption(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
        String userName = inParams.get("userName");
        String password = inParams.get("password");
        String vnfType = inParams.get("vnf_type");
        try {
            if (StringUtils.isBlank(userName) || StringUtils.isBlank(password) || StringUtils.isBlank(vnfType)) {
                throw new SvcLogicException("username or Password is missing");
            }
            String[] input = new String[] { vnfType, userName, password };
            WrapperEncryptionTool.main(input);
        } catch (Exception e) {
            throw new SvcLogicException(e.getMessage());
        }
    }

    public void getProperty(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
        String fn = "getproperty.deviceauthentication";
        String responsePrefix = inParams.get("prefix");
        String vnf_Type = ctx.getAttribute("vnf-type");
        String action = ctx.getAttribute("input.action");
        String protocol = ctx.getAttribute("APPC.protocol.PROTOCOL");
        String user = "";
        String password = "";
        String port = "0";
        String url = "";
        String key = "";
        QueryStatus status = null;
        Integer cnt = 0;
        String tenantAai = inParams.get("tenantAai");
        String cloudOwneraai = inParams.get("cldOwnerAai");
        String cloudRegionAai = inParams.get("cldRegionAai");
        String payloadFqdn = inParams.get("payloadFqdn");
        String payloadTenant = inParams.get("payloadTenant");
        String payloadCloudOwner = inParams.get("payloadCloudOwner");
        String payloadCloudRegion = inParams.get("payloadCloudRegion");
        ctx.setAttribute("payloadTenant", payloadTenant);
        ctx.setAttribute("payloadCloudOwner", payloadCloudOwner);
        ctx.setAttribute("payloadCloudRegion", payloadCloudRegion);
        ctx.setAttribute("tenantAai", tenantAai);
        ctx.setAttribute("cloudOwneraai", cloudOwneraai);
        ctx.setAttribute("cloudRegionAai", cloudRegionAai);
        responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
        String basicQuery = "SELECT USER_NAME ,PASSWORD,PORT_NUMBER ,URL FROM  DEVICE_AUTHENTICATION  WHERE VNF_TYPE = $"
                + Constants.VNF_TYPE + " AND PROTOCOL = $" + Constants.PROTOCOL + "" + " AND ACTION = $"
                + Constants.ACTION + "";
        String urlAppend = " ";
        try {
            if (serviceLogic != null && ctx != null) {
                if (protocol.equalsIgnoreCase("ansible")) {
                    if (payloadFqdn != null && payloadFqdn.trim().length() > 0) {
                        url = payloadFqdn;
                        log.info("url from payload" + url);
                        urlAppend = " AND URL = $" + Constants.URL + "";
                        key = basicQuery + urlAppend;
                    } else {
                        key = "SELECT COUNT(*) AS MULTIPLE FROM DEVICE_AUTHENTICATION WHERE VNF_TYPE = $"
                                + Constants.VNF_TYPE + " AND PROTOCOL = $" + Constants.PROTOCOL + " AND ACTION = $"
                                + Constants.ACTION + "";
                        status = serviceLogic.query("SQL", false, null, key, null, null, ctx);
                        log.info("Checking number of records  for ansible:" + key);
                        cnt = Integer.parseInt(ctx.getAttribute("MULTIPLE"));
                        if (cnt > 1) {
                            String fqdnwithPort = artifact.retrieveFqdn(ctx);
                            if (StringUtils.isNotBlank(fqdnwithPort) && fqdnwithPort.contains(":")) {

                                int index = StringUtils.ordinalIndexOf(fqdnwithPort, ":", 3);
                                url = fqdnwithPort.substring(0, index);
                                urlAppend = " AND URL = $" + Constants.URL + "";
                                key = basicQuery + urlAppend;
                            } else {
                                throw new SvcLogicException(
                                        fn + ": NOT_FOUND! No FQDN  match found in admin artifact  for " + vnf_Type
                                                + " " + protocol + "" + action + "");
                            }
                        } else if (cnt == 1) {
                            key = basicQuery;
                        } else {
                            if (status == QueryStatus.FAILURE) {
                                log.info(fn + ":: Error retrieving credentials");
                                throw new SvcLogicException("Error retrieving credentials");
                            }
                            // SELECT COUNT query will return SUCCESS with a count of 0 when no data is found
                            if (status == QueryStatus.SUCCESS) {
                                log.info(fn + ":: NOT_FOUND! No data found in device_authentication table for "
                                        + vnf_Type + " " + protocol + "" + action + "");
                                throw new SvcLogicException(
                                        fn + ":: NOT_FOUND! No data found in device_authentication table for "
                                                + vnf_Type + " " + protocol + "" + action + "");
                            }
                        }
                    }

                } else {
                    key = basicQuery;
                    log.info("Getting authentication details :" + key);
                }
                status = serviceLogic.query("SQL", false, null, key, null, null, ctx);
                if (status == QueryStatus.FAILURE) {
                    log.info(fn + ":: Error retrieving credentials");
                    throw new SvcLogicException("Error retrieving credentials");
                }
                if (status == QueryStatus.NOT_FOUND) {
                    log.info(fn + ":: NOT_FOUND! No data found in device_authentication table for " + vnf_Type + " "
                            + protocol + "" + action + "");
                    throw new SvcLogicException(fn + ":: NOT_FOUND! No data found in device_authentication table for "
                            + vnf_Type + " " + protocol + "" + action + "");
                }

                user = ctx.getAttribute("USER-NAME");
                password = ctx.getAttribute("PASSWORD");
                port = ctx.getAttribute("PORT-NUMBER");
                url = ctx.getAttribute("URL");
                log.info("data retrieved " + "user" + user + "pwd" + password + "port" + port + "url" + url);

                if (StringUtils.isNotBlank(user))
                    ctx.setAttribute(responsePrefix + "user", user);
                if (StringUtils.isNotBlank(password))
                    ctx.setAttribute(responsePrefix + "password", password);
                if (StringUtils.isNotBlank(url))
                    ctx.setAttribute(responsePrefix + "url", url);
                if (StringUtils.isNotBlank(port))
                    ctx.setAttribute(responsePrefix + "port", port);
                log.debug("user" + ctx.getAttribute(responsePrefix + "user"));
                log.debug("password" + ctx.getAttribute(responsePrefix + "password"));
                log.debug("url" + ctx.getAttribute(responsePrefix + "url"));
                log.debug("port" + ctx.getAttribute(responsePrefix + "port"));

            }
        } catch (Exception e) {
            ctx.setAttribute(responsePrefix + "status", "failure");
            ctx.setAttribute(responsePrefix + "error-message", e.getMessage());
            log.info("Caught exception", e);
            throw new SvcLogicException(e.getMessage());

        }
    }
}