Some minor improvements
[clamp.git] / src / main / java / org / onap / clamp / clds / config / ClampProperties.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017-2018 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  */
23
24 package org.onap.clamp.clds.config;
25
26 import com.fasterxml.jackson.databind.JsonNode;
27 import com.google.common.base.Splitter;
28
29 import java.io.IOException;
30 import java.net.URL;
31 import java.nio.charset.StandardCharsets;
32 import java.util.List;
33
34 import org.apache.commons.io.IOUtils;
35 import org.onap.clamp.clds.util.JacksonUtils;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.context.ApplicationContext;
38 import org.springframework.core.env.Environment;
39 import org.springframework.stereotype.Component;
40
41 /**
42  * Holds Clamp properties and add some functionalities.
43  */
44 @Component
45 public class ClampProperties {
46
47     @Autowired
48     private ApplicationContext appContext;
49     @Autowired
50     private Environment env;
51     public static final String CONFIG_PREFIX = "clamp.config.";
52     public static final String TOSCA_POLICY_TYPES_CONFIG= "tosca.policyTypes";
53     public static final String IMPORT_TOSCA_POLICY= "import.tosca.model";
54
55     /**
56      * get property value.
57      *
58      * @param key
59      *            The first key
60      * @return The string with the value
61      */
62     public String getStringValue(String key) {
63         return env.getProperty(CONFIG_PREFIX + key);
64     }
65
66     /**
67      * get property value for a combo key (key1 + "." + key2). If not found just
68      * use key1.
69      *
70      * @param key1
71      *            The first key
72      * @param key2
73      *            The second key after a dot
74      * @return The string with the value
75      */
76     public String getStringValue(String key1, String key2) {
77         String value = getStringValue(key1 + "." + key2);
78         if (value == null || value.length() == 0) {
79             value = getStringValue(key1);
80         }
81         return value;
82     }
83
84     /**
85      * Return json as objects that can be updated. The value obtained from the
86      * clds-reference file will be used as a filename.
87      *
88      * @param key
89      *            The key that will be used to access the clds-reference file
90      * @return A jsonNode
91      * @throws IOException
92      *             In case of issues with the JSON parser
93      */
94     public JsonNode getJsonTemplate(String key) throws IOException {
95         String fileReference = getStringValue(key);
96         return (fileReference != null)
97                 ? JacksonUtils.getObjectMapperInstance().readValue(getFileContentFromPath(fileReference),
98                         JsonNode.class)
99                 : null;
100     }
101
102     /**
103      * Return json as objects that can be updated. First try with combo key
104      * (key1 + "." + key2), otherwise default to just key1. The value obtained
105      * from the clds-reference file will be used as a filename.
106      *
107      * @param key1
108      *            The first key
109      * @param key2
110      *            The second key after a dot
111      * @return A JsonNode
112      * @throws IOException
113      *             In case of issues with the JSON parser
114      */
115     public JsonNode getJsonTemplate(String key1, String key2) throws IOException {
116         String fileReference = getStringValue(key1, key2);
117         return (fileReference != null)
118                 ? JacksonUtils.getObjectMapperInstance().readValue(getFileContentFromPath(fileReference),
119                         JsonNode.class)
120                 : null;
121     }
122
123     /**
124      * Return the file content. The value obtained from the clds-reference file
125      * will be used as a filename.
126      *
127      * @param key
128      *            The key that will be used to access the clds-reference file
129      * @return File content in String
130      * @throws IOException
131      *             In case of issues with the JSON parser
132      */
133     public String getFileContent(String key) throws IOException {
134         String fileReference = getStringValue(key);
135         return (fileReference != null) ? getFileContentFromPath(fileReference) : null;
136     }
137
138     /**
139      * Return the file content. First try with combo key (key1 + "." + key2),
140      * otherwise default to just key1. The value obtained from the
141      * clds-reference file will be used as a filename.
142      *
143      * @param key1
144      *            The first key
145      * @param key2
146      *            The second key after a dot
147      * @return File content in String
148      * @throws IOException
149      *             In case of issues with the JSON parser
150      */
151     public String getFileContent(String key1, String key2) throws IOException {
152         String fileReference = getStringValue(key1, key2);
153         return (fileReference != null) ? getFileContentFromPath(fileReference) : null;
154     }
155
156     private String getFileContentFromPath(String filepath) throws IOException {
157         URL url = appContext.getResource(filepath).getURL();
158         return IOUtils.toString(url, StandardCharsets.UTF_8);
159     }
160     
161         /**
162          * 
163          * 
164          * @param key
165          *       property key
166          * @param separator
167          *       property value separator
168          * @return
169          *       List of Strings split with a separator
170          */
171         public List<String> getStringList(String key, String separator) {
172                 return Splitter.on(separator).trimResults().omitEmptyStrings()
173                                 .splitToList(env.getProperty(CONFIG_PREFIX + key));
174         }
175 }