Fix incorrect passing of SvcLogicContext value
[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                 + "vnf-type AND PROTOCOL = $APPC.protocol.PROTOCOL AND ACTION = $input.action";
104         String urlAppend = " ";
105         try {
106             if (serviceLogic != null && ctx != null) {
107                 if (protocol.equalsIgnoreCase("ansible")) {
108                     if (payloadFqdn != null && payloadFqdn.trim().length() > 0) {
109                         url = payloadFqdn;
110                         log.info("url from payload" + url);
111                         urlAppend = " AND URL = $" + Constants.URL + "";
112                         key = basicQuery + urlAppend;
113                     } else {
114                         key = "SELECT COUNT(*) AS MULTIPLE FROM DEVICE_AUTHENTICATION WHERE VNF_TYPE = $vnf-type"
115                                 + " AND PROTOCOL = $APPC.protocol.PROTOCOL AND ACTION = $input.action";
116                         status = serviceLogic.query("SQL", false, null, key, null, null, ctx);
117                         log.info("Checking number of records  for ansible:" + key);
118                         cnt = Integer.parseInt(ctx.getAttribute("MULTIPLE"));
119                         if (cnt > 1) {
120                             String fqdnwithPort = artifact.retrieveFqdn(ctx);
121                             if (StringUtils.isNotBlank(fqdnwithPort) && fqdnwithPort.contains(":")) {
122
123                                 int index = StringUtils.ordinalIndexOf(fqdnwithPort, ":", 3);
124                                 url = fqdnwithPort.substring(0, index);
125                                 urlAppend = " AND URL = $" + Constants.URL + "";
126                                 key = basicQuery + urlAppend;
127                             } else {
128                                 throw new SvcLogicException(
129                                         fn + ": NOT_FOUND! No FQDN  match found in admin artifact  for " + vnf_Type
130                                                 + " " + protocol + "" + action + "");
131                             }
132                         } else if (cnt == 1) {
133                             key = basicQuery;
134                         } else {
135                             if (status == QueryStatus.FAILURE) {
136                                 log.info(fn + ":: Error retrieving credentials");
137                                 throw new SvcLogicException("Error retrieving credentials");
138                             }
139                             // SELECT COUNT query will return SUCCESS with a count of 0 when no data is found
140                             if (status == QueryStatus.SUCCESS) {
141                                 log.info(fn + ":: NOT_FOUND! No data found in device_authentication table for "
142                                         + vnf_Type + " " + protocol + "" + action + "");
143                                 throw new SvcLogicException(
144                                         fn + ":: NOT_FOUND! No data found in device_authentication table for "
145                                                 + vnf_Type + " " + protocol + "" + action + "");
146                             }
147                         }
148                     }
149
150                 } else {
151                     key = basicQuery;
152                     log.info("Getting authentication details :" + key);
153                 }
154                 status = serviceLogic.query("SQL", false, null, key, null, null, ctx);
155                 if (status == QueryStatus.FAILURE) {
156                     log.info(fn + ":: Error retrieving credentials");
157                     throw new SvcLogicException("Error retrieving credentials");
158                 }
159                 if (status == QueryStatus.NOT_FOUND) {
160                     log.info(fn + ":: NOT_FOUND! No data found in device_authentication table for " + vnf_Type + " "
161                             + protocol + "" + action + "");
162                     throw new SvcLogicException(fn + ":: NOT_FOUND! No data found in device_authentication table for "
163                             + vnf_Type + " " + protocol + "" + action + "");
164                 }
165
166                 user = ctx.getAttribute("USER-NAME");
167                 password = ctx.getAttribute("PASSWORD");
168                 port = ctx.getAttribute("PORT-NUMBER");
169                 url = ctx.getAttribute("URL");
170                 log.info("data retrieved " + "user" + user + "pwd" + password + "port" + port + "url" + url);
171
172                 if (StringUtils.isNotBlank(user))
173                     ctx.setAttribute(responsePrefix + "user", user);
174                 if (StringUtils.isNotBlank(password))
175                     ctx.setAttribute(responsePrefix + "password", password);
176                 if (StringUtils.isNotBlank(url))
177                     ctx.setAttribute(responsePrefix + "url", url);
178                 if (StringUtils.isNotBlank(port))
179                     ctx.setAttribute(responsePrefix + "port", port);
180                 log.debug("user" + ctx.getAttribute(responsePrefix + "user"));
181                 log.debug("password" + ctx.getAttribute(responsePrefix + "password"));
182                 log.debug("url" + ctx.getAttribute(responsePrefix + "url"));
183                 log.debug("port" + ctx.getAttribute(responsePrefix + "port"));
184
185             }
186         } catch (Exception e) {
187             ctx.setAttribute(responsePrefix + "status", "failure");
188             ctx.setAttribute(responsePrefix + "error-message", e.getMessage());
189             log.info("Caught exception", e);
190             throw new SvcLogicException(e.getMessage());
191
192         }
193     }
194 }