3b1f9682f3bb4ba86d6ebe250a5a6a00b64db9ab
[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 java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.Iterator;
29 import java.util.List;
30 import java.util.Properties;
31
32 import javax.annotation.PostConstruct;
33
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.beans.factory.annotation.Value;
36 import org.springframework.context.ApplicationContext;
37 import org.springframework.core.io.Resource;
38
39 import com.att.eelf.configuration.EELFLogger;
40 import com.att.eelf.configuration.EELFManager;
41 import com.fasterxml.jackson.databind.JsonNode;
42 import com.fasterxml.jackson.databind.ObjectMapper;
43
44 /**
45  * Holds reference properties.
46  */
47 public class RefProp {
48     protected static final EELFLogger       logger      = EELFManager.getInstance().getLogger(RefProp.class);
49     protected static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
50
51     @Autowired
52     private ApplicationContext      appContext;
53
54     private Properties              prop;
55
56     @Value("${org.onap.clamp.config.files.cldsReference:'classpath:/clds/clds-reference.properties'}")
57     private String                  cldsReferenceValuesFile;
58
59     /**
60      * Load reference properties via null constructor
61      *
62      * @throws IOException
63      */
64     public RefProp() throws IOException {
65     }
66
67     @PostConstruct
68     public void loadConfig() throws IOException {
69         prop = new Properties();
70         Resource resource = appContext.getResource(cldsReferenceValuesFile);
71         prop.load(resource.getInputStream());
72     }
73
74     /**
75      * get property value
76      *
77      * @param key
78      * @return
79      */
80     public String getStringValue(String key) {
81         return prop.getProperty(key);
82     }
83
84     /**
85      * get property value for a combo key (key1 + "." + key2). If not found just
86      * use key1.
87      *
88      * @param key1
89      * @param key2
90      * @return
91      */
92     public String getStringValue(String key1, String key2) {
93         String value = getStringValue(key1 + "." + key2);
94         if (value == null || value.length() == 0) {
95             value = getStringValue(key1);
96         }
97         return value;
98     }
99
100     /**
101      * Return json as objects that can be updated
102      *
103      * @param key
104      * @return
105      * @throws IOException
106      */
107     public JsonNode getJsonTemplate(String key) throws IOException {
108         ObjectMapper objectMapper = new ObjectMapper();
109         return objectMapper.readValue(getStringValue(key), JsonNode.class);
110     }
111
112     /**
113      * Return json as objects that can be updated. First try with combo key
114      * (key1 + "." + key2), otherwise default to just key1.
115      *
116      * @param key1
117      * @param key2
118      * @return
119      * @throws IOException
120      */
121     public JsonNode getJsonTemplate(String key1, String key2) throws IOException {
122         ObjectMapper objectMapper = new ObjectMapper();
123         return objectMapper.readValue(getStringValue(key1, key2), JsonNode.class);
124     }
125
126     /**
127      * Get list of values for a property field containing json and a
128      * field/keyword within that json.
129      *
130      * @param fieldName
131      * @param value
132      * @return
133      * @throws IOException
134      */
135     public List<String> decodeToList(String fieldName, String value) throws IOException {
136         JsonNode decode = getJsonTemplate(fieldName);
137         Iterator<JsonNode> itr = decode.path(value).elements();
138         ArrayList<String> al = new ArrayList<>();
139         while (itr.hasNext()) {
140             JsonNode node = itr.next();
141             al.add(node.asText());
142         }
143         return al;
144     }
145
146 }