8cab4315948918fac980c47d0c2029e7bdbaf1b3
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / common / impl / FSConfigurationSource.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  * Modifications copyright (c) 2019 Nokia
20  * ================================================================================
21  */
22
23 package org.openecomp.sdc.common.impl;
24
25 import java.util.Arrays;
26 import java.util.stream.Collectors;
27 import org.openecomp.sdc.common.api.ConfigurationListener;
28 import org.openecomp.sdc.common.api.ConfigurationSource;
29 import org.openecomp.sdc.common.api.Constants;
30 import org.openecomp.sdc.common.util.YamlToObjectConverter;
31
32 /**
33  * Read configuration from file system
34  * 
35  * @author esofer
36  *
37  */
38 public class FSConfigurationSource implements ConfigurationSource {
39
40     private final YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter();
41
42     private final ConfigFileChangeListener changeListener;
43     private final String appConfigDir;
44
45         public FSConfigurationSource(ConfigFileChangeListener changeListener, String appConfigDir) {
46                 super();
47                 this.changeListener = changeListener;
48                 this.appConfigDir = appConfigDir;
49         }
50
51         /*
52          * get and watch configuration changes. The file name we looking for is the lower case of the class name separated by "-".
53          * 
54          * (non-Javadoc)
55          * 
56          * @see org.openecomp.sdc.common.api.ConfigurationSource#getAndWatchConfiguration (java.lang.Class, org.openecomp.sdc.common.api.ConfigurationListener)
57          */
58         public <T> T getAndWatchConfiguration(Class<T> className, ConfigurationListener configurationListener) {
59
60                 String configFileName = calculateFileName(className);
61
62                 T object = yamlToObjectConverter.convert(this.appConfigDir, className, configFileName);
63
64                 if (configurationListener != null && changeListener != null) {
65                         if (object != null) {
66                                 changeListener.register(configFileName, configurationListener);
67                         }
68                 }
69
70                 return object;
71         }
72
73         public <T> void addWatchConfiguration(Class<T> className, ConfigurationListener configurationListener) {
74
75                 String configFileName = calculateFileName(className);
76
77                 if (configurationListener != null) {
78                         changeListener.register(configFileName, configurationListener);
79                 }
80
81         }
82
83         /**
84          * convert camel case string to list of words separated by "-" where each word is in lower case format. For example, MyClass will be calculated to be my-class.yaml .
85          * 
86          * @param className
87          * @return file name based on the class name
88          */
89     static <T> String calculateFileName(Class<T> className) {
90         String[] words = className.getSimpleName().split("(?=\\p{Upper})");
91
92         return Arrays.stream(words)
93             .map(String::toLowerCase)
94             .collect(Collectors.collectingAndThen(
95                 Collectors.joining("-"),
96                 str -> str + Constants.YAML_SUFFIX)
97             );
98     }
99
100 }