2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.encryptiontool.wrapper;
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 import org.onap.appc.encryptiontool.fqdn.ParseAdminArtifcat;
40 public class EncryptionToolDGWrapper implements SvcLogicJavaPlugin {
41 private static final EELFLogger log = EELFManager.getInstance().getLogger(EncryptionToolDGWrapper.class);
42 private SvcLogicResource serviceLogic;
43 private static EncryptionToolDGWrapper dgGeneralDBService = null;
44 ParseAdminArtifcat artifact = new ParseAdminArtifcat();
46 public static EncryptionToolDGWrapper initialise() {
47 dgGeneralDBService = new EncryptionToolDGWrapper();
48 return dgGeneralDBService;
51 public EncryptionToolDGWrapper() {
52 serviceLogic = new SqlResource();
55 protected EncryptionToolDGWrapper(SqlResource svcLogic) {
56 serviceLogic = svcLogic;
59 public void runEncryption(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
60 String userName = inParams.get("userName");
61 String password = inParams.get("password");
62 String vnfType = inParams.get("vnf_type");
64 if (StringUtils.isBlank(userName) || StringUtils.isBlank(password) || StringUtils.isBlank(vnfType)) {
65 throw new SvcLogicException("username or Password is missing");
67 String[] input = new String[] { vnfType, userName, password };
68 WrapperEncryptionTool.main(input);
69 } catch (Exception e) {
70 throw new SvcLogicException(e.getMessage());
74 public void getProperty(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
75 String fn = "getproperty.deviceauthentication";
76 String responsePrefix = inParams.get("prefix");
77 String vnf_Type = ctx.getAttribute("vnf-type");
78 String action = ctx.getAttribute("input.action");
79 String protocol = ctx.getAttribute("APPC.protocol.PROTOCOL");
85 QueryStatus status = null;
87 String tenantAai = inParams.get("tenantAai");
88 String cloudOwneraai = inParams.get("cldOwnerAai");
89 String cloudRegionAai = inParams.get("cldRegionAai");
90 String payloadFqdn = inParams.get("payloadFqdn");
91 String payloadTenant = inParams.get("payloadTenant");
92 String payloadCloudOwner = inParams.get("payloadCloudOwner");
93 String payloadCloudRegion = inParams.get("payloadCloudRegion");
94 ctx.setAttribute("payloadTenant", payloadTenant);
95 ctx.setAttribute("payloadCloudOwner", payloadCloudOwner);
96 ctx.setAttribute("payloadCloudRegion", payloadCloudRegion);
97 ctx.setAttribute("tenantAai", tenantAai);
98 ctx.setAttribute("cloudOwneraai", cloudOwneraai);
99 ctx.setAttribute("cloudRegionAai", cloudRegionAai);
100 responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
101 String basicQuery = "SELECT USER_NAME ,PASSWORD,PORT_NUMBER ,URL FROM DEVICE_AUTHENTICATION WHERE VNF_TYPE = $"
102 + Constants.VNF_TYPE + " AND PROTOCOL = $" + Constants.PROTOCOL + "" + " AND ACTION = $"
103 + Constants.ACTION + "";
104 String urlAppend = " ";
106 if (serviceLogic != null && ctx != null) {
107 if (protocol.equalsIgnoreCase("ansible")) {
108 if (payloadFqdn != null && payloadFqdn.trim().length() > 0) {
110 log.info("url from payload" + url);
111 urlAppend = " AND URL = $" + Constants.URL + "";
112 key = basicQuery + urlAppend;
114 key = "SELECT COUNT(*) AS MULTIPLE FROM DEVICE_AUTHENTICATION WHERE VNF_TYPE = $"
115 + Constants.VNF_TYPE + " AND PROTOCOL = $" + Constants.PROTOCOL + " AND ACTION = $"
116 + Constants.ACTION + "";
117 status = serviceLogic.query("SQL", false, null, key, null, null, ctx);
118 log.info("Checking number of records for ansible:" + key);
119 cnt = Integer.parseInt(ctx.getAttribute("MULTIPLE"));
121 String fqdnwithPort = artifact.retrieveFqdn(ctx);
122 if (StringUtils.isNotBlank(fqdnwithPort) && fqdnwithPort.contains(":")) {
124 int index = StringUtils.ordinalIndexOf(fqdnwithPort, ":", 3);
125 url = fqdnwithPort.substring(0, index);
126 urlAppend = " AND URL = $" + Constants.URL + "";
127 key = basicQuery + urlAppend;
129 throw new SvcLogicException(
130 fn + ": NOT_FOUND! No FQDN match found in admin artifact for " + vnf_Type
131 + " " + protocol + "" + action + "");
133 } else if (cnt == 1) {
136 if (status == QueryStatus.FAILURE) {
137 log.info(fn + ":: Error retrieving credentials");
138 throw new SvcLogicException("Error retrieving credentials");
140 if (status == QueryStatus.NOT_FOUND) {
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 + "");
152 log.info("Getting authentication details :" + key);
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");
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 + "");
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);
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"));
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());