2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 package org.onap.clamp.clds.config;
26 import com.fasterxml.jackson.databind.JsonNode;
27 import com.fasterxml.jackson.databind.ObjectMapper;
29 import java.io.IOException;
31 import java.nio.charset.StandardCharsets;
33 import org.apache.commons.io.IOUtils;
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;
40 * Holds Clamp properties and add some functionalities.
43 public class ClampProperties {
46 private ApplicationContext appContext;
48 private Environment env;
49 public static final String CONFIG_PREFIX = "clamp.config.";
56 * @return The string with the value
58 public String getStringValue(String key) {
59 return env.getProperty(CONFIG_PREFIX + key);
63 * get property value for a combo key (key1 + "." + key2). If not found just
69 * The second key after a dot
70 * @return The string with the value
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);
81 * Return json as objects that can be updated. The value obtained from the
82 * clds-reference file will be used as a filename.
85 * The key that will be used to access the clds-reference file
88 * In case of issues with the JSON parser
90 public JsonNode getJsonTemplate(String key) throws IOException {
91 ObjectMapper objectMapper = new ObjectMapper();
92 String fileReference = getStringValue(key);
93 return (fileReference != null) ? objectMapper.readValue(getFileContentFromPath(fileReference), JsonNode.class)
98 * Return json as objects that can be updated. First try with combo key
99 * (key1 + "." + key2), otherwise default to just key1. The value obtained
100 * from the clds-reference file will be used as a filename.
105 * The second key after a dot
107 * @throws IOException
108 * In case of issues with the JSON parser
110 public JsonNode getJsonTemplate(String key1, String key2) throws IOException {
111 ObjectMapper objectMapper = new ObjectMapper();
112 String fileReference = getStringValue(key1, key2);
113 return (fileReference != null) ? objectMapper.readValue(getFileContentFromPath(fileReference), JsonNode.class)
118 * Return the file content. The value obtained from the clds-reference file
119 * will be used as a filename.
122 * The key that will be used to access the clds-reference file
123 * @return File content in String
124 * @throws IOException
125 * In case of issues with the JSON parser
127 public String getFileContent(String key) throws IOException {
128 String fileReference = getStringValue(key);
129 return (fileReference != null) ? getFileContentFromPath(fileReference) : null;
133 * Return the file content. First try with combo key (key1 + "." + key2),
134 * otherwise default to just key1. The value obtained from the
135 * clds-reference file will be used as a filename.
140 * The second key after a dot
141 * @return File content in String
142 * @throws IOException
143 * In case of issues with the JSON parser
145 public String getFileContent(String key1, String key2) throws IOException {
146 String fileReference = getStringValue(key1, key2);
147 return (fileReference != null) ? getFileContentFromPath(fileReference) : null;
150 private String getFileContentFromPath(String filepath) throws IOException {
151 URL url = appContext.getResource(filepath).getURL();
152 return IOUtils.toString(url, StandardCharsets.UTF_8);