e730042068f1a428589e64a8baab1f773940a9ad
[appc.git] /
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 import org.onap.appc.encryptiontool.fqdn.ParseAdminArtifcat;
39
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();
45
46     public static EncryptionToolDGWrapper initialise() {
47         dgGeneralDBService = new EncryptionToolDGWrapper();
48         return dgGeneralDBService;
49     }
50
51     public EncryptionToolDGWrapper() {
52         serviceLogic = new SqlResource();
53     }
54
55     protected EncryptionToolDGWrapper(SqlResource svcLogic) {
56         serviceLogic = svcLogic;
57     }
58
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");
63         try {
64             if (StringUtils.isBlank(userName) || StringUtils.isBlank(password) || StringUtils.isBlank(vnfType)) {
65                 throw new SvcLogicException("username or Password is missing");
66             }
67             String[] input = new String[] { vnfType, userName, password };
68             WrapperEncryptionTool.main(input);
69         } catch (Exception e) {
70             throw new SvcLogicException(e.getMessage());
71         }
72     }
73
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");
80         String user = "";
81         String password = "";
82         String port = "0";
83         String url = "";
84         String key = "";
85         QueryStatus status = null;
86         Integer cnt = 0;
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 = " ";
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 = $"
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"));
120                         if (cnt > 1) {
121                             String fqdnwithPort = artifact.retrieveFqdn(ctx);
122                             if (StringUtils.isNotBlank(fqdnwithPort) && fqdnwithPort.contains(":")) {
123
124                                 int index = StringUtils.ordinalIndexOf(fqdnwithPort, ":", 3);
125                                 url = fqdnwithPort.substring(0, index);
126                                 urlAppend = " AND URL = $" + Constants.URL + "";
127                                 key = basicQuery + urlAppend;
128                             } else {
129                                 throw new SvcLogicException(
130                                         fn + ": NOT_FOUND! No FQDN  match found in admin artifact  for " + vnf_Type
131                                                 + " " + protocol + "" + action + "");
132                             }
133                         } else if (cnt == 1) {
134                             key = basicQuery;
135                         } else {
136                             if (status == QueryStatus.FAILURE) {
137                                 log.info(fn + ":: Error retrieving credentials");
138                                 throw new SvcLogicException("Error retrieving credentials");
139                             }
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 + "");
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 }