Fix for incorrect handling of SQL results
[appc.git] / appc-config / appc-encryption-tool / provider / src / main / java / org / onap / appc / encryptiontool / wrapper / EncryptionToolDGWrapper.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * ================================================================================
9  * Modifications Copyright (C) 2019 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  *
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.encryptiontool.wrapper;
27
28 import java.util.Map;
29
30 import org.apache.commons.lang.StringUtils;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
36 import com.att.eelf.configuration.EELFLogger;
37 import com.att.eelf.configuration.EELFManager;
38 import org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource;
39 import org.onap.appc.encryptiontool.fqdn.ParseAdminArtifcat;
40
41 public class EncryptionToolDGWrapper implements SvcLogicJavaPlugin {
42     private static final EELFLogger log = EELFManager.getInstance().getLogger(EncryptionToolDGWrapper.class);
43     private SvcLogicResource serviceLogic;
44     private static EncryptionToolDGWrapper dgGeneralDBService = null;
45     ParseAdminArtifcat artifact = new ParseAdminArtifcat();
46
47     public static EncryptionToolDGWrapper initialise() {
48         dgGeneralDBService = new EncryptionToolDGWrapper();
49         return dgGeneralDBService;
50     }
51
52     public EncryptionToolDGWrapper() {
53         serviceLogic = new SqlResource();
54     }
55
56     protected EncryptionToolDGWrapper(SqlResource svcLogic) {
57         serviceLogic = svcLogic;
58     }
59
60     public void runEncryption(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
61         String userName = inParams.get("userName");
62         String password = inParams.get("password");
63         String vnfType = inParams.get("vnf_type");
64         try {
65             if (StringUtils.isBlank(userName) || StringUtils.isBlank(password) || StringUtils.isBlank(vnfType)) {
66                 throw new SvcLogicException("username or Password is missing");
67             }
68             String[] input = new String[] { vnfType, userName, password };
69             WrapperEncryptionTool.main(input);
70         } catch (Exception e) {
71             throw new SvcLogicException(e.getMessage());
72         }
73     }
74
75     public void getProperty(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
76         String fn = "getproperty.deviceauthentication";
77         String responsePrefix = inParams.get("prefix");
78         String vnf_Type = ctx.getAttribute("vnf-type");
79         String action = ctx.getAttribute("input.action");
80         String protocol = ctx.getAttribute("APPC.protocol.PROTOCOL");
81         String user = "";
82         String password = "";
83         String port = "0";
84         String url = "";
85         String key = "";
86         QueryStatus status = null;
87         Integer cnt = 0;
88         String tenantAai = inParams.get("tenantAai");
89         String cloudOwneraai = inParams.get("cldOwnerAai");
90         String cloudRegionAai = inParams.get("cldRegionAai");
91         String payloadFqdn = inParams.get("payloadFqdn");
92         String payloadTenant = inParams.get("payloadTenant");
93         String payloadCloudOwner = inParams.get("payloadCloudOwner");
94         String payloadCloudRegion = inParams.get("payloadCloudRegion");
95         ctx.setAttribute("payloadTenant", payloadTenant);
96         ctx.setAttribute("payloadCloudOwner", payloadCloudOwner);
97         ctx.setAttribute("payloadCloudRegion", payloadCloudRegion);
98         ctx.setAttribute("tenantAai", tenantAai);
99         ctx.setAttribute("cloudOwneraai", cloudOwneraai);
100         ctx.setAttribute("cloudRegionAai", cloudRegionAai);
101         responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
102         String basicQuery = "SELECT USER_NAME ,PASSWORD,PORT_NUMBER ,URL FROM  DEVICE_AUTHENTICATION  WHERE VNF_TYPE = $"
103                 + Constants.VNF_TYPE + " AND PROTOCOL = $" + Constants.PROTOCOL + "" + " AND ACTION = $"
104                 + Constants.ACTION + "";
105         String urlAppend = " ";
106         try {
107             if (serviceLogic != null && ctx != null) {
108                 if (protocol.equalsIgnoreCase("ansible")) {
109                     if (payloadFqdn != null && payloadFqdn.trim().length() > 0) {
110                         url = payloadFqdn;
111                         log.info("url from payload" + url);
112                         urlAppend = " AND URL = $" + Constants.URL + "";
113                         key = basicQuery + urlAppend;
114                     } else {
115                         key = "SELECT COUNT(*) AS MULTIPLE FROM DEVICE_AUTHENTICATION WHERE VNF_TYPE = $"
116                                 + Constants.VNF_TYPE + " AND PROTOCOL = $" + Constants.PROTOCOL + " AND ACTION = $"
117                                 + Constants.ACTION + "";
118                         status = serviceLogic.query("SQL", false, null, key, null, null, ctx);
119                         log.info("Checking number of records  for ansible:" + key);
120                         cnt = Integer.parseInt(ctx.getAttribute("MULTIPLE"));
121                         if (cnt > 1) {
122                             String fqdnwithPort = artifact.retrieveFqdn(ctx);
123                             if (StringUtils.isNotBlank(fqdnwithPort) && fqdnwithPort.contains(":")) {
124
125                                 int index = StringUtils.ordinalIndexOf(fqdnwithPort, ":", 3);
126                                 url = fqdnwithPort.substring(0, index);
127                                 urlAppend = " AND URL = $" + Constants.URL + "";
128                                 key = basicQuery + urlAppend;
129                             } else {
130                                 throw new SvcLogicException(
131                                         fn + ": NOT_FOUND! No FQDN  match found in admin artifact  for " + vnf_Type
132                                                 + " " + protocol + "" + action + "");
133                             }
134                         } else if (cnt == 1) {
135                             key = basicQuery;
136                         } else {
137                             if (status == QueryStatus.FAILURE) {
138                                 log.info(fn + ":: Error retrieving credentials");
139                                 throw new SvcLogicException("Error retrieving credentials");
140                             }
141                             // SELECT COUNT query will return SUCCESS with a count of 0 when no data is found
142                             if (status == QueryStatus.SUCCESS) {
143                                 log.info(fn + ":: NOT_FOUND! No data found in device_authentication table for "
144                                         + vnf_Type + " " + protocol + "" + action + "");
145                                 throw new SvcLogicException(
146                                         fn + ":: NOT_FOUND! No data found in device_authentication table for "
147                                                 + vnf_Type + " " + protocol + "" + action + "");
148                             }
149                         }
150                     }
151
152                 } else {
153                     key = basicQuery;
154                     log.info("Getting authentication details :" + key);
155                 }
156                 status = serviceLogic.query("SQL", false, null, key, null, null, ctx);
157                 if (status == QueryStatus.FAILURE) {
158                     log.info(fn + ":: Error retrieving credentials");
159                     throw new SvcLogicException("Error retrieving credentials");
160                 }
161                 if (status == QueryStatus.NOT_FOUND) {
162                     log.info(fn + ":: NOT_FOUND! No data found in device_authentication table for " + vnf_Type + " "
163                             + protocol + "" + action + "");
164                     throw new SvcLogicException(fn + ":: NOT_FOUND! No data found in device_authentication table for "
165                             + vnf_Type + " " + protocol + "" + action + "");
166                 }
167
168                 user = ctx.getAttribute("USER-NAME");
169                 password = ctx.getAttribute("PASSWORD");
170                 port = ctx.getAttribute("PORT-NUMBER");
171                 url = ctx.getAttribute("URL");
172                 log.info("data retrieved " + "user" + user + "pwd" + password + "port" + port + "url" + url);
173
174                 if (StringUtils.isNotBlank(user))
175                     ctx.setAttribute(responsePrefix + "user", user);
176                 if (StringUtils.isNotBlank(password))
177                     ctx.setAttribute(responsePrefix + "password", password);
178                 if (StringUtils.isNotBlank(url))
179                     ctx.setAttribute(responsePrefix + "url", url);
180                 if (StringUtils.isNotBlank(port))
181                     ctx.setAttribute(responsePrefix + "port", port);
182                 log.debug("user" + ctx.getAttribute(responsePrefix + "user"));
183                 log.debug("password" + ctx.getAttribute(responsePrefix + "password"));
184                 log.debug("url" + ctx.getAttribute(responsePrefix + "url"));
185                 log.debug("port" + ctx.getAttribute(responsePrefix + "port"));
186
187             }
188         } catch (Exception e) {
189             ctx.setAttribute(responsePrefix + "status", "failure");
190             ctx.setAttribute(responsePrefix + "error-message", e.getMessage());
191             log.info("Caught exception", e);
192             throw new SvcLogicException(e.getMessage());
193
194         }
195     }
196 }