612592b58597b9ee778896b14e9201c2cd300cfd
[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.HashSet;
29 import java.util.Map;
30 import java.util.Properties;
31 import java.util.Set;
32
33 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 public class PropertiesNode implements SvcLogicJavaPlugin {
40
41     private static final Logger log = LoggerFactory.getLogger(PropertiesNode.class);
42
43     public void readProperties(Map<String, String> paramMap, SvcLogicContext ctx) throws SvcLogicException {
44         Parameters param = getParameters(paramMap);
45         Properties prop = new Properties();
46         try {
47             File file = new File(param.fileName);
48             try(InputStream in = new FileInputStream(file)){
49                 Map<String, String> mm = null;
50                 String pfx = param.contextPrefix != null ? param.contextPrefix + '.' : "";
51                 if(param.fileBasedParsing){
52                     byte[] data = new byte[(int) file.length()];
53                     if ("json".equalsIgnoreCase(getFileExtension(param.fileName))) {
54                         in.read(data);
55                         String str = new String(data, "UTF-8");
56                         mm = JsonParser.convertToProperties(str);
57                     } else if ("xml".equalsIgnoreCase(getFileExtension(param.fileName))) {
58                         in.read(data);
59                         String str = new String(data, "UTF-8");
60                         mm = XmlParser.convertToProperties(str, param.listNameList);
61                     } else {
62                         prop.load(in);
63                         for (Object key : prop.keySet()) {
64                             String name = (String) key;
65                             String value = prop.getProperty(name);
66                             if (value != null && value.trim().length() > 0) {
67                                 ctx.setAttribute(pfx + name, value.trim());
68                                 log.info("+++ " + pfx + name + ": [" + value + "]");
69                             }
70                         }
71                     }
72                     if (mm != null){
73                         for (Map.Entry<String,String> entry : mm.entrySet()){
74                             ctx.setAttribute(pfx + entry.getKey(), entry.getValue());
75                             log.info("+++ " + pfx + entry.getKey() + ": [" + entry.getValue() + "]");
76                         }
77                     }
78                 } else {
79                     prop.load(in);
80                     for (Object key : prop.keySet()) {
81                         String name = (String) key;
82                         String value = prop.getProperty(name);
83                         if (value != null && value.trim().length() > 0) {
84                             ctx.setAttribute(pfx + name, value.trim());
85                             log.info("+++ " + pfx + name + ": [" + value + "]");
86                         }
87                     }
88                 }
89             }
90         } catch (IOException e) {
91             throw new SvcLogicException("Cannot read property file: " + param.fileName + ": " + e.getMessage(), e);
92         }
93     }
94
95     /* Getting extension has to do the following
96     * ""                            -->   ""
97     * "name"                        -->   ""
98     * "name.txt"                    -->   "txt"
99     * ".htpasswd"                   -->   ""
100     * "name.with.many.dots.myext"   -->   "myext"
101     */
102     private static String getFileExtension(String fileName) {
103         if(fileName.lastIndexOf(".") != -1 && fileName.lastIndexOf(".") != 0)
104             return fileName.substring(fileName.lastIndexOf(".")+1);
105         else return "";
106     }
107
108     protected Parameters getParameters(Map<String, String> paramMap) throws SvcLogicException {
109         Parameters p = new Parameters();
110         p.fileName = parseParam(paramMap, "fileName", true, null);
111         p.contextPrefix = parseParam(paramMap, "contextPrefix", false, null);
112         p.listNameList = getListNameList(paramMap);
113         String fileBasedParsingStr = paramMap.get("fileBasedParsing");
114         p.fileBasedParsing = "true".equalsIgnoreCase(fileBasedParsingStr);
115         return p;
116     }
117
118     protected Set<String> getListNameList(Map<String, String> paramMap) {
119         Set<String> ll = new HashSet<>();
120         for (Map.Entry<String,String> entry : paramMap.entrySet())
121             if (entry.getKey().startsWith("listName"))
122                 ll.add(entry.getValue());
123         return ll;
124     }
125
126     private String parseParam(Map<String, String> paramMap, String name, boolean required, String def)
127             throws SvcLogicException {
128         String s = paramMap.get(name);
129
130         if (s == null || s.trim().length() == 0) {
131             if (!required)
132                 return def;
133             throw new SvcLogicException("Parameter " + name + " is required in PropertiesNode");
134         }
135
136         s = s.trim();
137         String value = "";
138         int i = 0;
139         int i1 = s.indexOf('%');
140         while (i1 >= 0) {
141             int i2 = s.indexOf('%', i1 + 1);
142             if (i2 < 0)
143                 throw new SvcLogicException("Cannot parse parameter " + name + ": " + s + ": no matching %");
144
145             String varName = s.substring(i1 + 1, i2);
146             String varValue = System.getenv(varName);
147             if (varValue == null)
148                 varValue = "";
149
150             value += s.substring(i, i1);
151             value += varValue;
152
153             i = i2 + 1;
154             i1 = s.indexOf('%', i);
155         }
156         value += s.substring(i);
157
158         log.info("Parameter " + name + ": " + value);
159         return value;
160     }
161 }