165b706be7c842d76b9484fc8db8a272ff48f2da
[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.FileInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.util.Map;
28 import java.util.Properties;
29
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.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class PropertiesNode implements SvcLogicJavaPlugin {
37
38         private static final Logger log = LoggerFactory.getLogger(PropertiesNode.class);
39
40         public void readProperties(Map<String, String> paramMap, SvcLogicContext ctx) throws SvcLogicException {
41                 String fileName = parseParam(paramMap, "fileName", true, null);
42                 String contextPrefix = parseParam(paramMap, "contextPrefix", false, null);
43
44                 try {
45                         Properties pp = new Properties();
46                         InputStream in = new FileInputStream(fileName);
47                         pp.load(in);
48                         for (Object key : pp.keySet()) {
49                                 String pfx = contextPrefix != null ? contextPrefix + '.' : "";
50                                 String name = (String) key;
51                                 String value = pp.getProperty(name);
52                                 if (value != null && value.trim().length() > 0) {
53                                         ctx.setAttribute(pfx + name, value.trim());
54                                         log.info("+++ " + pfx + name + ": [" + value + "]");
55                                 }
56                         }
57                 } catch (IOException e) {
58                         throw new SvcLogicException("Cannot read property file: " + fileName + ": " + e.getMessage(), e);
59                 }
60         }
61
62         private String parseParam(Map<String, String> paramMap, String name, boolean required, String def)
63                 throws SvcLogicException {
64                 String s = paramMap.get(name);
65
66                 if (s == null || s.trim().length() == 0) {
67                         if (!required)
68                                 return def;
69                         throw new SvcLogicException("Parameter " + name + " is required in PropertiesNode");
70                 }
71
72                 s = s.trim();
73                 String value = "";
74                 int i = 0;
75                 int i1 = s.indexOf('%');
76                 while (i1 >= 0) {
77                         int i2 = s.indexOf('%', i1 + 1);
78                         if (i2 < 0)
79                                 throw new SvcLogicException("Cannot parse parameter " + name + ": " + s + ": no matching %");
80
81                         String varName = s.substring(i1 + 1, i2);
82                         String varValue = System.getenv(varName);
83                         if (varValue == null)
84                                 varValue = "";
85
86                         value += s.substring(i, i1);
87                         value += varValue;
88
89                         i = i2 + 1;
90                         i1 = s.indexOf('%', i);
91                 }
92                 value += s.substring(i);
93
94                 log.info("Parameter " + name + ": " + value);
95                 return value;
96         }
97 }