[CLAMP-1] Initial ONAP CLAMP seed code commit
[clamp.git] / src / main / java / org / onap / clamp / clds / model / refprop / RefProp.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23
24 package org.onap.clamp.clds.model.refprop;
25
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;
32
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;
40
41 /**
42  * Holds reference properties.
43  */
44 public class RefProp {
45     private static final Logger logger = Logger.getLogger(RefProp.class.getName());
46
47     @Autowired
48     private ApplicationContext appContext;
49     
50     private Properties prop;
51
52     @Value("${org.onap.clamp.config.files.cldsReference:'classpath:/clds/clds-reference.properties'}")
53     private String cldsReferenceValuesFile;
54
55     /**
56      * Load reference properties via null constructor
57      *
58      * @throws IOException
59      */
60     public RefProp() throws IOException {
61     }
62     
63     @PostConstruct
64     public void loadConfig () throws IOException {
65         prop = new Properties();
66         Resource resource = appContext.getResource(cldsReferenceValuesFile);
67         prop.load(resource.getInputStream());
68     }
69
70     /**
71      * get property value
72      *
73      * @param key
74      * @return
75      */
76     public String getStringValue(String key) {
77         return prop.getProperty(key);
78     }
79
80     /**
81      * get property value for a combo key (key1 + "." + key2).  If not found just use key1.
82      *
83      * @param key1
84      * @param key2
85      * @return
86      */
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);
91         }
92         return value;
93     }
94
95     /**
96      * Return json as objects that can be updated
97      *
98      * @param key
99      * @return
100      * @throws IOException
101      */
102     public JsonNode getJsonTemplate(String key) throws IOException {
103         ObjectMapper objectMapper = new ObjectMapper();
104         return objectMapper.readValue(getStringValue(key), JsonNode.class);
105     }
106
107     /**
108      * Return json as objects that can be updated.  First try with combo key (key1 + "." + key2), otherwise default to just key1.
109      *
110      * @param key1
111      * @param key2
112      * @return
113      * @throws IOException
114      */
115     public JsonNode getJsonTemplate(String key1, String key2) throws IOException {
116         ObjectMapper objectMapper = new ObjectMapper();
117         return objectMapper.readValue(getStringValue(key1, key2), JsonNode.class);
118     }
119
120     /**
121      * Get list of values for a property field containing json and a field/keyword within that json.
122      *
123      * @param fieldName
124      * @param value
125      * @return
126      * @throws IOException
127      */
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());
135         }
136         return al;
137     }
138
139 }