Change json representation in op policy
[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 java.io.IOException;
27 import java.net.URL;
28 import java.nio.charset.StandardCharsets;
29 import org.apache.commons.io.IOUtils;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.context.ApplicationContext;
32 import org.springframework.core.env.Environment;
33 import org.springframework.stereotype.Component;
34
35 /**
36  * Holds Clamp properties and add some functionalities.
37  */
38 @Component
39 public class ClampProperties {
40
41     @Autowired
42     private ApplicationContext appContext;
43     @Autowired
44     private Environment env;
45     public static final String CONFIG_PREFIX = "clamp.config.";
46
47     /**
48      * get property value.
49      *
50      * @param key The first key
51      * @return The string with the value
52      */
53     public String getStringValue(String key) {
54         return env.getProperty(CONFIG_PREFIX + key);
55     }
56
57     /**
58      * get property value for a combo key (key1 + "." + key2). If not found just use
59      * key1.
60      *
61      * @param key1 The first key
62      * @param key2 The second key after a dot
63      * @return The string with the value
64      */
65     public String getStringValue(String key1, String key2) {
66         String value = getStringValue(key1 + "." + key2);
67         if (value == null || value.length() == 0) {
68             value = getStringValue(key1);
69         }
70         return value;
71     }
72
73     /**
74      * Return the file content. The value obtained from the clds-reference file will
75      * be used as a filename.
76      *
77      * @param key The key that will be used to access the clds-reference file
78      * @return File content in String
79      * @throws IOException In case of issues with the JSON parser
80      */
81     public String getFileContent(String key) throws IOException {
82         String fileReference = getStringValue(key);
83         return (fileReference != null) ? getFileContentFromPath(fileReference) : null;
84     }
85
86     /**
87      * Return the file content. First try with combo key (key1 + "." + key2),
88      * otherwise default to just key1. The value obtained from the clds-reference
89      * file will be used as a filename.
90      *
91      * @param key1 The first key
92      * @param key2 The second key after a dot
93      * @return File content in String
94      * @throws IOException In case of issues with the JSON parser
95      */
96     public String getFileContent(String key1, String key2) throws IOException {
97         String fileReference = getStringValue(key1, key2);
98         return (fileReference != null) ? getFileContentFromPath(fileReference) : null;
99     }
100
101     private String getFileContentFromPath(String filepath) throws IOException {
102         URL url = appContext.getResource(filepath).getURL();
103         return IOUtils.toString(url, StandardCharsets.UTF_8);
104     }
105 }