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