Rework configuration
[clamp.git] / src / main / java / org / onap / clamp / clds / config / CldsReferenceProperties.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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23
24 package org.onap.clamp.clds.config;
25
26 import com.fasterxml.jackson.databind.JsonNode;
27 import com.fasterxml.jackson.databind.ObjectMapper;
28
29 import java.io.IOException;
30 import java.net.URL;
31 import java.nio.charset.StandardCharsets;
32 import java.util.Properties;
33
34 import javax.annotation.PostConstruct;
35
36 import org.apache.commons.io.IOUtils;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.beans.factory.annotation.Value;
39 import org.springframework.context.ApplicationContext;
40 import org.springframework.core.io.Resource;
41 import org.springframework.stereotype.Component;
42
43 /**
44  * Holds reference properties.
45  */
46 @Component
47 public class CldsReferenceProperties {
48
49     @Autowired
50     private ApplicationContext appContext;
51     private Properties prop;
52     @Value("${org.onap.clamp.config.files.cldsReference:'classpath:/clds/clds-reference.properties'}")
53     private String cldsReferenceValuesFile;
54
55     @PostConstruct
56     public void loadConfiguration() throws IOException {
57         prop = new Properties();
58         Resource resource = appContext.getResource(cldsReferenceValuesFile);
59         prop.load(resource.getInputStream());
60     }
61
62     public CldsReferenceProperties(String referenceValuesFile) throws IOException {
63         cldsReferenceValuesFile = referenceValuesFile;
64         loadConfiguration();
65     }
66
67     public CldsReferenceProperties() {
68     }
69
70     /**
71      * get property value.
72      *
73      * @param key
74      *            The first key
75      * @return The string with the value
76      */
77     public String getStringValue(String key) {
78         return prop.getProperty(key);
79     }
80
81     /**
82      * get property value for a combo key (key1 + "." + key2). If not found just
83      * use key1.
84      *
85      * @param key1
86      *            The first key
87      * @param key2
88      *            The second key after a dot
89      * @return The string with the value
90      */
91     public String getStringValue(String key1, String key2) {
92         String value = getStringValue(key1 + "." + key2);
93         if (value == null || value.length() == 0) {
94             value = getStringValue(key1);
95         }
96         return value;
97     }
98
99     /**
100      * Return json as objects that can be updated. The value obtained from the
101      * clds-reference file will be used as a filename.
102      *
103      * @param key
104      *            The key that will be used to access the clds-reference file
105      * @return A jsonNode
106      * @throws IOException
107      *             In case of issues with the JSON parser
108      */
109     public JsonNode getJsonTemplate(String key) throws IOException {
110         ObjectMapper objectMapper = new ObjectMapper();
111         String fileReference = getStringValue(key);
112         return (fileReference != null) ? objectMapper.readValue(getFileContentFromPath(fileReference), JsonNode.class)
113                 : null;
114     }
115
116     /**
117      * Return json as objects that can be updated. First try with combo key
118      * (key1 + "." + key2), otherwise default to just key1. The value obtained
119      * from the clds-reference file will be used as a filename.
120      *
121      * @param key1
122      *            The first key
123      * @param key2
124      *            The second key after a dot
125      * @return A JsonNode
126      * @throws IOException
127      *             In case of issues with the JSON parser
128      */
129     public JsonNode getJsonTemplate(String key1, String key2) throws IOException {
130         ObjectMapper objectMapper = new ObjectMapper();
131         String fileReference = getStringValue(key1, key2);
132         return (fileReference != null) ? objectMapper.readValue(getFileContentFromPath(fileReference), JsonNode.class)
133                 : null;
134     }
135
136     /**
137      * Return the file content. The value obtained from the clds-reference file
138      * will be used as a filename.
139      *
140      * @param key
141      *            The key that will be used to access the clds-reference file
142      * @return File content in String
143      * @throws IOException
144      *             In case of issues with the JSON parser
145      */
146     public String getFileContent(String key) throws IOException {
147         String fileReference = getStringValue(key);
148         return (fileReference != null) ? getFileContentFromPath(fileReference) : null;
149     }
150
151     /**
152      * Return the file content. First try with combo key (key1 + "." + key2),
153      * otherwise default to just key1. The value obtained from the
154      * clds-reference file will be used as a filename.
155      *
156      * @param key1
157      *            The first key
158      * @param key2
159      *            The second key after a dot
160      * @return File content in String
161      * @throws IOException
162      *             In case of issues with the JSON parser
163      */
164     public String getFileContent(String key1, String key2) throws IOException {
165         String fileReference = getStringValue(key1, key2);
166         return (fileReference != null) ? getFileContentFromPath(fileReference) : null;
167     }
168
169     private String getFileContentFromPath(String filepath) throws IOException {
170         URL url = appContext.getResource(filepath).getURL();
171         return IOUtils.toString(url, StandardCharsets.UTF_8);
172     }
173 }