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 * ===================================================================
24 package org.onap.clamp.clds.config;
26 import com.fasterxml.jackson.databind.JsonNode;
27 import com.google.common.base.Splitter;
29 import java.io.IOException;
31 import java.nio.charset.StandardCharsets;
32 import java.util.List;
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;
42 * Holds Clamp properties and add some functionalities.
45 public class ClampProperties {
48 private ApplicationContext appContext;
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";
60 * @return The string with the value
62 public String getStringValue(String key) {
63 return env.getProperty(CONFIG_PREFIX + key);
67 * get property value for a combo key (key1 + "." + key2). If not found just
73 * The second key after a dot
74 * @return The string with the value
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);
85 * Return json as objects that can be updated. The value obtained from the
86 * clds-reference file will be used as a filename.
89 * The key that will be used to access the clds-reference file
92 * In case of issues with the JSON parser
94 public JsonNode getJsonTemplate(String key) throws IOException {
95 String fileReference = getStringValue(key);
96 return (fileReference != null)
97 ? JacksonUtils.getObjectMapperInstance().readValue(getFileContentFromPath(fileReference),
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.
110 * The second key after a dot
112 * @throws IOException
113 * In case of issues with the JSON parser
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),
124 * Return the file content. The value obtained from the clds-reference file
125 * will be used as a filename.
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
133 public String getFileContent(String key) throws IOException {
134 String fileReference = getStringValue(key);
135 return (fileReference != null) ? getFileContentFromPath(fileReference) : null;
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.
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
151 public String getFileContent(String key1, String key2) throws IOException {
152 String fileReference = getStringValue(key1, key2);
153 return (fileReference != null) ? getFileContentFromPath(fileReference) : null;
156 private String getFileContentFromPath(String filepath) throws IOException {
157 URL url = appContext.getResource(filepath).getURL();
158 return IOUtils.toString(url, StandardCharsets.UTF_8);
167 * property value separator
169 * List of Strings split with a separator
171 public List<String> getStringList(String key, String separator) {
172 return Splitter.on(separator).trimResults().omitEmptyStrings()
173 .splitToList(env.getProperty(CONFIG_PREFIX + key));