2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.model.refprop;
26 import com.fasterxml.jackson.databind.JsonNode;
27 import com.fasterxml.jackson.databind.ObjectMapper;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.beans.factory.annotation.Value;
30 import org.springframework.context.ApplicationContext;
31 import org.springframework.core.io.Resource;
33 import javax.annotation.PostConstruct;
34 import java.io.IOException;
35 import java.util.ArrayList;
36 import java.util.Iterator;
37 import java.util.List;
38 import java.util.Properties;
39 import java.util.logging.Logger;
42 * Holds reference properties.
44 public class RefProp {
45 private static final Logger logger = Logger.getLogger(RefProp.class.getName());
48 private ApplicationContext appContext;
50 private Properties prop;
52 @Value("${org.onap.clamp.config.files.cldsReference:'classpath:/clds/clds-reference.properties'}")
53 private String cldsReferenceValuesFile;
56 * Load reference properties via null constructor
60 public RefProp() throws IOException {
64 public void loadConfig () throws IOException {
65 prop = new Properties();
66 Resource resource = appContext.getResource(cldsReferenceValuesFile);
67 prop.load(resource.getInputStream());
76 public String getStringValue(String key) {
77 return prop.getProperty(key);
81 * get property value for a combo key (key1 + "." + key2). If not found just use key1.
87 public String getStringValue(String key1, String key2) {
88 String value = getStringValue(key1 + "." + key2);
89 if (value == null || value.length() == 0) {
90 value = getStringValue(key1);
96 * Return json as objects that can be updated
100 * @throws IOException
102 public JsonNode getJsonTemplate(String key) throws IOException {
103 ObjectMapper objectMapper = new ObjectMapper();
104 return objectMapper.readValue(getStringValue(key), JsonNode.class);
108 * Return json as objects that can be updated. First try with combo key (key1 + "." + key2), otherwise default to just key1.
113 * @throws IOException
115 public JsonNode getJsonTemplate(String key1, String key2) throws IOException {
116 ObjectMapper objectMapper = new ObjectMapper();
117 return objectMapper.readValue(getStringValue(key1, key2), JsonNode.class);
121 * Get list of values for a property field containing json and a field/keyword within that json.
126 * @throws IOException
128 public List<String> decodeToList(String fieldName, String value) throws IOException {
129 JsonNode decode = getJsonTemplate(fieldName);
130 Iterator<JsonNode> itr = decode.path(value).elements();
131 ArrayList<String> al = new ArrayList<>();
132 while (itr.hasNext()) {
133 JsonNode node = itr.next();
134 al.add(node.asText());