Changes to encryptionToolDGWrapper
[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  * 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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.encryptiontool.wrapper;
25
26 import java.util.Map;
27
28 import org.apache.commons.configuration.PropertiesConfiguration;
29 import org.apache.commons.lang.StringUtils;
30 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
35 import com.att.eelf.configuration.EELFLogger;
36 import com.att.eelf.configuration.EELFManager;
37 import org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource;
38
39 public class EncryptionToolDGWrapper implements SvcLogicJavaPlugin {
40     private static final EELFLogger log = EELFManager.getInstance().getLogger(EncryptionToolDGWrapper.class);
41     private SvcLogicResource serviceLogic;
42     private static EncryptionToolDGWrapper dgGeneralDBService = null;
43
44     public static EncryptionToolDGWrapper initialise() {
45         if (dgGeneralDBService == null) {
46             dgGeneralDBService = new EncryptionToolDGWrapper();
47         }
48         return dgGeneralDBService;
49     }
50
51     public EncryptionToolDGWrapper() {
52         if (serviceLogic == null) {
53             serviceLogic = new SqlResource();
54         }
55     }
56
57     protected EncryptionToolDGWrapper(SqlResource svcLogic) {
58         if (serviceLogic == null) {
59             serviceLogic = svcLogic;
60         }
61     }
62
63     public void runEncryption(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
64         String userName = inParams.get("userName");
65         String password = inParams.get("password");
66         String vnfType = inParams.get("vnf_type");
67         try {
68             if (StringUtils.isBlank(userName) || StringUtils.isBlank(password) || StringUtils.isBlank(vnfType)) {
69                 throw new SvcLogicException("username or Password is missing");
70             }
71             String[] input = new String[] { vnfType, userName, password };
72             WrapperEncryptionTool.main(input);
73         } catch (Exception e) {
74             throw new SvcLogicException(e.getMessage());
75         }
76     }
77
78     public void getProperty(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
79         String fn = "getproperty.deviceauthentication";
80         String responsePrefix = inParams.get("prefix");
81         String vnf_Type = ctx.getAttribute("vnf-type");
82         String action = ctx.getAttribute("input.action");
83         String protocol = ctx.getAttribute("APPC.protocol.PROTOCOL");
84         String user = "";
85         String password = "";
86         String port = "0";
87         String url = "";
88         QueryStatus status = null;
89
90         responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
91         try {
92             if (serviceLogic != null && ctx != null) {
93                 String key = "SELECT USER_NAME ,PASSWORD,PORT_NUMBER,URL FROM  DEVICE_AUTHENTICATION  WHERE VNF_TYPE = '"
94                         + vnf_Type + "' AND PROTOCOL = '" + protocol + "' AND ACTION = '" + action + "'";
95                 log.info("Getting authentication details :" + key);
96                 status = serviceLogic.query("SQL", false, null, key, null, null, ctx);
97                 if (status == QueryStatus.FAILURE) {
98                     log.info(fn + ":: Error retrieving credentials");
99                     throw new SvcLogicException("Error retrieving credentials");
100                 }
101                 if (status == QueryStatus.NOT_FOUND) {
102                     log.info(fn + ":: NOT_FOUND! No data found in device_authentication table for " + vnf_Type + " "
103                             + protocol + "" + action + "");
104                     throw new SvcLogicException(fn + ":: NOT_FOUND! No data found in device_authentication table for "
105                             + vnf_Type + " " + protocol + "" + action + "");
106                 }
107
108                 user = ctx.getAttribute("USER-NAME");
109                 password = ctx.getAttribute("PASSWORD");
110                 port = ctx.getAttribute("PORT-NUMBER");
111                 url = ctx.getAttribute("URL");
112                 log.info("data retrieved " + "user" + user + "pwd" + password + "port" + port + "url" + url);
113
114                 if (StringUtils.isNotBlank(user))
115                     ctx.setAttribute(responsePrefix + "user", user);
116                 if (StringUtils.isNotBlank(password))
117                     ctx.setAttribute(responsePrefix + "password", password);
118                 if (StringUtils.isNotBlank(url))
119                     ctx.setAttribute(responsePrefix + "url", url);
120                 if (StringUtils.isNotBlank(port))
121                     ctx.setAttribute(responsePrefix + "port", port);
122                 log.debug("user" + ctx.getAttribute(responsePrefix + "user"));
123                 log.debug("password" + ctx.getAttribute(responsePrefix + "password"));
124                 log.debug("url" + ctx.getAttribute(responsePrefix + "url"));
125                 log.debug("port" + ctx.getAttribute(responsePrefix + "port"));
126
127             }
128         } catch (Exception e) {
129             ctx.setAttribute(responsePrefix + "status", "failure");
130             ctx.setAttribute(responsePrefix + "error-message", e.getMessage());
131             log.info("Caught exception", e);
132             throw new SvcLogicException(e.getMessage());
133
134         }
135     }
136 }