Merge "CCSDK PropNode can read JSON and put to ctx"
[ccsdk/sli/plugins.git] / properties-node / provider / src / main / java / org / onap / ccsdk / sli / plugins / prop / PropertiesNode.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                      reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.ccsdk.sli.plugins.prop;
23
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.util.Map;
29 import java.util.Properties;
30
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.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public class PropertiesNode implements SvcLogicJavaPlugin {
38
39     private static final Logger log = LoggerFactory.getLogger(PropertiesNode.class);
40
41     public void readProperties(Map<String, String> paramMap, SvcLogicContext ctx) throws SvcLogicException {
42         Parameters param = getParameters(paramMap);
43         Properties prop = new Properties();
44         try {
45             File file = new File(param.fileName);
46             InputStream in = new FileInputStream(file);
47             Map<String, String> mm = null;
48             String pfx = param.contextPrefix != null ? param.contextPrefix + '.' : "";
49             if ("json".equalsIgnoreCase(getFileExtension(param.fileName))){
50                 byte[] data = new byte[(int) file.length()];
51                 in.read(data);
52                 in.close();
53                 String str = new String(data, "UTF-8");
54                 mm = JsonParser.convertToProperties(str);
55             } else {
56                 prop.load(in);
57                 for (Object key : prop.keySet()) {
58                     String name = (String) key;
59                     String value = prop.getProperty(name);
60                     if (value != null && value.trim().length() > 0) {
61                         ctx.setAttribute(pfx + name, value.trim());
62                         log.info("+++ " + pfx + name + ": [" + value + "]");
63                     }
64                 }
65             }
66             if (mm != null){
67                 for (Map.Entry<String,String> entry : mm.entrySet()){
68                     ctx.setAttribute(pfx + entry.getKey(), entry.getValue());
69                     log.info("+++ " + pfx + entry.getKey() + ": [" + entry.getValue() + "]");
70                 }
71             }
72
73         } catch (IOException e) {
74             throw new SvcLogicException("Cannot read property file: " + param.fileName + ": " + e.getMessage(), e);
75         }
76     }
77
78     /* Getting extension has to do the following
79     * ""                            -->   ""
80     * "name"                        -->   ""
81     * "name.txt"                    -->   "txt"
82     * ".htpasswd"                   -->   ""
83     * "name.with.many.dots.myext"   -->   "myext"
84     */
85     private static String getFileExtension(String fileName) {
86         if(fileName.lastIndexOf(".") != -1 && fileName.lastIndexOf(".") != 0)
87             return fileName.substring(fileName.lastIndexOf(".")+1);
88         else return "";
89     }
90
91     protected Parameters getParameters(Map<String, String> paramMap) throws SvcLogicException {
92         Parameters p = new Parameters();
93         p.fileName = parseParam(paramMap, "fileName", true, null);
94         p.contextPrefix = parseParam(paramMap, "contextPrefix", false, null);
95         return p;
96     }
97
98     private String parseParam(Map<String, String> paramMap, String name, boolean required, String def)
99             throws SvcLogicException {
100         String s = paramMap.get(name);
101
102         if (s == null || s.trim().length() == 0) {
103             if (!required)
104                 return def;
105             throw new SvcLogicException("Parameter " + name + " is required in PropertiesNode");
106         }
107
108         s = s.trim();
109         String value = "";
110         int i = 0;
111         int i1 = s.indexOf('%');
112         while (i1 >= 0) {
113             int i2 = s.indexOf('%', i1 + 1);
114             if (i2 < 0)
115                 throw new SvcLogicException("Cannot parse parameter " + name + ": " + s + ": no matching %");
116
117             String varName = s.substring(i1 + 1, i2);
118             String varValue = System.getenv(varName);
119             if (varValue == null)
120                 varValue = "";
121
122             value += s.substring(i, i1);
123             value += varValue;
124
125             i = i2 + 1;
126             i1 = s.indexOf('%', i);
127         }
128         value += s.substring(i);
129
130         log.info("Parameter " + name + ": " + value);
131         return value;
132     }
133 }