Remove Camunda and replace by Camel
[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-2018 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
29 import java.io.IOException;
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 import org.springframework.stereotype.Component;
39
40 /**
41  * Holds reference properties.
42  */
43 @Component
44 public class RefProp {
45
46     @Autowired
47     private ApplicationContext appContext;
48     private Properties prop;
49     @Value("${org.onap.clamp.config.files.cldsReference:'classpath:/clds/clds-reference.properties'}")
50     private String cldsReferenceValuesFile;
51
52     @PostConstruct
53     public void loadConfig() throws IOException {
54         prop = new Properties();
55         Resource resource = appContext.getResource(cldsReferenceValuesFile);
56         prop.load(resource.getInputStream());
57     }
58
59     /**
60      * get property value
61      *
62      * @param key
63      * @return
64      */
65     public String getStringValue(String key) {
66         return prop.getProperty(key);
67     }
68
69     /**
70      * get property value for a combo key (key1 + "." + key2). If not found just
71      * use key1.
72      *
73      * @param key1
74      * @param key2
75      * @return
76      */
77     public String getStringValue(String key1, String key2) {
78         String value = getStringValue(key1 + "." + key2);
79         if (value == null || value.length() == 0) {
80             value = getStringValue(key1);
81         }
82         return value;
83     }
84
85     /**
86      * Return json as objects that can be updated
87      *
88      * @param key
89      * @return
90      * @throws IOException
91      */
92     public JsonNode getJsonTemplate(String key) throws IOException {
93         ObjectMapper objectMapper = new ObjectMapper();
94         return objectMapper.readValue(getStringValue(key), JsonNode.class);
95     }
96
97     /**
98      * Return json as objects that can be updated. First try with combo key
99      * (key1 + "." + key2), otherwise default to just key1.
100      *
101      * @param key1
102      * @param key2
103      * @return
104      * @throws IOException
105      */
106     public JsonNode getJsonTemplate(String key1, String key2) throws IOException {
107         ObjectMapper objectMapper = new ObjectMapper();
108         return objectMapper.readValue(getStringValue(key1, key2), JsonNode.class);
109     }
110 }