Changes to encryption tool bundle
[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 com.att.eelf.configuration.EELFLogger;
34 import com.att.eelf.configuration.EELFManager;
35
36 public class EncryptionToolDGWrapper implements SvcLogicJavaPlugin {
37
38     private static final EELFLogger log = EELFManager.getInstance().getLogger(EncryptionToolDGWrapper.class);
39
40     public void runEncryption(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
41         String userName = inParams.get("userName");
42         String password = inParams.get("password");
43         String vnfType = inParams.get("vnf_type");
44
45         try {
46             if (StringUtils.isBlank(userName) || StringUtils.isBlank(password) || StringUtils.isBlank(vnfType)) {
47                 throw new SvcLogicException("username or Password is missing");
48             }
49
50             String[] input = new String[] { vnfType, userName, password };
51             WrapperEncryptionTool.main(input);
52
53         } catch (Exception e) {
54             throw new SvcLogicException(e.getMessage());
55         }
56
57     }
58
59     public void getProperty(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {
60         String responsePrefix = inParams.get("prefix");
61         String vnf_Type = ctx.getAttribute("vnf-type");
62         String action = ctx.getAttribute("input.action");
63         String protocol = ctx.getAttribute("APPC.protocol.PROTOCOL");
64         try {
65             responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
66             PropertiesConfiguration conf = new PropertiesConfiguration(
67                     Constants.APPC_CONFIG_DIR + "/appc_southbound.properties");
68             conf.setBasePath(null);
69             EncryptionTool et = EncryptionTool.getInstance();
70                         log.info("responsePrefix:"+responsePrefix);
71                         log.debug("key:"+vnf_Type+"."+protocol+"."+action);
72                 if(StringUtils.isNotBlank(vnf_Type) && StringUtils.isNotBlank(protocol) && StringUtils.isNotBlank(action))
73              {
74             String user = (String)conf.getProperty(vnf_Type + "." + protocol + "." + action + "." + "user");
75             String password = (String)conf.getProperty(vnf_Type + "." + protocol + "." + action + "." + "password");
76             String port = (String)conf.getProperty(vnf_Type + "." + protocol + "." + action + "." + "port");
77             String url = (String)conf.getProperty(vnf_Type + "." + protocol + "." + action + "." + "url");
78                 if (StringUtils.isBlank(user) || StringUtils.isBlank(password)) {
79             throw new SvcLogicException("Error-while fetching user or password");
80          }
81             if ( (user.startsWith("[") && user.endsWith("]")) || (password.startsWith("[") && password.endsWith("]"))|| (port.startsWith("[") && port.endsWith("]"))||(url.startsWith("[") && url.endsWith("]")) )
82             {
83                 throw new SvcLogicException("Duplicate entries found for  key "+vnf_Type + "." + protocol + "." + action +"in properties File");
84             }
85             if (StringUtils.isNotBlank(user))
86                 ctx.setAttribute(responsePrefix + "user", user);
87             if (StringUtils.isNotBlank(password))
88                 ctx.setAttribute(responsePrefix +  "password", et.decrypt(password));
89             if (StringUtils.isNotBlank(url))
90                 ctx.setAttribute(responsePrefix +  "url", url);
91             if (StringUtils.isNotBlank(port))
92                 ctx.setAttribute(responsePrefix + "port", port);
93             log.debug(ctx.getAttribute(responsePrefix + "user"));
94                         log.debug(ctx.getAttribute(responsePrefix + "password"));
95                         log.debug(ctx.getAttribute(responsePrefix + "url"));
96                         log.debug(ctx.getAttribute(responsePrefix + "port"));
97             }
98                 else
99                 {
100                     throw new SvcLogicException("Error-as any of properties such as vnf-type,protocol,action are missing in ctx");
101                 }
102         } catch (Exception e) {
103             ctx.setAttribute(responsePrefix + "status", "failure");
104             ctx.setAttribute(responsePrefix + "error-message", e.getMessage());
105             log.info("Caught exception", e);
106             throw new SvcLogicException(e.getMessage());
107         }
108     }
109 }